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

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