Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

sqlite3.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 8.92 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\db\driver;
 015   
 016  /**
 017  * SQLite3 Database Abstraction Layer
 018  * Minimum Requirement: 3.6.15+
 019  */
 020  class sqlite3 extends \phpbb\db\driver\driver
 021  {
 022      /**
 023      * @var    string        Stores errors during connection setup in case the driver is not available
 024      */
 025      protected $connect_error = '';
 026   
 027      /**
 028      * @var    \SQLite3    The SQLite3 database object to operate against
 029      */
 030      protected $dbo = null;
 031   
 032      /**
 033      * {@inheritDoc}
 034      */
 035      public function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
 036      {
 037          $this->persistency = false;
 038          $this->user = $sqluser;
 039          $this->server = $sqlserver . (($port) ? ':' . $port : '');
 040          $this->dbname = $database;
 041   
 042          if (!class_exists('SQLite3', false))
 043          {
 044              $this->connect_error = 'SQLite3 not found, is the extension installed?';
 045              return $this->sql_error('');
 046          }
 047   
 048          try
 049          {
 050              $this->dbo = new \SQLite3($this->server, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
 051              $this->db_connect_id = true;
 052          }
 053          catch (\Exception $e)
 054          {
 055              $this->connect_error = $e->getMessage();
 056              return array('message' => $this->connect_error);
 057          }
 058   
 059          return true;
 060      }
 061   
 062      /**
 063      * {@inheritDoc}
 064      */
 065      public function sql_server_info($raw = false, $use_cache = true)
 066      {
 067          global $cache;
 068   
 069          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false)
 070          {
 071              $version = \SQLite3::version();
 072   
 073              $this->sql_server_version = $version['versionString'];
 074   
 075              if (!empty($cache) && $use_cache)
 076              {
 077                  $cache->put('sqlite_version', $this->sql_server_version);
 078              }
 079          }
 080   
 081          return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version;
 082      }
 083   
 084      /**
 085      * SQL Transaction
 086      *
 087      * @param    string    $status        Should be one of the following strings:
 088      *                                begin, commit, rollback
 089      * @return    bool    Success/failure of the transaction query
 090      */
 091      protected function _sql_transaction($status = 'begin')
 092      {
 093          switch ($status)
 094          {
 095              case 'begin':
 096                  return $this->dbo->exec('BEGIN IMMEDIATE');
 097              break;
 098   
 099              case 'commit':
 100                  return $this->dbo->exec('COMMIT');
 101              break;
 102   
 103              case 'rollback':
 104                  return $this->dbo->exec('ROLLBACK');
 105              break;
 106          }
 107   
 108          return true;
 109      }
 110   
 111      /**
 112      * {@inheritDoc}
 113      */
 114      public function sql_query($query = '', $cache_ttl = 0)
 115      {
 116          if ($query != '')
 117          {
 118              global $cache;
 119   
 120              // EXPLAIN only in extra debug mode
 121              if (defined('DEBUG'))
 122              {
 123                  $this->sql_report('start', $query);
 124              }
 125              else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 126              {
 127                  $this->curtime = microtime(true);
 128              }
 129   
 130              $this->last_query_text = $query;
 131              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 132              $this->sql_add_num_queries($this->query_result);
 133   
 134              if ($this->query_result === false)
 135              {
 136                  if (($this->query_result = @$this->dbo->query($query)) === false)
 137                  {
 138                      $this->sql_error($query);
 139                  }
 140   
 141                  if (defined('DEBUG'))
 142                  {
 143                      $this->sql_report('stop', $query);
 144                  }
 145                  else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 146                  {
 147                      $this->sql_time += microtime(true) - $this->curtime;
 148                  }
 149   
 150                  if ($cache && $cache_ttl)
 151                  {
 152                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 153                  }
 154              }
 155              else if (defined('DEBUG'))
 156              {
 157                  $this->sql_report('fromcache', $query);
 158              }
 159          }
 160          else
 161          {
 162              return false;
 163          }
 164   
 165          return $this->query_result;
 166      }
 167   
 168      /**
 169      * Build LIMIT query
 170      *
 171      * @param    string    $query        The SQL query to execute
 172      * @param    int        $total        The number of rows to select
 173      * @param    int        $offset
 174      * @param    int        $cache_ttl    Either 0 to avoid caching or
 175      *                the time in seconds which the result shall be kept in cache
 176      * @return    mixed    Buffered, seekable result handle, false on error
 177      */
 178      protected function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 179      {
 180          $this->query_result = false;
 181   
 182          // if $total is set to 0 we do not want to limit the number of rows
 183          if ($total == 0)
 184          {
 185              $total = -1;
 186          }
 187   
 188          $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 189   
 190          return $this->sql_query($query, $cache_ttl);
 191      }
 192   
 193      /**
 194      * {@inheritDoc}
 195      */
 196      public function sql_affectedrows()
 197      {
 198          return ($this->db_connect_id) ? $this->dbo->changes() : false;
 199      }
 200   
 201      /**
 202      * {@inheritDoc}
 203      */
 204      public function sql_fetchrow($query_id = false)
 205      {
 206          global $cache;
 207   
 208          if ($query_id === false)
 209          {
 210              $query_id = $this->query_result;
 211          }
 212   
 213          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 214          {
 215              return $cache->sql_fetchrow($query_id);
 216          }
 217   
 218          return is_object($query_id) ? $query_id->fetchArray(SQLITE3_ASSOC) : false;
 219      }
 220   
 221      /**
 222      * {@inheritDoc}
 223      */
 224      public function sql_nextid()
 225      {
 226          return ($this->db_connect_id) ? $this->dbo->lastInsertRowID() : false;
 227      }
 228   
 229      /**
 230      * {@inheritDoc}
 231      */
 232      public function sql_freeresult($query_id = false)
 233      {
 234          global $cache;
 235   
 236          if ($query_id === false)
 237          {
 238              $query_id = $this->query_result;
 239          }
 240   
 241          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 242          {
 243              return $cache->sql_freeresult($query_id);
 244          }
 245   
 246          if ($query_id)
 247          {
 248              return @$query_id->finalize();
 249          }
 250      }
 251   
 252      /**
 253      * {@inheritDoc}
 254      */
 255      public function sql_escape($msg)
 256      {
 257          return \SQLite3::escapeString($msg);
 258      }
 259   
 260      /**
 261      * {@inheritDoc}
 262      *
 263      * For SQLite an underscore is an unknown character.
 264      */
 265      public function sql_like_expression($expression)
 266      {
 267          // Unlike LIKE, GLOB is unfortunately case sensitive.
 268          // We only catch * and ? here, not the character map possible on file globbing.
 269          $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
 270   
 271          $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
 272          $expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
 273   
 274          return 'GLOB \'' . $this->sql_escape($expression) . '\'';
 275      }
 276   
 277      /**
 278      * {@inheritDoc}
 279      *
 280      * For SQLite an underscore is an unknown character.
 281      */
 282      public function sql_not_like_expression($expression)
 283      {
 284          // Unlike NOT LIKE, NOT GLOB is unfortunately case sensitive
 285          // We only catch * and ? here, not the character map possible on file globbing.
 286          $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
 287   
 288          $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
 289          $expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
 290   
 291          return 'NOT GLOB \'' . $this->sql_escape($expression) . '\'';
 292      }
 293   
 294      /**
 295      * return sql error array
 296      *
 297      * @return array
 298      */
 299      protected function _sql_error()
 300      {
 301          if (class_exists('SQLite3', false) && isset($this->dbo))
 302          {
 303              $error = array(
 304                  'message'    => $this->dbo->lastErrorMsg(),
 305                  'code'        => $this->dbo->lastErrorCode(),
 306              );
 307          }
 308          else
 309          {
 310              $error = array(
 311                  'message'    => $this->connect_error,
 312                  'code'        => '',
 313              );
 314          }
 315   
 316          return $error;
 317      }
 318   
 319      /**
 320      * Build db-specific query data
 321      *
 322      * @param    string    $stage        Available stages: FROM, WHERE
 323      * @param    mixed    $data        A string containing the CROSS JOIN query or an array of WHERE clauses
 324      *
 325      * @return    string    The db-specific query fragment
 326      */
 327      protected function _sql_custom_build($stage, $data)
 328      {
 329          return $data;
 330      }
 331   
 332      /**
 333      * Close sql connection
 334      *
 335      * @return    bool        False if failure
 336      */
 337      protected function _sql_close()
 338      {
 339          return $this->dbo->close();
 340      }
 341   
 342      /**
 343      * Build db-specific report
 344      *
 345      * @param    string    $mode        Available modes: display, start, stop,
 346      *                                add_select_row, fromcache, record_fromcache
 347      * @param    string    $query        The Query that should be explained
 348      * @return    mixed        Either a full HTML page, boolean or null
 349      */
 350      protected function _sql_report($mode, $query = '')
 351      {
 352          switch ($mode)
 353          {
 354              case 'start':
 355   
 356                  $explain_query = $query;
 357                  if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 358                  {
 359                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 360                  }
 361                  else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 362                  {
 363                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 364                  }
 365   
 366                  if (preg_match('/^SELECT/', $explain_query))
 367                  {
 368                      $html_table = false;
 369   
 370                      if ($result = $this->dbo->query("EXPLAIN QUERY PLAN $explain_query"))
 371                      {
 372                          while ($row = $result->fetchArray(SQLITE3_ASSOC))
 373                          {
 374                              $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 375                          }
 376                      }
 377   
 378                      if ($html_table)
 379                      {
 380                          $this->html_hold .= '</table>';
 381                      }
 382                  }
 383   
 384              break;
 385   
 386              case 'fromcache':
 387                  $endtime = explode(' ', microtime());
 388                  $endtime = $endtime[0] + $endtime[1];
 389   
 390                  $result = $this->dbo->query($query);
 391                  while ($void = $result->fetchArray(SQLITE3_ASSOC))
 392                  {
 393                      // Take the time spent on parsing rows into account
 394                  }
 395   
 396                  $splittime = explode(' ', microtime());
 397                  $splittime = $splittime[0] + $splittime[1];
 398   
 399                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 400   
 401              break;
 402          }
 403      }
 404  }
 405