[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/includes/db/ -> postgres.php (source)

   1  <?php
   2  /** 
   3  *
   4  * @package dbal
   5  * @version $Id: postgres.php,v 1.40 2006/10/14 14:56:44 acydburn Exp $
   6  * @copyright (c) 2005 phpBB Group 
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
   8  *
   9  */
  10  
  11  /**
  12  * @ignore
  13  */
  14  if (!defined('IN_PHPBB'))
  15  {
  16      exit;
  17  }
  18  
  19  include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
  20  
  21  /**
  22  * PostgreSQL Database Abstraction Layer
  23  * Minimum Requirement is Version 7.3+
  24  * @package dbal
  25  */
  26  class dbal_postgres extends dbal
  27  {
  28      var $last_query_text = '';
  29      
  30      /**
  31      * Connect to server
  32      */
  33  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
  34      {
  35          $connect_string = '';
  36  
  37          if ($sqluser)
  38          {
  39              $connect_string .= "user=$sqluser ";
  40          }
  41  
  42          if ($sqlpassword)
  43          {
  44              $connect_string .= "password=$sqlpassword ";
  45          }
  46  
  47          if ($sqlserver)
  48          {
  49              if (strpos($sqlserver, ':') !== false)
  50              {
  51                  list($sqlserver, $port) = explode(':', $sqlserver);
  52              }
  53  
  54              if ($sqlserver !== 'localhost')
  55              {
  56                  $connect_string .= "host=$sqlserver ";
  57              }
  58          
  59              if ($port)
  60              {
  61                  $connect_string .= "port=$port ";
  62              }
  63          }
  64  
  65          if ($database)
  66          {
  67              $this->dbname = $database;
  68              $connect_string .= "dbname=$database";
  69          }
  70  
  71          $this->persistency = $persistency;
  72  
  73          $this->db_connect_id = ($this->persistency) ? @pg_pconnect($connect_string) : @pg_connect($connect_string);
  74  
  75          return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
  76      }
  77  
  78      /**
  79      * Version information about used database
  80      */
  81  	function sql_server_info()
  82      {
  83          if (version_compare(phpversion(), '5.0.0', '>='))
  84          {
  85              $version = @pg_version($this->db_connect_id);
  86              return 'PostgreSQL' . ((!empty($version)) ? ' ' . $version['client'] : '');
  87          }
  88          else
  89          {
  90              $query_id = @pg_query($this->db_connect_id, 'select version()');
  91              $row = @pg_fetch_assoc($query_id, null);
  92              @pg_free_result($query_id);
  93  
  94              $version = $row['version'];
  95              return ((!empty($version)) ? ' ' . $version : '');
  96          }
  97      }
  98  
  99      /**
 100      * SQL Transaction
 101      * @access private
 102      */
 103  	function _sql_transaction($status = 'begin')
 104      {
 105          switch ($status)
 106          {
 107              case 'begin':
 108                  return @pg_query($this->db_connect_id, 'BEGIN');
 109              break;
 110  
 111              case 'commit':
 112                  return @pg_query($this->db_connect_id, 'COMMIT');
 113              break;
 114  
 115              case 'rollback':
 116                  return @pg_query($this->db_connect_id, 'ROLLBACK');
 117              break;
 118          }
 119  
 120          return true;
 121      }
 122  
 123      /**
 124      * Base query method
 125      *
 126      * @param    string    $query        Contains the SQL query which shall be executed
 127      * @param    int        $cache_ttl    Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
 128      * @return    mixed                When casted to bool the returned value returns true on success and false on failure
 129      *
 130      * @access    public
 131      */
 132  	function sql_query($query = '', $cache_ttl = 0)
 133      {
 134          if ($query != '')
 135          {
 136              global $cache;
 137  
 138              // EXPLAIN only in extra debug mode
 139              if (defined('DEBUG_EXTRA'))
 140              {
 141                  $this->sql_report('start', $query);
 142              }
 143  
 144              $this->last_query_text = $query;
 145              $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 146              $this->sql_add_num_queries($this->query_result);
 147  
 148              if ($this->query_result === false)
 149              {
 150                  if (($this->query_result = @pg_query($this->db_connect_id, $query)) === false)
 151                  {
 152                      $this->sql_error($query);
 153                  }
 154  
 155                  if (defined('DEBUG_EXTRA'))
 156                  {
 157                      $this->sql_report('stop', $query);
 158                  }
 159  
 160                  if ($cache_ttl && method_exists($cache, 'sql_save'))
 161                  {
 162                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 163                      $cache->sql_save($query, $this->query_result, $cache_ttl);
 164                  }
 165                  else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 166                  {
 167                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 168                  }
 169              }
 170              else if (defined('DEBUG_EXTRA'))
 171              {
 172                  $this->sql_report('fromcache', $query);
 173              }
 174          }
 175          else
 176          {
 177              return false;
 178          }
 179  
 180          return ($this->query_result) ? $this->query_result : false;
 181      }
 182  
 183      /**
 184      * Build db-specific query data
 185      * @access private
 186      */
 187  	function _sql_custom_build($stage, $data)
 188      {
 189          return $data;
 190      }
 191  
 192      /**
 193      * Build LIMIT query
 194      */
 195  	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
 196      { 
 197          if ($query != '')
 198          {
 199              $this->query_result = false; 
 200  
 201              // if $total is set to 0 we do not want to limit the number of rows
 202              if ($total == 0)
 203              {
 204                  $total = -1;
 205              }
 206  
 207              $query .= "\n LIMIT $total OFFSET $offset";
 208  
 209              return $this->sql_query($query, $cache_ttl); 
 210          }
 211          else
 212          {
 213              return false; 
 214          }
 215      }
 216  
 217      /**
 218      * Return number of affected rows
 219      */
 220  	function sql_affectedrows()
 221      {
 222          return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
 223      }
 224  
 225      /**
 226      * Fetch current row
 227      */
 228  	function sql_fetchrow($query_id = false)
 229      {
 230          global $cache;
 231  
 232          if ($query_id === false)
 233          {
 234              $query_id = $this->query_result;
 235          }
 236  
 237          if (isset($cache->sql_rowset[$query_id]))
 238          {
 239              return $cache->sql_fetchrow($query_id);
 240          }
 241  
 242          return ($query_id !== false) ? @pg_fetch_assoc($query_id, null) : false;
 243      }
 244  
 245      /**
 246      * Seek to given row number
 247      * rownum is zero-based
 248      */
 249  	function sql_rowseek($rownum, $query_id = false)
 250      {
 251          global $cache;
 252  
 253          if ($query_id === false)
 254          {
 255              $query_id = $this->query_result;
 256          }
 257  
 258          if (isset($cache->sql_rowset[$query_id]))
 259          {
 260              return $cache->sql_rowseek($rownum, $query_id);
 261          }
 262  
 263          return ($query_id !== false) ? @pg_result_seek($query_id, $rownum) : false;
 264      }
 265  
 266      /**
 267      * Get last inserted id after insert statement
 268      */
 269  	function sql_nextid()
 270      {
 271          $query_id = $this->query_result;
 272  
 273          if ($query_id !== false && $this->last_query_text != '')
 274          {
 275              if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
 276              {
 277                  $query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
 278                  $temp_q_id =  @pg_query($this->db_connect_id, $query);
 279  
 280                  if (!$temp_q_id)
 281                  {
 282                      return false;
 283                  }
 284  
 285                  $temp_result = @pg_fetch_assoc($temp_q_id, NULL);
 286                  @pg_free_result($query_id);
 287  
 288                  return ($temp_result) ? $temp_result['last_value'] : false;
 289              }
 290          }
 291  
 292          return false;
 293      }
 294  
 295      /**
 296      * Free sql result
 297      */
 298  	function sql_freeresult($query_id = false)
 299      {
 300          global $cache;
 301  
 302          if ($query_id === false)
 303          {
 304              $query_id = $this->query_result;
 305          }
 306  
 307          if (isset($cache->sql_rowset[$query_id]))
 308          {
 309              return $cache->sql_freeresult($query_id);
 310          }
 311  
 312          if (isset($this->open_queries[(int) $query_id]))
 313          {
 314              unset($this->open_queries[(int) $query_id]);
 315              return @pg_free_result($query_id);
 316          }
 317  
 318          return false;
 319      }
 320  
 321      /**
 322      * Escape string used in sql query
 323      * Note: Do not use for bytea values if we may use them at a later stage
 324      */
 325  	function sql_escape($msg)
 326      {
 327          return @pg_escape_string($msg);
 328      }
 329  
 330      /**
 331      * return sql error array
 332      * @access private
 333      */
 334  	function _sql_error()
 335      {
 336          return array(
 337              'message'    => (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id),
 338              'code'        => ''
 339          );
 340      }
 341  
 342      /**
 343      * Close sql connection
 344      * @access private
 345      */
 346  	function _sql_close()
 347      {
 348          return @pg_close($this->db_connect_id);
 349      }
 350  
 351      /**
 352      * Build db-specific report
 353      * @access private
 354      */
 355  	function _sql_report($mode, $query = '')
 356      {
 357          switch ($mode)
 358          {
 359              case 'start':
 360  
 361                  $explain_query = $query;
 362                  if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 363                  {
 364                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 365                  }
 366                  else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 367                  {
 368                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 369                  }
 370  
 371                  if (preg_match('/^SELECT/', $explain_query))
 372                  {
 373                      $html_table = false;
 374  
 375                      if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
 376                      {
 377                          while ($row = @pg_fetch_assoc($result, NULL))
 378                          {
 379                              $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 380                          }
 381                      }
 382                      @pg_free_result($result);
 383  
 384                      if ($html_table)
 385                      {
 386                          $this->html_hold .= '</table>';
 387                      }
 388                  }
 389  
 390              break;
 391  
 392              case 'fromcache':
 393                  $endtime = explode(' ', microtime());
 394                  $endtime = $endtime[0] + $endtime[1];
 395  
 396                  $result = @pg_query($this->db_connect_id, $query);
 397                  while ($void = @pg_fetch_assoc($result, NULL))
 398                  {
 399                      // Take the time spent on parsing rows into account
 400                  }
 401                  @pg_free_result($result);
 402  
 403                  $splittime = explode(' ', microtime());
 404                  $splittime = $splittime[0] + $splittime[1];
 405  
 406                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 407  
 408              break;
 409          }
 410      }
 411  }
 412  
 413  ?>


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6