[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/phpbb/db/driver/ -> mssql_odbc.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  * Unified ODBC functions
  18  * Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
  19  * Here we only support MSSQL Server 2000+ because of the provided schema
  20  *
  21  * @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
  22  * If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
  23  * @note odbc.defaultbinmode may affect UTF8 characters
  24  */
  25  class mssql_odbc extends \phpbb\db\driver\mssql_base
  26  {
  27      var $last_query_text = '';
  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          $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          $max_size = @ini_get('odbc.defaultlrl');
  43          if (!empty($max_size))
  44          {
  45              $unit = strtolower(substr($max_size, -1, 1));
  46              $max_size = (int) $max_size;
  47  
  48              if ($unit == 'k')
  49              {
  50                  $max_size = floor($max_size / 1024);
  51              }
  52              else if ($unit == 'g')
  53              {
  54                  $max_size *= 1024;
  55              }
  56              else if (is_numeric($unit))
  57              {
  58                  $max_size = floor((int) ($max_size . $unit) / 1048576);
  59              }
  60              $max_size = max(8, $max_size) . 'M';
  61  
  62              @ini_set('odbc.defaultlrl', $max_size);
  63          }
  64  
  65          if ($this->persistency)
  66          {
  67              if (!function_exists('odbc_pconnect'))
  68              {
  69                  $this->connect_error = 'odbc_pconnect function does not exist, is odbc extension installed?';
  70                  return $this->sql_error('');
  71              }
  72              $this->db_connect_id = @odbc_pconnect($this->server, $this->user, $sqlpassword);
  73          }
  74          else
  75          {
  76              if (!function_exists('odbc_connect'))
  77              {
  78                  $this->connect_error = 'odbc_connect function does not exist, is odbc extension installed?';
  79                  return $this->sql_error('');
  80              }
  81              $this->db_connect_id = @odbc_connect($this->server, $this->user, $sqlpassword);
  82          }
  83  
  84          return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
  85      }
  86  
  87      /**
  88      * {@inheritDoc}
  89      */
  90  	function sql_server_info($raw = false, $use_cache = true)
  91      {
  92          global $cache;
  93  
  94          if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
  95          {
  96              $result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
  97  
  98              $row = false;
  99              if ($result_id)
 100              {
 101                  $row = odbc_fetch_array($result_id);
 102                  odbc_free_result($result_id);
 103              }
 104  
 105              $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
 106  
 107              if (!empty($cache) && $use_cache)
 108              {
 109                  $cache->put('mssqlodbc_version', $this->sql_server_version);
 110              }
 111          }
 112  
 113          if ($raw)
 114          {
 115              return $this->sql_server_version;
 116          }
 117  
 118          return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
 119      }
 120  
 121      /**
 122      * SQL Transaction
 123      * @access private
 124      */
 125  	function _sql_transaction($status = 'begin')
 126      {
 127          switch ($status)
 128          {
 129              case 'begin':
 130                  return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
 131              break;
 132  
 133              case 'commit':
 134                  return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
 135              break;
 136  
 137              case 'rollback':
 138                  return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
 139              break;
 140          }
 141  
 142          return true;
 143      }
 144  
 145      /**
 146      * {@inheritDoc}
 147      */
 148  	function sql_query($query = '', $cache_ttl = 0)
 149      {
 150          if ($query != '')
 151          {
 152              global $cache;
 153  
 154              if ($this->debug_sql_explain)
 155              {
 156                  $this->sql_report('start', $query);
 157              }
 158              else if ($this->debug_load_time)
 159              {
 160                  $this->curtime = microtime(true);
 161              }
 162  
 163              $this->last_query_text = $query;
 164              $this->query_result = ($cache && $cache_ttl) ? $cache->sql_load($query) : false;
 165              $this->sql_add_num_queries($this->query_result);
 166  
 167              if ($this->query_result === false)
 168              {
 169                  if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
 170                  {
 171                      $this->sql_error($query);
 172                  }
 173  
 174                  if ($this->debug_sql_explain)
 175                  {
 176                      $this->sql_report('stop', $query);
 177                  }
 178                  else if ($this->debug_load_time)
 179                  {
 180                      $this->sql_time += microtime(true) - $this->curtime;
 181                  }
 182  
 183                  if (!$this->query_result)
 184                  {
 185                      return false;
 186                  }
 187  
 188                  if ($cache && $cache_ttl)
 189                  {
 190                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 191                      $this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 192                  }
 193                  else if (strpos($query, 'SELECT') === 0)
 194                  {
 195                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 196                  }
 197              }
 198              else if ($this->debug_sql_explain)
 199              {
 200                  $this->sql_report('fromcache', $query);
 201              }
 202          }
 203          else
 204          {
 205              return false;
 206          }
 207  
 208          return $this->query_result;
 209      }
 210  
 211      /**
 212      * Build LIMIT query
 213      */
 214  	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 215      {
 216          $this->query_result = false;
 217  
 218          // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
 219          if ($total)
 220          {
 221              // We need to grab the total number of rows + the offset number of rows to get the correct result
 222              if (strpos($query, 'SELECT DISTINCT') === 0)
 223              {
 224                  $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
 225              }
 226              else
 227              {
 228                  $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
 229              }
 230          }
 231  
 232          $result = $this->sql_query($query, $cache_ttl);
 233  
 234          // Seek by $offset rows
 235          if ($offset)
 236          {
 237              $this->sql_rowseek($offset, $result);
 238          }
 239  
 240          return $result;
 241      }
 242  
 243      /**
 244      * {@inheritDoc}
 245      */
 246  	function sql_affectedrows()
 247      {
 248          return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
 249      }
 250  
 251      /**
 252      * {@inheritDoc}
 253      */
 254  	function sql_fetchrow($query_id = false)
 255      {
 256          global $cache;
 257  
 258          if ($query_id === false)
 259          {
 260              $query_id = $this->query_result;
 261          }
 262  
 263          if ($cache && $cache->sql_exists($query_id))
 264          {
 265              return $cache->sql_fetchrow($query_id);
 266          }
 267  
 268          return ($query_id) ? odbc_fetch_array($query_id) : false;
 269      }
 270  
 271      /**
 272      * {@inheritDoc}
 273      */
 274  	function sql_nextid()
 275      {
 276          $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
 277  
 278          if ($result_id)
 279          {
 280              if (odbc_fetch_array($result_id))
 281              {
 282                  $id = odbc_result($result_id, 1);
 283                  odbc_free_result($result_id);
 284                  return $id;
 285              }
 286              odbc_free_result($result_id);
 287          }
 288  
 289          return false;
 290      }
 291  
 292      /**
 293      * {@inheritDoc}
 294      */
 295  	function sql_freeresult($query_id = false)
 296      {
 297          global $cache;
 298  
 299          if ($query_id === false)
 300          {
 301              $query_id = $this->query_result;
 302          }
 303  
 304          if ($cache && !is_object($query_id) && $cache->sql_exists($query_id))
 305          {
 306              return $cache->sql_freeresult($query_id);
 307          }
 308  
 309          if (isset($this->open_queries[(int) $query_id]))
 310          {
 311              unset($this->open_queries[(int) $query_id]);
 312              return odbc_free_result($query_id);
 313          }
 314  
 315          return false;
 316      }
 317  
 318      /**
 319      * return sql error array
 320      * @access private
 321      */
 322  	function _sql_error()
 323      {
 324          if (function_exists('odbc_errormsg'))
 325          {
 326              $error = array(
 327                  'message'    => @odbc_errormsg(),
 328                  'code'        => @odbc_error(),
 329              );
 330          }
 331          else
 332          {
 333              $error = array(
 334                  'message'    => $this->connect_error,
 335                  'code'        => '',
 336              );
 337          }
 338  
 339          return $error;
 340      }
 341  
 342      /**
 343      * Close sql connection
 344      * @access private
 345      */
 346  	function _sql_close()
 347      {
 348          return @odbc_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              break;
 361  
 362              case 'fromcache':
 363                  $endtime = explode(' ', microtime());
 364                  $endtime = $endtime[0] + $endtime[1];
 365  
 366                  $result = @odbc_exec($this->db_connect_id, $query);
 367                  if ($result)
 368                  {
 369                      while ($void = odbc_fetch_array($result))
 370                      {
 371                          // Take the time spent on parsing rows into account
 372                      }
 373                      odbc_free_result($result);
 374                  }
 375  
 376                  $splittime = explode(' ', microtime());
 377                  $splittime = $splittime[0] + $splittime[1];
 378  
 379                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 380  
 381              break;
 382          }
 383      }
 384  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1