includes/db/mssql.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     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 * MSSQL Database Abstraction Layer
       
    23 * Minimum Requirement is MSSQL 2000+
       
    24 * @package dbal
       
    25 */
       
    26 class dbal_mssql extends dbal
       
    27 {
       
    28 	/**
       
    29 	* Connect to server
       
    30 	*/
       
    31 	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
       
    32 	{
       
    33 		$this->persistency = $persistency;
       
    34 		$this->user = $sqluser;
       
    35 		$this->dbname = $database;
       
    36 
       
    37 		$port_delimiter = (defined('PHP_OS') && substr(PHP_OS, 0, 3) === 'WIN') ? ',' : ':';
       
    38 		$this->server = $sqlserver . (($port) ? $port_delimiter . $port : '');
       
    39 
       
    40 		@ini_set('mssql.charset', 'UTF-8');
       
    41 		@ini_set('mssql.textlimit', 2147483647);
       
    42 		@ini_set('mssql.textsize', 2147483647);
       
    43 
       
    44 		if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.1', '>=')))
       
    45 		{
       
    46 			$this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mssql_connect($this->server, $this->user, $sqlpassword, $new_link);
       
    47 		}
       
    48 		else
       
    49 		{
       
    50 			$this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword);
       
    51 		}
       
    52 
       
    53 		if ($this->db_connect_id && $this->dbname != '')
       
    54 		{
       
    55 			if (!@mssql_select_db($this->dbname, $this->db_connect_id))
       
    56 			{
       
    57 				@mssql_close($this->db_connect_id);
       
    58 				return false;
       
    59 			}
       
    60 		}
       
    61 
       
    62 		return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
       
    63 	}
       
    64 
       
    65 	/**
       
    66 	* Version information about used database
       
    67 	* @param bool $raw if true, only return the fetched sql_server_version
       
    68 	* @param bool $use_cache If true, it is safe to retrieve the value from the cache
       
    69 	* @return string sql server version
       
    70 	*/
       
    71 	function sql_server_info($raw = false, $use_cache = true)
       
    72 	{
       
    73 		global $cache;
       
    74 
       
    75 		if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('mssql_version')) === false)
       
    76 		{
       
    77 			$result_id = @mssql_query("SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')", $this->db_connect_id);
       
    78 
       
    79 			$row = false;
       
    80 			if ($result_id)
       
    81 			{
       
    82 				$row = @mssql_fetch_assoc($result_id);
       
    83 				@mssql_free_result($result_id);
       
    84 			}
       
    85 
       
    86 			$this->sql_server_version = ($row) ? trim(implode(' ', $row)) : 0;
       
    87 
       
    88 			if (!empty($cache) && $use_cache)
       
    89 			{
       
    90 				$cache->put('mssql_version', $this->sql_server_version);
       
    91 			}
       
    92 		}
       
    93 
       
    94 		if ($raw)
       
    95 		{
       
    96 			return $this->sql_server_version;
       
    97 		}
       
    98 
       
    99 		return ($this->sql_server_version) ? 'MSSQL<br />' . $this->sql_server_version : 'MSSQL';
       
   100 	}
       
   101 
       
   102 	/**
       
   103 	* SQL Transaction
       
   104 	* @access private
       
   105 	*/
       
   106 	function _sql_transaction($status = 'begin')
       
   107 	{
       
   108 		switch ($status)
       
   109 		{
       
   110 			case 'begin':
       
   111 				return @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
       
   112 			break;
       
   113 
       
   114 			case 'commit':
       
   115 				return @mssql_query('COMMIT TRANSACTION', $this->db_connect_id);
       
   116 			break;
       
   117 
       
   118 			case 'rollback':
       
   119 				return @mssql_query('ROLLBACK TRANSACTION', $this->db_connect_id);
       
   120 			break;
       
   121 		}
       
   122 
       
   123 		return true;
       
   124 	}
       
   125 
       
   126 	/**
       
   127 	* Base query method
       
   128 	*
       
   129 	* @param	string	$query		Contains the SQL query which shall be executed
       
   130 	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
       
   131 	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
       
   132 	*
       
   133 	* @access	public
       
   134 	*/
       
   135 	function sql_query($query = '', $cache_ttl = 0)
       
   136 	{
       
   137 		if ($query != '')
       
   138 		{
       
   139 			global $cache;
       
   140 
       
   141 			// EXPLAIN only in extra debug mode
       
   142 			if (defined('DEBUG_EXTRA'))
       
   143 			{
       
   144 				$this->sql_report('start', $query);
       
   145 			}
       
   146 
       
   147 			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
       
   148 			$this->sql_add_num_queries($this->query_result);
       
   149 
       
   150 			if ($this->query_result === false)
       
   151 			{
       
   152 				if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false)
       
   153 				{
       
   154 					$this->sql_error($query);
       
   155 				}
       
   156 
       
   157 				if (defined('DEBUG_EXTRA'))
       
   158 				{
       
   159 					$this->sql_report('stop', $query);
       
   160 				}
       
   161 
       
   162 				if ($cache_ttl && method_exists($cache, 'sql_save'))
       
   163 				{
       
   164 					$this->open_queries[(int) $this->query_result] = $this->query_result;
       
   165 					$cache->sql_save($query, $this->query_result, $cache_ttl);
       
   166 				}
       
   167 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
       
   168 				{
       
   169 					$this->open_queries[(int) $this->query_result] = $this->query_result;
       
   170 				}
       
   171 			}
       
   172 			else if (defined('DEBUG_EXTRA'))
       
   173 			{
       
   174 				$this->sql_report('fromcache', $query);
       
   175 			}
       
   176 		}
       
   177 		else
       
   178 		{
       
   179 			return false;
       
   180 		}
       
   181 
       
   182 		return $this->query_result;
       
   183 	}
       
   184 
       
   185 	/**
       
   186 	* Build LIMIT query
       
   187 	*/
       
   188 	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
       
   189 	{
       
   190 		$this->query_result = false;
       
   191 
       
   192 		// Since TOP is only returning a set number of rows we won't need it if total is set to 0 (return all rows)
       
   193 		if ($total)
       
   194 		{
       
   195 			// We need to grab the total number of rows + the offset number of rows to get the correct result
       
   196 			if (strpos($query, 'SELECT DISTINCT') === 0)
       
   197 			{
       
   198 				$query = 'SELECT DISTINCT TOP ' . ($total + $offset) . ' ' . substr($query, 15);
       
   199 			}
       
   200 			else
       
   201 			{
       
   202 				$query = 'SELECT TOP ' . ($total + $offset) . ' ' . substr($query, 6);
       
   203 			}
       
   204 		}
       
   205 
       
   206 		$result = $this->sql_query($query, $cache_ttl);
       
   207 
       
   208 		// Seek by $offset rows
       
   209 		if ($offset)
       
   210 		{
       
   211 			$this->sql_rowseek($offset, $result);
       
   212 		}
       
   213 
       
   214 		return $result;
       
   215 	}
       
   216 
       
   217 	/**
       
   218 	* Return number of affected rows
       
   219 	*/
       
   220 	function sql_affectedrows()
       
   221 	{
       
   222 		return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false;
       
   223 	}
       
   224 
       
   225 	/**
       
   226 	* Fetch current row
       
   227 	*/
       
   228 	function sql_fetchrow($query_id = false)
       
   229 	{
       
   230 		global $cache;
       
   231 
       
   232 		if ($query_id === false)
       
   233 		{
       
   234 			$query_id = $this->query_result;
       
   235 		}
       
   236 
       
   237 		if (isset($cache->sql_rowset[$query_id]))
       
   238 		{
       
   239 			return $cache->sql_fetchrow($query_id);
       
   240 		}
       
   241 
       
   242 		if ($query_id === false)
       
   243 		{
       
   244 			return false;
       
   245 		}
       
   246 
       
   247 		$row = @mssql_fetch_assoc($query_id);
       
   248 
       
   249 		// I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
       
   250 		if ($row)
       
   251 		{
       
   252 			foreach ($row as $key => $value)
       
   253 			{
       
   254 				$row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
       
   255 			}
       
   256 		}
       
   257 
       
   258 		return $row;
       
   259 	}
       
   260 
       
   261 	/**
       
   262 	* Seek to given row number
       
   263 	* rownum is zero-based
       
   264 	*/
       
   265 	function sql_rowseek($rownum, &$query_id)
       
   266 	{
       
   267 		global $cache;
       
   268 
       
   269 		if ($query_id === false)
       
   270 		{
       
   271 			$query_id = $this->query_result;
       
   272 		}
       
   273 
       
   274 		if (isset($cache->sql_rowset[$query_id]))
       
   275 		{
       
   276 			return $cache->sql_rowseek($rownum, $query_id);
       
   277 		}
       
   278 
       
   279 		return ($query_id !== false) ? @mssql_data_seek($query_id, $rownum) : false;
       
   280 	}
       
   281 
       
   282 	/**
       
   283 	* Get last inserted id after insert statement
       
   284 	*/
       
   285 	function sql_nextid()
       
   286 	{
       
   287 		$result_id = @mssql_query('SELECT SCOPE_IDENTITY()', $this->db_connect_id);
       
   288 		if ($result_id)
       
   289 		{
       
   290 			if ($row = @mssql_fetch_assoc($result_id))
       
   291 			{
       
   292 				@mssql_free_result($result_id);
       
   293 				return $row['computed'];
       
   294 			}
       
   295 			@mssql_free_result($result_id);
       
   296 		}
       
   297 
       
   298 		return false;
       
   299 	}
       
   300 
       
   301 	/**
       
   302 	* Free sql result
       
   303 	*/
       
   304 	function sql_freeresult($query_id = false)
       
   305 	{
       
   306 		global $cache;
       
   307 
       
   308 		if ($query_id === false)
       
   309 		{
       
   310 			$query_id = $this->query_result;
       
   311 		}
       
   312 
       
   313 		if (isset($cache->sql_rowset[$query_id]))
       
   314 		{
       
   315 			return $cache->sql_freeresult($query_id);
       
   316 		}
       
   317 
       
   318 		if (isset($this->open_queries[$query_id]))
       
   319 		{
       
   320 			unset($this->open_queries[$query_id]);
       
   321 			return @mssql_free_result($query_id);
       
   322 		}
       
   323 
       
   324 		return false;
       
   325 	}
       
   326 
       
   327 	/**
       
   328 	* Escape string used in sql query
       
   329 	*/
       
   330 	function sql_escape($msg)
       
   331 	{
       
   332 		return str_replace(array("'", "\0"), array("''", ''), $msg);
       
   333 	}
       
   334 
       
   335 	/**
       
   336 	* Build LIKE expression
       
   337 	* @access private
       
   338 	*/
       
   339 	function _sql_like_expression($expression)
       
   340 	{
       
   341 		return $expression . " ESCAPE '\\'";
       
   342 	}
       
   343 
       
   344 	/**
       
   345 	* return sql error array
       
   346 	* @access private
       
   347 	*/
       
   348 	function _sql_error()
       
   349 	{
       
   350 		$error = array(
       
   351 			'message'	=> @mssql_get_last_message(),
       
   352 			'code'		=> ''
       
   353 		);
       
   354 
       
   355 		// Get error code number
       
   356 		$result_id = @mssql_query('SELECT @@ERROR as code', $this->db_connect_id);
       
   357 		if ($result_id)
       
   358 		{
       
   359 			$row = @mssql_fetch_assoc($result_id);
       
   360 			$error['code'] = $row['code'];
       
   361 			@mssql_free_result($result_id);
       
   362 		}
       
   363 
       
   364 		// Get full error message if possible
       
   365 		$sql = 'SELECT CAST(description as varchar(255)) as message
       
   366 			FROM master.dbo.sysmessages
       
   367 			WHERE error = ' . $error['code'];
       
   368 		$result_id = @mssql_query($sql);
       
   369 		
       
   370 		if ($result_id)
       
   371 		{
       
   372 			$row = @mssql_fetch_assoc($result_id);
       
   373 			if (!empty($row['message']))
       
   374 			{
       
   375 				$error['message'] .= '<br />' . $row['message'];
       
   376 			}
       
   377 			@mssql_free_result($result_id);
       
   378 		}
       
   379 
       
   380 		return $error;
       
   381 	}
       
   382 
       
   383 	/**
       
   384 	* Build db-specific query data
       
   385 	* @access private
       
   386 	*/
       
   387 	function _sql_custom_build($stage, $data)
       
   388 	{
       
   389 		return $data;
       
   390 	}
       
   391 
       
   392 	/**
       
   393 	* Close sql connection
       
   394 	* @access private
       
   395 	*/
       
   396 	function _sql_close()
       
   397 	{
       
   398 		return @mssql_close($this->db_connect_id);
       
   399 	}
       
   400 
       
   401 	/**
       
   402 	* Build db-specific report
       
   403 	* @access private
       
   404 	*/
       
   405 	function _sql_report($mode, $query = '')
       
   406 	{
       
   407 		switch ($mode)
       
   408 		{
       
   409 			case 'start':
       
   410 				$html_table = false;
       
   411 				@mssql_query('SET SHOWPLAN_TEXT ON;', $this->db_connect_id);
       
   412 				if ($result = @mssql_query($query, $this->db_connect_id))
       
   413 				{
       
   414 					@mssql_next_result($result);
       
   415 					while ($row = @mssql_fetch_row($result))
       
   416 					{
       
   417 						$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
       
   418 					}
       
   419 				}
       
   420 				@mssql_query('SET SHOWPLAN_TEXT OFF;', $this->db_connect_id);
       
   421 				@mssql_free_result($result);
       
   422 
       
   423 				if ($html_table)
       
   424 				{
       
   425 					$this->html_hold .= '</table>';
       
   426 				}
       
   427 			break;
       
   428 
       
   429 			case 'fromcache':
       
   430 				$endtime = explode(' ', microtime());
       
   431 				$endtime = $endtime[0] + $endtime[1];
       
   432 
       
   433 				$result = @mssql_query($query, $this->db_connect_id);
       
   434 				while ($void = @mssql_fetch_assoc($result))
       
   435 				{
       
   436 					// Take the time spent on parsing rows into account
       
   437 				}
       
   438 				@mssql_free_result($result);
       
   439 
       
   440 				$splittime = explode(' ', microtime());
       
   441 				$splittime = $splittime[0] + $splittime[1];
       
   442 
       
   443 				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
       
   444 
       
   445 			break;
       
   446 		}
       
   447 	}
       
   448 }
       
   449 
       
   450 ?>