]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move lastInsertId to adapter classes
authorBart Visscher <bartv@thisnet.nl>
Fri, 22 Mar 2013 17:36:40 +0000 (18:36 +0100)
committerBart Visscher <bartv@thisnet.nl>
Sun, 21 Jul 2013 21:17:36 +0000 (23:17 +0200)
lib/db.php
lib/db/adapter.php
lib/db/adapteroci8.php
lib/db/adapterpgsql.php
lib/db/adaptersqlsrv.php
lib/db/connection.php

index a3233706109d3d987272c96df5622e4ef870e4de..c1c419df5459749490e6435427498b89b3312058 100644 (file)
@@ -345,34 +345,7 @@ class OC_DB {
         */
        public static function insertid($table=null) {
                self::connect();
-               $type = OC_Config::getValue( "dbtype", "sqlite" );
-               if( $type === 'pgsql' ) {
-                       $result = self::executeAudited('SELECT lastval() AS id');
-                       $row = $result->fetchRow();
-                       self::raiseExceptionOnError($row, 'fetching row for insertid failed');
-                       return $row['id'];
-               } else if( $type === 'mssql') {
-                       if($table !== null) {
-                               $table = self::$connection->replaceTablePrefix( $table );
-                       }
-                       return self::$connection->lastInsertId($table);
-               }
-               if( $type === 'oci' ) {
-                       if($table !== null) {
-                               $prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
-                               $suffix = '_SEQ';
-                               $table = '"'.str_replace( '*PREFIX*', $prefix, $table ).$suffix.'"';
-                       }
-                       return self::$connection->lastInsertId($table);
-               } else {
-                       if($table !== null) {
-                               $suffix = OC_Config::getValue( "dbsequencesuffix", "_id_seq" );
-                               $table = self::$connection->replaceTablePrefix( $table ).$suffix;
-                       }
-                       $result = self::$connection->lastInsertId($table);
-               }
-               self::raiseExceptionOnError($result, 'insertid failed');
-               return $result;
+               return self::$connection->lastInsertId($table);
        }
 
        /**
index c62e35495cdc57012ea787a083718491652c7ba3..b0c9aab9c7a0712619f2ea9aac8919fcb959e7e9 100644 (file)
@@ -14,4 +14,8 @@ class Adapter {
        public function __construct($conn) {
                $this->conn = $conn;
        }
+
+       public function lastInsertId($table) {
+               return $this->conn->realLastInsertId($table);
+       }
 }
index 7ffff4909e79241fee0e19aa44d8d2f781eadec7..50c4d078243d2d953944ee4ff7b42a69f92d45d2 100644 (file)
 namespace OC\DB;
 
 class AdapterOCI8 extends Adapter {
+       public function lastInsertId($table) {
+               if($table !== null) {
+                       $suffix = '_SEQ';
+                       $table = '"'.$table.$suffix.'"';
+                       $table = $this->conn->replaceTablePrefix( $table );
+               }
+               return $this->conn->lastInsertId($table);
+       }
 }
index d3b09357c0ad0917c81cc5a74adc2f2c12bf047f..0084aad470e36902e51575ef62c729be5203023d 100644 (file)
@@ -10,4 +10,7 @@
 namespace OC\DB;
 
 class AdapterPgSql extends Adapter {
+       public function lastInsertId($table) {
+               return $this->conn->fetchColumn('SELECT lastval()');
+       }
 }
index 281daf81d178c0b55cbc83081197946ce057118e..602c70456e58303d53c3ef3fa014d176be6445de 100644 (file)
 namespace OC\DB;
 
 class AdapterSQLSrv extends Adapter {
+       public function lastInsertId($table) {
+               if($table !== null) {
+                       $table = $this->conn->replaceTablePrefix( $table );
+               }
+               return $this->conn->lastInsertId($table);
+       }
 }
index 40809330da64cfb413c413efa7f1fb0c2701405a..7d56452678c1d71001fb5e3c5b530d75cf2a7500 100644 (file)
@@ -67,7 +67,8 @@ class Connection extends \Doctrine\DBAL\Connection {
         */
        public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
        {
-               // TODO: prefix
+               $query = $this->replaceTablePrefix($query);
+               // TODO: fixup
                return parent::executeQuery($query, $params, $types, $qcp);
        }
 
@@ -85,10 +86,36 @@ class Connection extends \Doctrine\DBAL\Connection {
         */
        public function executeUpdate($query, array $params = array(), array $types = array())
        {
-               // TODO: prefix
+               $query = $this->replaceTablePrefix($query);
+               // TODO: fixup
                return parent::executeUpdate($query, $params, $types);
        }
 
+       /**
+        * Returns the ID of the last inserted row, or the last value from a sequence object,
+        * depending on the underlying driver.
+        *
+        * Note: This method may not return a meaningful or consistent result across different drivers,
+        * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
+        * columns or sequences.
+        *
+        * @param string $seqName Name of the sequence object from which the ID should be returned.
+        * @return string A string representation of the last inserted ID.
+        */
+       public function lastInsertId($seqName = null)
+       {
+               if ($seqName) {
+                       $seqName = $this->replaceTablePrefix($seqName);
+               }
+               return $this->adapter->lastInsertId($seqName);
+       }
+
+       // internal use
+       public function realLastInsertId($seqName = null)
+       {
+               return parent::lastInsertId($seqName);
+       }
+
        // internal use
        public function replaceTablePrefix($statement) {
                return str_replace( '*PREFIX*', $this->table_prefix, $statement );