From a48e9c488b7ccf22a911a0fb33b69ef782f63a81 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 25 Feb 2013 21:35:11 +0100 Subject: [PATCH] Move prepared query cache handling to Connection wrapper --- lib/db.php | 27 +++++++++------------------ lib/db/connection.php | 28 +++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/db.php b/lib/db.php index c1c419df545..c5690bdd8a9 100644 --- a/lib/db.php +++ b/lib/db.php @@ -43,9 +43,6 @@ class DatabaseException extends Exception { class OC_DB { const BACKEND_DOCTRINE=2; - static private $preparedQueries = array(); - static private $cachingEnabled = true; - /** * @var \Doctrine\DBAL\Connection */ @@ -102,7 +99,6 @@ class OC_DB { return true; } } - self::$preparedQueries = array(); // The global data we need $name = OC_Config::getValue( "dbname", "owncloud" ); $host = OC_Config::getValue( "dbhost", "" ); @@ -186,6 +182,11 @@ class OC_DB { $connectionParams['table_prefix'] = OC_Config::getValue( "dbtableprefix", "oc_" ); try { self::$DOCTRINE = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); + if ($type === 'sqlite' || $type === 'sqlite3') { + // Sqlite doesn't handle query caching and schema changes + // TODO: find a better way to handle this + self::$connection->disableQueryStatementCaching(); + } } catch(\Doctrine\DBAL\DBALException $e) { OC_Log::write('core', $e->getMessage(), OC_Log::FATAL); OC_User::setUserId(null); @@ -219,12 +220,8 @@ class OC_DB { } $platform = self::$connection->getDatabasePlatform(); $query = $platform->modifyLimitQuery($query, $limit, $offset); - } else { - if (isset(self::$preparedQueries[$query]) and self::$cachingEnabled) { - return self::$preparedQueries[$query]; } } - $rawQuery = $query; // Optimize the query $query = self::processQuery( $query ); @@ -248,12 +245,6 @@ class OC_DB { // differentiate between query and manipulation $result=new OC_DB_StatementWrapper($result, $isManipulation); } - if ((is_null($limit) || $limit == -1) and self::$cachingEnabled ) { - $type = OC_Config::getValue( "dbtype", "sqlite" ); - if( $type != 'sqlite' && $type != 'sqlite3' ) { - self::$preparedQueries[$rawQuery] = $result; - } - } return $result; } @@ -358,7 +349,6 @@ class OC_DB { // Cut connection if required if(self::$connection) { self::$connection->close(); - self::$preparedQueries = array(); } return true; @@ -672,9 +662,10 @@ class OC_DB { * @param bool $enabled */ static public function enableCaching($enabled) { - if (!$enabled) { - self::$preparedQueries = array(); + if ($enabled) { + self::$connection->enableQueryStatementCaching(); + } else { + self::$connection->disableQueryStatementCaching(); } - self::$cachingEnabled = $enabled; } } diff --git a/lib/db/connection.php b/lib/db/connection.php index 7d56452678c..763251c1903 100644 --- a/lib/db/connection.php +++ b/lib/db/connection.php @@ -17,6 +17,9 @@ class Connection extends \Doctrine\DBAL\Connection { protected $adapter; + protected $preparedQueries = array(); + protected $cachingQueryStatementEnabled = true; + /** * Initializes a new instance of the Connection class. * @@ -47,9 +50,19 @@ class Connection extends \Doctrine\DBAL\Connection { */ public function prepare( $statement, $limit=null, $offset=null ) { $statement = $this->replaceTablePrefix($statement); - // TODO: limit & offset - // TODO: prepared statement cache - return parent::prepare($statement); + if (!is_null($limit) && $limit != -1) { + // TODO: limit & offset + } else { + if (isset($this->preparedQueries[$statement]) && $this->cachingQueryStatementEnabled) { + return $this->preparedQueries[$statement]; + } + } + $rawQuery = $statement; + $result = parent::prepare($statement); + if ((is_null($limit) || $limit == -1) && $this->cachingQueryStatementEnabled) { + $this->preparedQueries[$rawQuery] = $result; + } + return $result; } /** @@ -120,4 +133,13 @@ class Connection extends \Doctrine\DBAL\Connection { public function replaceTablePrefix($statement) { return str_replace( '*PREFIX*', $this->table_prefix, $statement ); } + + public function enableQueryStatementCaching() { + $this->cachingQueryStatementEnabled = true; + } + + public function disableQueryStatementCaching() { + $this->cachingQueryStatementEnabled = false; + $this->preparedQueries = array(); + } } -- 2.39.5