5
* @version $Id: mssql_odbc.php,v 1.37 2007/10/05 14:36:32 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
* Unified ODBC functions
23
* Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
24
* Here we only support MSSQL Server 2000+ because of the provided schema
26
* @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
27
* If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
28
* @note odbc.defaultbinmode may affect UTF8 characters
32
class dbal_mssql_odbc extends dbal
34
var $last_query_text = '';
39
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
41
$this->persistency = $persistency;
42
$this->user = $sqluser;
43
$this->server = $sqlserver . (($port) ? ':' . $port : '');
44
$this->dbname = $database;
46
$max_size = @ini_get('odbc.defaultlrl');
47
if (!empty($max_size))
49
$unit = strtolower(substr($max_size, -1, 1));
50
$max_size = (int) $max_size;
54
$max_size = floor($max_size / 1024);
56
else if ($unit == 'g')
60
else if (is_numeric($unit))
62
$max_size = floor((int) ($max_size . $unit) / 1048576);
64
$max_size = max(8, $max_size) . 'M';
66
@ini_set('odbc.defaultlrl', $max_size);
69
$this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
71
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
75
* Version information about used database
77
function sql_server_info()
79
$result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
84
$row = @odbc_fetch_array($result_id);
85
@odbc_free_result($result_id);
90
return 'MSSQL (ODBC)<br />' . implode(' ', $row);
93
return 'MSSQL (ODBC)';
100
function _sql_transaction($status = 'begin')
105
return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
109
return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
113
return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
123
* @param string $query Contains the SQL query which shall be executed
124
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
125
* @return mixed When casted to bool the returned value returns true on success and false on failure
129
function sql_query($query = '', $cache_ttl = 0)
135
// EXPLAIN only in extra debug mode
136
if (defined('DEBUG_EXTRA'))
138
$this->sql_report('start', $query);
141
$this->last_query_text = $query;
142
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
143
$this->sql_add_num_queries($this->query_result);
145
if ($this->query_result === false)
147
if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
149
$this->sql_error($query);
152
if (defined('DEBUG_EXTRA'))
154
$this->sql_report('stop', $query);
157
if ($cache_ttl && method_exists($cache, 'sql_save'))
159
$this->open_queries[(int) $this->query_result] = $this->query_result;
160
$cache->sql_save($query, $this->query_result, $cache_ttl);
162
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
164
$this->open_queries[(int) $this->query_result] = $this->query_result;
167
else if (defined('DEBUG_EXTRA'))
169
$this->sql_report('fromcache', $query);
177
return ($this->query_result) ? $this->query_result : false;
183
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
185
$this->query_result = false;
187
// Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
190
// We need to grab the total number of rows + the offset number of rows to get the correct result
191
if (strpos($query, 'SELECT DISTINCT') === 0)
193
$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
197
$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
201
$result = $this->sql_query($query, $cache_ttl);
203
// Seek by $offset rows
206
$this->sql_rowseek($offset, $result);
213
* Return number of affected rows
215
function sql_affectedrows()
217
return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
222
* @note number of bytes returned depends on odbc.defaultlrl php.ini setting. If it is limited to 4K for example only 4K of data is returned max.
224
function sql_fetchrow($query_id = false, $debug = false)
228
if ($query_id === false)
230
$query_id = $this->query_result;
233
if (isset($cache->sql_rowset[$query_id]))
235
return $cache->sql_fetchrow($query_id);
238
return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
242
* Seek to given row number
243
* rownum is zero-based
245
function sql_rowseek($rownum, &$query_id)
249
if ($query_id === false)
251
$query_id = $this->query_result;
254
if (isset($cache->sql_rowset[$query_id]))
256
return $cache->sql_rowseek($rownum, $query_id);
259
if ($query_id === false)
264
$this->sql_freeresult($query_id);
265
$query_id = $this->sql_query($this->last_query_text);
267
if ($query_id === false)
272
// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
273
for ($i = 0; $i < $rownum; $i++)
275
if (!$this->sql_fetchrow($query_id))
285
* Get last inserted id after insert statement
287
function sql_nextid()
289
$result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
293
if (@odbc_fetch_array($result_id))
295
$id = @odbc_result($result_id, 1);
296
@odbc_free_result($result_id);
299
@odbc_free_result($result_id);
308
function sql_freeresult($query_id = false)
312
if ($query_id === false)
314
$query_id = $this->query_result;
317
if (isset($cache->sql_rowset[$query_id]))
319
return $cache->sql_freeresult($query_id);
322
if (isset($this->open_queries[(int) $query_id]))
324
unset($this->open_queries[(int) $query_id]);
325
return @odbc_free_result($query_id);
332
* Escape string used in sql query
334
function sql_escape($msg)
336
return str_replace("'", "''", $msg);
340
* Build LIKE expression
343
function _sql_like_expression($expression)
345
return $expression . " ESCAPE '\\'";
349
* Build db-specific query data
352
function _sql_custom_build($stage, $data)
358
* return sql error array
361
function _sql_error()
364
'message' => @odbc_errormsg(),
365
'code' => @odbc_error()
370
* Close sql connection
373
function _sql_close()
375
return @odbc_close($this->db_connect_id);
379
* Build db-specific report
382
function _sql_report($mode, $query = '')
390
$endtime = explode(' ', microtime());
391
$endtime = $endtime[0] + $endtime[1];
393
$result = @odbc_exec($this->db_connect_id, $query);
394
while ($void = @odbc_fetch_array($result))
396
// Take the time spent on parsing rows into account
398
@odbc_free_result($result);
400
$splittime = explode(' ', microtime());
401
$splittime = $splittime[0] + $splittime[1];
403
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
b'\\ No newline at end of file'