From e3c5fea989f26a4ad16b841be25ea485c8aad8c4 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 22 Mar 2013 18:36:40 +0100 Subject: [PATCH] Move lastInsertId to adapter classes --- lib/db.php | 29 +---------------------------- lib/db/adapter.php | 4 ++++ lib/db/adapteroci8.php | 8 ++++++++ lib/db/adapterpgsql.php | 3 +++ lib/db/adaptersqlsrv.php | 6 ++++++ lib/db/connection.php | 31 +++++++++++++++++++++++++++++-- 6 files changed, 51 insertions(+), 30 deletions(-) diff --git a/lib/db.php b/lib/db.php index a3233706109..c1c419df545 100644 --- a/lib/db.php +++ b/lib/db.php @@ -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); } /** diff --git a/lib/db/adapter.php b/lib/db/adapter.php index c62e35495cd..b0c9aab9c7a 100644 --- a/lib/db/adapter.php +++ b/lib/db/adapter.php @@ -14,4 +14,8 @@ class Adapter { public function __construct($conn) { $this->conn = $conn; } + + public function lastInsertId($table) { + return $this->conn->realLastInsertId($table); + } } diff --git a/lib/db/adapteroci8.php b/lib/db/adapteroci8.php index 7ffff4909e7..50c4d078243 100644 --- a/lib/db/adapteroci8.php +++ b/lib/db/adapteroci8.php @@ -10,4 +10,12 @@ 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); + } } diff --git a/lib/db/adapterpgsql.php b/lib/db/adapterpgsql.php index d3b09357c0a..0084aad470e 100644 --- a/lib/db/adapterpgsql.php +++ b/lib/db/adapterpgsql.php @@ -10,4 +10,7 @@ namespace OC\DB; class AdapterPgSql extends Adapter { + public function lastInsertId($table) { + return $this->conn->fetchColumn('SELECT lastval()'); + } } diff --git a/lib/db/adaptersqlsrv.php b/lib/db/adaptersqlsrv.php index 281daf81d17..602c70456e5 100644 --- a/lib/db/adaptersqlsrv.php +++ b/lib/db/adaptersqlsrv.php @@ -10,4 +10,10 @@ namespace OC\DB; class AdapterSQLSrv extends Adapter { + public function lastInsertId($table) { + if($table !== null) { + $table = $this->conn->replaceTablePrefix( $table ); + } + return $this->conn->lastInsertId($table); + } } diff --git a/lib/db/connection.php b/lib/db/connection.php index 40809330da6..7d56452678c 100644 --- a/lib/db/connection.php +++ b/lib/db/connection.php @@ -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 ); -- 2.39.5