[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/includes/db/ -> sqlite.php (source)

   1  <?php
   2  /** 
   3  *
   4  * @package dbal
   5  * @version $Id: sqlite.php,v 1.32 2006/10/14 14:56:44 acydburn Exp $
   6  * @copyright (c) 2005 phpBB Group 
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
   8  *
   9  */
  10  
  11  /**
  12  * @ignore
  13  */
  14  if (!defined('IN_PHPBB'))
  15  {
  16      exit;
  17  }
  18  
  19  include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
  20  
  21  /**
  22  * Sqlite Database Abstraction Layer
  23  * Minimum Requirement: 2.8.2+
  24  * @package dbal
  25  */
  26  class dbal_sqlite extends dbal
  27  {
  28      /**
  29      * Connect to server
  30      */
  31  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
  32      {
  33          $this->persistency = $persistency;
  34          $this->user = $sqluser;
  35          $this->server = $sqlserver . (($port) ? ':' . $port : '');
  36          $this->dbname = $database;
  37  
  38          $error = '';
  39          $this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error);
  40  
  41          if ($this->db_connect_id)
  42          {
  43              @sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
  44          }
  45  
  46          
  47          return ($this->db_connect_id) ? true : array('message' => $error);
  48      }
  49  
  50      /**
  51      * Version information about used database
  52      */
  53  	function sql_server_info()
  54      {
  55          return 'SQLite ' . @sqlite_libversion();
  56      }
  57  
  58      /**
  59      * SQL Transaction
  60      * @access private
  61      */
  62  	function _sql_transaction($status = 'begin')
  63      {
  64          switch ($status)
  65          {
  66              case 'begin':
  67                  return @sqlite_query('BEGIN', $this->db_connect_id);
  68              break;
  69  
  70              case 'commit':
  71                  return @sqlite_query('COMMIT', $this->db_connect_id);
  72              break;
  73  
  74              case 'rollback':
  75                  return @sqlite_query('ROLLBACK', $this->db_connect_id);
  76              break;
  77          }
  78  
  79          return true;
  80      }
  81  
  82      /**
  83      * Base query method
  84      *
  85      * @param    string    $query        Contains the SQL query which shall be executed
  86      * @param    int        $cache_ttl    Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  87      * @return    mixed                When casted to bool the returned value returns true on success and false on failure
  88      *
  89      * @access    public
  90      */
  91  	function sql_query($query = '', $cache_ttl = 0)
  92      {
  93          if ($query != '')
  94          {
  95              global $cache;
  96  
  97              // EXPLAIN only in extra debug mode
  98              if (defined('DEBUG_EXTRA'))
  99              {
 100                  $this->sql_report('start', $query);
 101              }
 102  
 103              $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 104              $this->sql_add_num_queries($this->query_result);
 105  
 106              if ($this->query_result === false)
 107              {
 108                  if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false)
 109                  {
 110                      $this->sql_error($query);
 111                  }
 112  
 113                  if (defined('DEBUG_EXTRA'))
 114                  {
 115                      $this->sql_report('stop', $query);
 116                  }
 117  
 118                  if ($cache_ttl && method_exists($cache, 'sql_save'))
 119                  {
 120                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 121                      $cache->sql_save($query, $this->query_result, $cache_ttl);
 122                  }
 123                  else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 124                  {
 125                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 126                  }
 127              }
 128              else if (defined('DEBUG_EXTRA'))
 129              {
 130                  $this->sql_report('fromcache', $query);
 131              }
 132          }
 133          else
 134          {
 135              return false;
 136          }
 137  
 138          return ($this->query_result) ? $this->query_result : false;
 139      }
 140  
 141      /**
 142      * Build LIMIT query
 143      */
 144  	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
 145      {
 146          if ($query != '')
 147          {
 148              $this->query_result = false; 
 149  
 150              // if $total is set to 0 we do not want to limit the number of rows
 151              if ($total == 0)
 152              {
 153                  $total = -1;
 154              }
 155  
 156              $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 157  
 158              return $this->sql_query($query, $cache_ttl); 
 159          }
 160          else
 161          {
 162              return false;
 163          }
 164      }
 165  
 166      /**
 167      * Return number of affected rows
 168      */
 169  	function sql_affectedrows()
 170      {
 171          return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
 172      }
 173  
 174      /**
 175      * Fetch current row
 176      */
 177  	function sql_fetchrow($query_id = false)
 178      {
 179          global $cache;
 180  
 181          if ($query_id === false)
 182          {
 183              $query_id = $this->query_result;
 184          }
 185  
 186          if (isset($cache->sql_rowset[$query_id]))
 187          {
 188              return $cache->sql_fetchrow($query_id);
 189          }
 190  
 191          return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false;
 192      }
 193  
 194      /**
 195      * Seek to given row number
 196      * rownum is zero-based
 197      */
 198  	function sql_rowseek($rownum, $query_id = false)
 199      {
 200          global $cache;
 201  
 202          if ($query_id === false)
 203          {
 204              $query_id = $this->query_result;
 205          }
 206  
 207          if (isset($cache->sql_rowset[$query_id]))
 208          {
 209              return $cache->sql_rowseek($rownum, $query_id);
 210          }
 211  
 212          return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false;
 213      }
 214  
 215      /**
 216      * Get last inserted id after insert statement
 217      */
 218  	function sql_nextid()
 219      {
 220          return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
 221      }
 222  
 223      /**
 224      * Free sql result
 225      */
 226  	function sql_freeresult($query_id = false)
 227      {
 228          global $cache;
 229  
 230          if ($query_id === false)
 231          {
 232              $query_id = $this->query_result;
 233          }
 234  
 235          if (isset($cache->sql_rowset[$query_id]))
 236          {
 237              return $cache->sql_freeresult($query_id);
 238          }
 239  
 240          return true;
 241      }
 242  
 243      /**
 244      * Escape string used in sql query
 245      */
 246  	function sql_escape($msg)
 247      {
 248          return @sqlite_escape_string($msg);
 249      }
 250  
 251      /**
 252      * return sql error array
 253      * @access private
 254      */
 255  	function _sql_error()
 256      {
 257          return array(
 258              'message'    => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
 259              'code'        => @sqlite_last_error($this->db_connect_id)
 260          );
 261      }
 262  
 263      /**
 264      * Build db-specific query data
 265      * @access private
 266      */
 267  	function _sql_custom_build($stage, $data)
 268      {
 269          return $data;
 270      }
 271  
 272      /**
 273      * Close sql connection
 274      * @access private
 275      */
 276  	function _sql_close()
 277      {
 278          return @sqlite_close($this->db_connect_id);
 279      }
 280  
 281      /**
 282      * Build db-specific report
 283      * @access private
 284      */
 285  	function _sql_report($mode, $query = '')
 286      {
 287          switch ($mode)
 288          {
 289              case 'start':
 290              break;
 291  
 292              case 'fromcache':
 293                  $endtime = explode(' ', microtime());
 294                  $endtime = $endtime[0] + $endtime[1];
 295  
 296                  $result = @sqlite_query($query, $this->db_connect_id);
 297                  while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
 298                  {
 299                      // Take the time spent on parsing rows into account
 300                  }
 301  
 302                  $splittime = explode(' ', microtime());
 303                  $splittime = $splittime[0] + $splittime[1];
 304  
 305                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 306  
 307              break;
 308          }
 309      }
 310  }
 311  
 312  ?>


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6