[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/db/driver/ -> mysql.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  * MySQL4 Database Abstraction Layer
  18  * Compatible with:
  19  * MySQL 3.23+
  20  * MySQL 4.0+
  21  * MySQL 4.1+
  22  * MySQL 5.0+
  23  */
  24  class mysql extends \phpbb\db\driver\mysql_base
  25  {
  26      var $multi_insert = true;
  27      var $connect_error = '';
  28  
  29      /**
  30      * {@inheritDoc}
  31      */
  32  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  33      {
  34          $this->persistency = $persistency;
  35          $this->user = $sqluser;
  36          $this->server = $sqlserver . (($port) ? ':' . $port : '');
  37          $this->dbname = $database;
  38  
  39          $this->sql_layer = 'mysql4';
  40  
  41          if ($this->persistency)
  42          {
  43              if (!function_exists('mysql_pconnect'))
  44              {
  45                  $this->connect_error = 'mysql_pconnect function does not exist, is mysql extension installed?';
  46                  return $this->sql_error('');
  47              }
  48              $this->db_connect_id = @mysql_pconnect($this->server, $this->user, $sqlpassword);
  49          }
  50          else
  51          {
  52              if (!function_exists('mysql_connect'))
  53              {
  54                  $this->connect_error = 'mysql_connect function does not exist, is mysql extension installed?';
  55                  return $this->sql_error('');
  56              }
  57              $this->db_connect_id = @mysql_connect($this->server, $this->user, $sqlpassword, $new_link);
  58          }
  59  
  60          if ($this->db_connect_id && $this->dbname != '')
  61          {
  62              if (@mysql_select_db($this->dbname, $this->db_connect_id))
  63              {
  64                  // Determine what version we are using and if it natively supports UNICODE
  65                  if (version_compare($this->sql_server_info(true), '4.1.0', '>='))
  66                  {
  67                      @mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
  68  
  69                      // enforce strict mode on databases that support it
  70                      if (version_compare($this->sql_server_info(true), '5.0.2', '>='))
  71                      {
  72                          $result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
  73                          if ($result)
  74                          {
  75                              $row = mysql_fetch_assoc($result);
  76                              mysql_free_result($result);
  77                              $modes = array_map('trim', explode(',', $row['sql_mode']));
  78                          }
  79                          else
  80                          {
  81                              $modes = array();
  82                          }
  83  
  84                          // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
  85                          if (!in_array('TRADITIONAL', $modes))
  86                          {
  87                              if (!in_array('STRICT_ALL_TABLES', $modes))
  88                              {
  89                                  $modes[] = 'STRICT_ALL_TABLES';
  90                              }
  91  
  92                              if (!in_array('STRICT_TRANS_TABLES', $modes))
  93                              {
  94                                  $modes[] = 'STRICT_TRANS_TABLES';
  95                              }
  96                          }
  97  
  98                          $mode = implode(',', $modes);
  99                          @mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
 100                      }
 101                  }
 102                  else if (version_compare($this->sql_server_info(true), '4.0.0', '<'))
 103                  {
 104                      $this->sql_layer = 'mysql';
 105                  }
 106  
 107                  return $this->db_connect_id;
 108              }
 109          }
 110  
 111          return $this->sql_error('');
 112      }
 113  
 114      /**
 115      * {@inheritDoc}
 116      */
 117  	function sql_server_info($raw = false, $use_cache = true)
 118      {
 119          global $cache;
 120  
 121          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mysql_version')) === false)
 122          {
 123              $result = @mysql_query('SELECT VERSION() AS version', $this->db_connect_id);
 124              if ($result)
 125              {
 126                  $row = mysql_fetch_assoc($result);
 127                  mysql_free_result($result);
 128  
 129                  $this->sql_server_version = $row['version'];
 130  
 131                  if (!empty($cache) && $use_cache)
 132                  {
 133                      $cache->put('mysql_version', $this->sql_server_version);
 134                  }
 135              }
 136          }
 137  
 138          return ($raw) ? $this->sql_server_version : 'MySQL ' . $this->sql_server_version;
 139      }
 140  
 141      /**
 142      * SQL Transaction
 143      * @access private
 144      */
 145  	function _sql_transaction($status = 'begin')
 146      {
 147          switch ($status)
 148          {
 149              case 'begin':
 150                  return @mysql_query('BEGIN', $this->db_connect_id);
 151              break;
 152  
 153              case 'commit':
 154                  return @mysql_query('COMMIT', $this->db_connect_id);
 155              break;
 156  
 157              case 'rollback':
 158                  return @mysql_query('ROLLBACK', $this->db_connect_id);
 159              break;
 160          }
 161  
 162          return true;
 163      }
 164  
 165      /**
 166      * {@inheritDoc}
 167      */
 168  	function sql_query($query = '', $cache_ttl = 0)
 169      {
 170          if ($query != '')
 171          {
 172              global $cache;
 173  
 174              // EXPLAIN only in extra debug mode
 175              if (defined('DEBUG'))
 176              {
 177                  $this->sql_report('start', $query);
 178              }
 179              else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 180              {
 181                  $this->curtime = microtime(true);
 182              }
 183  
 184              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 185              $this->sql_add_num_queries($this->query_result);
 186  
 187              if ($this->query_result === false)
 188              {
 189                  if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false)
 190                  {
 191                      $this->sql_error($query);
 192                  }
 193  
 194                  if (defined('DEBUG'))
 195                  {
 196                      $this->sql_report('stop', $query);
 197                  }
 198                  else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 199                  {
 200                      $this->sql_time += microtime(true) - $this->curtime;
 201                  }
 202  
 203                  if (!$this->query_result)
 204                  {
 205                      return false;
 206                  }
 207  
 208                  if ($cache && $cache_ttl)
 209                  {
 210                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 211                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 212                  }
 213                  else if (strpos($query, 'SELECT') === 0)
 214                  {
 215                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 216                  }
 217              }
 218              else if (defined('DEBUG'))
 219              {
 220                  $this->sql_report('fromcache', $query);
 221              }
 222          }
 223          else
 224          {
 225              return false;
 226          }
 227  
 228          return $this->query_result;
 229      }
 230  
 231      /**
 232      * {@inheritDoc}
 233      */
 234  	function sql_affectedrows()
 235      {
 236          if ($this->db_connect_id)
 237          {
 238              // We always want the number of matched rows
 239              // instead of changed rows, when running an update.
 240              // So when mysql_info() returns the number of matched rows
 241              // we return that one instead of mysql_affected_rows()
 242              $mysql_info = @mysql_info($this->db_connect_id);
 243              if ($mysql_info !== false)
 244              {
 245                  $match = array();
 246                  preg_match('#^Rows matched: (\d)+  Changed: (\d)+  Warnings: (\d)+$#', $mysql_info, $match);
 247                  if (isset($match[1]))
 248                  {
 249                      return $match[1];
 250                  }
 251              }
 252  
 253              return @mysql_affected_rows($this->db_connect_id);
 254          }
 255          return false;
 256      }
 257  
 258      /**
 259      * {@inheritDoc}
 260      */
 261  	function sql_fetchrow($query_id = false)
 262      {
 263          global $cache;
 264  
 265          if ($query_id === false)
 266          {
 267              $query_id = $this->query_result;
 268          }
 269  
 270          if ($cache && $cache->sql_exists($query_id))
 271          {
 272              return $cache->sql_fetchrow($query_id);
 273          }
 274  
 275          return ($query_id) ? mysql_fetch_assoc($query_id) : false;
 276      }
 277  
 278      /**
 279      * {@inheritDoc}
 280      */
 281  	function sql_rowseek($rownum, &$query_id)
 282      {
 283          global $cache;
 284  
 285          if ($query_id === false)
 286          {
 287              $query_id = $this->query_result;
 288          }
 289  
 290          if ($cache && $cache->sql_exists($query_id))
 291          {
 292              return $cache->sql_rowseek($rownum, $query_id);
 293          }
 294  
 295          return ($query_id !== false) ? @mysql_data_seek($query_id, $rownum) : false;
 296      }
 297  
 298      /**
 299      * {@inheritDoc}
 300      */
 301  	function sql_nextid()
 302      {
 303          return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false;
 304      }
 305  
 306      /**
 307      * {@inheritDoc}
 308      */
 309  	function sql_freeresult($query_id = false)
 310      {
 311          global $cache;
 312  
 313          if ($query_id === false)
 314          {
 315              $query_id = $this->query_result;
 316          }
 317  
 318          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 319          {
 320              return $cache->sql_freeresult($query_id);
 321          }
 322  
 323          if (isset($this->open_queries[(int) $query_id]))
 324          {
 325              unset($this->open_queries[(int) $query_id]);
 326              return mysql_free_result($query_id);
 327          }
 328  
 329          return false;
 330      }
 331  
 332      /**
 333      * {@inheritDoc}
 334      */
 335  	function sql_escape($msg)
 336      {
 337          if (!$this->db_connect_id)
 338          {
 339              return @mysql_real_escape_string($msg);
 340          }
 341  
 342          return @mysql_real_escape_string($msg, $this->db_connect_id);
 343      }
 344  
 345      /**
 346      * return sql error array
 347      * @access private
 348      */
 349  	function _sql_error()
 350      {
 351          if ($this->db_connect_id)
 352          {
 353              $error = array(
 354                  'message'    => @mysql_error($this->db_connect_id),
 355                  'code'        => @mysql_errno($this->db_connect_id),
 356              );
 357          }
 358          else if (function_exists('mysql_error'))
 359          {
 360              $error = array(
 361                  'message'    => @mysql_error(),
 362                  'code'        => @mysql_errno(),
 363              );
 364          }
 365          else
 366          {
 367              $error = array(
 368                  'message'    => $this->connect_error,
 369                  'code'        => '',
 370              );
 371          }
 372  
 373          return $error;
 374      }
 375  
 376      /**
 377      * Close sql connection
 378      * @access private
 379      */
 380  	function _sql_close()
 381      {
 382          return @mysql_close($this->db_connect_id);
 383      }
 384  
 385      /**
 386      * Build db-specific report
 387      * @access private
 388      */
 389  	function _sql_report($mode, $query = '')
 390      {
 391          static $test_prof;
 392  
 393          // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
 394          if ($test_prof === null)
 395          {
 396              $test_prof = false;
 397              if (version_compare($this->sql_server_info(true), '5.0.37', '>=') && version_compare($this->sql_server_info(true), '5.1', '<'))
 398              {
 399                  $test_prof = true;
 400              }
 401          }
 402  
 403          switch ($mode)
 404          {
 405              case 'start':
 406  
 407                  $explain_query = $query;
 408                  if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 409                  {
 410                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 411                  }
 412                  else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 413                  {
 414                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 415                  }
 416  
 417                  if (preg_match('/^SELECT/', $explain_query))
 418                  {
 419                      $html_table = false;
 420  
 421                      // begin profiling
 422                      if ($test_prof)
 423                      {
 424                          @mysql_query('SET profiling = 1;', $this->db_connect_id);
 425                      }
 426  
 427                      if ($result = @mysql_query("EXPLAIN $explain_query", $this->db_connect_id))
 428                      {
 429                          while ($row = mysql_fetch_assoc($result))
 430                          {
 431                              $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 432                          }
 433                          mysql_free_result($result);
 434                      }
 435  
 436                      if ($html_table)
 437                      {
 438                          $this->html_hold .= '</table>';
 439                      }
 440  
 441                      if ($test_prof)
 442                      {
 443                          $html_table = false;
 444  
 445                          // get the last profile
 446                          if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
 447                          {
 448                              $this->html_hold .= '<br />';
 449                              while ($row = mysql_fetch_assoc($result))
 450                              {
 451                                  // make <unknown> HTML safe
 452                                  if (!empty($row['Source_function']))
 453                                  {
 454                                      $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
 455                                  }
 456  
 457                                  // remove unsupported features
 458                                  foreach ($row as $key => $val)
 459                                  {
 460                                      if ($val === null)
 461                                      {
 462                                          unset($row[$key]);
 463                                      }
 464                                  }
 465                                  $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 466                              }
 467                              mysql_free_result($result);
 468                          }
 469  
 470                          if ($html_table)
 471                          {
 472                              $this->html_hold .= '</table>';
 473                          }
 474  
 475                          @mysql_query('SET profiling = 0;', $this->db_connect_id);
 476                      }
 477                  }
 478  
 479              break;
 480  
 481              case 'fromcache':
 482                  $endtime = explode(' ', microtime());
 483                  $endtime = $endtime[0] + $endtime[1];
 484  
 485                  $result = @mysql_query($query, $this->db_connect_id);
 486                  if ($result)
 487                  {
 488                      while ($void = mysql_fetch_assoc($result))
 489                      {
 490                          // Take the time spent on parsing rows into account
 491                      }
 492                      mysql_free_result($result);
 493                  }
 494  
 495                  $splittime = explode(' ', microtime());
 496                  $splittime = $splittime[0] + $splittime[1];
 497  
 498                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 499  
 500              break;
 501          }
 502      }
 503  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1