Skip to content

Commit

Permalink
Merge branch 'fix/#2634-last-insert-id-fetching-in-sql-server-syntax-…
Browse files Browse the repository at this point in the history
…fix' into 2.5

Backport #2634 into 2.5.*
  • Loading branch information
Ocramius committed Feb 6, 2017
2 parents 77b764a + 1ea99a2 commit 31c41ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 15 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public function __construct($dsn, $user = null, $password = null, array $options
/**
* @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)
{
$val = parent::quote($value, $type);
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ public function exec($statement)
public function lastInsertId($name = null)
{
if ($name !== null) {
$sql = "SELECT IDENT_CURRENT(".$this->quote($name).") AS LastInsertId";
$stmt = $this->prepare($sql);
$stmt->execute();
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
$stmt->execute(array($name));

return $stmt->fetchColumn();
}
Expand Down

0 comments on commit 31c41ee

Please sign in to comment.