Commit 55338541 authored by Steve Müller's avatar Steve Müller

fix fetching last insert ID for sequences on SQL Server

parent 1425f8f0
...@@ -37,6 +37,21 @@ class Connection extends PDOConnection implements \Doctrine\DBAL\Driver\Connecti ...@@ -37,6 +37,21 @@ class Connection extends PDOConnection implements \Doctrine\DBAL\Driver\Connecti
$this->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array(Statement::class, array())); $this->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array(Statement::class, array()));
} }
/**
* {@inheritDoc}
*/
public function lastInsertId($name = null)
{
if (null === $name) {
return parent::lastInsertId($name);
}
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
$stmt->execute(array($name));
return $stmt->fetchColumn();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -130,9 +130,8 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection ...@@ -130,9 +130,8 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
public function lastInsertId($name = null) public function lastInsertId($name = null)
{ {
if ($name !== null) { if ($name !== null) {
$sql = "SELECT IDENT_CURRENT(".$this->quote($name).") AS LastInsertId"; $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
$stmt = $this->prepare($sql); $stmt->execute(array($name));
$stmt->execute();
return $stmt->fetchColumn(); return $stmt->fetchColumn();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment