diff --git a/lib/db.php b/lib/db.php index 95145fb7c5..a323370610 100644 --- a/lib/db.php +++ b/lib/db.php @@ -57,7 +57,6 @@ class OC_DB { static private $DOCTRINE=null; static private $inTransaction=false; - static private $prefix=null; static private $type=null; /** @@ -184,6 +183,7 @@ class OC_DB { return false; } $connectionParams['wrapperClass'] = 'OC\DB\Connection'; + $connectionParams['table_prefix'] = OC_Config::getValue( "dbtableprefix", "oc_" ); try { self::$DOCTRINE = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); } catch(\Doctrine\DBAL\DBALException $e) { @@ -353,8 +353,7 @@ class OC_DB { return $row['id']; } else if( $type === 'mssql') { if($table !== null) { - $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); - $table = str_replace( '*PREFIX*', $prefix, $table ); + $table = self::$connection->replaceTablePrefix( $table ); } return self::$connection->lastInsertId($table); } @@ -367,9 +366,8 @@ class OC_DB { return self::$connection->lastInsertId($table); } else { if($table !== null) { - $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); $suffix = OC_Config::getValue( "dbsequencesuffix", "_id_seq" ); - $table = str_replace( '*PREFIX*', $prefix, $table ).$suffix; + $table = self::$connection->replaceTablePrefix( $table ).$suffix; } $result = self::$connection->lastInsertId($table); } @@ -443,8 +441,7 @@ class OC_DB { */ public static function insertIfNotExist($table, $input) { self::connect(); - $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); - $table = str_replace( '*PREFIX*', $prefix, $table ); + $table = self::$connection->replaceTablePrefix( $table ); if(is_null(self::$type)) { self::$type=OC_Config::getValue( "dbtype", "sqlite" ); @@ -508,15 +505,11 @@ class OC_DB { */ private static function processQuery( $query ) { self::connect(); - // We need Database type and table prefix + // We need Database type if(is_null(self::$type)) { self::$type=OC_Config::getValue( "dbtype", "sqlite" ); } $type = self::$type; - if(is_null(self::$prefix)) { - self::$prefix=OC_Config::getValue( "dbtableprefix", "oc_" ); - } - $prefix = self::$prefix; // differences in escaping of table names ('`' for mysql) and getting the current timestamp if( $type == 'sqlite' || $type == 'sqlite3' ) { @@ -541,9 +534,6 @@ class OC_DB { $query = self::fixLimitClauseForMSSQL($query); } - // replace table name prefix - $query = str_replace( '*PREFIX*', $prefix, $query ); - return $query; } diff --git a/lib/db/connection.php b/lib/db/connection.php index 5466609e67..40809330da 100644 --- a/lib/db/connection.php +++ b/lib/db/connection.php @@ -14,7 +14,6 @@ use Doctrine\Common\EventManager; class Connection extends \Doctrine\DBAL\Connection { protected $table_prefix; - protected $sequence_suffix; protected $adapter; @@ -32,8 +31,12 @@ class Connection extends \Doctrine\DBAL\Connection { if (!isset($params['adapter'])) { throw new Exception('adapter not set'); } + if (!isset($params['table_prefix'])) { + throw new Exception('table_prefix not set'); + } parent::__construct($params, $driver, $config, $eventManager); $this->adapter = new $params['adapter']($this); + $this->table_prefix = $params['table_prefix']; } /** @@ -43,7 +46,7 @@ class Connection extends \Doctrine\DBAL\Connection { * @return \Doctrine\DBAL\Driver\Statement The prepared statement. */ public function prepare( $statement, $limit=null, $offset=null ) { - // TODO: prefix + $statement = $this->replaceTablePrefix($statement); // TODO: limit & offset // TODO: prepared statement cache return parent::prepare($statement); @@ -85,4 +88,9 @@ class Connection extends \Doctrine\DBAL\Connection { // TODO: prefix return parent::executeUpdate($query, $params, $types); } + + // internal use + public function replaceTablePrefix($statement) { + return str_replace( '*PREFIX*', $this->table_prefix, $statement ); + } }