[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

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

   1  <?php
   2  /** 
   3  *
   4  * @package dbal
   5  * @version $Id: mysqli.php,v 1.27 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  * MySQLi Database Abstraction Layer
  23  * mysqli-extension has to be compiled with:
  24  * MySQL 4.1+ or MySQL 5.0+
  25  * @package dbal
  26  */
  27  class dbal_mysqli extends dbal
  28  {
  29      /**
  30      * Connect to server
  31      */
  32  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
  33      {
  34          $this->persistency = $persistency;
  35          $this->user = $sqluser;
  36          $this->server = $sqlserver;
  37          $this->dbname = $database;
  38          $port = (!$port) ? NULL : $port;
  39  
  40          // Persistant connections not supported by the mysqli extension?
  41          $this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port);
  42  
  43          if ($this->db_connect_id && $this->dbname != '')
  44          {
  45              @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
  46              return $this->db_connect_id;
  47          }
  48  
  49          return $this->sql_error('');
  50      }
  51  
  52      /**
  53      * Version information about used database
  54      */
  55  	function sql_server_info()
  56      {
  57          return 'MySQL(i) ' . @mysqli_get_server_info($this->db_connect_id);
  58      }
  59  
  60      /**
  61      * SQL Transaction
  62      * @access private
  63      */
  64  	function _sql_transaction($status = 'begin')
  65      {
  66          switch ($status)
  67          {
  68              case 'begin':
  69                  return @mysqli_autocommit($this->db_connect_id, false);
  70              break;
  71  
  72              case 'commit':
  73                  $result = @mysqli_commit($this->db_connect_id);
  74                  @mysqli_autocommit($this->db_connect_id, true);
  75                  return $result;
  76              break;
  77  
  78              case 'rollback':
  79                  $result = @mysqli_rollback($this->db_connect_id);
  80                  @mysqli_autocommit($this->db_connect_id, true);
  81                  return $result;
  82              break;
  83          }
  84  
  85          return true;
  86      }
  87  
  88      /**
  89      * Base query method
  90      *
  91      * @param    string    $query        Contains the SQL query which shall be executed
  92      * @param    int        $cache_ttl    Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  93      * @return    mixed                When casted to bool the returned value returns true on success and false on failure
  94      *
  95      * @access    public
  96      */
  97  	function sql_query($query = '', $cache_ttl = 0)
  98      {
  99          if ($query != '')
 100          {
 101              global $cache;
 102  
 103              // EXPLAIN only in extra debug mode
 104              if (defined('DEBUG_EXTRA'))
 105              {
 106                  $this->sql_report('start', $query);
 107              }
 108  
 109              $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 110              $this->sql_add_num_queries($this->query_result);
 111  
 112              if ($this->query_result === false)
 113              {
 114                  if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
 115                  {
 116                      $this->sql_error($query);
 117                  }
 118  
 119                  if (defined('DEBUG_EXTRA'))
 120                  {
 121                      $this->sql_report('stop', $query);
 122                  }
 123  
 124                  if ($cache_ttl && method_exists($cache, 'sql_save'))
 125                  {
 126                      $cache->sql_save($query, $this->query_result, $cache_ttl);
 127                  }
 128              }
 129              else if (defined('DEBUG_EXTRA'))
 130              {
 131                  $this->sql_report('fromcache', $query);
 132              }
 133          }
 134          else
 135          {
 136              return false;
 137          }
 138  
 139          return ($this->query_result) ? $this->query_result : false;
 140      }
 141  
 142      /**
 143      * Build LIMIT query
 144      */
 145  	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
 146      {
 147          if ($query != '')
 148          {
 149              $this->query_result = false;
 150  
 151              // if $total is set to 0 we do not want to limit the number of rows
 152              if ($total == 0)
 153              {
 154                  // MySQL 4.1+ no longer supports -1 in limit queries
 155                  $total = '18446744073709551615';
 156              }
 157  
 158              $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 159  
 160              return $this->sql_query($query, $cache_ttl);
 161          }
 162          else
 163          {
 164              return false;
 165          }
 166      }
 167  
 168      /**
 169      * Return number of affected rows
 170      */
 171  	function sql_affectedrows()
 172      {
 173          return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
 174      }
 175  
 176      /**
 177      * Fetch current row
 178      */
 179  	function sql_fetchrow($query_id = false)
 180      {
 181          global $cache;
 182  
 183          if ($query_id === false)
 184          {
 185              $query_id = $this->query_result;
 186          }
 187  
 188          if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
 189          {
 190              return $cache->sql_fetchrow($query_id);
 191          }
 192  
 193          return ($query_id !== false) ? @mysqli_fetch_assoc($query_id) : false;
 194      }
 195  
 196      /**
 197      * Seek to given row number
 198      * rownum is zero-based
 199      */
 200  	function sql_rowseek($rownum, $query_id = false)
 201      {
 202          global $cache;
 203  
 204          if ($query_id === false)
 205          {
 206              $query_id = $this->query_result;
 207          }
 208  
 209          if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
 210          {
 211              return $cache->sql_rowseek($rownum, $query_id);
 212          }
 213  
 214          return ($query_id !== false) ? @mysqli_data_seek($query_id, $rownum) : false;
 215      }
 216  
 217      /**
 218      * Get last inserted id after insert statement
 219      */
 220  	function sql_nextid()
 221      {
 222          return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
 223      }
 224  
 225      /**
 226      * Free sql result
 227      */
 228  	function sql_freeresult($query_id = false)
 229      {
 230          global $cache;
 231  
 232          if ($query_id === false)
 233          {
 234              $query_id = $this->query_result;
 235          }
 236  
 237          if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
 238          {
 239              return $cache->sql_freeresult($query_id);
 240          }
 241  
 242          return @mysqli_free_result($query_id);
 243      }
 244  
 245      /**
 246      * Escape string used in sql query
 247      */
 248  	function sql_escape($msg)
 249      {
 250          return @mysqli_real_escape_string($this->db_connect_id, $msg);
 251      }
 252  
 253      /**
 254      * Build db-specific query data
 255      * @access private
 256      */
 257  	function _sql_custom_build($stage, $data)
 258      {
 259          switch ($stage)
 260          {
 261              case 'FROM':
 262                  $data = '(' . $data . ')';
 263              break;
 264          }
 265  
 266          return $data;
 267      }
 268  
 269      /**
 270      * return sql error array
 271      * @access private
 272      */
 273  	function _sql_error()
 274      {
 275          if (!$this->db_connect_id)
 276          {
 277              return array(
 278                  'message'    => @mysqli_connect_error(),
 279                  'code'        => @mysqli_connect_errno()
 280              );
 281          }
 282  
 283          return array(
 284              'message'    => @mysqli_error($this->db_connect_id),
 285              'code'        => @mysqli_errno($this->db_connect_id)
 286          );
 287      }
 288  
 289      /**
 290      * Close sql connection
 291      * @access private
 292      */
 293  	function _sql_close()
 294      {
 295          return @mysqli_close($this->db_connect_id);
 296      }
 297  
 298      /**
 299      * Build db-specific report
 300      * @access private
 301      */
 302  	function _sql_report($mode, $query = '')
 303      {
 304          switch ($mode)
 305          {
 306              case 'start':
 307  
 308                  $explain_query = $query;
 309                  if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 310                  {
 311                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 312                  }
 313                  else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 314                  {
 315                      $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 316                  }
 317  
 318                  if (preg_match('/^SELECT/', $explain_query))
 319                  {
 320                      $html_table = false;
 321  
 322                      if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
 323                      {
 324                          while ($row = @mysqli_fetch_assoc($result))
 325                          {
 326                              $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 327                          }
 328                      }
 329                      @mysqli_free_result($result);
 330  
 331                      if ($html_table)
 332                      {
 333                          $this->html_hold .= '</table>';
 334                      }
 335                  }
 336  
 337              break;
 338  
 339              case 'fromcache':
 340                  $endtime = explode(' ', microtime());
 341                  $endtime = $endtime[0] + $endtime[1];
 342  
 343                  $result = @mysqli_query($this->db_connect_id, $query);
 344                  while ($void = @mysqli_fetch_assoc($result))
 345                  {
 346                      // Take the time spent on parsing rows into account
 347                  }
 348                  @mysqli_free_result($result);
 349  
 350                  $splittime = explode(' ', microtime());
 351                  $splittime = $splittime[0] + $splittime[1];
 352  
 353                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 354  
 355              break;
 356          }
 357      }
 358  }
 359  
 360  ?>


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