Skip to content

Commit 9d47f3e

Browse files
authored
[12.x] add SQLite support for whereNotMorphedTo method (#57698)
* add SQLite support for whereNotMorphedTo method * Converts <=> operator to SQLite's IS operator in SQLiteGrammar
1 parent 32650e9 commit 9d47f3e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ protected function wrapUnion($sql)
4343
return 'select * from ('.$sql.')';
4444
}
4545

46+
/**
47+
* Compile a basic where clause.
48+
*
49+
* @param \Illuminate\Database\Query\Builder $query
50+
* @param array $where
51+
* @return string
52+
*/
53+
protected function whereBasic(Builder $query, $where)
54+
{
55+
if ($where['operator'] === '<=>') {
56+
$column = $this->wrap($where['column']);
57+
$value = $this->parameter($where['value']);
58+
59+
return "{$column} IS {$value}";
60+
}
61+
62+
return parent::whereBasic($query, $where);
63+
}
64+
4665
/**
4766
* Compile a "where like" clause.
4867
*

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,33 @@ public function testOrWhereNotMorphedToClass()
21552155
$this->assertEquals(['baz', EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
21562156
}
21572157

2158+
public function testWhereNotMorphedToWithSQLite()
2159+
{
2160+
$model = new EloquentBuilderTestModelParentStub;
2161+
$this->mockConnectionForModel($model, 'SQLite');
2162+
2163+
$relatedModel = new EloquentBuilderTestModelCloseRelatedStub;
2164+
$relatedModel->id = 1;
2165+
2166+
$builder = $model->whereNotMorphedTo('morph', $relatedModel);
2167+
2168+
$this->assertStringNotContainsString('<=>', $builder->toSql());
2169+
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not (("eloquent_builder_test_model_parent_stubs"."morph_type" IS ? and "eloquent_builder_test_model_parent_stubs"."morph_id" in (?)))', $builder->toSql());
2170+
$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
2171+
}
2172+
2173+
public function testWhereNotMorphedToClassWithSQLite()
2174+
{
2175+
$model = new EloquentBuilderTestModelParentStub;
2176+
$this->mockConnectionForModel($model, 'SQLite');
2177+
2178+
$builder = $model->whereNotMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);
2179+
2180+
$this->assertStringNotContainsString('<=>', $builder->toSql());
2181+
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not "eloquent_builder_test_model_parent_stubs"."morph_type" IS ?', $builder->toSql());
2182+
$this->assertEquals([EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
2183+
}
2184+
21582185
public function testWhereMorphedToAlias()
21592186
{
21602187
$model = new EloquentBuilderTestModelParentStub;

0 commit comments

Comments
 (0)