From dcd073bb44136ac0d22ade2a5ecf759e7e2ae3bc Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Tue, 20 Jan 2009 16:54:40 +0000 Subject: [PATCH] sqlite works now... git-svn-id: file:///svn/phpbb/trunk@9282 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/db/dbal.php | 18 +- phpBB/includes/db/mysqli.php | 1 - phpBB/includes/db/sqlite.php | 335 ++++++++++++++++------------------- 3 files changed, 160 insertions(+), 194 deletions(-) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index 99474635b6..e3588a9cd5 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -457,7 +457,7 @@ abstract class phpbb_dbal /** * Run DB-specific code to build SQL Report to explain queries, show statistics and runtime information. Called by {@link phpbb_dbal::sql_report() sql_report()}. * - * This function only executes if the GET parameter 'explain' is true and DEBUG_EXTRA enabled. + * This function only executes if the GET parameter 'explain' is true and phpbb::$base_config['debug_extra'] enabled. * * @param string $mode The mode to handle. 'display' is used for displaying the report, all other modes are internal. * @param string $query Query to document/explain. Only used internally to build the plan. @@ -483,7 +483,7 @@ abstract class phpbb_dbal } // EXPLAIN only in extra debug mode - if (defined('DEBUG_EXTRA')) + if (phpbb::$base_config['debug_extra']) { $this->sql_report('start', $query); } @@ -499,7 +499,7 @@ abstract class phpbb_dbal if ($this->query_result !== false) { - if (defined('DEBUG_EXTRA')) + if (phpbb::$base_config['debug_extra']) { $this->sql_report('fromcache', $query); } @@ -512,7 +512,7 @@ abstract class phpbb_dbal $this->sql_error($query); } - if (defined('DEBUG_EXTRA')) + if (phpbb::$base_config['debug_extra']) { $this->sql_report('stop', $query); } @@ -1084,8 +1084,8 @@ abstract class phpbb_dbal // Show complete SQL error and path to administrators only // Additionally show complete error on installation or if extended debug mode is enabled - // The DEBUG_EXTRA constant is for development only! - if ((phpbb::registered('acl') && phpbb::$acl->acl_get('a_')) || defined('IN_INSTALL') || defined('DEBUG_EXTRA')) + // The phpbb::$base_config['debug_extra'] variable is for development only! + if ((phpbb::registered('acl') && phpbb::$acl->acl_get('a_')) || defined('IN_INSTALL') || phpbb::$base_config['debug_extra']) { $message .= ($sql) ? '

SQL

' . htmlspecialchars($sql) : ''; $message .= '
'; @@ -1139,7 +1139,7 @@ abstract class phpbb_dbal /** * Build SQL Report to explain queries, show statistics and runtime information. * - * This function only executes if the GET parameter 'explain' is true and DEBUG_EXTRA enabled. + * This function only executes if the GET parameter 'explain' is true and phpbb::$base_config['debug_extra'] enabled. * * @param string $mode The mode to handle. 'display' is used for displaying the report, all other modes are internal. * @param string $query Query to document/explain. Only used internally to build the plan. @@ -1354,7 +1354,7 @@ abstract class phpbb_dbal $var_name = preg_replace('/[\n\r\s\t]+/', ' ', $query); $var_name = md5($this->sql_layer . '_' . $var_name); - $data = phpbb::$acm->get($var_name, 'sql'); + $data = phpbb::$acm->get_sql($var_name); if ($data !== false) { @@ -1398,7 +1398,7 @@ abstract class phpbb_dbal } $this->sql_freeresult($this->query_result); - phpbb::$acm->put($var_name, $data, $cache_ttl, 'sql'); + phpbb::$acm->put_sql($var_name, $data, $cache_ttl); $this->query_result = ++$this->cache_index; $this->cache_rowset[$this->query_result] = $data['rowset']; diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php index 783ce43fef..acd789b086 100644 --- a/phpBB/includes/db/mysqli.php +++ b/phpBB/includes/db/mysqli.php @@ -328,7 +328,6 @@ class phpbb_dbal_mysqli extends phpbb_dbal ); } - /** * Run DB-specific code to build SQL Report to explain queries, show statistics and runtime information. See {@link phpbb_dbal::_sql_report() _sql_report()} for details. */ diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php index dc2701c1ce..aa971e8484 100644 --- a/phpBB/includes/db/sqlite.php +++ b/phpBB/includes/db/sqlite.php @@ -16,31 +16,79 @@ if (!defined('IN_PHPBB')) exit; } -include_once(PHPBB_ROOT_PATH . 'includes/db/dbal.' . PHP_EXT); - /** * Sqlite Database Abstraction Layer * Minimum Requirement: 2.8.2+ * @package dbal */ -class dbal_sqlite extends dbal +class phpbb_dbal_sqlite extends phpbb_dbal { - // like MS ACCESS, SQLite does not support COUNT(DISTINCT ...) - var $count_distinct = false; - - // can't truncate a table - var $truncate = false; - - var $dbms_type = 'sqlite'; + /** + * @var string Database type. No distinction between versions or used extensions. + */ + public $dbms_type = 'sqlite'; /** - * Connect to server + * Database features + * + * + * + * @var array */ - function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) + public $features = array( + 'multi_insert' => true, + // like MS ACCESS, SQLite does not support COUNT(DISTINCT ...) + 'count_distinct' => false, + 'multi_table_deletion' => true, + // can't truncate a table + 'truncate' => false, + ); + + /** + * @var array Database type map, column layout information + */ + public $dbms_type_map = array( + 'INT:' => 'int(%d)', + 'BINT' => 'bigint(20)', + 'UINT' => 'INTEGER UNSIGNED', //'mediumint(8) UNSIGNED', + 'UINT:' => 'INTEGER UNSIGNED', // 'int(%d) UNSIGNED', + 'TINT:' => 'tinyint(%d)', + 'USINT' => 'INTEGER UNSIGNED', //'mediumint(4) UNSIGNED', + 'BOOL' => 'INTEGER UNSIGNED', //'tinyint(1) UNSIGNED', + 'VCHAR' => 'varchar(255)', + 'VCHAR:' => 'varchar(%d)', + 'CHAR:' => 'char(%d)', + 'XSTEXT' => 'text(65535)', + 'STEXT' => 'text(65535)', + 'TEXT' => 'text(65535)', + 'MTEXT' => 'mediumtext(16777215)', + 'XSTEXT_UNI'=> 'text(65535)', + 'STEXT_UNI' => 'text(65535)', + 'TEXT_UNI' => 'text(65535)', + 'MTEXT_UNI' => 'mediumtext(16777215)', + 'TIMESTAMP' => 'INTEGER UNSIGNED', //'int(11) UNSIGNED', + 'DECIMAL' => 'decimal(5,2)', + 'DECIMAL:' => 'decimal(%d,2)', + 'PDECIMAL' => 'decimal(6,3)', + 'PDECIMAL:' => 'decimal(%d,3)', + 'VCHAR_UNI' => 'varchar(255)', + 'VCHAR_UNI:'=> 'varchar(%d)', + 'VARBINARY' => 'blob', + ); + + /** + * Connect to server. See {@link phpbb_dbal::sql_connect() sql_connect()} for details. + */ + public function sql_connect($server, $user, $password, $database, $port = false, $persistency = false , $new_link = false) { $this->persistency = $persistency; - $this->user = $sqluser; - $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->user = $user; + $this->server = $server . (($port) ? ':' . $port : ''); $this->dbname = $database; $error = ''; @@ -49,38 +97,69 @@ class dbal_sqlite extends dbal if ($this->db_connect_id) { @sqlite_query('PRAGMA short_column_names = 1', $this->db_connect_id); -// @sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id); + @sqlite_query('PRAGMA encoding = "UTF-8"', $this->db_connect_id); } return ($this->db_connect_id) ? true : array('message' => $error); } /** - * Version information about used database - * @param bool $raw if true, only return the fetched sql_server_version - * @return string sql server version + * Version information about used database. See {@link phpbb_dbal::sql_server_info() sql_server_info()} for details. */ - function sql_server_info($raw = false) + public function sql_server_info($raw = false) { - global $cache; - - if (empty($cache) || ($this->sql_server_version = $cache->get('sqlite_version')) === false) + if (!phpbb::registered('acm') || ($this->sql_server_version = phpbb::$acm->get('#sqlite_version')) === false) { $result = @sqlite_query('SELECT sqlite_version() AS version', $this->db_connect_id); $row = @sqlite_fetch_array($result, SQLITE_ASSOC); $this->sql_server_version = (!empty($row['version'])) ? $row['version'] : 0; - $cache->put('sqlite_version', $this->sql_server_version); + + if (phpbb::registered('acm')) + { + phpbb::$acm->put('#sqlite_version', $this->sql_server_version); + } } return ($raw) ? $this->sql_server_version : 'SQLite ' . $this->sql_server_version; } /** - * SQL Transaction - * @access private + * DB-specific base query method. See {@link phpbb_dbal::_sql_query() _sql_query()} for details. */ - function _sql_transaction($status = 'begin') + protected function _sql_query($query) + { + return @sqlite_query($query, $this->db_connect_id); + } + + /** + * Build LIMIT query and run it. See {@link phpbb_dbal::_sql_query_limit() _sql_query_limit()} for details. + */ + protected function _sql_query_limit($query, $total, $offset, $cache_ttl) + { + // if $total is set to 0 we do not want to limit the number of rows + if ($total == 0) + { + $total = -1; + } + + $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); + + return $this->sql_query($query, $cache_ttl); + } + + /** + * Close sql connection. See {@link phpbb_dbal::_sql_close() _sql_close()} for details. + */ + protected function _sql_close() + { + return @sqlite_close($this->db_connect_id); + } + + /** + * SQL Transaction. See {@link phpbb_dbal::_sql_transaction() _sql_transaction()} for details. + */ + protected function _sql_transaction($status) { switch ($status) { @@ -101,154 +180,44 @@ class dbal_sqlite extends dbal } /** - * Base query method - * - * @param string $query Contains the SQL query which shall be executed - * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache - * @return mixed When casted to bool the returned value returns true on success and false on failure - * - * @access public + * Return number of affected rows. See {@link phpbb_dbal::sql_affectedrows() sql_affectedrows()} for details. */ - function sql_query($query = '', $cache_ttl = 0) - { - if ($query != '') - { - global $cache; - - // EXPLAIN only in extra debug mode - if (defined('DEBUG_EXTRA')) - { - $this->sql_report('start', $query); - } - - $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; - $this->sql_add_num_queries($this->query_result); - - if ($this->query_result === false) - { - if (($this->query_result = @sqlite_query($query, $this->db_connect_id)) === false) - { - $this->sql_error($query); - } - - if (defined('DEBUG_EXTRA')) - { - $this->sql_report('stop', $query); - } - - if ($cache_ttl && method_exists($cache, 'sql_save')) - { - $this->open_queries[(int) $this->query_result] = $this->query_result; - $cache->sql_save($query, $this->query_result, $cache_ttl); - } - else if (strpos($query, 'SELECT') === 0 && $this->query_result) - { - $this->open_queries[(int) $this->query_result] = $this->query_result; - } - } - else if (defined('DEBUG_EXTRA')) - { - $this->sql_report('fromcache', $query); - } - } - else - { - return false; - } - - return $this->query_result; - } - - /** - * Build LIMIT query - */ - function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) - { - $this->query_result = false; - - // if $total is set to 0 we do not want to limit the number of rows - if ($total == 0) - { - $total = -1; - } - - $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); - - return $this->sql_query($query, $cache_ttl); - } - - /** - * Return number of affected rows - */ - function sql_affectedrows() + public function sql_affectedrows() { return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false; } /** - * Fetch current row + * Get last inserted id after insert statement. See {@link phpbb_dbal::sql_nextid() sql_nextid()} for details. */ - function sql_fetchrow($query_id = false) - { - global $cache; - - if ($query_id === false) - { - $query_id = $this->query_result; - } - - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_fetchrow($query_id); - } - - return ($query_id !== false) ? @sqlite_fetch_array($query_id, SQLITE_ASSOC) : false; - } - - /** - * Get last inserted id after insert statement - */ - function sql_nextid() + public function sql_nextid() { return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false; } /** - * Free sql result + * Fetch current row. See {@link phpbb_dbal::_sql_fetchrow() _sql_fetchrow()} for details. */ - function sql_freeresult($query_id = false) + protected function _sql_fetchrow($query_id) { - global $cache; - - if ($query_id === false) - { - $query_id = $this->query_result; - } - - if (isset($cache->sql_rowset[$query_id])) - { - return $cache->sql_freeresult($query_id); - } + return @sqlite_fetch_array($query_id, SQLITE_ASSOC); + } + /** + * Free query result. See {@link phpbb_dbal::_sql_freeresult() _sql_freeresult()} for details. + */ + protected function _sql_freeresult($query_id) + { return true; } /** - * Escape string used in sql query + * Correctly adjust LIKE expression for special characters. See {@link phpbb_dbal::_sql_like_expression() _sql_like_expression()} for details. */ - function sql_escape($msg) - { - return @sqlite_escape_string($msg); - } - - /** - * Correctly adjust LIKE expression for special characters - * For SQLite an underscore is a not-known character... this may change with SQLite3 - */ - function sql_like_expression($expression) + protected function _sql_like_expression($expression) { // Unlike LIKE, GLOB is case sensitive (unfortunatly). SQLite users need to live with it! - // We only catch * and ? here, not the character map possible on file globbing. + // We only catch * and ? here, not the character map possible for file globbing. $expression = str_replace(array(chr(0) . '_', chr(0) . '%'), array(chr(0) . '?', chr(0) . '*'), $expression); $expression = str_replace(array('?', '*'), array("\?", "\*"), $expression); @@ -258,9 +227,17 @@ class dbal_sqlite extends dbal } /** - * Expose a DBMS specific function + * Escape string used in sql query. See {@link phpbb_dbal::sql_escape() sql_escape()} for details. */ - function sql_function($type, $col) + public function sql_escape($msg) + { + return @sqlite_escape_string($msg); + } + + /** + * Expose a DBMS specific function. See {@link phpbb_dbal::sql_function() sql_function()} for details. + */ + public function sql_function($type, $col) { switch ($type) { @@ -271,26 +248,35 @@ class dbal_sqlite extends dbal } } - function sql_handle_data($type, $table, $data, $where = '') + /** + * Handle data by using prepared statements. See {@link phpbb_dbal::sql_handle_data() sql_handle_data()} for details. + * @todo implement correctly by using types. ;) + */ + public function sql_handle_data($type, $table, $data, $where = '') { if ($type === 'UPDATE') { - $this->sql_query('INSERT INTO ' . $table . ' ' . - $this->sql_build_array('INSERT', $data)); + $where = ($where) ? ' WHERE ' . $where : ''; + $this->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', $data) . $where); } else { - $this->sql_query('UPDATE ' . $table . ' - SET ' . $db->sql_build_array('UPDATE', $data) . - $where); + $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $data)); } } /** - * return sql error array - * @access private + * Build DB-specific query bits. See {@link phpbb_dbal::_sql_custom_build() _sql_custom_build()} for details. */ - function _sql_error() + protected function _sql_custom_build($stage, $data) + { + return $data; + } + + /** + * return sql error array. See {@link phpbb_dbal::_sql_error() _sql_error()} for details. + */ + protected function _sql_error() { return array( 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)), @@ -299,28 +285,9 @@ class dbal_sqlite extends dbal } /** - * Build db-specific query data - * @access private + * Run DB-specific code to build SQL Report to explain queries, show statistics and runtime information. See {@link phpbb_dbal::_sql_report() _sql_report()} for details. */ - function _sql_custom_build($stage, $data) - { - return $data; - } - - /** - * Close sql connection - * @access private - */ - function _sql_close() - { - return @sqlite_close($this->db_connect_id); - } - - /** - * Build db-specific report - * @access private - */ - function _sql_report($mode, $query = '') + protected function _sql_report($mode, $query = '') { switch ($mode) {