[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/db/driver/ -> postgres.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  * PostgreSQL Database Abstraction Layer
  18  * Minimum Requirement is Version 8.3+
  19  */
  20  class postgres extends \phpbb\db\driver\driver
  21  {
  22      var $multi_insert = true;
  23      var $last_query_text = '';
  24      var $connect_error = '';
  25  
  26      /**
  27      * {@inheritDoc}
  28      */
  29  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  30      {
  31          $connect_string = '';
  32  
  33          if ($sqluser)
  34          {
  35              $connect_string .= "user=$sqluser ";
  36          }
  37  
  38          if ($sqlpassword)
  39          {
  40              $connect_string .= "password='$sqlpassword' ";
  41          }
  42  
  43          if ($sqlserver)
  44          {
  45              // $sqlserver can carry a port separated by : for compatibility reasons
  46              // If $sqlserver has more than one : it's probably an IPv6 address.
  47              // In this case we only allow passing a port via the $port variable.
  48              if (substr_count($sqlserver, ':') === 1)
  49              {
  50                  list($sqlserver, $port) = explode(':', $sqlserver);
  51              }
  52  
  53              if ($sqlserver !== 'localhost')
  54              {
  55                  $connect_string .= "host=$sqlserver ";
  56              }
  57  
  58              if ($port)
  59              {
  60                  $connect_string .= "port=$port ";
  61              }
  62          }
  63  
  64          $schema = '';
  65  
  66          if ($database)
  67          {
  68              $this->dbname = $database;
  69              if (strpos($database, '.') !== false)
  70              {
  71                  list($database, $schema) = explode('.', $database);
  72              }
  73              $connect_string .= "dbname=$database";
  74          }
  75  
  76          $this->persistency = $persistency;
  77  
  78          if ($this->persistency)
  79          {
  80              if (!function_exists('pg_pconnect'))
  81              {
  82                  $this->connect_error = 'pg_pconnect function does not exist, is pgsql extension installed?';
  83                  return $this->sql_error('');
  84              }
  85              $collector = new \phpbb\error_collector;
  86              $collector->install();
  87              $this->db_connect_id = (!$new_link) ? @pg_pconnect($connect_string) : @pg_pconnect($connect_string, PGSQL_CONNECT_FORCE_NEW);
  88          }
  89          else
  90          {
  91              if (!function_exists('pg_connect'))
  92              {
  93                  $this->connect_error = 'pg_connect function does not exist, is pgsql extension installed?';
  94                  return $this->sql_error('');
  95              }
  96              $collector = new \phpbb\error_collector;
  97              $collector->install();
  98              $this->db_connect_id = (!$new_link) ? @pg_connect($connect_string) : @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW);
  99          }
 100  
 101          $collector->uninstall();
 102  
 103          if ($this->db_connect_id)
 104          {
 105              if ($schema !== '')
 106              {
 107                  @pg_query($this->db_connect_id, 'SET search_path TO ' . $schema);
 108              }
 109              return $this->db_connect_id;
 110          }
 111  
 112          $this->connect_error = $collector->format_errors();
 113          return $this->sql_error('');
 114      }
 115  
 116      /**
 117      * {@inheritDoc}
 118      */
 119  	function sql_server_info($raw = false, $use_cache = true)
 120      {
 121          global $cache;
 122  
 123          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('pgsql_version')) === false)
 124          {
 125              $query_id = @pg_query($this->db_connect_id, 'SELECT VERSION() AS version');
 126              if ($query_id)
 127              {
 128                  $row = pg_fetch_assoc($query_id, null);
 129                  pg_free_result($query_id);
 130  
 131                  $this->sql_server_version = (!empty($row['version'])) ? trim(substr($row['version'], 10)) : 0;
 132  
 133                  if (!empty($cache) && $use_cache)
 134                  {
 135                      $cache->put('pgsql_version', $this->sql_server_version);
 136                  }
 137              }
 138          }
 139  
 140          return ($raw) ? $this->sql_server_version : 'PostgreSQL ' . $this->sql_server_version;
 141      }
 142  
 143      /**
 144      * SQL Transaction
 145      * @access private
 146      */
 147  	function _sql_transaction($status = 'begin')
 148      {
 149          switch ($status)
 150          {
 151              case 'begin':
 152                  return @pg_query($this->db_connect_id, 'BEGIN');
 153              break;
 154  
 155              case 'commit':
 156                  return @pg_query($this->db_connect_id, 'COMMIT');
 157              break;
 158  
 159              case 'rollback':
 160                  return @pg_query($this->db_connect_id, 'ROLLBACK');
 161              break;
 162          }
 163  
 164          return true;
 165      }
 166  
 167      /**
 168      * {@inheritDoc}
 169      */
 170  	function sql_query($query = '', $cache_ttl = 0)
 171      {
 172          if ($query != '')
 173          {
 174              global $cache;
 175  
 176              // EXPLAIN only in extra debug mode
 177              if (defined('DEBUG'))
 178              {
 179                  $this->sql_report('start', $query);
 180              }
 181              else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 182              {
 183                  $this->curtime = microtime(true);
 184              }
 185  
 186              $this->last_query_text = $query;
 187              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 188              $this->sql_add_num_queries($this->query_result);
 189  
 190              if ($this->query_result === false)
 191              {
 192                  if (($this->query_result = @pg_query($this->db_connect_id, $query)) === false)
 193                  {
 194                      $this->sql_error($query);
 195                  }
 196  
 197                  if (defined('DEBUG'))
 198                  {
 199                      $this->sql_report('stop', $query);
 200                  }
 201                  else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 202                  {
 203                      $this->sql_time += microtime(true) - $this->curtime;
 204                  }
 205  
 206                  if (!$this->query_result)
 207                  {
 208                      return false;
 209                  }
 210  
 211                  if ($cache && $cache_ttl)
 212                  {
 213                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 214                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 215                  }
 216                  else if (strpos($query, 'SELECT') === 0)
 217                  {
 218                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 219                  }
 220              }
 221              else if (defined('DEBUG'))
 222              {
 223                  $this->sql_report('fromcache', $query);
 224              }
 225          }
 226          else
 227          {
 228              return false;
 229          }
 230  
 231          return $this->query_result;
 232      }
 233  
 234      /**
 235      * Build db-specific query data
 236      * @access private
 237      */
 238  	function _sql_custom_build($stage, $data)
 239      {
 240          return $data;
 241      }
 242  
 243      /**
 244      * Build LIMIT query
 245      */
 246  	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 247      {
 248          $this->query_result = false;
 249  
 250          // if $total is set to 0 we do not want to limit the number of rows
 251          if ($total == 0)
 252          {
 253              $total = 'ALL';
 254          }
 255  
 256          $query .= "\n LIMIT $total OFFSET $offset";
 257  
 258          return $this->sql_query($query, $cache_ttl);
 259      }
 260  
 261      /**
 262      * {@inheritDoc}
 263      */
 264  	function sql_affectedrows()
 265      {
 266          return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
 267      }
 268  
 269      /**
 270      * {@inheritDoc}
 271      */
 272  	function sql_fetchrow($query_id = false)
 273      {
 274          global $cache;
 275  
 276          if ($query_id === false)
 277          {
 278              $query_id = $this->query_result;
 279          }
 280  
 281          if ($cache && $cache->sql_exists($query_id))
 282          {
 283              return $cache->sql_fetchrow($query_id);
 284          }
 285  
 286          return ($query_id) ? pg_fetch_assoc($query_id, null) : false;
 287      }
 288  
 289      /**
 290      * {@inheritDoc}
 291      */
 292  	function sql_rowseek($rownum, &$query_id)
 293      {
 294          global $cache;
 295  
 296          if ($query_id === false)
 297          {
 298              $query_id = $this->query_result;
 299          }
 300  
 301          if ($cache && $cache->sql_exists($query_id))
 302          {
 303              return $cache->sql_rowseek($rownum, $query_id);
 304          }
 305  
 306          return ($query_id) ? @pg_result_seek($query_id, $rownum) : false;
 307      }
 308  
 309      /**
 310      * {@inheritDoc}
 311      */
 312  	function sql_nextid()
 313      {
 314          $query_id = $this->query_result;
 315  
 316          if ($query_id !== false && $this->last_query_text != '')
 317          {
 318              if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
 319              {
 320                  $query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
 321                  $temp_q_id = @pg_query($this->db_connect_id, $query);
 322  
 323                  if (!$temp_q_id)
 324                  {
 325                      return false;
 326                  }
 327  
 328                  $temp_result = pg_fetch_assoc($temp_q_id, null);
 329                  pg_free_result($query_id);
 330  
 331                  return ($temp_result) ? $temp_result['last_value'] : false;
 332              }
 333          }
 334  
 335          return false;
 336      }
 337  
 338      /**
 339      * {@inheritDoc}
 340      */
 341  	function sql_freeresult($query_id = false)
 342      {
 343          global $cache;
 344  
 345          if ($query_id === false)
 346          {
 347              $query_id = $this->query_result;
 348          }
 349  
 350          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 351          {
 352              return $cache->sql_freeresult($query_id);
 353          }
 354  
 355          if (isset($this->open_queries[(int) $query_id]))
 356          {
 357              unset($this->open_queries[(int) $query_id]);
 358              return pg_free_result($query_id);
 359          }
 360  
 361          return false;
 362      }
 363  
 364      /**
 365      * {@inheritDoc}
 366      */
 367  	function sql_escape($msg)
 368      {
 369          return @pg_escape_string($msg);
 370      }
 371  
 372      /**
 373      * Build LIKE expression
 374      * @access private
 375      */
 376  	function _sql_like_expression($expression)
 377      {
 378          return $expression;
 379      }
 380  
 381      /**
 382      * Build NOT LIKE expression
 383      * @access private
 384      */
 385  	function _sql_not_like_expression($expression)
 386      {
 387          return $expression;
 388      }
 389  
 390      /**
 391      * {@inheritDoc}
 392      */
 393  	function cast_expr_to_bigint($expression)
 394      {
 395          return 'CAST(' . $expression . ' as DECIMAL(255, 0))';
 396      }
 397  
 398      /**
 399      * {@inheritDoc}
 400      */
 401  	function cast_expr_to_string($expression)
 402      {
 403          return 'CAST(' . $expression . ' as VARCHAR(255))';
 404      }
 405  
 406      /**
 407      * return sql error array
 408      * @access private
 409      */
 410  	function _sql_error()
 411      {
 412          // pg_last_error only works when there is an established connection.
 413          // Connection errors have to be tracked by us manually.
 414          if ($this->db_connect_id)
 415          {
 416              $message = @pg_last_error($this->db_connect_id);
 417          }
 418          else
 419          {
 420              $message = $this->connect_error;
 421          }
 422  
 423          return array(
 424              'message'    => $message,
 425              'code'        => ''
 426          );
 427      }
 428  
 429      /**
 430      * Close sql connection
 431      * @access private
 432      */
 433  	function _sql_close()
 434      {
 435          return @pg_close($this->db_connect_id);
 436      }
 437  
 438      /**
 439      * Build db-specific report
 440      * @access private
 441      */
 442  	function _sql_report($mode, $query = '')
 443      {
 444          switch ($mode)
 445          {
 446              case 'start':
 447  
 448                  $explain_query = $query;
 449                  if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 450                  {
 451                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 452                  }
 453                  else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 454                  {
 455                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 456                  }
 457  
 458                  if (preg_match('/^SELECT/', $explain_query))
 459                  {
 460                      $html_table = false;
 461  
 462                      if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
 463                      {
 464                          while ($row = pg_fetch_assoc($result, null))
 465                          {
 466                              $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 467                          }
 468                          pg_free_result($result);
 469                      }
 470  
 471                      if ($html_table)
 472                      {
 473                          $this->html_hold .= '</table>';
 474                      }
 475                  }
 476  
 477              break;
 478  
 479              case 'fromcache':
 480                  $endtime = explode(' ', microtime());
 481                  $endtime = $endtime[0] + $endtime[1];
 482  
 483                  $result = @pg_query($this->db_connect_id, $query);
 484                  if ($result)
 485                  {
 486                      while ($void = pg_fetch_assoc($result, null))
 487                      {
 488                          // Take the time spent on parsing rows into account
 489                      }
 490                      pg_free_result($result);
 491                  }
 492  
 493                  $splittime = explode(' ', microtime());
 494                  $splittime = $splittime[0] + $splittime[1];
 495  
 496                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 497  
 498              break;
 499          }
 500      }
 501  }


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