Skip to content

Commit b35452d

Browse files
driesvintsrodrigopedrataylorotwell
authored
[6.x] SQL Server fix backport (#33453)
* improve SQL Server last insert id retrieval * formatting Co-authored-by: Rodrigo Pedra Brum <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7e8d08e commit b35452d

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ public function compileExists(Builder $query)
305305
return $this->compileSelect($existsQuery->selectRaw('1 [exists]')->limit(1));
306306
}
307307

308+
/**
309+
* Compile an insert and get ID statement into SQL.
310+
*
311+
* @param \Illuminate\Database\Query\Builder $query
312+
* @param array $values
313+
* @param string $sequence
314+
* @return string
315+
*/
316+
public function compileInsertGetId(Builder $query, $values, $sequence)
317+
{
318+
return 'set nocount on;'.$this->compileInsert($query, $values).';select scope_identity() as '.$this->wrap($sequence ?: 'id');
319+
}
320+
308321
/**
309322
* Compile an update statement with joins into SQL.
310323
*

src/Illuminate/Database/Query/Processors/SqlServerProcessor.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Illuminate\Database\Query\Processors;
44

5-
use Exception;
6-
use Illuminate\Database\Connection;
75
use Illuminate\Database\Query\Builder;
86

97
class SqlServerProcessor extends Processor
@@ -21,38 +19,15 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
2119
{
2220
$connection = $query->getConnection();
2321

24-
$connection->insert($sql, $values);
22+
$connection->recordsHaveBeenModified();
2523

26-
if ($connection->getConfig('odbc') === true) {
27-
$id = $this->processInsertGetIdForOdbc($connection);
28-
} else {
29-
$id = $connection->getPdo()->lastInsertId();
30-
}
24+
$result = $connection->selectFromWriteConnection($sql, $values)[0];
3125

32-
return is_numeric($id) ? (int) $id : $id;
33-
}
26+
$sequence = $sequence ?: 'id';
3427

35-
/**
36-
* Process an "insert get ID" query for ODBC.
37-
*
38-
* @param \Illuminate\Database\Connection $connection
39-
* @return int
40-
*
41-
* @throws \Exception
42-
*/
43-
protected function processInsertGetIdForOdbc(Connection $connection)
44-
{
45-
$result = $connection->selectFromWriteConnection(
46-
'SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid'
47-
);
48-
49-
if (! $result) {
50-
throw new Exception('Unable to retrieve lastInsertID for ODBC.');
51-
}
28+
$id = is_object($result) ? $result->{$sequence} : $result[$sequence];
5229

53-
$row = $result[0];
54-
55-
return is_object($row) ? $row->insertid : $row['insertid'];
30+
return is_numeric($id) ? (int) $id : $id;
5631
}
5732

5833
/**

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ public function testInsertGetIdWithEmptyValues()
20712071
$builder->from('users')->insertGetId([]);
20722072

20732073
$builder = $this->getSqlServerBuilder();
2074-
$builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into [users] default values', [], null);
2074+
$builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'set nocount on;insert into [users] default values;select scope_identity() as [id]', [], null);
20752075
$builder->from('users')->insertGetId([]);
20762076
}
20772077

@@ -2410,6 +2410,14 @@ public function testPostgresInsertGetId()
24102410
$this->assertEquals(1, $result);
24112411
}
24122412

2413+
public function testSqlServerInsertGetId()
2414+
{
2415+
$builder = $this->getSqlServerBuilder();
2416+
$builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'set nocount on;insert into [users] ([email]) values (?);select scope_identity() as [id]', ['foo'], 'id')->andReturn(1);
2417+
$result = $builder->from('users')->insertGetId(['email' => 'foo'], 'id');
2418+
$this->assertEquals(1, $result);
2419+
}
2420+
24132421
public function testMySqlWrapping()
24142422
{
24152423
$builder = $this->getMySqlBuilder();

0 commit comments

Comments
 (0)