Move prepared query cache handling to Connection wrapper

remotes/origin/stable6
Bart Visscher 2013-02-25 21:35:11 +07:00
parent e3c5fea989
commit a48e9c488b
2 changed files with 34 additions and 21 deletions

@ -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;
}
}

@ -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();
}
}