[ Index ]

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


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