1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/db/mssql.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,450 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package dbal
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
1.23 +
1.24 +/**
1.25 +* MSSQL Database Abstraction Layer
1.26 +* Minimum Requirement is MSSQL 2000+
1.27 +* @package dbal
1.28 +*/
1.29 +class dbal_mssql extends dbal
1.30 +{
1.31 + /**
1.32 + * Connect to server
1.33 + */
1.34 + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
1.35 + {
1.36 + $this->persistency = $persistency;
1.37 + $this->user = $sqluser;
1.38 + $this->dbname = $database;
1.39 +
1.40 + $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
1.41 + $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
1.42 +
1.43 + @ini_set('mssql.charset', 'UTF-8');
1.44 + @ini_set('mssql.textlimit', 2147483647);
1.45 + @ini_set('mssql.textsize', 2147483647);
1.46 +
1.47 + if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
1.48 + {
1.49 + $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
1.50 + }
1.51 + else
1.52 + {
1.53 + $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
1.54 + }
1.55 +
1.56 + if ($this->db_connect_id && $this->dbname != '')
1.57 + {
1.58 + if (!@mssql_select_db($this->dbname, $this->db_connect_id))
1.59 + {
1.60 + @mssql_close($this->db_connect_id);
1.61 + return false;
1.62 + }
1.63 + }
1.64 +
1.65 + return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
1.66 + }
1.67 +
1.68 + /**
1.69 + * Version information about used database
1.70 + * @param bool $raw if true, only return the fetched sql_server_version
1.71 + * @param bool $use_cache If true, it is safe to retrieve the value from the cache
1.72 + * @return string sql server version
1.73 + */
1.74 + function sql_server_info($raw = false, $use_cache = true)
1.75 + {
1.76 + global $cache;
1.77 +
1.78 + if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssql_version')) === false)
1.79 + {
1.80 + $result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
1.81 +
1.82 + $row = false;
1.83 + if ($result_id)
1.84 + {
1.85 + $row = @mssql_fetch_assoc($result_id);
1.86 + @mssql_free_result($result_id);
1.87 + }
1.88 +
1.89 + $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
1.90 +
1.91 + if (!empty($cache) && $use_cache)
1.92 + {
1.93 + $cache->put('mssql_version', $this->sql_server_version);
1.94 + }
1.95 + }
1.96 +
1.97 + if ($raw)
1.98 + {
1.99 + return $this->sql_server_version;
1.100 + }
1.101 +
1.102 + return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
1.103 + }
1.104 +
1.105 + /**
1.106 + * SQL Transaction
1.107 + * @access private
1.108 + */
1.109 + function _sql_transaction($status = 'begin')
1.110 + {
1.111 + switch ($status)
1.112 + {
1.113 + case 'begin':
1.114 + return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
1.115 + break;
1.116 +
1.117 + case 'commit':
1.118 + return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
1.119 + break;
1.120 +
1.121 + case 'rollback':
1.122 + return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
1.123 + break;
1.124 + }
1.125 +
1.126 + return true;
1.127 + }
1.128 +
1.129 + /**
1.130 + * Base query method
1.131 + *
1.132 + * @param string $query Contains the SQL query which shall be executed
1.133 + * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
1.134 + * @return mixed When casted to bool the returned value returns true on success and false on failure
1.135 + *
1.136 + * @access public
1.137 + */
1.138 + function sql_query($query = '', $cache_ttl = 0)
1.139 + {
1.140 + if ($query != '')
1.141 + {
1.142 + global $cache;
1.143 +
1.144 + // EXPLAIN only in extra debug mode
1.145 + if (defined('DEBUG_EXTRA'))
1.146 + {
1.147 + $this->sql_report('start', $query);
1.148 + }
1.149 +
1.150 + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
1.151 + $this->sql_add_num_queries($this->query_result);
1.152 +
1.153 + if ($this->query_result === false)
1.154 + {
1.155 + if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
1.156 + {
1.157 + $this->sql_error($query);
1.158 + }
1.159 +
1.160 + if (defined('DEBUG_EXTRA'))
1.161 + {
1.162 + $this->sql_report('stop', $query);
1.163 + }
1.164 +
1.165 + if ($cache_ttl && method_exists($cache, 'sql_save'))
1.166 + {
1.167 + $this->open_queries[(int) $this->query_result] = $this->query_result;
1.168 + $cache->sql_save($query, $this->query_result, $cache_ttl);
1.169 + }
1.170 + else if (strpos($query, 'SELECT') === 0 && $this->query_result)
1.171 + {
1.172 + $this->open_queries[(int) $this->query_result] = $this->query_result;
1.173 + }
1.174 + }
1.175 + else if (defined('DEBUG_EXTRA'))
1.176 + {
1.177 + $this->sql_report('fromcache', $query);
1.178 + }
1.179 + }
1.180 + else
1.181 + {
1.182 + return false;
1.183 + }
1.184 +
1.185 + return $this->query_result;
1.186 + }
1.187 +
1.188 + /**
1.189 + * Build LIMIT query
1.190 + */
1.191 + function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
1.192 + {
1.193 + $this->query_result = false;
1.194 +
1.195 + // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
1.196 + if ($total)
1.197 + {
1.198 + // We need to grab the total number of rows + the offset number of rows to get the correct result
1.199 + if (strpos($query, 'SELECT DISTINCT') === 0)
1.200 + {
1.201 + $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
1.202 + }
1.203 + else
1.204 + {
1.205 + $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
1.206 + }
1.207 + }
1.208 +
1.209 + $result = $this->sql_query($query, $cache_ttl);
1.210 +
1.211 + // Seek by $offset rows
1.212 + if ($offset)
1.213 + {
1.214 + $this->sql_rowseek($offset, $result);
1.215 + }
1.216 +
1.217 + return $result;
1.218 + }
1.219 +
1.220 + /**
1.221 + * Return number of affected rows
1.222 + */
1.223 + function sql_affectedrows()
1.224 + {
1.225 + return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
1.226 + }
1.227 +
1.228 + /**
1.229 + * Fetch current row
1.230 + */
1.231 + function sql_fetchrow($query_id = false)
1.232 + {
1.233 + global $cache;
1.234 +
1.235 + if ($query_id === false)
1.236 + {
1.237 + $query_id = $this->query_result;
1.238 + }
1.239 +
1.240 + if (isset($cache->sql_rowset[$query_id]))
1.241 + {
1.242 + return $cache->sql_fetchrow($query_id);
1.243 + }
1.244 +
1.245 + if ($query_id === false)
1.246 + {
1.247 + return false;
1.248 + }
1.249 +
1.250 + $row = @mssql_fetch_assoc($query_id);
1.251 +
1.252 + // I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
1.253 + if ($row)
1.254 + {
1.255 + foreach ($row as $key => $value)
1.256 + {
1.257 + $row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
1.258 + }
1.259 + }
1.260 +
1.261 + return $row;
1.262 + }
1.263 +
1.264 + /**
1.265 + * Seek to given row number
1.266 + * rownum is zero-based
1.267 + */
1.268 + function sql_rowseek($rownum, &$query_id)
1.269 + {
1.270 + global $cache;
1.271 +
1.272 + if ($query_id === false)
1.273 + {
1.274 + $query_id = $this->query_result;
1.275 + }
1.276 +
1.277 + if (isset($cache->sql_rowset[$query_id]))
1.278 + {
1.279 + return $cache->sql_rowseek($rownum, $query_id);
1.280 + }
1.281 +
1.282 + return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
1.283 + }
1.284 +
1.285 + /**
1.286 + * Get last inserted id after insert statement
1.287 + */
1.288 + function sql_nextid()
1.289 + {
1.290 + $result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
1.291 + if ($result_id)
1.292 + {
1.293 + if ($row = @mssql_fetch_assoc($result_id))
1.294 + {
1.295 + @mssql_free_result($result_id);
1.296 + return $row['computed'];
1.297 + }
1.298 + @mssql_free_result($result_id);
1.299 + }
1.300 +
1.301 + return false;
1.302 + }
1.303 +
1.304 + /**
1.305 + * Free sql result
1.306 + */
1.307 + function sql_freeresult($query_id = false)
1.308 + {
1.309 + global $cache;
1.310 +
1.311 + if ($query_id === false)
1.312 + {
1.313 + $query_id = $this->query_result;
1.314 + }
1.315 +
1.316 + if (isset($cache->sql_rowset[$query_id]))
1.317 + {
1.318 + return $cache->sql_freeresult($query_id);
1.319 + }
1.320 +
1.321 + if (isset($this->open_queries[$query_id]))
1.322 + {
1.323 + unset($this->open_queries[$query_id]);
1.324 + return @mssql_free_result($query_id);
1.325 + }
1.326 +
1.327 + return false;
1.328 + }
1.329 +
1.330 + /**
1.331 + * Escape string used in sql query
1.332 + */
1.333 + function sql_escape($msg)
1.334 + {
1.335 + return str_replace(array("'", "\0"), array("''", ''), $msg);
1.336 + }
1.337 +
1.338 + /**
1.339 + * Build LIKE expression
1.340 + * @access private
1.341 + */
1.342 + function _sql_like_expression($expression)
1.343 + {
1.344 + return $expression . " ESCAPE '\\'";
1.345 + }
1.346 +
1.347 + /**
1.348 + * return sql error array
1.349 + * @access private
1.350 + */
1.351 + function _sql_error()
1.352 + {
1.353 + $error = array(
1.354 + 'message' => @mssql_get_last_message(),
1.355 + 'code' => ''
1.356 + );
1.357 +
1.358 + // Get error code number
1.359 + $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
1.360 + if ($result_id)
1.361 + {
1.362 + $row = @mssql_fetch_assoc($result_id);
1.363 + $error['code'] = $row['code'];
1.364 + @mssql_free_result($result_id);
1.365 + }
1.366 +
1.367 + // Get full error message if possible
1.368 + $sql = 'SELECT CAST(description as varchar(255)) as message
1.369 + FROM master.dbo.sysmessages
1.370 + WHERE error = ' . $error['code'];
1.371 + $result_id = @mssql_query($sql);
1.372 +
1.373 + if ($result_id)
1.374 + {
1.375 + $row = @mssql_fetch_assoc($result_id);
1.376 + if (!empty($row['message']))
1.377 + {
1.378 + $error['message'] .= '<br />' . $row['message'];
1.379 + }
1.380 + @mssql_free_result($result_id);
1.381 + }
1.382 +
1.383 + return $error;
1.384 + }
1.385 +
1.386 + /**
1.387 + * Build db-specific query data
1.388 + * @access private
1.389 + */
1.390 + function _sql_custom_build($stage, $data)
1.391 + {
1.392 + return $data;
1.393 + }
1.394 +
1.395 + /**
1.396 + * Close sql connection
1.397 + * @access private
1.398 + */
1.399 + function _sql_close()
1.400 + {
1.401 + return @mssql_close($this->db_connect_id);
1.402 + }
1.403 +
1.404 + /**
1.405 + * Build db-specific report
1.406 + * @access private
1.407 + */
1.408 + function _sql_report($mode, $query = '')
1.409 + {
1.410 + switch ($mode)
1.411 + {
1.412 + case 'start':
1.413 + $html_table = false;
1.414 + @mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
1.415 + if ($result = @mssql_query($query, $this->db_connect_id))
1.416 + {
1.417 + @mssql_next_result($result);
1.418 + while ($row = @mssql_fetch_row($result))
1.419 + {
1.420 + $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
1.421 + }
1.422 + }
1.423 + @mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
1.424 + @mssql_free_result($result);
1.425 +
1.426 + if ($html_table)
1.427 + {
1.428 + $this->html_hold .= '</table>';
1.429 + }
1.430 + break;
1.431 +
1.432 + case 'fromcache':
1.433 + $endtime = explode(' ', microtime());
1.434 + $endtime = $endtime[0] + $endtime[1];
1.435 +
1.436 + $result = @mssql_query($query, $this->db_connect_id);
1.437 + while ($void = @mssql_fetch_assoc($result))
1.438 + {
1.439 + // Take the time spent on parsing rows into account
1.440 + }
1.441 + @mssql_free_result($result);
1.442 +
1.443 + $splittime = explode(' ', microtime());
1.444 + $splittime = $splittime[0] + $splittime[1];
1.445 +
1.446 + $this->sql_report('record_fromcache', $query, $endtime, $splittime);
1.447 +
1.448 + break;
1.449 + }
1.450 + }
1.451 +}
1.452 +
1.453 +?>
1.454 \ No newline at end of file