Move lastInsertId to adapter classes

This commit is contained in:
Bart Visscher 2013-03-22 18:36:40 +01:00
parent cd98ff1eaf
commit e3c5fea989
6 changed files with 51 additions and 30 deletions

View 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);
}
/**

View File

@ -14,4 +14,8 @@ class Adapter {
public function __construct($conn) {
$this->conn = $conn;
}
public function lastInsertId($table) {
return $this->conn->realLastInsertId($table);
}
}

View File

@ -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);
}
}

View File

@ -10,4 +10,7 @@
namespace OC\DB;
class AdapterPgSql extends Adapter {
public function lastInsertId($table) {
return $this->conn->fetchColumn('SELECT lastval()');
}
}

View File

@ -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);
}
}

View 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 );