|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
* @version $Id: mssql_odbc.php 8967 2008-10-02 12:04:12Z acydburn $
|
|
|
4c79b5 |
* @copyright (c) 2005 phpBB Group
|
|
|
4c79b5 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @ignore
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
if (!defined('IN_PHPBB'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
exit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Unified ODBC functions
|
|
|
4c79b5 |
* Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
|
|
|
4c79b5 |
* Here we only support MSSQL Server 2000+ because of the provided schema
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
|
|
|
4c79b5 |
* If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
|
|
|
4c79b5 |
* @note odbc.defaultbinmode may affect UTF8 characters
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class dbal_mssql_odbc extends dbal
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
var $last_query_text = '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Connect to server
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->persistency = $persistency;
|
|
|
4c79b5 |
$this->user = $sqluser;
|
|
|
4c79b5 |
$this->dbname = $database;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
|
|
|
4c79b5 |
$this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$max_size = @ini_get('odbc.defaultlrl');
|
|
|
4c79b5 |
if (!empty($max_size))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$unit = strtolower(substr($max_size, -1, 1));
|
|
|
4c79b5 |
$max_size = (int) $max_size;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($unit == 'k')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$max_size = floor($max_size / 1024);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($unit == 'g')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$max_size *= 1024;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (is_numeric($unit))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$max_size = floor((int) ($max_size . $unit) / 1048576);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$max_size = max(8, $max_size) . 'M';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
@ini_set('odbc.defaultlrl', $max_size);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Version information about used database
|
|
|
4c79b5 |
* @param bool $raw if true, only return the fetched sql_server_version
|
|
|
4c79b5 |
* @return string sql server version
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_server_info($raw = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = false;
|
|
|
4c79b5 |
if ($result_id)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$row = @odbc_fetch_array($result_id);
|
|
|
4c79b5 |
@odbc_free_result($result_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!empty($cache))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$cache->put('mssqlodbc_version', $this->sql_server_version);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($raw)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $this->sql_server_version;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($this->sql_server_version) ? 'MSSQL (ODBC) ' . $this->sql_server_version : 'MSSQL (ODBC)';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* SQL Transaction
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_transaction($status = 'begin')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($status)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'begin':
|
|
|
4c79b5 |
return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'commit':
|
|
|
4c79b5 |
return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'rollback':
|
|
|
4c79b5 |
return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Base query method
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param string $query Contains the SQL query which shall be executed
|
|
|
4c79b5 |
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
|
|
|
4c79b5 |
* @return mixed When casted to bool the returned value returns true on success and false on failure
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access public
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_query($query = '', $cache_ttl = 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($query != '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// EXPLAIN only in extra debug mode
|
|
|
4c79b5 |
if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('start', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->last_query_text = $query;
|
|
|
4c79b5 |
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
|
|
|
4c79b5 |
$this->sql_add_num_queries($this->query_result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->query_result === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_error($query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('stop', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($cache_ttl && method_exists($cache, 'sql_save'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
4c79b5 |
$cache->sql_save($query, $this->query_result, $cache_ttl);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('fromcache', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build LIMIT query
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->query_result = false;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
|
|
|
4c79b5 |
if ($total)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// We need to grab the total number of rows + the offset number of rows to get the correct result
|
|
|
4c79b5 |
if (strpos($query, 'SELECT DISTINCT') === 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result = $this->sql_query($query, $cache_ttl);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Seek by $offset rows
|
|
|
4c79b5 |
if ($offset)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_rowseek($offset, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Return number of affected rows
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_affectedrows()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Fetch current row
|
|
|
4c79b5 |
* @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.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_fetchrow($query_id = false, $debug = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_fetchrow($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Seek to given row number
|
|
|
4c79b5 |
* rownum is zero-based
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_rowseek($rownum, &$query_id)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_rowseek($rownum, $query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->sql_freeresult($query_id);
|
|
|
4c79b5 |
$query_id = $this->sql_query($this->last_query_text);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
|
|
|
4c79b5 |
for ($i = 0; $i < $rownum; $i++)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!$this->sql_fetchrow($query_id))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Get last inserted id after insert statement
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_nextid()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($result_id)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (@odbc_fetch_array($result_id))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$id = @odbc_result($result_id, 1);
|
|
|
4c79b5 |
@odbc_free_result($result_id);
|
|
|
4c79b5 |
return $id;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
@odbc_free_result($result_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Free sql result
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_freeresult($query_id = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_freeresult($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($this->open_queries[(int) $query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
unset($this->open_queries[(int) $query_id]);
|
|
|
4c79b5 |
return @odbc_free_result($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Escape string used in sql query
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_escape($msg)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return str_replace(array("'", "\0"), array("''", ''), $msg);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build LIKE expression
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_like_expression($expression)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $expression . " ESCAPE '\\'";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build db-specific query data
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_custom_build($stage, $data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $data;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* return sql error array
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_error()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'message' => @odbc_errormsg(),
|
|
|
4c79b5 |
'code' => @odbc_error()
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Close sql connection
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_close()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return @odbc_close($this->db_connect_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build db-specific report
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_report($mode, $query = '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($mode)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'start':
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'fromcache':
|
|
|
4c79b5 |
$endtime = explode(' ', microtime());
|
|
|
4c79b5 |
$endtime = $endtime[0] + $endtime[1];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result = @odbc_exec($this->db_connect_id, $query);
|
|
|
4c79b5 |
while ($void = @odbc_fetch_array($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// Take the time spent on parsing rows into account
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
@odbc_free_result($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$splittime = explode(' ', microtime());
|
|
|
4c79b5 |
$splittime = $splittime[0] + $splittime[1];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
?>
|