[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/db/driver/ -> mssqlnative.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  /**
  15  * This is the MS SQL Server Native database abstraction layer.
  16  * PHP mssql native driver required.
  17  * @author Chris Pucci
  18  *
  19  */
  20  
  21  namespace phpbb\db\driver;
  22  
  23  class mssqlnative extends \phpbb\db\driver\mssql_base
  24  {
  25      var $m_insert_id = null;
  26      var $last_query_text = '';
  27      var $query_options = array();
  28      var $connect_error = '';
  29  
  30      /**
  31      * {@inheritDoc}
  32      */
  33  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  34      {
  35          // Test for driver support, to avoid suppressed fatal error
  36          if (!function_exists('sqlsrv_connect'))
  37          {
  38              $this->connect_error = 'Native MS SQL Server driver for PHP is missing or needs to be updated. Version 1.1 or later is required to install phpBB3. You can download the driver from: http://www.microsoft.com/sqlserver/2005/en/us/PHP-Driver.aspx';
  39              return $this->sql_error('');
  40          }
  41  
  42          //set up connection variables
  43          $this->persistency = $persistency;
  44          $this->user = $sqluser;
  45          $this->dbname = $database;
  46          $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
  47          $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
  48  
  49          //connect to database
  50          $this->db_connect_id = sqlsrv_connect($this->server, array(
  51              'Database' => $this->dbname,
  52              'UID' => $this->user,
  53              'PWD' => $sqlpassword,
  54              'CharacterSet' => 'UTF-8'
  55          ));
  56  
  57          return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
  58      }
  59  
  60      /**
  61      * {@inheritDoc}
  62      */
  63  	function sql_server_info($raw = false, $use_cache = true)
  64      {
  65          global $cache;
  66  
  67          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssql_version')) === false)
  68          {
  69              $arr_server_info = sqlsrv_server_info($this->db_connect_id);
  70              $this->sql_server_version = $arr_server_info['SQLServerVersion'];
  71  
  72              if (!empty($cache) && $use_cache)
  73              {
  74                  $cache->put('mssql_version', $this->sql_server_version);
  75              }
  76          }
  77  
  78          if ($raw)
  79          {
  80              return $this->sql_server_version;
  81          }
  82  
  83          return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
  84      }
  85  
  86      /**
  87      * {@inheritDoc}
  88      */
  89  	function sql_buffer_nested_transactions()
  90      {
  91          return true;
  92      }
  93  
  94      /**
  95      * SQL Transaction
  96      * @access private
  97      */
  98  	function _sql_transaction($status = 'begin')
  99      {
 100          switch ($status)
 101          {
 102              case 'begin':
 103                  return sqlsrv_begin_transaction($this->db_connect_id);
 104              break;
 105  
 106              case 'commit':
 107                  return sqlsrv_commit($this->db_connect_id);
 108              break;
 109  
 110              case 'rollback':
 111                  return sqlsrv_rollback($this->db_connect_id);
 112              break;
 113          }
 114          return true;
 115      }
 116  
 117      /**
 118      * {@inheritDoc}
 119      */
 120  	function sql_query($query = '', $cache_ttl = 0)
 121      {
 122          if ($query != '')
 123          {
 124              global $cache;
 125  
 126              // EXPLAIN only in extra debug mode
 127              if (defined('DEBUG'))
 128              {
 129                  $this->sql_report('start', $query);
 130              }
 131              else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 132              {
 133                  $this->curtime = microtime(true);
 134              }
 135  
 136              $this->last_query_text = $query;
 137              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 138              $this->sql_add_num_queries($this->query_result);
 139  
 140              if ($this->query_result === false)
 141              {
 142                  if (($this->query_result = @sqlsrv_query($this->db_connect_id, $query, array(), $this->query_options)) === false)
 143                  {
 144                      $this->sql_error($query);
 145                  }
 146                  // reset options for next query
 147                  $this->query_options = array();
 148  
 149                  if (defined('DEBUG'))
 150                  {
 151                      $this->sql_report('stop', $query);
 152                  }
 153                  else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 154                  {
 155                      $this->sql_time += microtime(true) - $this->curtime;
 156                  }
 157  
 158                  if (!$this->query_result)
 159                  {
 160                      return false;
 161                  }
 162  
 163                  if ($cache && $cache_ttl)
 164                  {
 165                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 166                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 167                  }
 168                  else if (strpos($query, 'SELECT') === 0)
 169                  {
 170                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 171                  }
 172              }
 173              else if (defined('DEBUG'))
 174              {
 175                  $this->sql_report('fromcache', $query);
 176              }
 177          }
 178          else
 179          {
 180              return false;
 181          }
 182          return $this->query_result;
 183      }
 184  
 185      /**
 186      * Build LIMIT query
 187      */
 188  	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 189      {
 190          $this->query_result = false;
 191  
 192          // total == 0 means all results - not zero results
 193          if ($offset == 0 && $total !== 0)
 194          {
 195              if (strpos($query, "SELECT") === false)
 196              {
 197                  $query = "TOP {$total} " . $query;
 198              }
 199              else
 200              {
 201                  $query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP '.$total, $query);
 202              }
 203          }
 204          else if ($offset > 0)
 205          {
 206              $query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP(10000000) ', $query);
 207              $query = 'SELECT *
 208                      FROM (SELECT sub2.*, ROW_NUMBER() OVER(ORDER BY sub2.line2) AS line3
 209                      FROM (SELECT 1 AS line2, sub1.* FROM (' . $query . ') AS sub1) as sub2) AS sub3';
 210  
 211              if ($total > 0)
 212              {
 213                  $query .= ' WHERE line3 BETWEEN ' . ($offset+1) . ' AND ' . ($offset + $total);
 214              }
 215              else
 216              {
 217                  $query .= ' WHERE line3 > ' . $offset;
 218              }
 219          }
 220  
 221          $result = $this->sql_query($query, $cache_ttl);
 222  
 223          return $result;
 224      }
 225  
 226      /**
 227      * {@inheritDoc}
 228      */
 229  	function sql_affectedrows()
 230      {
 231          return ($this->db_connect_id) ? @sqlsrv_rows_affected($this->query_result) : false;
 232      }
 233  
 234      /**
 235      * {@inheritDoc}
 236      */
 237  	function sql_fetchrow($query_id = false)
 238      {
 239          global $cache;
 240  
 241          if ($query_id === false)
 242          {
 243              $query_id = $this->query_result;
 244          }
 245  
 246          if ($cache && $cache->sql_exists($query_id))
 247          {
 248              return $cache->sql_fetchrow($query_id);
 249          }
 250  
 251          if (!$query_id)
 252          {
 253              return false;
 254          }
 255  
 256          $row = sqlsrv_fetch_array($query_id, SQLSRV_FETCH_ASSOC);
 257  
 258          if ($row)
 259          {
 260              foreach ($row as $key => $value)
 261              {
 262                  $row[$key] = ($value === ' ' || $value === null) ? '' : $value;
 263              }
 264  
 265              // remove helper values from LIMIT queries
 266              if (isset($row['line2']))
 267              {
 268                  unset($row['line2'], $row['line3']);
 269              }
 270          }
 271          return ($row !== null) ? $row : false;
 272      }
 273  
 274      /**
 275      * {@inheritDoc}
 276      */
 277  	function sql_nextid()
 278      {
 279          $result_id = @sqlsrv_query($this->db_connect_id, 'SELECT @@IDENTITY');
 280  
 281          if ($result_id)
 282          {
 283              $row = sqlsrv_fetch_array($result_id);
 284              $id = $row[0];
 285              sqlsrv_free_stmt($result_id);
 286              return $id;
 287          }
 288          else
 289          {
 290              return false;
 291          }
 292      }
 293  
 294      /**
 295      * {@inheritDoc}
 296      */
 297  	function sql_freeresult($query_id = false)
 298      {
 299          global $cache;
 300  
 301          if ($query_id === false)
 302          {
 303              $query_id = $this->query_result;
 304          }
 305  
 306          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 307          {
 308              return $cache->sql_freeresult($query_id);
 309          }
 310  
 311          if (isset($this->open_queries[(int) $query_id]))
 312          {
 313              unset($this->open_queries[(int) $query_id]);
 314              return sqlsrv_free_stmt($query_id);
 315          }
 316  
 317          return false;
 318      }
 319  
 320      /**
 321      * return sql error array
 322      * @access private
 323      */
 324  	function _sql_error()
 325      {
 326          if (function_exists('sqlsrv_errors'))
 327          {
 328              $errors = @sqlsrv_errors(SQLSRV_ERR_ERRORS);
 329              $error_message = '';
 330              $code = 0;
 331  
 332              if ($errors != null)
 333              {
 334                  foreach ($errors as $error)
 335                  {
 336                      $error_message .= "SQLSTATE: " . $error['SQLSTATE'] . "\n";
 337                      $error_message .= "code: " . $error['code'] . "\n";
 338                      $code = $error['code'];
 339                      $error_message .= "message: " . $error['message'] . "\n";
 340                  }
 341                  $this->last_error_result = $error_message;
 342                  $error = $this->last_error_result;
 343              }
 344              else
 345              {
 346                  $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
 347              }
 348  
 349              $error = array(
 350                  'message'    => $error,
 351                  'code'        => $code,
 352              );
 353          }
 354          else
 355          {
 356              $error = array(
 357                  'message'    => $this->connect_error,
 358                  'code'        => '',
 359              );
 360          }
 361  
 362          return $error;
 363      }
 364  
 365      /**
 366      * Close sql connection
 367      * @access private
 368      */
 369  	function _sql_close()
 370      {
 371          return @sqlsrv_close($this->db_connect_id);
 372      }
 373  
 374      /**
 375      * Build db-specific report
 376      * @access private
 377      */
 378  	function _sql_report($mode, $query = '')
 379      {
 380          switch ($mode)
 381          {
 382              case 'start':
 383                  $html_table = false;
 384                  @sqlsrv_query($this->db_connect_id, 'SET SHOWPLAN_TEXT ON;');
 385                  if ($result = @sqlsrv_query($this->db_connect_id, $query))
 386                  {
 387                      sqlsrv_next_result($result);
 388                      while ($row = sqlsrv_fetch_array($result))
 389                      {
 390                          $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 391                      }
 392                      sqlsrv_free_stmt($result);
 393                  }
 394                  @sqlsrv_query($this->db_connect_id, 'SET SHOWPLAN_TEXT OFF;');
 395  
 396                  if ($html_table)
 397                  {
 398                      $this->html_hold .= '</table>';
 399                  }
 400              break;
 401  
 402              case 'fromcache':
 403                  $endtime = explode(' ', microtime());
 404                  $endtime = $endtime[0] + $endtime[1];
 405  
 406                  $result = @sqlsrv_query($this->db_connect_id, $query);
 407                  if ($result)
 408                  {
 409                      while ($void = sqlsrv_fetch_array($result))
 410                      {
 411                          // Take the time spent on parsing rows into account
 412                      }
 413                      sqlsrv_free_stmt($result);
 414                  }
 415  
 416                  $splittime = explode(' ', microtime());
 417                  $splittime = $splittime[0] + $splittime[1];
 418  
 419                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 420  
 421              break;
 422          }
 423      }
 424  
 425      /**
 426      * Utility method used to retrieve number of rows
 427      * Emulates mysql_num_rows
 428      * Used in acp_database.php -> write_data_mssqlnative()
 429      * Requires a static or keyset cursor to be definde via
 430      * mssqlnative_set_query_options()
 431      */
 432  	function mssqlnative_num_rows($res)
 433      {
 434          if ($res !== false)
 435          {
 436              return sqlsrv_num_rows($res);
 437          }
 438          else
 439          {
 440              return false;
 441          }
 442      }
 443  
 444      /**
 445      * Allows setting mssqlnative specific query options passed to sqlsrv_query as 4th parameter.
 446      */
 447  	function mssqlnative_set_query_options($options)
 448      {
 449          $this->query_options = $options;
 450      }
 451  }


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