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->dbname = $database;
45 $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
46 $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
48 $max_size = @ini_get('odbc.defaultlrl');
49 if (!empty($max_size))
51 $unit = strtolower(substr($max_size, -1, 1));
52 $max_size = (int) $max_size;
56 $max_size = floor($max_size / 1024);
58 else if ($unit == 'g')
62 else if (is_numeric($unit))
64 $max_size = floor((int) ($max_size . $unit) / 1048576);
66 $max_size = max(8, $max_size) . 'M';
68 @ini_set('odbc.defaultlrl', $max_size);
71 $this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
73 return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
77 * Version information about used database
78 * @param bool $raw if true, only return the fetched sql_server_version
79 * @param bool $use_cache If true, it is safe to retrieve the value from the cache
80 * @return string sql server version
82 function sql_server_info($raw = false, $use_cache = true)
86 if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
88 $result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
93 $row = @odbc_fetch_array($result_id);
94 @odbc_free_result($result_id);
97 $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
99 if (!empty($cache) && $use_cache)
101 $cache->put('mssqlodbc_version', $this->sql_server_version);
107 return $this->sql_server_version;
110 return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
117 function _sql_transaction($status = 'begin')
122 return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
126 return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
130 return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
140 * @param string $query Contains the SQL query which shall be executed
141 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
142 * @return mixed When casted to bool the returned value returns true on success and false on failure
146 function sql_query($query = '', $cache_ttl = 0)
152 // EXPLAIN only in extra debug mode
153 if (defined('DEBUG_EXTRA'))
155 $this->sql_report('start', $query);
158 $this->last_query_text = $query;
159 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
160 $this->sql_add_num_queries($this->query_result);
162 if ($this->query_result === false)
164 if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
166 $this->sql_error($query);
169 if (defined('DEBUG_EXTRA'))
171 $this->sql_report('stop', $query);
174 if ($cache_ttl && method_exists($cache, 'sql_save'))
176 $this->open_queries[(int) $this->query_result] = $this->query_result;
177 $cache->sql_save($query, $this->query_result, $cache_ttl);
179 else if (strpos($query, 'SELECT') === 0 && $this->query_result)
181 $this->open_queries[(int) $this->query_result] = $this->query_result;
184 else if (defined('DEBUG_EXTRA'))
186 $this->sql_report('fromcache', $query);
194 return $this->query_result;
200 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
202 $this->query_result = false;
204 // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
207 // We need to grab the total number of rows + the offset number of rows to get the correct result
208 if (strpos($query, 'SELECT DISTINCT') === 0)
210 $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
214 $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
218 $result = $this->sql_query($query, $cache_ttl);
220 // Seek by $offset rows
223 $this->sql_rowseek($offset, $result);
230 * Return number of affected rows
232 function sql_affectedrows()
234 return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
239 * @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.
241 function sql_fetchrow($query_id = false, $debug = false)
245 if ($query_id === false)
247 $query_id = $this->query_result;
250 if (isset($cache->sql_rowset[$query_id]))
252 return $cache->sql_fetchrow($query_id);
255 return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
259 * Seek to given row number
260 * rownum is zero-based
262 function sql_rowseek($rownum, &$query_id)
266 if ($query_id === false)
268 $query_id = $this->query_result;
271 if (isset($cache->sql_rowset[$query_id]))
273 return $cache->sql_rowseek($rownum, $query_id);
276 if ($query_id === false)
281 $this->sql_freeresult($query_id);
282 $query_id = $this->sql_query($this->last_query_text);
284 if ($query_id === false)
289 // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
290 for ($i = 0; $i < $rownum; $i++)
292 if (!$this->sql_fetchrow($query_id))
302 * Get last inserted id after insert statement
304 function sql_nextid()
306 $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
310 if (@odbc_fetch_array($result_id))
312 $id = @odbc_result($result_id, 1);
313 @odbc_free_result($result_id);
316 @odbc_free_result($result_id);
325 function sql_freeresult($query_id = false)
329 if ($query_id === false)
331 $query_id = $this->query_result;
334 if (isset($cache->sql_rowset[$query_id]))
336 return $cache->sql_freeresult($query_id);
339 if (isset($this->open_queries[(int) $query_id]))
341 unset($this->open_queries[(int) $query_id]);
342 return @odbc_free_result($query_id);
349 * Escape string used in sql query
351 function sql_escape($msg)
353 return str_replace(array("'", "\0"), array("''", ''), $msg);
357 * Build LIKE expression
360 function _sql_like_expression($expression)
362 return $expression . " ESCAPE '\\'";
366 * Build db-specific query data
369 function _sql_custom_build($stage, $data)
375 * return sql error array
378 function _sql_error()
381 'message' => @odbc_errormsg(),
382 'code' => @odbc_error()
387 * Close sql connection
390 function _sql_close()
392 return @odbc_close($this->db_connect_id);
396 * Build db-specific report
399 function _sql_report($mode, $query = '')
407 $endtime = explode(' ', microtime());
408 $endtime = $endtime[0] + $endtime[1];
410 $result = @odbc_exec($this->db_connect_id, $query);
411 while ($void = @odbc_fetch_array($result))
413 // Take the time spent on parsing rows into account
415 @odbc_free_result($result);
417 $splittime = explode(' ', microtime());
418 $splittime = $splittime[0] + $splittime[1];
420 $this->sql_report('record_fromcache', $query, $endtime, $splittime);