Skip to content

Commit 168c0c2

Browse files
Fix mismatched signature for spatial index in Laravel (#104)
Changed spatialIndex to handle a different number of parameters. Fixed JSON IS NULL and JSON IS NOT NULL functions (in newer versions of the database charset changed, and string comparison became case sensitive).
1 parent 113b337 commit 168c0c2

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/Query/SingleStoreQueryGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected function whereNull(Builder $query, $where)
146146
if ($this->isJsonSelector($columnValue)) {
147147
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
148148

149-
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) = \'NULL\')';
149+
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) = \'null\')';
150150
}
151151

152152
return $this->wrap($where['column']).' is null';
@@ -158,7 +158,7 @@ protected function whereNotNull(Builder $query, $where)
158158
if ($this->isJsonSelector($columnValue)) {
159159
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
160160

161-
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) != \'NULL\')';
161+
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) != \'null\')';
162162
}
163163

164164
return $this->wrap($where['column']).' is not null';

src/Schema/Blueprint/ModifiesIndexes.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ public function sortKey($columns = null, $direction = 'asc')
2828
/**
2929
* @return SpatialIndexCommand
3030
*/
31-
public function spatialIndex($columns, $name = null)
31+
public function spatialIndex(...$args)
3232
{
33-
parent::spatialIndex($columns, $name);
33+
$columns = $args[0] ?? null;
34+
$name = $args[1] ?? null;
35+
$operatorClass = $args[2] ?? null;
36+
37+
// Laravel 12.21.0+ passes: $columns, $name = null, $operatorClass = null
38+
// Laravel <12.21.0 passes: $columns, $name = null
39+
if ((new \ReflectionMethod(parent::class, 'spatialIndex'))->getNumberOfParameters() === 3) {
40+
return parent::spatialIndex($columns, $name, $operatorClass);
41+
} else {
42+
return parent::spatialIndex($columns, $name);
43+
}
3444

3545
return $this->recastLastCommand(SpatialIndexCommand::class);
3646
}

tests/Hybrid/HybridTestHelpers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ protected function mockedConnection()
4848
$connection->shouldReceive('getSchemaGrammar')->andReturn($grammar);
4949
$connection->shouldReceive('getTablePrefix')->andReturn('');
5050
$connection->shouldReceive('getSchemaBuilder')->andReturn(new SingleStoreSchemaBuilder($connection));
51+
$connection->allows('isMaria')->andReturn(false);
52+
$connection->allows('getServerVersion')->andReturn('8.9.0');
5153

5254
return $connection;
5355
}

tests/Hybrid/Json/JsonWhereTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function where_null()
110110

111111
$this->assertEquals(
112112
// @TODO check docs
113-
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON(data, 'value1')) = 'NULL') order by `id` asc",
113+
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON(data, 'value1')) = 'null') order by `id` asc",
114114
$query->toSql()
115115
);
116116

@@ -163,7 +163,7 @@ public function where_not_null()
163163

164164
$this->assertEquals(
165165
// @TODO check docs
166-
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON(data, 'value1')) != 'NULL') order by `id` asc",
166+
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON(data, 'value1')) != 'null') order by `id` asc",
167167
$query->toSql()
168168
);
169169

0 commit comments

Comments
 (0)