6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
19 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
22 * MSSQL Database Abstraction Layer
23 * Minimum Requirement is MSSQL 2000+
26 class dbal_mssql extends dbal
31 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
33 $this->persistency = $persistency;
34 $this->user = $sqluser;
35 $this->dbname = $database;
37 $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
38 $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
40 @ini_set('mssql.charset', 'UTF-8');
41 @ini_set('mssql.textlimit', 2147483647);
42 @ini_set('mssql.textsize', 2147483647);
44 if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
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);
50 $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
53 if ($this->db_connect_id && $this->dbname != '')
55 if (!@mssql_select_db($this->dbname, $this->db_connect_id))
57 @mssql_close($this->db_connect_id);
62 return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
66 * Version information about used database
67 * @param bool $raw if true, only return the fetched sql_server_version
68 * @param bool $use_cache If true, it is safe to retrieve the value from the cache
69 * @return string sql server version
71 function sql_server_info($raw = false, $use_cache = true)
75 if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssql_version')) === false)
77 $result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
82 $row = @mssql_fetch_assoc($result_id);
83 @mssql_free_result($result_id);
86 $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
88 if (!empty($cache) && $use_cache)
90 $cache->put('mssql_version', $this->sql_server_version);
96 return $this->sql_server_version;
99 return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
106 function _sql_transaction($status = 'begin')
111 return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
115 return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
119 return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
129 * @param string $query Contains the SQL query which shall be executed
130 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
131 * @return mixed When casted to bool the returned value returns true on success and false on failure
135 function sql_query($query = '', $cache_ttl = 0)
141 // EXPLAIN only in extra debug mode
142 if (defined('DEBUG_EXTRA'))
144 $this->sql_report('start', $query);
147 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
148 $this->sql_add_num_queries($this->query_result);
150 if ($this->query_result === false)
152 if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
154 $this->sql_error($query);
157 if (defined('DEBUG_EXTRA'))
159 $this->sql_report('stop', $query);
162 if ($cache_ttl && method_exists($cache, 'sql_save'))
164 $this->open_queries[(int) $this->query_result] = $this->query_result;
165 $cache->sql_save($query, $this->query_result, $cache_ttl);
167 else if (strpos($query, 'SELECT') === 0 && $this->query_result)
169 $this->open_queries[(int) $this->query_result] = $this->query_result;
172 else if (defined('DEBUG_EXTRA'))
174 $this->sql_report('fromcache', $query);
182 return $this->query_result;
188 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
190 $this->query_result = false;
192 // 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 // We need to grab the total number of rows + the offset number of rows to get the correct result
196 if (strpos($query, 'SELECT DISTINCT') === 0)
198 $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
202 $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
206 $result = $this->sql_query($query, $cache_ttl);
208 // Seek by $offset rows
211 $this->sql_rowseek($offset, $result);
218 * Return number of affected rows
220 function sql_affectedrows()
222 return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
228 function sql_fetchrow($query_id = false)
232 if ($query_id === false)
234 $query_id = $this->query_result;
237 if (isset($cache->sql_rowset[$query_id]))
239 return $cache->sql_fetchrow($query_id);
242 if ($query_id === false)
247 $row = @mssql_fetch_assoc($query_id);
249 // I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
252 foreach ($row as $key => $value)
254 $row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
262 * Seek to given row number
263 * rownum is zero-based
265 function sql_rowseek($rownum, &$query_id)
269 if ($query_id === false)
271 $query_id = $this->query_result;
274 if (isset($cache->sql_rowset[$query_id]))
276 return $cache->sql_rowseek($rownum, $query_id);
279 return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
283 * Get last inserted id after insert statement
285 function sql_nextid()
287 $result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
290 if ($row = @mssql_fetch_assoc($result_id))
292 @mssql_free_result($result_id);
293 return $row['computed'];
295 @mssql_free_result($result_id);
304 function sql_freeresult($query_id = false)
308 if ($query_id === false)
310 $query_id = $this->query_result;
313 if (isset($cache->sql_rowset[$query_id]))
315 return $cache->sql_freeresult($query_id);
318 if (isset($this->open_queries[$query_id]))
320 unset($this->open_queries[$query_id]);
321 return @mssql_free_result($query_id);
328 * Escape string used in sql query
330 function sql_escape($msg)
332 return str_replace(array("'", "\0"), array("''", ''), $msg);
336 * Build LIKE expression
339 function _sql_like_expression($expression)
341 return $expression . " ESCAPE '\\'";
345 * return sql error array
348 function _sql_error()
351 'message' => @mssql_get_last_message(),
355 // Get error code number
356 $result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
359 $row = @mssql_fetch_assoc($result_id);
360 $error['code'] = $row['code'];
361 @mssql_free_result($result_id);
364 // Get full error message if possible
365 $sql = 'SELECT CAST(description as varchar(255)) as message
366 FROM master.dbo.sysmessages
367 WHERE error = ' . $error['code'];
368 $result_id = @mssql_query($sql);
372 $row = @mssql_fetch_assoc($result_id);
373 if (!empty($row['message']))
375 $error['message'] .= '<br />' . $row['message'];
377 @mssql_free_result($result_id);
384 * Build db-specific query data
387 function _sql_custom_build($stage, $data)
393 * Close sql connection
396 function _sql_close()
398 return @mssql_close($this->db_connect_id);
402 * Build db-specific report
405 function _sql_report($mode, $query = '')
411 @mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
412 if ($result = @mssql_query($query, $this->db_connect_id))
414 @mssql_next_result($result);
415 while ($row = @mssql_fetch_row($result))
417 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
420 @mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
421 @mssql_free_result($result);
425 $this->html_hold .= '</table>';
430 $endtime = explode(' ', microtime());
431 $endtime = $endtime[0] + $endtime[1];
433 $result = @mssql_query($query, $this->db_connect_id);
434 while ($void = @mssql_fetch_assoc($result))
436 // Take the time spent on parsing rows into account
438 @mssql_free_result($result);
440 $splittime = explode(' ', microtime());
441 $splittime = $splittime[0] + $splittime[1];
443 $this->sql_report('record_fromcache', $query, $endtime, $splittime);