]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move prepared query cache handling to Connection wrapper
authorBart Visscher <bartv@thisnet.nl>
Mon, 25 Feb 2013 20:35:11 +0000 (21:35 +0100)
committerBart Visscher <bartv@thisnet.nl>
Sun, 21 Jul 2013 21:17:36 +0000 (23:17 +0200)
lib/db.php
lib/db/connection.php

index c1c419df5459749490e6435427498b89b3312058..c5690bdd8a961b3790cecf46edd029015b9463ca 100644 (file)
@@ -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;
        }
 }
index 7d56452678c1d71001fb5e3c5b530d75cf2a7500..763251c1903a84768e744c4ee89f76579ca26f7e 100644 (file)
@@ -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();
+       }
 }