5
* @version $Id: mssql.php,v 1.51 2007/10/12 10:04:31 acydburn Exp $
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->server = $sqlserver . (($port) ? ':' . $port : '');
36
$this->dbname = $database;
38
@ini_set('mssql.charset', 'UTF-8');
39
@ini_set('mssql.textlimit', 2147483647);
40
@ini_set('mssql.textsize', 2147483647);
42
if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
44
$this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
48
$this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
51
if ($this->db_connect_id && $this->dbname != '')
53
if (!@mssql_select_db($this->dbname, $this->db_connect_id))
55
@mssql_close($this->db_connect_id);
60
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
64
* Version information about used database
66
function sql_server_info()
68
$result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
73
$row = @mssql_fetch_assoc($result_id);
74
@mssql_free_result($result_id);
79
return 'MSSQL<br />' . implode(' ', $row);
89
function _sql_transaction($status = 'begin')
94
return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
98
return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
102
return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
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
118
function sql_query($query = '', $cache_ttl = 0)
124
// EXPLAIN only in extra debug mode
125
if (defined('DEBUG_EXTRA'))
127
$this->sql_report('start', $query);
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);
133
if ($this->query_result === false)
135
if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
137
$this->sql_error($query);
140
if (defined('DEBUG_EXTRA'))
142
$this->sql_report('stop', $query);
145
if ($cache_ttl && method_exists($cache, 'sql_save'))
147
$this->open_queries[(int) $this->query_result] = $this->query_result;
148
$cache->sql_save($query, $this->query_result, $cache_ttl);
150
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
152
$this->open_queries[(int) $this->query_result] = $this->query_result;
155
else if (defined('DEBUG_EXTRA'))
157
$this->sql_report('fromcache', $query);
165
return ($this->query_result) ? $this->query_result : false;
171
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
173
$this->query_result = false;
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)
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)
181
$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
185
$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
189
$result = $this->sql_query($query, $cache_ttl);
191
// Seek by $offset rows
194
$this->sql_rowseek($offset, $result);
201
* Return number of affected rows
203
function sql_affectedrows()
205
return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
211
function sql_fetchrow($query_id = false)
215
if ($query_id === false)
217
$query_id = $this->query_result;
220
if (isset($cache->sql_rowset[$query_id]))
222
return $cache->sql_fetchrow($query_id);
225
if ($query_id === false)
230
$row = @mssql_fetch_assoc($query_id);
232
// I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
235
foreach ($row as $key => $value)
237
$row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
245
* Seek to given row number
246
* rownum is zero-based
248
function sql_rowseek($rownum, &$query_id)
252
if ($query_id === false)
254
$query_id = $this->query_result;
257
if (isset($cache->sql_rowset[$query_id]))
259
return $cache->sql_rowseek($rownum, $query_id);
262
return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
266
* Get last inserted id after insert statement
268
function sql_nextid()
270
$result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
273
if ($row = @mssql_fetch_assoc($result_id))
275
@mssql_free_result($result_id);
276
return $row['computed'];
278
@mssql_free_result($result_id);
287
function sql_freeresult($query_id = false)
291
if ($query_id === false)
293
$query_id = $this->query_result;
296
if (isset($cache->sql_rowset[$query_id]))
298
return $cache->sql_freeresult($query_id);
301
if (isset($this->open_queries[$query_id]))
303
unset($this->open_queries[$query_id]);
304
return @mssql_free_result($query_id);
311
* Escape string used in sql query
313
function sql_escape($msg)
315
return str_replace("'", "''", $msg);
319
* Build LIKE expression
322
function _sql_like_expression($expression)
324
return $expression . " ESCAPE '\\'";
328
* return sql error array
331
function _sql_error()
334
'message' => @mssql_get_last_message(),
338
// Get error code number
339
$result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
342
$row = @mssql_fetch_assoc($result_id);
343
$error['code'] = $row['code'];
344
@mssql_free_result($result_id);
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);
355
$row = @mssql_fetch_assoc($result_id);
356
if (!empty($row['message']))
358
$error['message'] .= '<br />' . $row['message'];
360
@mssql_free_result($result_id);
367
* Build db-specific query data
370
function _sql_custom_build($stage, $data)
376
* Close sql connection
379
function _sql_close()
381
return @mssql_close($this->db_connect_id);
385
* Build db-specific report
388
function _sql_report($mode, $query = '')
394
@mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
395
if ($result = @mssql_query($query, $this->db_connect_id))
397
@mssql_next_result($result);
398
while ($row = @mssql_fetch_row($result))
400
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
403
@mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
404
@mssql_free_result($result);
408
$this->html_hold .= '</table>';
413
$endtime = explode(' ', microtime());
414
$endtime = $endtime[0] + $endtime[1];
416
$result = @mssql_query($query, $this->db_connect_id);
417
while ($void = @mssql_fetch_assoc($result))
419
// Take the time spent on parsing rows into account
421
@mssql_free_result($result);
423
$splittime = explode(' ', microtime());
424
$splittime = $splittime[0] + $splittime[1];
426
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
b'\\ No newline at end of file'