includes/db/sqlite.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/db/sqlite.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,339 @@
     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 +* Sqlite Database Abstraction Layer
    1.26 +* Minimum Requirement: 2.8.2+
    1.27 +* @package dbal
    1.28 +*/
    1.29 +class dbal_sqlite extends dbal
    1.30 +{
    1.31 +	/**
    1.32 +	* Connect to server
    1.33 +	*/
    1.34 +	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
    1.35 +	{
    1.36 +		$this->persistency = $persistency;
    1.37 +		$this->user = $sqluser;
    1.38 +		$this->server = $sqlserver . (($port) ? ':' . $port : '');
    1.39 +		$this->dbname = $database;
    1.40 +
    1.41 +		$error = '';
    1.42 +		$this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error);
    1.43 +
    1.44 +		if ($this->db_connect_id)
    1.45 +		{
    1.46 +			@sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
    1.47 +//			@sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id);
    1.48 +		}
    1.49 +
    1.50 +		return ($this->db_connect_id) ? true : array('message' => $error);
    1.51 +	}
    1.52 +
    1.53 +	/**
    1.54 +	* Version information about used database
    1.55 +	* @param bool $raw if true, only return the fetched sql_server_version
    1.56 +	* @param bool $use_cache if true, it is safe to retrieve the stored value from the cache
    1.57 +	* @return string sql server version
    1.58 +	*/
    1.59 +	function sql_server_info($raw = false, $use_cache = true)
    1.60 +	{
    1.61 +		global $cache;
    1.62 +
    1.63 +		if (!$use_cache || empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false)
    1.64 +		{
    1.65 +			$result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id);
    1.66 +			$row = @sqlite_fetch_array($result, SQLITE_ASSOC);
    1.67 +
    1.68 +			$this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0;
    1.69 +
    1.70 +			if (!empty($cache) && $use_cache)
    1.71 +			{
    1.72 +				$cache->put('sqlite_version', $this->sql_server_version);
    1.73 +			}
    1.74 +		}
    1.75 +
    1.76 +		return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version;
    1.77 +	}
    1.78 +
    1.79 +	/**
    1.80 +	* SQL Transaction
    1.81 +	* @access private
    1.82 +	*/
    1.83 +	function _sql_transaction($status = 'begin')
    1.84 +	{
    1.85 +		switch ($status)
    1.86 +		{
    1.87 +			case 'begin':
    1.88 +				return @sqlite_query('BEGIN', $this->db_connect_id);
    1.89 +			break;
    1.90 +
    1.91 +			case 'commit':
    1.92 +				return @sqlite_query('COMMIT', $this->db_connect_id);
    1.93 +			break;
    1.94 +
    1.95 +			case 'rollback':
    1.96 +				return @sqlite_query('ROLLBACK', $this->db_connect_id);
    1.97 +			break;
    1.98 +		}
    1.99 +
   1.100 +		return true;
   1.101 +	}
   1.102 +
   1.103 +	/**
   1.104 +	* Base query method
   1.105 +	*
   1.106 +	* @param	string	$query		Contains the SQL query which shall be executed
   1.107 +	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
   1.108 +	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
   1.109 +	*
   1.110 +	* @access	public
   1.111 +	*/
   1.112 +	function sql_query($query = '', $cache_ttl = 0)
   1.113 +	{
   1.114 +		if ($query != '')
   1.115 +		{
   1.116 +			global $cache;
   1.117 +
   1.118 +			// EXPLAIN only in extra debug mode
   1.119 +			if (defined('DEBUG_EXTRA'))
   1.120 +			{
   1.121 +				$this->sql_report('start', $query);
   1.122 +			}
   1.123 +
   1.124 +			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
   1.125 +			$this->sql_add_num_queries($this->query_result);
   1.126 +
   1.127 +			if ($this->query_result === false)
   1.128 +			{
   1.129 +				if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false)
   1.130 +				{
   1.131 +					$this->sql_error($query);
   1.132 +				}
   1.133 +
   1.134 +				if (defined('DEBUG_EXTRA'))
   1.135 +				{
   1.136 +					$this->sql_report('stop', $query);
   1.137 +				}
   1.138 +
   1.139 +				if ($cache_ttl && method_exists($cache, 'sql_save'))
   1.140 +				{
   1.141 +					$this->open_queries[(int) $this->query_result] = $this->query_result;
   1.142 +					$cache->sql_save($query, $this->query_result, $cache_ttl);
   1.143 +				}
   1.144 +				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
   1.145 +				{
   1.146 +					$this->open_queries[(int) $this->query_result] = $this->query_result;
   1.147 +				}
   1.148 +			}
   1.149 +			else if (defined('DEBUG_EXTRA'))
   1.150 +			{
   1.151 +				$this->sql_report('fromcache', $query);
   1.152 +			}
   1.153 +		}
   1.154 +		else
   1.155 +		{
   1.156 +			return false;
   1.157 +		}
   1.158 +
   1.159 +		return $this->query_result;
   1.160 +	}
   1.161 +
   1.162 +	/**
   1.163 +	* Build LIMIT query
   1.164 +	*/
   1.165 +	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
   1.166 +	{
   1.167 +		$this->query_result = false;
   1.168 +
   1.169 +		// if $total is set to 0 we do not want to limit the number of rows
   1.170 +		if ($total == 0)
   1.171 +		{
   1.172 +			$total = -1;
   1.173 +		}
   1.174 +
   1.175 +		$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
   1.176 +
   1.177 +		return $this->sql_query($query, $cache_ttl);
   1.178 +	}
   1.179 +
   1.180 +	/**
   1.181 +	* Return number of affected rows
   1.182 +	*/
   1.183 +	function sql_affectedrows()
   1.184 +	{
   1.185 +		return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
   1.186 +	}
   1.187 +
   1.188 +	/**
   1.189 +	* Fetch current row
   1.190 +	*/
   1.191 +	function sql_fetchrow($query_id = false)
   1.192 +	{
   1.193 +		global $cache;
   1.194 +
   1.195 +		if ($query_id === false)
   1.196 +		{
   1.197 +			$query_id = $this->query_result;
   1.198 +		}
   1.199 +
   1.200 +		if (isset($cache->sql_rowset[$query_id]))
   1.201 +		{
   1.202 +			return $cache->sql_fetchrow($query_id);
   1.203 +		}
   1.204 +
   1.205 +		return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false;
   1.206 +	}
   1.207 +
   1.208 +	/**
   1.209 +	* Seek to given row number
   1.210 +	* rownum is zero-based
   1.211 +	*/
   1.212 +	function sql_rowseek($rownum, &$query_id)
   1.213 +	{
   1.214 +		global $cache;
   1.215 +
   1.216 +		if ($query_id === false)
   1.217 +		{
   1.218 +			$query_id = $this->query_result;
   1.219 +		}
   1.220 +
   1.221 +		if (isset($cache->sql_rowset[$query_id]))
   1.222 +		{
   1.223 +			return $cache->sql_rowseek($rownum, $query_id);
   1.224 +		}
   1.225 +
   1.226 +		return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false;
   1.227 +	}
   1.228 +
   1.229 +	/**
   1.230 +	* Get last inserted id after insert statement
   1.231 +	*/
   1.232 +	function sql_nextid()
   1.233 +	{
   1.234 +		return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
   1.235 +	}
   1.236 +
   1.237 +	/**
   1.238 +	* Free sql result
   1.239 +	*/
   1.240 +	function sql_freeresult($query_id = false)
   1.241 +	{
   1.242 +		global $cache;
   1.243 +
   1.244 +		if ($query_id === false)
   1.245 +		{
   1.246 +			$query_id = $this->query_result;
   1.247 +		}
   1.248 +
   1.249 +		if (isset($cache->sql_rowset[$query_id]))
   1.250 +		{
   1.251 +			return $cache->sql_freeresult($query_id);
   1.252 +		}
   1.253 +
   1.254 +		return true;
   1.255 +	}
   1.256 +
   1.257 +	/**
   1.258 +	* Escape string used in sql query
   1.259 +	*/
   1.260 +	function sql_escape($msg)
   1.261 +	{
   1.262 +		return @sqlite_escape_string($msg);
   1.263 +	}
   1.264 +
   1.265 +	/**
   1.266 +	* Correctly adjust LIKE expression for special characters
   1.267 +	* For SQLite an underscore is a not-known character... this may change with SQLite3
   1.268 +	*/
   1.269 +	function sql_like_expression($expression)
   1.270 +	{
   1.271 +		// Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
   1.272 +		// We only catch * and ? here, not the character map possible on file globbing.
   1.273 +		$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
   1.274 +
   1.275 +		$expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
   1.276 +		$expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
   1.277 +
   1.278 +		return 'GLOB \'' . $this->sql_escape($expression) . '\'';
   1.279 +	}
   1.280 +
   1.281 +	/**
   1.282 +	* return sql error array
   1.283 +	* @access private
   1.284 +	*/
   1.285 +	function _sql_error()
   1.286 +	{
   1.287 +		return array(
   1.288 +			'message'	=> @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
   1.289 +			'code'		=> @sqlite_last_error($this->db_connect_id)
   1.290 +		);
   1.291 +	}
   1.292 +
   1.293 +	/**
   1.294 +	* Build db-specific query data
   1.295 +	* @access private
   1.296 +	*/
   1.297 +	function _sql_custom_build($stage, $data)
   1.298 +	{
   1.299 +		return $data;
   1.300 +	}
   1.301 +
   1.302 +	/**
   1.303 +	* Close sql connection
   1.304 +	* @access private
   1.305 +	*/
   1.306 +	function _sql_close()
   1.307 +	{
   1.308 +		return @sqlite_close($this->db_connect_id);
   1.309 +	}
   1.310 +
   1.311 +	/**
   1.312 +	* Build db-specific report
   1.313 +	* @access private
   1.314 +	*/
   1.315 +	function _sql_report($mode, $query = '')
   1.316 +	{
   1.317 +		switch ($mode)
   1.318 +		{
   1.319 +			case 'start':
   1.320 +			break;
   1.321 +
   1.322 +			case 'fromcache':
   1.323 +				$endtime = explode(' ', microtime());
   1.324 +				$endtime = $endtime[0] + $endtime[1];
   1.325 +
   1.326 +				$result = @sqlite_query($query, $this->db_connect_id);
   1.327 +				while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
   1.328 +				{
   1.329 +					// Take the time spent on parsing rows into account
   1.330 +				}
   1.331 +
   1.332 +				$splittime = explode(' ', microtime());
   1.333 +				$splittime = $splittime[0] + $splittime[1];
   1.334 +
   1.335 +				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
   1.336 +
   1.337 +			break;
   1.338 +		}
   1.339 +	}
   1.340 +}
   1.341 +
   1.342 +?>
   1.343 \ No newline at end of file