Skip to content

Commit 5b47c76

Browse files
committed
Merge branch '6.x' into 7.x
2 parents 7cf519a + d6fc44b commit 5b47c76

File tree

8 files changed

+71
-37
lines changed

8 files changed

+71
-37
lines changed

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

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

326-
/**
327-
* Compile an insert and get ID statement into SQL.
328-
*
329-
* @param \Illuminate\Database\Query\Builder $query
330-
* @param array $values
331-
* @param string $sequence
332-
* @return string
333-
*/
334-
public function compileInsertGetId(Builder $query, $values, $sequence)
335-
{
336-
return 'set nocount on;'.$this->compileInsert($query, $values).';select scope_identity() as '.$this->wrap($sequence ?: 'id');
337-
}
338-
339326
/**
340327
* Compile an update statement with joins into SQL.
341328
*

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
/**

src/Illuminate/Validation/Rules/DatabaseRule.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public function resolveTableName($table)
6262
return $table;
6363
}
6464

65-
$model = new $table;
65+
if (is_subclass_of($table, Model::class)) {
66+
return (new $table)->getTable();
67+
}
6668

67-
return $model instanceof Model
68-
? $model->getTable()
69-
: $table;
69+
return $table;
7070
}
7171

7272
/**

src/Illuminate/Validation/Validator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ protected function shouldBeExcluded($attribute)
428428
*/
429429
protected function removeAttribute($attribute)
430430
{
431-
unset($this->data[$attribute], $this->rules[$attribute]);
431+
Arr::forget($this->data, $attribute);
432+
Arr::forget($this->rules, $attribute);
432433
}
433434

434435
/**

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,7 @@ public function testInsertGetIdWithEmptyValues()
21302130
$builder->from('users')->insertGetId([]);
21312131

21322132
$builder = $this->getSqlServerBuilder();
2133-
$builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'set nocount on;insert into [users] default values;select scope_identity() as [id]', [], null);
2133+
$builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into [users] default values', [], null);
21342134
$builder->from('users')->insertGetId([]);
21352135
}
21362136

@@ -2474,14 +2474,6 @@ public function testPostgresInsertGetId()
24742474
$this->assertEquals(1, $result);
24752475
}
24762476

2477-
public function testSqlServerInsertGetId()
2478-
{
2479-
$builder = $this->getSqlServerBuilder();
2480-
$builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'set nocount on;insert into [users] ([email]) values (?);select scope_identity() as [id]', ['foo'], 'id')->andReturn(1);
2481-
$result = $builder->from('users')->insertGetId(['email' => 'foo'], 'id');
2482-
$this->assertEquals(1, $result);
2483-
}
2484-
24852477
public function testMySqlWrapping()
24862478
{
24872479
$builder = $this->getMySqlBuilder();

tests/Validation/ValidationExistsRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
5858
$rule = new Exists(NoTableNameModel::class, 'column');
5959
$rule->where('foo', 'bar');
6060
$this->assertSame('exists:no_table_name_models,column,foo,"bar"', (string) $rule);
61+
62+
$rule = new Exists(ClassWithRequiredConstructorParameters::class, 'column');
63+
$rule->where('foo', 'bar');
64+
$this->assertSame('exists:'.ClassWithRequiredConstructorParameters::class.',column,foo,"bar"', (string) $rule);
6165
}
6266

6367
public function testItChoosesValidRecordsUsingWhereInRule()
@@ -203,3 +207,15 @@ class NoTableNameModel extends Eloquent
203207
protected $guarded = [];
204208
public $timestamps = false;
205209
}
210+
211+
class ClassWithRequiredConstructorParameters
212+
{
213+
private $bar;
214+
private $baz;
215+
216+
public function __construct($bar, $baz)
217+
{
218+
$this->bar = $bar;
219+
$this->baz = $baz;
220+
}
221+
}

tests/Validation/ValidationUniqueRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
2626
$rule->where('foo', 'bar');
2727
$this->assertSame('unique:no_table_names,NULL,NULL,id,foo,"bar"', (string) $rule);
2828

29+
$rule = new Unique(ClassWithNonEmptyConstructor::class);
30+
$rule->where('foo', 'bar');
31+
$this->assertSame('unique:'.ClassWithNonEmptyConstructor::class.',NULL,NULL,id,foo,"bar"', (string) $rule);
32+
2933
$rule = new Unique('table', 'column');
3034
$rule->ignore('Taylor, Otwell', 'id_column');
3135
$rule->where('foo', 'bar');
@@ -78,3 +82,15 @@ class NoTableName extends Model
7882
protected $guarded = [];
7983
public $timestamps = false;
8084
}
85+
86+
class ClassWithNonEmptyConstructor
87+
{
88+
private $bar;
89+
private $baz;
90+
91+
public function __construct($bar, $baz)
92+
{
93+
$this->bar = $bar;
94+
$this->baz = $baz;
95+
}
96+
}

tests/Validation/ValidationValidatorTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5223,13 +5223,10 @@ public function providesPassingExcludeIfData()
52235223
'vehicles' => [
52245224
[
52255225
'type' => 'car', 'wheels' => [
5226-
// The shape field for these blue wheels were correctly excluded (if they weren't, they would
5227-
// fail the validation). They still appear in the validated data. This behaviour is unrelated
5228-
// to the "exclude" type rules.
52295226
['color' => 'red', 'shape' => 'square'],
5230-
['color' => 'blue', 'shape' => 'hexagon'],
5227+
['color' => 'blue'],
52315228
['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
5232-
['color' => 'blue', 'shape' => 'triangle'],
5229+
['color' => 'blue'],
52335230
],
52345231
],
52355232
['type' => 'boat'],

0 commit comments

Comments
 (0)