[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/db/driver/ -> sqlite.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\db\driver;
  15  
  16  /**
  17  * Sqlite Database Abstraction Layer
  18  * Minimum Requirement: 2.8.2+
  19  */
  20  class sqlite extends \phpbb\db\driver\driver
  21  {
  22      var $connect_error = '';
  23  
  24      /**
  25      * {@inheritDoc}
  26      */
  27  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  28      {
  29          $this->persistency = $persistency;
  30          $this->user = $sqluser;
  31          $this->server = $sqlserver . (($port) ? ':' . $port : '');
  32          $this->dbname = $database;
  33  
  34          $error = '';
  35          if ($this->persistency)
  36          {
  37              if (!function_exists('sqlite_popen'))
  38              {
  39                  $this->connect_error = 'sqlite_popen function does not exist, is sqlite extension installed?';
  40                  return $this->sql_error('');
  41              }
  42              $this->db_connect_id = @sqlite_popen($this->server, 0666, $error);
  43          }
  44          else
  45          {
  46              if (!function_exists('sqlite_open'))
  47              {
  48                  $this->connect_error = 'sqlite_open function does not exist, is sqlite extension installed?';
  49                  return $this->sql_error('');
  50              }
  51              $this->db_connect_id = @sqlite_open($this->server, 0666, $error);
  52          }
  53  
  54          if ($this->db_connect_id)
  55          {
  56              @sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
  57  //            @sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id);
  58          }
  59  
  60          return ($this->db_connect_id) ? true : array('message' => $error);
  61      }
  62  
  63      /**
  64      * {@inheritDoc}
  65      */
  66  	function sql_server_info($raw = false, $use_cache = true)
  67      {
  68          global $cache;
  69  
  70          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false)
  71          {
  72              $result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id);
  73              $row = @sqlite_fetch_array($result, SQLITE_ASSOC);
  74  
  75              $this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0;
  76  
  77              if (!empty($cache) && $use_cache)
  78              {
  79                  $cache->put('sqlite_version', $this->sql_server_version);
  80              }
  81          }
  82  
  83          return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version;
  84      }
  85  
  86      /**
  87      * SQL Transaction
  88      * @access private
  89      */
  90  	function _sql_transaction($status = 'begin')
  91      {
  92          switch ($status)
  93          {
  94              case 'begin':
  95                  return @sqlite_query('BEGIN', $this->db_connect_id);
  96              break;
  97  
  98              case 'commit':
  99                  return @sqlite_query('COMMIT', $this->db_connect_id);
 100              break;
 101  
 102              case 'rollback':
 103                  return @sqlite_query('ROLLBACK', $this->db_connect_id);
 104              break;
 105          }
 106  
 107          return true;
 108      }
 109  
 110      /**
 111      * {@inheritDoc}
 112      */
 113  	function sql_query($query = '', $cache_ttl = 0)
 114      {
 115          if ($query != '')
 116          {
 117              global $cache;
 118  
 119              // EXPLAIN only in extra debug mode
 120              if (defined('DEBUG'))
 121              {
 122                  $this->sql_report('start', $query);
 123              }
 124              else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 125              {
 126                  $this->curtime = microtime(true);
 127              }
 128  
 129              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 130              $this->sql_add_num_queries($this->query_result);
 131  
 132              if ($this->query_result === false)
 133              {
 134                  if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false)
 135                  {
 136                      $this->sql_error($query);
 137                  }
 138  
 139                  if (defined('DEBUG'))
 140                  {
 141                      $this->sql_report('stop', $query);
 142                  }
 143                  else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 144                  {
 145                      $this->sql_time += microtime(true) - $this->curtime;
 146                  }
 147  
 148                  if ($cache && $cache_ttl)
 149                  {
 150                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 151                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 152                  }
 153                  else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 154                  {
 155                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 156                  }
 157              }
 158              else if (defined('DEBUG'))
 159              {
 160                  $this->sql_report('fromcache', $query);
 161              }
 162          }
 163          else
 164          {
 165              return false;
 166          }
 167  
 168          return $this->query_result;
 169      }
 170  
 171      /**
 172      * Build LIMIT query
 173      */
 174  	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 175      {
 176          $this->query_result = false;
 177  
 178          // if $total is set to 0 we do not want to limit the number of rows
 179          if ($total == 0)
 180          {
 181              $total = -1;
 182          }
 183  
 184          $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 185  
 186          return $this->sql_query($query, $cache_ttl);
 187      }
 188  
 189      /**
 190      * {@inheritDoc}
 191      */
 192  	function sql_affectedrows()
 193      {
 194          return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
 195      }
 196  
 197      /**
 198      * {@inheritDoc}
 199      */
 200  	function sql_fetchrow($query_id = false)
 201      {
 202          global $cache;
 203  
 204          if ($query_id === false)
 205          {
 206              $query_id = $this->query_result;
 207          }
 208  
 209          if ($cache && $cache->sql_exists($query_id))
 210          {
 211              return $cache->sql_fetchrow($query_id);
 212          }
 213  
 214          return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false;
 215      }
 216  
 217      /**
 218      * {@inheritDoc}
 219      */
 220  	function sql_rowseek($rownum, &$query_id)
 221      {
 222          global $cache;
 223  
 224          if ($query_id === false)
 225          {
 226              $query_id = $this->query_result;
 227          }
 228  
 229          if ($cache && $cache->sql_exists($query_id))
 230          {
 231              return $cache->sql_rowseek($rownum, $query_id);
 232          }
 233  
 234          return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false;
 235      }
 236  
 237      /**
 238      * {@inheritDoc}
 239      */
 240  	function sql_nextid()
 241      {
 242          return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
 243      }
 244  
 245      /**
 246      * {@inheritDoc}
 247      */
 248  	function sql_freeresult($query_id = false)
 249      {
 250          global $cache;
 251  
 252          if ($query_id === false)
 253          {
 254              $query_id = $this->query_result;
 255          }
 256  
 257          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 258          {
 259              return $cache->sql_freeresult($query_id);
 260          }
 261  
 262          return true;
 263      }
 264  
 265      /**
 266      * {@inheritDoc}
 267      */
 268  	function sql_escape($msg)
 269      {
 270          return @sqlite_escape_string($msg);
 271      }
 272  
 273      /**
 274      * {@inheritDoc}
 275      *
 276      * For SQLite an underscore is a not-known character... this may change with SQLite3
 277      */
 278  	function sql_like_expression($expression)
 279      {
 280          // Unlike LIKE, GLOB is unfortunately case sensitive.
 281          // We only catch * and ? here, not the character map possible on file globbing.
 282          $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
 283  
 284          $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
 285          $expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
 286  
 287          return 'GLOB \'' . $this->sql_escape($expression) . '\'';
 288      }
 289  
 290      /**
 291      * {@inheritDoc}
 292      *
 293      * For SQLite an underscore is a not-known character...
 294      */
 295  	function sql_not_like_expression($expression)
 296      {
 297          // Unlike NOT LIKE, NOT GLOB is unfortunately case sensitive.
 298          // We only catch * and ? here, not the character map possible on file globbing.
 299          $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
 300  
 301          $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
 302          $expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
 303  
 304          return 'NOT GLOB \'' . $this->sql_escape($expression) . '\'';
 305      }
 306  
 307      /**
 308      * return sql error array
 309      * @access private
 310      */
 311  	function _sql_error()
 312      {
 313          if (function_exists('sqlite_error_string'))
 314          {
 315              $error = array(
 316                  'message'    => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
 317                  'code'        => @sqlite_last_error($this->db_connect_id),
 318              );
 319          }
 320          else
 321          {
 322              $error = array(
 323                  'message'    => $this->connect_error,
 324                  'code'        => '',
 325              );
 326          }
 327  
 328          return $error;
 329      }
 330  
 331      /**
 332      * Build db-specific query data
 333      * @access private
 334      */
 335  	function _sql_custom_build($stage, $data)
 336      {
 337          return $data;
 338      }
 339  
 340      /**
 341      * Close sql connection
 342      * @access private
 343      */
 344  	function _sql_close()
 345      {
 346          return @sqlite_close($this->db_connect_id);
 347      }
 348  
 349      /**
 350      * Build db-specific report
 351      * @access private
 352      */
 353  	function _sql_report($mode, $query = '')
 354      {
 355          switch ($mode)
 356          {
 357              case 'start':
 358              break;
 359  
 360              case 'fromcache':
 361                  $endtime = explode(' ', microtime());
 362                  $endtime = $endtime[0] + $endtime[1];
 363  
 364                  $result = @sqlite_query($query, $this->db_connect_id);
 365                  while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
 366                  {
 367                      // Take the time spent on parsing rows into account
 368                  }
 369  
 370                  $splittime = explode(' ', microtime());
 371                  $splittime = $splittime[0] + $splittime[1];
 372  
 373                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 374  
 375              break;
 376          }
 377      }
 378  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1