1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/db/mssql_odbc.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,427 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package dbal
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
1.23 +
1.24 +/**
1.25 +* Unified ODBC functions
1.26 +* Unified ODBC functions support any database having ODBC driver, for example Adabas D, IBM DB2, iODBC, Solid, Sybase SQL Anywhere...
1.27 +* Here we only support MSSQL Server 2000+ because of the provided schema
1.28 +*
1.29 +* @note number of bytes returned for returning data depends on odbc.defaultlrl php.ini setting.
1.30 +* If it is limited to 4K for example only 4K of data is returned max, resulting in incomplete theme data for example.
1.31 +* @note odbc.defaultbinmode may affect UTF8 characters
1.32 +*
1.33 +* @package dbal
1.34 +*/
1.35 +class dbal_mssql_odbc extends dbal
1.36 +{
1.37 + var $last_query_text = '';
1.38 +
1.39 + /**
1.40 + * Connect to server
1.41 + */
1.42 + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
1.43 + {
1.44 + $this->persistency = $persistency;
1.45 + $this->user = $sqluser;
1.46 + $this->dbname = $database;
1.47 +
1.48 + $port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
1.49 + $this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
1.50 +
1.51 + $max_size = @ini_get('odbc.defaultlrl');
1.52 + if (!empty($max_size))
1.53 + {
1.54 + $unit = strtolower(substr($max_size, -1, 1));
1.55 + $max_size = (int) $max_size;
1.56 +
1.57 + if ($unit == 'k')
1.58 + {
1.59 + $max_size = floor($max_size / 1024);
1.60 + }
1.61 + else if ($unit == 'g')
1.62 + {
1.63 + $max_size *= 1024;
1.64 + }
1.65 + else if (is_numeric($unit))
1.66 + {
1.67 + $max_size = floor((int) ($max_size . $unit) / 1048576);
1.68 + }
1.69 + $max_size = max(8, $max_size) . 'M';
1.70 +
1.71 + @ini_set('odbc.defaultlrl', $max_size);
1.72 + }
1.73 +
1.74 + $this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
1.75 +
1.76 + return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
1.77 + }
1.78 +
1.79 + /**
1.80 + * Version information about used database
1.81 + * @param bool $raw if true, only return the fetched sql_server_version
1.82 + * @param bool $use_cache If true, it is safe to retrieve the value from the cache
1.83 + * @return string sql server version
1.84 + */
1.85 + function sql_server_info($raw = false, $use_cache = true)
1.86 + {
1.87 + global $cache;
1.88 +
1.89 + if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
1.90 + {
1.91 + $result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
1.92 +
1.93 + $row = false;
1.94 + if ($result_id)
1.95 + {
1.96 + $row = @odbc_fetch_array($result_id);
1.97 + @odbc_free_result($result_id);
1.98 + }
1.99 +
1.100 + $this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
1.101 +
1.102 + if (!empty($cache) && $use_cache)
1.103 + {
1.104 + $cache->put('mssqlodbc_version', $this->sql_server_version);
1.105 + }
1.106 + }
1.107 +
1.108 + if ($raw)
1.109 + {
1.110 + return $this->sql_server_version;
1.111 + }
1.112 +
1.113 + return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
1.114 + }
1.115 +
1.116 + /**
1.117 + * SQL Transaction
1.118 + * @access private
1.119 + */
1.120 + function _sql_transaction($status = 'begin')
1.121 + {
1.122 + switch ($status)
1.123 + {
1.124 + case 'begin':
1.125 + return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
1.126 + break;
1.127 +
1.128 + case 'commit':
1.129 + return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
1.130 + break;
1.131 +
1.132 + case 'rollback':
1.133 + return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
1.134 + break;
1.135 + }
1.136 +
1.137 + return true;
1.138 + }
1.139 +
1.140 + /**
1.141 + * Base query method
1.142 + *
1.143 + * @param string $query Contains the SQL query which shall be executed
1.144 + * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
1.145 + * @return mixed When casted to bool the returned value returns true on success and false on failure
1.146 + *
1.147 + * @access public
1.148 + */
1.149 + function sql_query($query = '', $cache_ttl = 0)
1.150 + {
1.151 + if ($query != '')
1.152 + {
1.153 + global $cache;
1.154 +
1.155 + // EXPLAIN only in extra debug mode
1.156 + if (defined('DEBUG_EXTRA'))
1.157 + {
1.158 + $this->sql_report('start', $query);
1.159 + }
1.160 +
1.161 + $this->last_query_text = $query;
1.162 + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
1.163 + $this->sql_add_num_queries($this->query_result);
1.164 +
1.165 + if ($this->query_result === false)
1.166 + {
1.167 + if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
1.168 + {
1.169 + $this->sql_error($query);
1.170 + }
1.171 +
1.172 + if (defined('DEBUG_EXTRA'))
1.173 + {
1.174 + $this->sql_report('stop', $query);
1.175 + }
1.176 +
1.177 + if ($cache_ttl && method_exists($cache, 'sql_save'))
1.178 + {
1.179 + $this->open_queries[(int) $this->query_result] = $this->query_result;
1.180 + $cache->sql_save($query, $this->query_result, $cache_ttl);
1.181 + }
1.182 + else if (strpos($query, 'SELECT') === 0 && $this->query_result)
1.183 + {
1.184 + $this->open_queries[(int) $this->query_result] = $this->query_result;
1.185 + }
1.186 + }
1.187 + else if (defined('DEBUG_EXTRA'))
1.188 + {
1.189 + $this->sql_report('fromcache', $query);
1.190 + }
1.191 + }
1.192 + else
1.193 + {
1.194 + return false;
1.195 + }
1.196 +
1.197 + return $this->query_result;
1.198 + }
1.199 +
1.200 + /**
1.201 + * Build LIMIT query
1.202 + */
1.203 + function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
1.204 + {
1.205 + $this->query_result = false;
1.206 +
1.207 + // Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
1.208 + if ($total)
1.209 + {
1.210 + // We need to grab the total number of rows + the offset number of rows to get the correct result
1.211 + if (strpos($query, 'SELECT DISTINCT') === 0)
1.212 + {
1.213 + $query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
1.214 + }
1.215 + else
1.216 + {
1.217 + $query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
1.218 + }
1.219 + }
1.220 +
1.221 + $result = $this->sql_query($query, $cache_ttl);
1.222 +
1.223 + // Seek by $offset rows
1.224 + if ($offset)
1.225 + {
1.226 + $this->sql_rowseek($offset, $result);
1.227 + }
1.228 +
1.229 + return $result;
1.230 + }
1.231 +
1.232 + /**
1.233 + * Return number of affected rows
1.234 + */
1.235 + function sql_affectedrows()
1.236 + {
1.237 + return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
1.238 + }
1.239 +
1.240 + /**
1.241 + * Fetch current row
1.242 + * @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.
1.243 + */
1.244 + function sql_fetchrow($query_id = false, $debug = false)
1.245 + {
1.246 + global $cache;
1.247 +
1.248 + if ($query_id === false)
1.249 + {
1.250 + $query_id = $this->query_result;
1.251 + }
1.252 +
1.253 + if (isset($cache->sql_rowset[$query_id]))
1.254 + {
1.255 + return $cache->sql_fetchrow($query_id);
1.256 + }
1.257 +
1.258 + return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
1.259 + }
1.260 +
1.261 + /**
1.262 + * Seek to given row number
1.263 + * rownum is zero-based
1.264 + */
1.265 + function sql_rowseek($rownum, &$query_id)
1.266 + {
1.267 + global $cache;
1.268 +
1.269 + if ($query_id === false)
1.270 + {
1.271 + $query_id = $this->query_result;
1.272 + }
1.273 +
1.274 + if (isset($cache->sql_rowset[$query_id]))
1.275 + {
1.276 + return $cache->sql_rowseek($rownum, $query_id);
1.277 + }
1.278 +
1.279 + if ($query_id === false)
1.280 + {
1.281 + return false;
1.282 + }
1.283 +
1.284 + $this->sql_freeresult($query_id);
1.285 + $query_id = $this->sql_query($this->last_query_text);
1.286 +
1.287 + if ($query_id === false)
1.288 + {
1.289 + return false;
1.290 + }
1.291 +
1.292 + // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
1.293 + for ($i = 0; $i < $rownum; $i++)
1.294 + {
1.295 + if (!$this->sql_fetchrow($query_id))
1.296 + {
1.297 + return false;
1.298 + }
1.299 + }
1.300 +
1.301 + return true;
1.302 + }
1.303 +
1.304 + /**
1.305 + * Get last inserted id after insert statement
1.306 + */
1.307 + function sql_nextid()
1.308 + {
1.309 + $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
1.310 +
1.311 + if ($result_id)
1.312 + {
1.313 + if (@odbc_fetch_array($result_id))
1.314 + {
1.315 + $id = @odbc_result($result_id, 1);
1.316 + @odbc_free_result($result_id);
1.317 + return $id;
1.318 + }
1.319 + @odbc_free_result($result_id);
1.320 + }
1.321 +
1.322 + return false;
1.323 + }
1.324 +
1.325 + /**
1.326 + * Free sql result
1.327 + */
1.328 + function sql_freeresult($query_id = false)
1.329 + {
1.330 + global $cache;
1.331 +
1.332 + if ($query_id === false)
1.333 + {
1.334 + $query_id = $this->query_result;
1.335 + }
1.336 +
1.337 + if (isset($cache->sql_rowset[$query_id]))
1.338 + {
1.339 + return $cache->sql_freeresult($query_id);
1.340 + }
1.341 +
1.342 + if (isset($this->open_queries[(int) $query_id]))
1.343 + {
1.344 + unset($this->open_queries[(int) $query_id]);
1.345 + return @odbc_free_result($query_id);
1.346 + }
1.347 +
1.348 + return false;
1.349 + }
1.350 +
1.351 + /**
1.352 + * Escape string used in sql query
1.353 + */
1.354 + function sql_escape($msg)
1.355 + {
1.356 + return str_replace(array("'", "\0"), array("''", ''), $msg);
1.357 + }
1.358 +
1.359 + /**
1.360 + * Build LIKE expression
1.361 + * @access private
1.362 + */
1.363 + function _sql_like_expression($expression)
1.364 + {
1.365 + return $expression . " ESCAPE '\\'";
1.366 + }
1.367 +
1.368 + /**
1.369 + * Build db-specific query data
1.370 + * @access private
1.371 + */
1.372 + function _sql_custom_build($stage, $data)
1.373 + {
1.374 + return $data;
1.375 + }
1.376 +
1.377 + /**
1.378 + * return sql error array
1.379 + * @access private
1.380 + */
1.381 + function _sql_error()
1.382 + {
1.383 + return array(
1.384 + 'message' => @odbc_errormsg(),
1.385 + 'code' => @odbc_error()
1.386 + );
1.387 + }
1.388 +
1.389 + /**
1.390 + * Close sql connection
1.391 + * @access private
1.392 + */
1.393 + function _sql_close()
1.394 + {
1.395 + return @odbc_close($this->db_connect_id);
1.396 + }
1.397 +
1.398 + /**
1.399 + * Build db-specific report
1.400 + * @access private
1.401 + */
1.402 + function _sql_report($mode, $query = '')
1.403 + {
1.404 + switch ($mode)
1.405 + {
1.406 + case 'start':
1.407 + break;
1.408 +
1.409 + case 'fromcache':
1.410 + $endtime = explode(' ', microtime());
1.411 + $endtime = $endtime[0] + $endtime[1];
1.412 +
1.413 + $result = @odbc_exec($this->db_connect_id, $query);
1.414 + while ($void = @odbc_fetch_array($result))
1.415 + {
1.416 + // Take the time spent on parsing rows into account
1.417 + }
1.418 + @odbc_free_result($result);
1.419 +
1.420 + $splittime = explode(' ', microtime());
1.421 + $splittime = $splittime[0] + $splittime[1];
1.422 +
1.423 + $this->sql_report('record_fromcache', $query, $endtime, $splittime);
1.424 +
1.425 + break;
1.426 + }
1.427 + }
1.428 +}
1.429 +
1.430 +?>
1.431 \ No newline at end of file