]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move query statement fixup handling to Connection wrapper
authorBart Visscher <bartv@thisnet.nl>
Mon, 25 Feb 2013 21:49:55 +0000 (22:49 +0100)
committerBart Visscher <bartv@thisnet.nl>
Sun, 21 Jul 2013 21:17:37 +0000 (23:17 +0200)
lib/db.php
lib/db/adapter.php
lib/db/adapteroci8.php
lib/db/adapterpgsql.php
lib/db/adaptersqlite.php
lib/db/adaptersqlsrv.php
lib/db/connection.php

index 4451f8139056f0ab9c6a3f75b1df699b50aefe00..0ad8a9b35d8c0a282fa4984b534a58cfedcedd69 100644 (file)
@@ -213,11 +213,6 @@ class OC_DB {
         * SQL query via Doctrine prepare(), needs to be execute()'d!
         */
        static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) {
-               // Optimize the query
-               $query = self::processQuery( $query );
-               if(OC_Config::getValue( "log_query", false)) {
-                       OC_Log::write('core', 'DB prepare : '.$query, OC_Log::DEBUG);
-               }
                self::connect();
                
                if ($isManipulation === null) {
@@ -448,49 +443,6 @@ class OC_DB {
                return $result;
        }
 
-       /**
-        * @brief does minor changes to query
-        * @param string $query Query string
-        * @return string corrected query string
-        *
-        * This function replaces *PREFIX* with the value of $CONFIG_DBTABLEPREFIX
-        * and replaces the ` with ' or " according to the database driver.
-        */
-       private static function processQuery( $query ) {
-               self::connect();
-               // We need Database type
-               if(is_null(self::$type)) {
-                       self::$type=OC_Config::getValue( "dbtype", "sqlite" );
-               }
-               $type = self::$type;
-
-               // differences in escaping of table names ('`' for mysql) and getting the current timestamp
-               if( $type == 'sqlite' || $type == 'sqlite3' ) {
-                       $query = str_replace( '`', '"', $query );
-                       $query = str_ireplace( 'NOW()', 'datetime(\'now\')', $query );
-                       $query = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $query );
-               } elseif( $type == 'pgsql' ) {
-                       $query = str_replace( '`', '"', $query );
-                       $query = str_ireplace( 'UNIX_TIMESTAMP()', 'cast(extract(epoch from current_timestamp) as integer)',
-                               $query );
-               } elseif( $type == 'oci'  ) {
-                       $query = str_replace( '`', '"', $query );
-                       $query = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $query );
-                       $query = str_ireplace( 'UNIX_TIMESTAMP()', "(cast(sys_extract_utc(systimestamp) as date) - date'1970-01-01') * 86400", $query );
-               }elseif( $type == 'mssql' ) {
-                       $query = preg_replace( "/\`(.*?)`/", "[$1]", $query );
-                       $query = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $query );
-                       $query = str_replace( 'LENGTH(', 'LEN(', $query );
-                       $query = str_replace( 'SUBSTR(', 'SUBSTRING(', $query );
-                       $query = str_ireplace( 'UNIX_TIMESTAMP()', 'DATEDIFF(second,{d \'1970-01-01\'},GETDATE())', $query );
-               }
-
-               return $query;
-       }
-
-               return $query;
-       }
-
        /**
         * @brief drop a table
         * @param string $tableName the table to drop
index b0c9aab9c7a0712619f2ea9aac8919fcb959e7e9..3b83853289a4c4d0ba2af640d9423de1b666a283 100644 (file)
@@ -18,4 +18,8 @@ class Adapter {
        public function lastInsertId($table) {
                return $this->conn->realLastInsertId($table);
        }
+
+       public function fixupStatement($statement) {
+               return $statement;
+       }
 }
index 50c4d078243d2d953944ee4ff7b42a69f92d45d2..6123007cb2fc75e3ba70283cb1521652d4462cb5 100644 (file)
@@ -18,4 +18,11 @@ class AdapterOCI8 extends Adapter {
                }
                return $this->conn->lastInsertId($table);
        }
+
+       public function fixupStatement($statement) {
+               $statement = str_replace( '`', '"', $statement );
+               $statement = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $statement );
+               $statement = str_ireplace( 'UNIX_TIMESTAMP()', "(cast(sys_extract_utc(systimestamp) as date) - date'1970-01-01') * 86400", $statement );
+               return $statement;
+       }
 }
index 0084aad470e36902e51575ef62c729be5203023d..acfc42016290043de16249b96a6615789e255206 100644 (file)
@@ -13,4 +13,10 @@ class AdapterPgSql extends Adapter {
        public function lastInsertId($table) {
                return $this->conn->fetchColumn('SELECT lastval()');
        }
+
+       public function fixupStatement($statement) {
+               $statement = str_replace( '`', '"', $statement );
+               $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'cast(extract(epoch from current_timestamp) as integer)', $statement );
+               return $statement;
+       }
 }
index 0b8107ac53eb83f7c01a0f5346c9d8dcd65cda51..f0057ab489fc0879ad17434b66adfb2a82dec10a 100644 (file)
 namespace OC\DB;
 
 class AdapterSqlite extends Adapter {
+       public function fixupStatement($statement) {
+               $statement = str_replace( '`', '"', $statement );
+               $statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
+               $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
+               return $statement;
+       }
 }
index 602c70456e58303d53c3ef3fa014d176be6445de..d0a67af28a7a65b83481e7939264966c5b326ced 100644 (file)
@@ -16,4 +16,13 @@ class AdapterSQLSrv extends Adapter {
                }
                return $this->conn->lastInsertId($table);
        }
+
+       public function fixupStatement($statement) {
+               $statement = preg_replace( "/\`(.*?)`/", "[$1]", $statement );
+               $statement = str_ireplace( 'NOW()', 'CURRENT_TIMESTAMP', $statement );
+               $statement = str_replace( 'LENGTH(', 'LEN(', $statement );
+               $statement = str_replace( 'SUBSTR(', 'SUBSTRING(', $statement );
+               $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'DATEDIFF(second,{d \'1970-01-01\'},GETDATE())', $statement );
+               return $statement;
+       }
 }
index c8695b131909e9ac2f130fec279fca9555ef700d..8442efc47c9f9c251e152490b3a649d86f871d65 100644 (file)
@@ -50,6 +50,8 @@ class Connection extends \Doctrine\DBAL\Connection {
         */
        public function prepare( $statement, $limit=null, $offset=null ) {
                $statement = $this->replaceTablePrefix($statement);
+               $statement = $this->adapter->fixupStatement($statement);
+
                if ($limit === -1) {
                        $limit = null;
                }
@@ -62,6 +64,9 @@ class Connection extends \Doctrine\DBAL\Connection {
                        }
                }
                $rawQuery = $statement;
+               if(\OC_Config::getValue( "log_query", false)) {
+                       \OC_Log::write('core', 'DB prepare : '.$statement, \OC_Log::DEBUG);
+               }
                $result = parent::prepare($statement);
                if (is_null($limit) && $this->cachingQueryStatementEnabled) {
                        $this->preparedQueries[$rawQuery] = $result;
@@ -85,7 +90,7 @@ class Connection extends \Doctrine\DBAL\Connection {
        public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
        {
                $query = $this->replaceTablePrefix($query);
-               // TODO: fixup
+               $query = $this->adapter->fixupStatement($query);
                return parent::executeQuery($query, $params, $types, $qcp);
        }
 
@@ -104,7 +109,7 @@ class Connection extends \Doctrine\DBAL\Connection {
        public function executeUpdate($query, array $params = array(), array $types = array())
        {
                $query = $this->replaceTablePrefix($query);
-               // TODO: fixup
+               $query = $this->adapter->fixupStatement($query);
                return parent::executeUpdate($query, $params, $types);
        }