[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/db/driver/ -> mssql.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  * MSSQL Database Abstraction Layer
  18  * Minimum Requirement is MSSQL 2000+
  19  */
  20  class mssql extends \phpbb\db\driver\driver
  21  {
  22      var $connect_error = '';
  23  
  24      /**
  25      * {@inheritDoc}
  26      */
  27  	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  28      {
  29          if (!function_exists('mssql_connect'))
  30          {
  31              $this->connect_error = 'mssql_connect function does not exist, is mssql extension installed?';
  32              return $this->sql_error('');
  33          }
  34  
  35          $this->persistency = $persistency;
  36          $this->user = $sqluser;
  37          $this->dbname = $database;
  38  
  39          $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
  40          $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
  41  
  42          @ini_set('mssql.charset', 'UTF-8');
  43          @ini_set('mssql.textlimit', 2147483647);
  44          @ini_set('mssql.textsize', 2147483647);
  45  
  46          $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
  47  
  48          if ($this->db_connect_id && $this->dbname != '')
  49          {
  50              if (!@mssql_select_db($this->dbname, $this->db_connect_id))
  51              {
  52                  @mssql_close($this->db_connect_id);
  53                  return false;
  54              }
  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              $result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
  70  
  71              $row = false;
  72              if ($result_id)
  73              {
  74                  $row = @mssql_fetch_assoc($result_id);
  75                  @mssql_free_result($result_id);
  76              }
  77  
  78              $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
  79  
  80              if (!empty($cache) && $use_cache)
  81              {
  82                  $cache->put('mssql_version', $this->sql_server_version);
  83              }
  84          }
  85  
  86          if ($raw)
  87          {
  88              return $this->sql_server_version;
  89          }
  90  
  91          return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
  92      }
  93  
  94      /**
  95      * {@inheritDoc}
  96      */
  97  	public function sql_concatenate($expr1, $expr2)
  98      {
  99          return $expr1 . ' + ' . $expr2;
 100      }
 101  
 102      /**
 103      * SQL Transaction
 104      * @access private
 105      */
 106  	function _sql_transaction($status = 'begin')
 107      {
 108          switch ($status)
 109          {
 110              case 'begin':
 111                  return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
 112              break;
 113  
 114              case 'commit':
 115                  return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
 116              break;
 117  
 118              case 'rollback':
 119                  return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
 120              break;
 121          }
 122  
 123          return true;
 124      }
 125  
 126      /**
 127      * {@inheritDoc}
 128      */
 129  	function sql_query($query = '', $cache_ttl = 0)
 130      {
 131          if ($query != '')
 132          {
 133              global $cache;
 134  
 135              // EXPLAIN only in extra debug mode
 136              if (defined('DEBUG'))
 137              {
 138                  $this->sql_report('start', $query);
 139              }
 140              else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 141              {
 142                  $this->curtime = microtime(true);
 143              }
 144  
 145              $this->query_result = ($cache && $cache_ttl) ? $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 = @mssql_query($query, $this->db_connect_id)) === false)
 151                  {
 152                      $this->sql_error($query);
 153                  }
 154  
 155                  if (defined('DEBUG'))
 156                  {
 157                      $this->sql_report('stop', $query);
 158                  }
 159                  else if (defined('PHPBB_DISPLAY_LOAD_TIME'))
 160                  {
 161                      $this->sql_time += microtime(true) - $this->curtime;
 162                  }
 163  
 164                  if ($cache && $cache_ttl)
 165                  {
 166                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 167                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 168                  }
 169                  else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 170                  {
 171                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 172                  }
 173              }
 174              else if (defined('DEBUG'))
 175              {
 176                  $this->sql_report('fromcache', $query);
 177              }
 178          }
 179          else
 180          {
 181              return false;
 182          }
 183  
 184          return $this->query_result;
 185      }
 186  
 187      /**
 188      * Build LIMIT query
 189      */
 190  	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 191      {
 192          $this->query_result = false;
 193  
 194          // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
 195          if ($total)
 196          {
 197              // We need to grab the total number of rows + the offset number of rows to get the correct result
 198              if (strpos($query, 'SELECT DISTINCT') === 0)
 199              {
 200                  $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
 201              }
 202              else
 203              {
 204                  $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
 205              }
 206          }
 207  
 208          $result = $this->sql_query($query, $cache_ttl);
 209  
 210          // Seek by $offset rows
 211          if ($offset)
 212          {
 213              $this->sql_rowseek($offset, $result);
 214          }
 215  
 216          return $result;
 217      }
 218  
 219      /**
 220      * {@inheritDoc}
 221      */
 222  	function sql_affectedrows()
 223      {
 224          return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
 225      }
 226  
 227      /**
 228      * {@inheritDoc}
 229      */
 230  	function sql_fetchrow($query_id = false)
 231      {
 232          global $cache;
 233  
 234          if ($query_id === false)
 235          {
 236              $query_id = $this->query_result;
 237          }
 238  
 239          if ($cache && $cache->sql_exists($query_id))
 240          {
 241              return $cache->sql_fetchrow($query_id);
 242          }
 243  
 244          if ($query_id === false)
 245          {
 246              return false;
 247          }
 248  
 249          $row = @mssql_fetch_assoc($query_id);
 250  
 251          // I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
 252          if ($row)
 253          {
 254              foreach ($row as $key => $value)
 255              {
 256                  $row[$key] = ($value === ' ' || $value === null) ? '' : $value;
 257              }
 258          }
 259  
 260          return $row;
 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) ? @mssql_data_seek($query_id, $rownum) : false;
 281      }
 282  
 283      /**
 284      * {@inheritDoc}
 285      */
 286  	function sql_nextid()
 287      {
 288          $result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
 289          if ($result_id)
 290          {
 291              if ($row = @mssql_fetch_assoc($result_id))
 292              {
 293                  @mssql_free_result($result_id);
 294                  return $row['computed'];
 295              }
 296              @mssql_free_result($result_id);
 297          }
 298  
 299          return false;
 300      }
 301  
 302      /**
 303      * {@inheritDoc}
 304      */
 305  	function sql_freeresult($query_id = false)
 306      {
 307          global $cache;
 308  
 309          if ($query_id === false)
 310          {
 311              $query_id = $this->query_result;
 312          }
 313  
 314          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 315          {
 316              return $cache->sql_freeresult($query_id);
 317          }
 318  
 319          if (isset($this->open_queries[(int) $query_id]))
 320          {
 321              unset($this->open_queries[(int) $query_id]);
 322              return @mssql_free_result($query_id);
 323          }
 324  
 325          return false;
 326      }
 327  
 328      /**
 329      * {@inheritDoc}
 330      */
 331  	function sql_escape($msg)
 332      {
 333          return str_replace(array("'", "\0"), array("''", ''), $msg);
 334      }
 335  
 336      /**
 337      * {@inheritDoc}
 338      */
 339  	function sql_lower_text($column_name)
 340      {
 341          return "LOWER(SUBSTRING($column_name, 1, DATALENGTH($column_name)))";
 342      }
 343  
 344      /**
 345      * Build LIKE expression
 346      * @access private
 347      */
 348  	function _sql_like_expression($expression)
 349      {
 350          return $expression . " ESCAPE '\\'";
 351      }
 352  
 353      /**
 354      * Build NOT LIKE expression
 355      * @access private
 356      */
 357  	function _sql_not_like_expression($expression)
 358      {
 359          return $expression . " ESCAPE '\\'";
 360      }
 361  
 362      /**
 363      * return sql error array
 364      * @access private
 365      */
 366  	function _sql_error()
 367      {
 368          if (function_exists('mssql_get_last_message'))
 369          {
 370              $error = array(
 371                  'message'    => @mssql_get_last_message(),
 372                  'code'        => '',
 373              );
 374  
 375              // Get error code number
 376              $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
 377              if ($result_id)
 378              {
 379                  $row = @mssql_fetch_assoc($result_id);
 380                  $error['code'] = $row['code'];
 381                  @mssql_free_result($result_id);
 382              }
 383  
 384              // Get full error message if possible
 385              $sql = 'SELECT CAST(description as varchar(255)) as message
 386                  FROM master.dbo.sysmessages
 387                  WHERE error = ' . $error['code'];
 388              $result_id = @mssql_query($sql);
 389  
 390              if ($result_id)
 391              {
 392                  $row = @mssql_fetch_assoc($result_id);
 393                  if (!empty($row['message']))
 394                  {
 395                      $error['message'] .= '<br />' . $row['message'];
 396                  }
 397                  @mssql_free_result($result_id);
 398              }
 399          }
 400          else
 401          {
 402              $error = array(
 403                  'message'    => $this->connect_error,
 404                  'code'        => '',
 405              );
 406          }
 407  
 408          return $error;
 409      }
 410  
 411      /**
 412      * Build db-specific query data
 413      * @access private
 414      */
 415  	function _sql_custom_build($stage, $data)
 416      {
 417          return $data;
 418      }
 419  
 420      /**
 421      * Close sql connection
 422      * @access private
 423      */
 424  	function _sql_close()
 425      {
 426          return @mssql_close($this->db_connect_id);
 427      }
 428  
 429      /**
 430      * Build db-specific report
 431      * @access private
 432      */
 433  	function _sql_report($mode, $query = '')
 434      {
 435          switch ($mode)
 436          {
 437              case 'start':
 438                  $html_table = false;
 439                  @mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
 440                  if ($result = @mssql_query($query, $this->db_connect_id))
 441                  {
 442                      @mssql_next_result($result);
 443                      while ($row = @mssql_fetch_row($result))
 444                      {
 445                          $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 446                      }
 447                  }
 448                  @mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
 449                  @mssql_free_result($result);
 450  
 451                  if ($html_table)
 452                  {
 453                      $this->html_hold .= '</table>';
 454                  }
 455              break;
 456  
 457              case 'fromcache':
 458                  $endtime = explode(' ', microtime());
 459                  $endtime = $endtime[0] + $endtime[1];
 460  
 461                  $result = @mssql_query($query, $this->db_connect_id);
 462                  while ($void = @mssql_fetch_assoc($result))
 463                  {
 464                      // Take the time spent on parsing rows into account
 465                  }
 466                  @mssql_free_result($result);
 467  
 468                  $splittime = explode(' ', microtime());
 469                  $splittime = $splittime[0] + $splittime[1];
 470  
 471                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 472  
 473              break;
 474          }
 475      }
 476  }


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