*/
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);
}
/**
public function __construct($conn) {
$this->conn = $conn;
}
+
+ public function lastInsertId($table) {
+ return $this->conn->realLastInsertId($table);
+ }
}
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);
+ }
}
namespace OC\DB;
class AdapterPgSql extends Adapter {
+ public function lastInsertId($table) {
+ return $this->conn->fetchColumn('SELECT lastval()');
+ }
}
namespace OC\DB;
class AdapterSQLSrv extends Adapter {
+ public function lastInsertId($table) {
+ if($table !== null) {
+ $table = $this->conn->replaceTablePrefix( $table );
+ }
+ return $this->conn->lastInsertId($table);
+ }
}
*/
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);
}
*/
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 );