Skip to content

Commit d6fc44b

Browse files
authored
Merge pull request #33496 from rodrigopedra/6.x
[6.x] revert PR #33453 (backport of #33430)
2 parents 3a1c4ef + 97c8d87 commit d6fc44b

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,6 @@ 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-
321308
/**
322309
* Compile an update statement with joins into SQL.
323310
*

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

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

33
namespace Illuminate\Database\Query\Processors;
44

5+
use Exception;
6+
use Illuminate\Database\Connection;
57
use Illuminate\Database\Query\Builder;
68

79
class SqlServerProcessor extends Processor
@@ -19,15 +21,38 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
1921
{
2022
$connection = $query->getConnection();
2123

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

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

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

28-
$id = is_object($result) ? $result->{$sequence} : $result[$sequence];
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+
);
2948

30-
return is_numeric($id) ? (int) $id : $id;
49+
if (! $result) {
50+
throw new Exception('Unable to retrieve lastInsertID for ODBC.');
51+
}
52+
53+
$row = $result[0];
54+
55+
return is_object($row) ? $row->insertid : $row['insertid'];
3156
}
3257

3358
/**

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 1 addition & 9 deletions
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, 'set nocount on;insert into [users] default values;select scope_identity() as [id]', [], null);
2074+
$builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into [users] default values', [], null);
20752075
$builder->from('users')->insertGetId([]);
20762076
}
20772077

@@ -2410,14 +2410,6 @@ 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-
24212413
public function testMySqlWrapping()
24222414
{
24232415
$builder = $this->getMySqlBuilder();

0 commit comments

Comments
 (0)