]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move db prefix handling to Connection wrapper
authorBart Visscher <bartv@thisnet.nl>
Mon, 25 Feb 2013 07:19:49 +0000 (08:19 +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 95145fb7c50ab5feee00a04776e45f42553a50f7..a3233706109d3d987272c96df5622e4ef870e4de 100644 (file)
@@ -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;
        }
 
index 5466609e6713dd698a6e28d1e73ed05d164f7225..40809330da64cfb413c413efa7f1fb0c2701405a 100644 (file)
@@ -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 );
+       }
 }