Commit 1ea99a2f authored by Steve Müller's avatar Steve Müller Committed by Marco Pivetta

fix fetching last insert ID for sequences on SQL Server

parent 77b764ad
...@@ -40,6 +40,21 @@ class Connection extends PDOConnection implements \Doctrine\DBAL\Driver\Connecti ...@@ -40,6 +40,21 @@ class Connection extends PDOConnection implements \Doctrine\DBAL\Driver\Connecti
/** /**
* @override * @override
*/ */
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}
*/
public function quote($value, $type=\PDO::PARAM_STR) public function quote($value, $type=\PDO::PARAM_STR)
{ {
$val = parent::quote($value, $type); $val = parent::quote($value, $type);
......
...@@ -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