Blame Identity/Models/Html/phpBB/3.0.4/includes/db/sqlite.php

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