Blame Identity/Webenv/phpBB/3.0.4/includes/db/sqlite.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package dbal
ef5584
* @version $Id: sqlite.php 8814 2008-09-04 12:01:47Z acydburn $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
ef5584
ef5584
/**
ef5584
* Sqlite Database Abstraction Layer
ef5584
* Minimum Requirement: 2.8.2+
ef5584
* @package dbal
ef5584
*/
ef5584
class dbal_sqlite extends dbal
ef5584
{
ef5584
	/**
ef5584
	* Connect to server
ef5584
	*/
ef5584
	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
ef5584
	{
ef5584
		$this->persistency = $persistency;
ef5584
		$this->user = $sqluser;
ef5584
		$this->server = $sqlserver . (($port) ? ':' . $port : '');
ef5584
		$this->dbname = $database;
ef5584
ef5584
		$error = '';
ef5584
		$this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0666, $error) : @sqlite_open($this->server, 0666, $error);
ef5584
ef5584
		if ($this->db_connect_id)
ef5584
		{
ef5584
			@sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id);
ef5584
//			@sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id);
ef5584
		}
ef5584
ef5584
		return ($this->db_connect_id) ? true : array('message' => $error);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Version information about used database
ef5584
	* @param bool $raw if true, only return the fetched sql_server_version
ef5584
	* @return string sql server version
ef5584
	*/
ef5584
	function sql_server_info($raw = false)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if (empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false)
ef5584
		{
ef5584
			$result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id);
ef5584
			$row = @sqlite_fetch_array($result, SQLITE_ASSOC);
ef5584
ef5584
			$this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0;
ef5584
			$cache->put('sqlite_version', $this->sql_server_version);
ef5584
		}
ef5584
ef5584
		return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* SQL Transaction
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_transaction($status = 'begin')
ef5584
	{
ef5584
		switch ($status)
ef5584
		{
ef5584
			case 'begin':
ef5584
				return @sqlite_query('BEGIN', $this->db_connect_id);
ef5584
			break;
ef5584
ef5584
			case 'commit':
ef5584
				return @sqlite_query('COMMIT', $this->db_connect_id);
ef5584
			break;
ef5584
ef5584
			case 'rollback':
ef5584
				return @sqlite_query('ROLLBACK', $this->db_connect_id);
ef5584
			break;
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Base query method
ef5584
	*
ef5584
	* @param	string	$query		Contains the SQL query which shall be executed
ef5584
	* @param	int		$cache_ttl	Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
ef5584
	* @return	mixed				When casted to bool the returned value returns true on success and false on failure
ef5584
	*
ef5584
	* @access	public
ef5584
	*/
ef5584
	function sql_query($query = '', $cache_ttl = 0)
ef5584
	{
ef5584
		if ($query != '')
ef5584
		{
ef5584
			global $cache;
ef5584
ef5584
			// EXPLAIN only in extra debug mode
ef5584
			if (defined('DEBUG_EXTRA'))
ef5584
			{
ef5584
				$this->sql_report('start', $query);
ef5584
			}
ef5584
ef5584
			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
ef5584
			$this->sql_add_num_queries($this->query_result);
ef5584
ef5584
			if ($this->query_result === false)
ef5584
			{
ef5584
				if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false)
ef5584
				{
ef5584
					$this->sql_error($query);
ef5584
				}
ef5584
ef5584
				if (defined('DEBUG_EXTRA'))
ef5584
				{
ef5584
					$this->sql_report('stop', $query);
ef5584
				}
ef5584
ef5584
				if ($cache_ttl && method_exists($cache, 'sql_save'))
ef5584
				{
ef5584
					$this->open_queries[(int) $this->query_result] = $this->query_result;
ef5584
					$cache->sql_save($query, $this->query_result, $cache_ttl);
ef5584
				}
ef5584
				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
ef5584
				{
ef5584
					$this->open_queries[(int) $this->query_result] = $this->query_result;
ef5584
				}
ef5584
			}
ef5584
			else if (defined('DEBUG_EXTRA'))
ef5584
			{
ef5584
				$this->sql_report('fromcache', $query);
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		return $this->query_result;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build LIMIT query
ef5584
	*/
ef5584
	function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
ef5584
	{
ef5584
		$this->query_result = false;
ef5584
ef5584
		// if $total is set to 0 we do not want to limit the number of rows
ef5584
		if ($total == 0)
ef5584
		{
ef5584
			$total = -1;
ef5584
		}
ef5584
ef5584
		$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
ef5584
ef5584
		return $this->sql_query($query, $cache_ttl);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Return number of affected rows
ef5584
	*/
ef5584
	function sql_affectedrows()
ef5584
	{
ef5584
		return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Fetch current row
ef5584
	*/
ef5584
	function sql_fetchrow($query_id = false)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if (isset($cache->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return $cache->sql_fetchrow($query_id);
ef5584
		}
ef5584
ef5584
		return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Seek to given row number
ef5584
	* rownum is zero-based
ef5584
	*/
ef5584
	function sql_rowseek($rownum, &$query_id)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if (isset($cache->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return $cache->sql_rowseek($rownum, $query_id);
ef5584
		}
ef5584
ef5584
		return ($query_id !== false) ? @sqlite_seek($query_id, $rownum) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get last inserted id after insert statement
ef5584
	*/
ef5584
	function sql_nextid()
ef5584
	{
ef5584
		return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Free sql result
ef5584
	*/
ef5584
	function sql_freeresult($query_id = false)
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		if ($query_id === false)
ef5584
		{
ef5584
			$query_id = $this->query_result;
ef5584
		}
ef5584
ef5584
		if (isset($cache->sql_rowset[$query_id]))
ef5584
		{
ef5584
			return $cache->sql_freeresult($query_id);
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Escape string used in sql query
ef5584
	*/
ef5584
	function sql_escape($msg)
ef5584
	{
ef5584
		return @sqlite_escape_string($msg);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Correctly adjust LIKE expression for special characters
ef5584
	* For SQLite an underscore is a not-known character... this may change with SQLite3
ef5584
	*/
ef5584
	function sql_like_expression($expression)
ef5584
	{
ef5584
		// Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it!
ef5584
		// We only catch * and ? here, not the character map possible on file globbing.
ef5584
		$expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression);
ef5584
ef5584
		$expression = str_replace(array('?', '*'), array("\?", "\*"), $expression);
ef5584
		$expression = str_replace(array(chr(0) . "\?", chr(0) . "\*"), array('?', '*'), $expression);
ef5584
ef5584
		return 'GLOB \'' . $this->sql_escape($expression) . '\'';
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* return sql error array
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_error()
ef5584
	{
ef5584
		return array(
ef5584
			'message'	=> @sqlite_error_string(@sqlite_last_error($this->db_connect_id)),
ef5584
			'code'		=> @sqlite_last_error($this->db_connect_id)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build db-specific query data
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_custom_build($stage, $data)
ef5584
	{
ef5584
		return $data;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Close sql connection
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_close()
ef5584
	{
ef5584
		return @sqlite_close($this->db_connect_id);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build db-specific report
ef5584
	* @access private
ef5584
	*/
ef5584
	function _sql_report($mode, $query = '')
ef5584
	{
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'start':
ef5584
			break;
ef5584
ef5584
			case 'fromcache':
ef5584
				$endtime = explode(' ', microtime());
ef5584
				$endtime = $endtime[0] + $endtime[1];
ef5584
ef5584
				$result = @sqlite_query($query, $this->db_connect_id);
ef5584
				while ($void = @sqlite_fetch_array($result, SQLITE_ASSOC))
ef5584
				{
ef5584
					// Take the time spent on parsing rows into account
ef5584
				}
ef5584
ef5584
				$splittime = explode(' ', microtime());
ef5584
				$splittime = $splittime[0] + $splittime[1];
ef5584
ef5584
				$this->sql_report('record_fromcache', $query, $endtime, $splittime);
ef5584
ef5584
			break;
ef5584
		}
ef5584
	}
ef5584
}
ef5584
ef5584
?>