includes/db/firebird.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/db/firebird.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,569 @@
     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 +* Firebird/Interbase Database Abstraction Layer
    1.26 +* Minimum Requirement is Firebird 2.1
    1.27 +* @package dbal
    1.28 +*/
    1.29 +class dbal_firebird extends dbal
    1.30 +{
    1.31 +	var $last_query_text = '';
    1.32 +	var $service_handle = false;
    1.33 +	var $affected_rows = 0;
    1.34 +	var $connect_error = '';
    1.35 +
    1.36 +	/**
    1.37 +	* Connect to server
    1.38 +	*/
    1.39 +	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
    1.40 +	{
    1.41 +		$this->persistency = $persistency;
    1.42 +		$this->user = $sqluser;
    1.43 +		$this->server = $sqlserver . (($port) ? ':' . $port : '');
    1.44 +		$this->dbname = str_replace('\\', '/', $database);
    1.45 +
    1.46 +		// There are three possibilities to connect to an interbase db
    1.47 +		if (!$this->server)
    1.48 +		{
    1.49 +			$use_database = $this->dbname;
    1.50 +		}
    1.51 +		else if (strpos($this->server, '//') === 0)
    1.52 +		{
    1.53 +			$use_database = $this->server . $this->dbname;
    1.54 +		}
    1.55 +		else
    1.56 +		{
    1.57 +			$use_database = $this->server . ':' . $this->dbname;
    1.58 +		}
    1.59 +
    1.60 +		if ($this->persistency)
    1.61 +		{
    1.62 +			if (!function_exists('ibase_pconnect'))
    1.63 +			{
    1.64 +				$this->connect_error = 'ibase_pconnect function does not exist, is interbase extension installed?';
    1.65 +				return $this->sql_error('');
    1.66 +			}
    1.67 +			$this->db_connect_id = @ibase_pconnect($use_database, $this->user, $sqlpassword, false, false, 3);
    1.68 +		}
    1.69 +		else
    1.70 +		{
    1.71 +			if (!function_exists('ibase_connect'))
    1.72 +			{
    1.73 +				$this->connect_error = 'ibase_connect function does not exist, is interbase extension installed?';
    1.74 +				return $this->sql_error('');
    1.75 +			}
    1.76 +			$this->db_connect_id = @ibase_connect($use_database, $this->user, $sqlpassword, false, false, 3);
    1.77 +		}
    1.78 +
    1.79 +		// Do not call ibase_service_attach if connection failed,
    1.80 +		// otherwise error message from ibase_(p)connect call will be clobbered.
    1.81 +		if ($this->db_connect_id && function_exists('ibase_service_attach') && $this->server)
    1.82 +		{
    1.83 +			$this->service_handle = @ibase_service_attach($this->server, $this->user, $sqlpassword);
    1.84 +		}
    1.85 +		else
    1.86 +		{
    1.87 +			$this->service_handle = false;
    1.88 +		}
    1.89 +
    1.90 +		return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
    1.91 +	}
    1.92 +
    1.93 +	/**
    1.94 +	* Version information about used database
    1.95 +	* @param bool $raw if true, only return the fetched sql_server_version
    1.96 +	* @param bool $use_cache forced to false for Interbase
    1.97 +	* @return string sql server version
    1.98 +	*/
    1.99 +	function sql_server_info($raw = false, $use_cache = true)
   1.100 +	{
   1.101 +		/**
   1.102 +		* force $use_cache false.  I didn't research why the caching code there is no caching code
   1.103 +		* but I assume its because the IB extension provides a direct method to access it
   1.104 +		* without a query.
   1.105 +		*/
   1.106 +
   1.107 +		$use_cache = false;
   1.108 +
   1.109 +		if ($this->service_handle !== false && function_exists('ibase_server_info'))
   1.110 +		{
   1.111 +			return @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION);
   1.112 +		}
   1.113 +
   1.114 +		return ($raw) ? '2.1' : 'Firebird/Interbase';
   1.115 +	}
   1.116 +
   1.117 +	/**
   1.118 +	* SQL Transaction
   1.119 +	* @access private
   1.120 +	*/
   1.121 +	function _sql_transaction($status = 'begin')
   1.122 +	{
   1.123 +		switch ($status)
   1.124 +		{
   1.125 +			case 'begin':
   1.126 +				return true;
   1.127 +			break;
   1.128 +
   1.129 +			case 'commit':
   1.130 +				return @ibase_commit();
   1.131 +			break;
   1.132 +
   1.133 +			case 'rollback':
   1.134 +				return @ibase_rollback();
   1.135 +			break;
   1.136 +		}
   1.137 +
   1.138 +		return true;
   1.139 +	}
   1.140 +
   1.141 +	/**
   1.142 +	* Base query method
   1.143 +	*
   1.144 +	* @param	string	$query		Contains the SQL query which shall be executed
   1.145 +	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
   1.146 +	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
   1.147 +	*
   1.148 +	* @access	public
   1.149 +	*/
   1.150 +	function sql_query($query = '', $cache_ttl = 0)
   1.151 +	{
   1.152 +		if ($query != '')
   1.153 +		{
   1.154 +			global $cache;
   1.155 +
   1.156 +			// EXPLAIN only in extra debug mode
   1.157 +			if (defined('DEBUG_EXTRA'))
   1.158 +			{
   1.159 +				$this->sql_report('start', $query);
   1.160 +			}
   1.161 +
   1.162 +			$this->last_query_text = $query;
   1.163 +			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
   1.164 +			$this->sql_add_num_queries($this->query_result);
   1.165 +
   1.166 +			if ($this->query_result === false)
   1.167 +			{
   1.168 +				$array = array();
   1.169 +				// We overcome Firebird's 32767 char limit by binding vars
   1.170 +				if (strlen($query) > 32767)
   1.171 +				{
   1.172 +					if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
   1.173 +					{
   1.174 +						if (strlen($regs[3]) > 32767)
   1.175 +						{
   1.176 +							preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
   1.177 +
   1.178 +							$inserts = $vals[0];
   1.179 +							unset($vals);
   1.180 +
   1.181 +							foreach ($inserts as $key => $value)
   1.182 +							{
   1.183 +								if (!empty($value) && $value[0] === "'" && strlen($value) > 32769) // check to see if this thing is greater than the max + 'x2
   1.184 +								{
   1.185 +									$inserts[$key] = '?';
   1.186 +									$array[] = str_replace("''", "'", substr($value, 1, -1));
   1.187 +								}
   1.188 +							}
   1.189 +
   1.190 +							$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
   1.191 +						}
   1.192 +					}
   1.193 +					else if (preg_match('/^(UPDATE ([\\w_]++)\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|\\d+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data))
   1.194 +					{
   1.195 +						if (strlen($data[3]) > 32767)
   1.196 +						{
   1.197 +							$update = $data[1];
   1.198 +							$where = $data[4];
   1.199 +							preg_match_all('/(\\w++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[3], $temp, PREG_SET_ORDER);
   1.200 +							unset($data);
   1.201 +
   1.202 +							$cols = array();
   1.203 +							foreach ($temp as $value)
   1.204 +							{
   1.205 +								if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32769) // check to see if this thing is greater than the max + 'x2
   1.206 +								{
   1.207 +									$array[] = str_replace("''", "'", substr($value[2], 1, -1));
   1.208 +									$cols[] = $value[1] . '=?';
   1.209 +								}
   1.210 +								else
   1.211 +								{
   1.212 +									$cols[] = $value[1] . '=' . $value[2];
   1.213 +								}
   1.214 +							}
   1.215 +
   1.216 +							$query = $update . implode(', ', $cols) . ' ' . $where;
   1.217 +							unset($cols);
   1.218 +						}
   1.219 +					}
   1.220 +				}
   1.221 +
   1.222 +				if (!function_exists('ibase_affected_rows') && (preg_match('/^UPDATE ([\w_]++)\s+SET [\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\s*[\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+\s+(WHERE.*)?$/s', $query, $regs) || preg_match('/^DELETE FROM ([\w_]++)\s*(WHERE\s*.*)?$/s', $query, $regs)))
   1.223 +				{
   1.224 +					$affected_sql = 'SELECT COUNT(*) as num_rows_affected FROM ' . $regs[1];
   1.225 +					if (!empty($regs[2]))
   1.226 +					{
   1.227 +						$affected_sql .= ' ' . $regs[2];
   1.228 +					}
   1.229 +
   1.230 +					if (!($temp_q_id = @ibase_query($this->db_connect_id, $affected_sql)))
   1.231 +					{
   1.232 +						return false;
   1.233 +					}
   1.234 +
   1.235 +					$temp_result = @ibase_fetch_assoc($temp_q_id);
   1.236 +					@ibase_free_result($temp_q_id);
   1.237 +
   1.238 +					$this->affected_rows = ($temp_result) ? $temp_result['NUM_ROWS_AFFECTED'] : false;
   1.239 +				}
   1.240 +
   1.241 +				if (sizeof($array))
   1.242 +				{
   1.243 +					$p_query = @ibase_prepare($this->db_connect_id, $query);
   1.244 +					array_unshift($array, $p_query);
   1.245 +					$this->query_result = call_user_func_array('ibase_execute', $array);
   1.246 +					unset($array);
   1.247 +
   1.248 +					if ($this->query_result === false)
   1.249 +					{
   1.250 +						$this->sql_error($query);
   1.251 +					}
   1.252 +				}
   1.253 +				else if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false)
   1.254 +				{
   1.255 +					$this->sql_error($query);
   1.256 +				}
   1.257 +
   1.258 +				if (defined('DEBUG_EXTRA'))
   1.259 +				{
   1.260 +					$this->sql_report('stop', $query);
   1.261 +				}
   1.262 +
   1.263 +				if (!$this->transaction)
   1.264 +				{
   1.265 +					if (function_exists('ibase_commit_ret'))
   1.266 +					{
   1.267 +						@ibase_commit_ret();
   1.268 +					}
   1.269 +					else
   1.270 +					{
   1.271 +						// way cooler than ibase_commit_ret :D
   1.272 +						@ibase_query('COMMIT RETAIN;');
   1.273 +					}
   1.274 +				}
   1.275 +
   1.276 +				if ($cache_ttl && method_exists($cache, 'sql_save'))
   1.277 +				{
   1.278 +					$this->open_queries[(int) $this->query_result] = $this->query_result;
   1.279 +					$cache->sql_save($query, $this->query_result, $cache_ttl);
   1.280 +				}
   1.281 +				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
   1.282 +				{
   1.283 +					$this->open_queries[(int) $this->query_result] = $this->query_result;
   1.284 +				}
   1.285 +			}
   1.286 +			else if (defined('DEBUG_EXTRA'))
   1.287 +			{
   1.288 +				$this->sql_report('fromcache', $query);
   1.289 +			}
   1.290 +		}
   1.291 +		else
   1.292 +		{
   1.293 +			return false;
   1.294 +		}
   1.295 +
   1.296 +		return $this->query_result;
   1.297 +	}
   1.298 +
   1.299 +	/**
   1.300 +	* Build LIMIT query
   1.301 +	*/
   1.302 +	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
   1.303 +	{
   1.304 +		$this->query_result = false;
   1.305 +
   1.306 +		$query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6);
   1.307 +
   1.308 +		return $this->sql_query($query, $cache_ttl);
   1.309 +	}
   1.310 +
   1.311 +	/**
   1.312 +	* Return number of affected rows
   1.313 +	*/
   1.314 +	function sql_affectedrows()
   1.315 +	{
   1.316 +		// PHP 5+ function
   1.317 +		if (function_exists('ibase_affected_rows'))
   1.318 +		{
   1.319 +			return ($this->db_connect_id) ? @ibase_affected_rows($this->db_connect_id) : false;
   1.320 +		}
   1.321 +		else
   1.322 +		{
   1.323 +			return $this->affected_rows;
   1.324 +		}
   1.325 +	}
   1.326 +
   1.327 +	/**
   1.328 +	* Fetch current row
   1.329 +	*/
   1.330 +	function sql_fetchrow($query_id = false)
   1.331 +	{
   1.332 +		global $cache;
   1.333 +
   1.334 +		if ($query_id === false)
   1.335 +		{
   1.336 +			$query_id = $this->query_result;
   1.337 +		}
   1.338 +
   1.339 +		if (isset($cache->sql_rowset[$query_id]))
   1.340 +		{
   1.341 +			return $cache->sql_fetchrow($query_id);
   1.342 +		}
   1.343 +
   1.344 +		if ($query_id === false)
   1.345 +		{
   1.346 +			return false;
   1.347 +		}
   1.348 +
   1.349 +		$row = array();
   1.350 +		$cur_row = @ibase_fetch_object($query_id, IBASE_TEXT);
   1.351 +
   1.352 +		if (!$cur_row)
   1.353 +		{
   1.354 +			return false;
   1.355 +		}
   1.356 +
   1.357 +		foreach (get_object_vars($cur_row) as $key => $value)
   1.358 +		{
   1.359 +			$row[strtolower($key)] = (is_string($value)) ? trim(str_replace(array("\\0", "\\n"), array("\0", "\n"), $value)) : $value;
   1.360 +		}
   1.361 +
   1.362 +		return (sizeof($row)) ? $row : false;
   1.363 +	}
   1.364 +
   1.365 +	/**
   1.366 +	* Seek to given row number
   1.367 +	* rownum is zero-based
   1.368 +	*/
   1.369 +	function sql_rowseek($rownum, &$query_id)
   1.370 +	{
   1.371 +		global $cache;
   1.372 +
   1.373 +		if ($query_id === false)
   1.374 +		{
   1.375 +			$query_id = $this->query_result;
   1.376 +		}
   1.377 +
   1.378 +		if (isset($cache->sql_rowset[$query_id]))
   1.379 +		{
   1.380 +			return $cache->sql_rowseek($rownum, $query_id);
   1.381 +		}
   1.382 +
   1.383 +		if ($query_id === false)
   1.384 +		{
   1.385 +			return;
   1.386 +		}
   1.387 +
   1.388 +		$this->sql_freeresult($query_id);
   1.389 +		$query_id = $this->sql_query($this->last_query_text);
   1.390 +
   1.391 +		if ($query_id === false)
   1.392 +		{
   1.393 +			return false;
   1.394 +		}
   1.395 +
   1.396 +		// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
   1.397 +		for ($i = 0; $i < $rownum; $i++)
   1.398 +		{
   1.399 +			if (!$this->sql_fetchrow($query_id))
   1.400 +			{
   1.401 +				return false;
   1.402 +			}
   1.403 +		}
   1.404 +
   1.405 +		return true;
   1.406 +	}
   1.407 +
   1.408 +	/**
   1.409 +	* Get last inserted id after insert statement
   1.410 +	*/
   1.411 +	function sql_nextid()
   1.412 +	{
   1.413 +		$query_id = $this->query_result;
   1.414 +
   1.415 +		if ($query_id !== false && $this->last_query_text != '')
   1.416 +		{
   1.417 +			if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#i', $this->last_query_text, $tablename))
   1.418 +			{
   1.419 +				$sql = 'SELECT GEN_ID(' . $tablename[1] . '_gen, 0) AS new_id FROM RDB$DATABASE';
   1.420 +
   1.421 +				if (!($temp_q_id = @ibase_query($this->db_connect_id, $sql)))
   1.422 +				{
   1.423 +					return false;
   1.424 +				}
   1.425 +
   1.426 +				$temp_result = @ibase_fetch_assoc($temp_q_id);
   1.427 +				@ibase_free_result($temp_q_id);
   1.428 +
   1.429 +				return ($temp_result) ? $temp_result['NEW_ID'] : false;
   1.430 +			}
   1.431 +		}
   1.432 +
   1.433 +		return false;
   1.434 +	}
   1.435 +
   1.436 +	/**
   1.437 +	* Free sql result
   1.438 +	*/
   1.439 +	function sql_freeresult($query_id = false)
   1.440 +	{
   1.441 +		global $cache;
   1.442 +
   1.443 +		if ($query_id === false)
   1.444 +		{
   1.445 +			$query_id = $this->query_result;
   1.446 +		}
   1.447 +
   1.448 +		if (isset($cache->sql_rowset[$query_id]))
   1.449 +		{
   1.450 +			return $cache->sql_freeresult($query_id);
   1.451 +		}
   1.452 +
   1.453 +		if (isset($this->open_queries[(int) $query_id]))
   1.454 +		{
   1.455 +			unset($this->open_queries[(int) $query_id]);
   1.456 +			return @ibase_free_result($query_id);
   1.457 +		}
   1.458 +
   1.459 +		return false;
   1.460 +	}
   1.461 +
   1.462 +	/**
   1.463 +	* Escape string used in sql query
   1.464 +	*/
   1.465 +	function sql_escape($msg)
   1.466 +	{
   1.467 +		return str_replace(array("'", "\0"), array("''", ''), $msg);
   1.468 +	}
   1.469 +
   1.470 +	/**
   1.471 +	* Build LIKE expression
   1.472 +	* @access private
   1.473 +	*/
   1.474 +	function _sql_like_expression($expression)
   1.475 +	{
   1.476 +		return $expression . " ESCAPE '\\'";
   1.477 +	}
   1.478 +
   1.479 +	/**
   1.480 +	* Build db-specific query data
   1.481 +	* @access private
   1.482 +	*/
   1.483 +	function _sql_custom_build($stage, $data)
   1.484 +	{
   1.485 +		return $data;
   1.486 +	}
   1.487 +
   1.488 +	function _sql_bit_and($column_name, $bit, $compare = '')
   1.489 +	{
   1.490 +		return 'BIN_AND(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : '');
   1.491 +	}
   1.492 +
   1.493 +	function _sql_bit_or($column_name, $bit, $compare = '')
   1.494 +	{
   1.495 +		return 'BIN_OR(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : '');
   1.496 +	}
   1.497 +
   1.498 +	/**
   1.499 +	* return sql error array
   1.500 +	* @access private
   1.501 +	*/
   1.502 +	function _sql_error()
   1.503 +	{
   1.504 +		// Need special handling here because ibase_errmsg returns
   1.505 +		// connection errors, however if the interbase extension
   1.506 +		// is not installed then ibase_errmsg does not exist and
   1.507 +		// we cannot call it.
   1.508 +		if (function_exists('ibase_errmsg'))
   1.509 +		{
   1.510 +			$msg = @ibase_errmsg();
   1.511 +			if (!$msg)
   1.512 +			{
   1.513 +				$msg = $this->connect_error;
   1.514 +			}
   1.515 +		}
   1.516 +		else
   1.517 +		{
   1.518 +			$msg = $this->connect_error;
   1.519 +		}
   1.520 +		return array(
   1.521 +			'message'	=> $msg,
   1.522 +			'code'		=> (@function_exists('ibase_errcode') ? @ibase_errcode() : '')
   1.523 +		);
   1.524 +	}
   1.525 +
   1.526 +	/**
   1.527 +	* Close sql connection
   1.528 +	* @access private
   1.529 +	*/
   1.530 +	function _sql_close()
   1.531 +	{
   1.532 +		if ($this->service_handle !== false)
   1.533 +		{
   1.534 +			@ibase_service_detach($this->service_handle);
   1.535 +		}
   1.536 +
   1.537 +		return @ibase_close($this->db_connect_id);
   1.538 +	}
   1.539 +
   1.540 +	/**
   1.541 +	* Build db-specific report
   1.542 +	* @access private
   1.543 +	*/
   1.544 +	function _sql_report($mode, $query = '')
   1.545 +	{
   1.546 +		switch ($mode)
   1.547 +		{
   1.548 +			case 'start':
   1.549 +			break;
   1.550 +
   1.551 +			case 'fromcache':
   1.552 +				$endtime = explode(' ', microtime());
   1.553 +				$endtime = $endtime[0] + $endtime[1];
   1.554 +
   1.555 +				$result = @ibase_query($this->db_connect_id, $query);
   1.556 +				while ($void = @ibase_fetch_object($result, IBASE_TEXT))
   1.557 +				{
   1.558 +					// Take the time spent on parsing rows into account
   1.559 +				}
   1.560 +				@ibase_free_result($result);
   1.561 +
   1.562 +				$splittime = explode(' ', microtime());
   1.563 +				$splittime = $splittime[0] + $splittime[1];
   1.564 +
   1.565 +				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
   1.566 +
   1.567 +			break;
   1.568 +		}
   1.569 +	}
   1.570 +}
   1.571 +
   1.572 +?>
   1.573 \ No newline at end of file