Blame Extras/phpBB/3.0.4/includes/db/sqlite.php

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