Verzeichnisstruktur phpBB-3.0.0


Veröffentlicht
12.12.2007

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

mssql.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 9.06 KiB


 001  <?php
 002  /**
 003  *
 004  * @package dbal
 005  * @version $Id$
 006  * @copyright (c) 2005 phpBB Group
 007  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 008  *
 009  */
 010   
 011  /**
 012  * @ignore
 013  */
 014  if (!defined('IN_PHPBB'))
 015  {
 016      exit;
 017  }
 018   
 019  include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
 020   
 021  /**
 022  * MSSQL Database Abstraction Layer
 023  * Minimum Requirement is MSSQL 2000+
 024  * @package dbal
 025  */
 026  class dbal_mssql extends dbal
 027  {
 028      /**
 029      * Connect to server
 030      */
 031      function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
 032      {
 033          $this->persistency = $persistency;
 034          $this->user = $sqluser;
 035          $this->server = $sqlserver . (($port) ? ':' . $port : '');
 036          $this->dbname = $database;
 037   
 038          @ini_set('mssql.charset', 'UTF-8');
 039          @ini_set('mssql.textlimit', 2147483647);
 040          @ini_set('mssql.textsize', 2147483647);
 041   
 042          if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
 043          {
 044              $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
 045          }
 046          else
 047          {
 048              $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
 049          }
 050   
 051          if ($this->db_connect_id && $this->dbname != '')
 052          {
 053              if (!@mssql_select_db($this->dbname, $this->db_connect_id))
 054              {
 055                  @mssql_close($this->db_connect_id);
 056                  return false;
 057              }
 058          }
 059   
 060          return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
 061      }
 062   
 063      /**
 064      * Version information about used database
 065      */
 066      function sql_server_info()
 067      {
 068          $result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
 069   
 070          $row = false;
 071          if ($result_id)
 072          {
 073              $row = @mssql_fetch_assoc($result_id);
 074              @mssql_free_result($result_id);
 075          }
 076   
 077          if ($row)
 078          {
 079              return 'MSSQL<br />' . implode(' ', $row);
 080          }
 081   
 082          return 'MSSQL';
 083      }
 084   
 085      /**
 086      * SQL Transaction
 087      * @access private
 088      */
 089      function _sql_transaction($status = 'begin')
 090      {
 091          switch ($status)
 092          {
 093              case 'begin':
 094                  return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
 095              break;
 096   
 097              case 'commit':
 098                  return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
 099              break;
 100   
 101              case 'rollback':
 102                  return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
 103              break;
 104          }
 105   
 106          return true;
 107      }
 108   
 109      /**
 110      * Base query method
 111      *
 112      * @param    string    $query        Contains the SQL query which shall be executed
 113      * @param    int        $cache_ttl    Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
 114      * @return    mixed                When casted to bool the returned value returns true on success and false on failure
 115      *
 116      * @access    public
 117      */
 118      function sql_query($query = '', $cache_ttl = 0)
 119      {
 120          if ($query != '')
 121          {
 122              global $cache;
 123   
 124              // EXPLAIN only in extra debug mode
 125              if (defined('DEBUG_EXTRA'))
 126              {
 127                  $this->sql_report('start', $query);
 128              }
 129   
 130              $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 131              $this->sql_add_num_queries($this->query_result);
 132   
 133              if ($this->query_result === false)
 134              {
 135                  if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
 136                  {
 137                      $this->sql_error($query);
 138                  }
 139   
 140                  if (defined('DEBUG_EXTRA'))
 141                  {
 142                      $this->sql_report('stop', $query);
 143                  }
 144   
 145                  if ($cache_ttl && method_exists($cache, 'sql_save'))
 146                  {
 147                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 148                      $cache->sql_save($query, $this->query_result, $cache_ttl);
 149                  }
 150                  else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 151                  {
 152                      $this->open_queries[(int) $this->query_result] = $this->query_result;
 153                  }
 154              }
 155              else if (defined('DEBUG_EXTRA'))
 156              {
 157                  $this->sql_report('fromcache', $query);
 158              }
 159          }
 160          else
 161          {
 162              return false;
 163          }
 164   
 165          return ($this->query_result) ? $this->query_result : false;
 166      }
 167   
 168      /**
 169      * Build LIMIT query
 170      */
 171      function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 172      {
 173          $this->query_result = false;
 174   
 175          // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
 176          if ($total)
 177          {
 178              // We need to grab the total number of rows + the offset number of rows to get the correct result
 179              if (strpos($query, 'SELECT DISTINCT') === 0)
 180              {
 181                  $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
 182              }
 183              else
 184              {
 185                  $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
 186              }
 187          }
 188   
 189          $result = $this->sql_query($query, $cache_ttl);
 190   
 191          // Seek by $offset rows
 192          if ($offset)
 193          {
 194              $this->sql_rowseek($offset, $result);
 195          }
 196   
 197          return $result;
 198      }
 199   
 200      /**
 201      * Return number of affected rows
 202      */
 203      function sql_affectedrows()
 204      {
 205          return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
 206      }
 207   
 208      /**
 209      * Fetch current row
 210      */
 211      function sql_fetchrow($query_id = false)
 212      {
 213          global $cache;
 214   
 215          if ($query_id === false)
 216          {
 217              $query_id = $this->query_result;
 218          }
 219   
 220          if (isset($cache->sql_rowset[$query_id]))
 221          {
 222              return $cache->sql_fetchrow($query_id);
 223          }
 224   
 225          if ($query_id === false)
 226          {
 227              return false;
 228          }
 229   
 230          $row = @mssql_fetch_assoc($query_id);
 231   
 232          // I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
 233          if ($row)
 234          {
 235              foreach ($row as $key => $value)
 236              {
 237                  $row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
 238              }
 239          }
 240   
 241          return $row;
 242      }
 243   
 244      /**
 245      * Seek to given row number
 246      * rownum is zero-based
 247      */
 248      function sql_rowseek($rownum, &$query_id)
 249      {
 250          global $cache;
 251   
 252          if ($query_id === false)
 253          {
 254              $query_id = $this->query_result;
 255          }
 256   
 257          if (isset($cache->sql_rowset[$query_id]))
 258          {
 259              return $cache->sql_rowseek($rownum, $query_id);
 260          }
 261   
 262          return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
 263      }
 264   
 265      /**
 266      * Get last inserted id after insert statement
 267      */
 268      function sql_nextid()
 269      {
 270          $result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
 271          if ($result_id)
 272          {
 273              if ($row = @mssql_fetch_assoc($result_id))
 274              {
 275                  @mssql_free_result($result_id);
 276                  return $row['computed'];
 277              }
 278              @mssql_free_result($result_id);
 279          }
 280   
 281          return false;
 282      }
 283   
 284      /**
 285      * Free sql result
 286      */
 287      function sql_freeresult($query_id = false)
 288      {
 289          global $cache;
 290   
 291          if ($query_id === false)
 292          {
 293              $query_id = $this->query_result;
 294          }
 295   
 296          if (isset($cache->sql_rowset[$query_id]))
 297          {
 298              return $cache->sql_freeresult($query_id);
 299          }
 300   
 301          if (isset($this->open_queries[$query_id]))
 302          {
 303              unset($this->open_queries[$query_id]);
 304              return @mssql_free_result($query_id);
 305          }
 306   
 307          return false;
 308      }
 309   
 310      /**
 311      * Escape string used in sql query
 312      */
 313      function sql_escape($msg)
 314      {
 315          return str_replace("'", "''", $msg);
 316      }
 317   
 318      /**
 319      * Build LIKE expression
 320      * @access private
 321      */
 322      function _sql_like_expression($expression)
 323      {
 324          return $expression . " ESCAPE '\\'";
 325      }
 326   
 327      /**
 328      * return sql error array
 329      * @access private
 330      */
 331      function _sql_error()
 332      {
 333          $error = array(
 334              'message'    => @mssql_get_last_message(),
 335              'code'        => ''
 336          );
 337   
 338          // Get error code number
 339          $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
 340          if ($result_id)
 341          {
 342              $row = @mssql_fetch_assoc($result_id);
 343              $error['code'] = $row['code'];
 344              @mssql_free_result($result_id);
 345          }
 346   
 347          // Get full error message if possible
 348          $sql = 'SELECT CAST(description as varchar(255)) as message
 349              FROM master.dbo.sysmessages
 350              WHERE error = ' . $error['code'];
 351          $result_id = @mssql_query($sql);
 352          
 353          if ($result_id)
 354          {
 355              $row = @mssql_fetch_assoc($result_id);
 356              if (!empty($row['message']))
 357              {
 358                  $error['message'] .= '<br />' . $row['message'];
 359              }
 360              @mssql_free_result($result_id);
 361          }
 362   
 363          return $error;
 364      }
 365   
 366      /**
 367      * Build db-specific query data
 368      * @access private
 369      */
 370      function _sql_custom_build($stage, $data)
 371      {
 372          return $data;
 373      }
 374   
 375      /**
 376      * Close sql connection
 377      * @access private
 378      */
 379      function _sql_close()
 380      {
 381          return @mssql_close($this->db_connect_id);
 382      }
 383   
 384      /**
 385      * Build db-specific report
 386      * @access private
 387      */
 388      function _sql_report($mode, $query = '')
 389      {
 390          switch ($mode)
 391          {
 392              case 'start':
 393                  $html_table = false;
 394                  @mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
 395                  if ($result = @mssql_query($query, $this->db_connect_id))
 396                  {
 397                      @mssql_next_result($result);
 398                      while ($row = @mssql_fetch_row($result))
 399                      {
 400                          $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 401                      }
 402                  }
 403                  @mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
 404                  @mssql_free_result($result);
 405   
 406                  if ($html_table)
 407                  {
 408                      $this->html_hold .= '</table>';
 409                  }
 410              break;
 411   
 412              case 'fromcache':
 413                  $endtime = explode(' ', microtime());
 414                  $endtime = $endtime[0] + $endtime[1];
 415   
 416                  $result = @mssql_query($query, $this->db_connect_id);
 417                  while ($void = @mssql_fetch_assoc($result))
 418                  {
 419                      // Take the time spent on parsing rows into account
 420                  }
 421                  @mssql_free_result($result);
 422   
 423                  $splittime = explode(' ', microtime());
 424                  $splittime = $splittime[0] + $splittime[1];
 425   
 426                  $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 427   
 428              break;
 429          }
 430      }
 431  }
 432   
 433  ?>