includes/db/mssql_odbc.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:05:07 +0400
changeset 4 e9e194e102a1
parent 0 ceef6d64c21d
permissions -rw-r--r--
Config
     1 <?php
     2 /**
     3 *
     4 * @package dbal
     5 * @version $Id$
     6 * @copyright (c) 2005 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 * @ignore
    13 */
    14 if (!defined('IN_PHPBB'))
    15 {
    16 	exit;
    17 }
    18 
    19 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
    20 
    21 /**
    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
    25 *
    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
    29 *
    30 * @package dbal
    31 */
    32 class dbal_mssql_odbc extends dbal
    33 {
    34 	var $last_query_text = '';
    35 
    36 	/**
    37 	* Connect to server
    38 	*/
    39 	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
    40 	{
    41 		$this->persistency = $persistency;
    42 		$this->user = $sqluser;
    43 		$this->dbname = $database;
    44 
    45 		$port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
    46 		$this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
    47 
    48 		$max_size = @ini_get('odbc.defaultlrl');
    49 		if (!empty($max_size))
    50 		{
    51 			$unit = strtolower(substr($max_size, -1, 1));
    52 			$max_size = (int) $max_size;
    53 
    54 			if ($unit == 'k')
    55 			{
    56 				$max_size = floor($max_size / 1024);
    57 			}
    58 			else if ($unit == 'g')
    59 			{
    60 				$max_size *= 1024;
    61 			}
    62 			else if (is_numeric($unit))
    63 			{
    64 				$max_size = floor((int) ($max_size . $unit) / 1048576);
    65 			}
    66 			$max_size = max(8, $max_size) . 'M';
    67 
    68 			@ini_set('odbc.defaultlrl', $max_size);
    69 		}
    70 
    71 		$this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword);
    72 
    73 		return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
    74 	}
    75 
    76 	/**
    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
    81 	*/
    82 	function sql_server_info($raw = false, $use_cache = true)
    83 	{
    84 		global $cache;
    85 
    86 		if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssqlodbc_version')) === false)
    87 		{
    88 			$result_id = @odbc_exec($this->db_connect_id, "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')");
    89 
    90 			$row = false;
    91 			if ($result_id)
    92 			{
    93 				$row = @odbc_fetch_array($result_id);
    94 				@odbc_free_result($result_id);
    95 			}
    96 
    97 			$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
    98 
    99 			if (!empty($cache) && $use_cache)
   100 			{
   101 				$cache->put('mssqlodbc_version', $this->sql_server_version);
   102 			}
   103 		}
   104 
   105 		if ($raw)
   106 		{
   107 			return $this->sql_server_version;
   108 		}
   109 
   110 		return ($this->sql_server_version) ? 'MSSQL (ODBC)<br />' . $this->sql_server_version : 'MSSQL (ODBC)';
   111 	}
   112 
   113 	/**
   114 	* SQL Transaction
   115 	* @access private
   116 	*/
   117 	function _sql_transaction($status = 'begin')
   118 	{
   119 		switch ($status)
   120 		{
   121 			case 'begin':
   122 				return @odbc_exec($this->db_connect_id, 'BEGIN TRANSACTION');
   123 			break;
   124 
   125 			case 'commit':
   126 				return @odbc_exec($this->db_connect_id, 'COMMIT TRANSACTION');
   127 			break;
   128 
   129 			case 'rollback':
   130 				return @odbc_exec($this->db_connect_id, 'ROLLBACK TRANSACTION');
   131 			break;
   132 		}
   133 
   134 		return true;
   135 	}
   136 
   137 	/**
   138 	* Base query method
   139 	*
   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
   143 	*
   144 	* @access	public
   145 	*/
   146 	function sql_query($query = '', $cache_ttl = 0)
   147 	{
   148 		if ($query != '')
   149 		{
   150 			global $cache;
   151 
   152 			// EXPLAIN only in extra debug mode
   153 			if (defined('DEBUG_EXTRA'))
   154 			{
   155 				$this->sql_report('start', $query);
   156 			}
   157 
   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);
   161 
   162 			if ($this->query_result === false)
   163 			{
   164 				if (($this->query_result = @odbc_exec($this->db_connect_id, $query)) === false)
   165 				{
   166 					$this->sql_error($query);
   167 				}
   168 
   169 				if (defined('DEBUG_EXTRA'))
   170 				{
   171 					$this->sql_report('stop', $query);
   172 				}
   173 
   174 				if ($cache_ttl && method_exists($cache, 'sql_save'))
   175 				{
   176 					$this->open_queries[(int) $this->query_result] = $this->query_result;
   177 					$cache->sql_save($query, $this->query_result, $cache_ttl);
   178 				}
   179 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
   180 				{
   181 					$this->open_queries[(int) $this->query_result] = $this->query_result;
   182 				}
   183 			}
   184 			else if (defined('DEBUG_EXTRA'))
   185 			{
   186 				$this->sql_report('fromcache', $query);
   187 			}
   188 		}
   189 		else
   190 		{
   191 			return false;
   192 		}
   193 
   194 		return $this->query_result;
   195 	}
   196 
   197 	/**
   198 	* Build LIMIT query
   199 	*/
   200 	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
   201 	{
   202 		$this->query_result = false;
   203 
   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)
   205 		if ($total)
   206 		{
   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)
   209 			{
   210 				$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
   211 			}
   212 			else
   213 			{
   214 				$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
   215 			}
   216 		}
   217 
   218 		$result = $this->sql_query($query, $cache_ttl);
   219 
   220 		// Seek by $offset rows
   221 		if ($offset)
   222 		{
   223 			$this->sql_rowseek($offset, $result);
   224 		}
   225 
   226 		return $result;
   227 	}
   228 
   229 	/**
   230 	* Return number of affected rows
   231 	*/
   232 	function sql_affectedrows()
   233 	{
   234 		return ($this->db_connect_id) ? @odbc_num_rows($this->query_result) : false;
   235 	}
   236 
   237 	/**
   238 	* Fetch current row
   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.
   240 	*/
   241 	function sql_fetchrow($query_id = false, $debug = false)
   242 	{
   243 		global $cache;
   244 
   245 		if ($query_id === false)
   246 		{
   247 			$query_id = $this->query_result;
   248 		}
   249 
   250 		if (isset($cache->sql_rowset[$query_id]))
   251 		{
   252 			return $cache->sql_fetchrow($query_id);
   253 		}
   254 
   255 		return ($query_id !== false) ? @odbc_fetch_array($query_id) : false;
   256 	}
   257 
   258 	/**
   259 	* Seek to given row number
   260 	* rownum is zero-based
   261 	*/
   262 	function sql_rowseek($rownum, &$query_id)
   263 	{
   264 		global $cache;
   265 
   266 		if ($query_id === false)
   267 		{
   268 			$query_id = $this->query_result;
   269 		}
   270 
   271 		if (isset($cache->sql_rowset[$query_id]))
   272 		{
   273 			return $cache->sql_rowseek($rownum, $query_id);
   274 		}
   275 
   276 		if ($query_id === false)
   277 		{
   278 			return false;
   279 		}
   280 
   281 		$this->sql_freeresult($query_id);
   282 		$query_id = $this->sql_query($this->last_query_text);
   283 
   284 		if ($query_id === false)
   285 		{
   286 			return false;
   287 		}
   288 
   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++)
   291 		{
   292 			if (!$this->sql_fetchrow($query_id))
   293 			{
   294 				return false;
   295 			}
   296 		}
   297 
   298 		return true;
   299 	}
   300 
   301 	/**
   302 	* Get last inserted id after insert statement
   303 	*/
   304 	function sql_nextid()
   305 	{
   306 		$result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY');
   307 
   308 		if ($result_id)
   309 		{
   310 			if (@odbc_fetch_array($result_id))
   311 			{
   312 				$id = @odbc_result($result_id, 1);
   313 				@odbc_free_result($result_id);
   314 				return $id;
   315 			}
   316 			@odbc_free_result($result_id);
   317 		}
   318 
   319 		return false;
   320 	}
   321 
   322 	/**
   323 	* Free sql result
   324 	*/
   325 	function sql_freeresult($query_id = false)
   326 	{
   327 		global $cache;
   328 
   329 		if ($query_id === false)
   330 		{
   331 			$query_id = $this->query_result;
   332 		}
   333 
   334 		if (isset($cache->sql_rowset[$query_id]))
   335 		{
   336 			return $cache->sql_freeresult($query_id);
   337 		}
   338 
   339 		if (isset($this->open_queries[(int) $query_id]))
   340 		{
   341 			unset($this->open_queries[(int) $query_id]);
   342 			return @odbc_free_result($query_id);
   343 		}
   344 
   345 		return false;
   346 	}
   347 
   348 	/**
   349 	* Escape string used in sql query
   350 	*/
   351 	function sql_escape($msg)
   352 	{
   353 		return str_replace(array("'", "\0"), array("''", ''), $msg);
   354 	}
   355 
   356 	/**
   357 	* Build LIKE expression
   358 	* @access private
   359 	*/
   360 	function _sql_like_expression($expression)
   361 	{
   362 		return $expression . " ESCAPE '\\'";
   363 	}
   364 
   365 	/**
   366 	* Build db-specific query data
   367 	* @access private
   368 	*/
   369 	function _sql_custom_build($stage, $data)
   370 	{
   371 		return $data;
   372 	}
   373 
   374 	/**
   375 	* return sql error array
   376 	* @access private
   377 	*/
   378 	function _sql_error()
   379 	{
   380 		return array(
   381 			'message'	=> @odbc_errormsg(),
   382 			'code'		=> @odbc_error()
   383 		);
   384 	}
   385 
   386 	/**
   387 	* Close sql connection
   388 	* @access private
   389 	*/
   390 	function _sql_close()
   391 	{
   392 		return @odbc_close($this->db_connect_id);
   393 	}
   394 
   395 	/**
   396 	* Build db-specific report
   397 	* @access private
   398 	*/
   399 	function _sql_report($mode, $query = '')
   400 	{
   401 		switch ($mode)
   402 		{
   403 			case 'start':
   404 			break;
   405 
   406 			case 'fromcache':
   407 				$endtime = explode(' ', microtime());
   408 				$endtime = $endtime[0] + $endtime[1];
   409 
   410 				$result = @odbc_exec($this->db_connect_id, $query);
   411 				while ($void = @odbc_fetch_array($result))
   412 				{
   413 					// Take the time spent on parsing rows into account
   414 				}
   415 				@odbc_free_result($result);
   416 
   417 				$splittime = explode(' ', microtime());
   418 				$splittime = $splittime[0] + $splittime[1];
   419 
   420 				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
   421 
   422 			break;
   423 		}
   424 	}
   425 }
   426 
   427 ?>