-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PostgresGrammar supports JSON array key braces
Similar to MySQL, SQL Server, and SQLite, make Postgres support `$query->where('column->json_key[0]', 'foo')`. Postgres also allows equivalent call `$query->where('column->json_key->0', 'foo')`. Unlike the other database drivers, the SQL doesn't compile to a JSON path expression. The array indices must be parsed from the string, separating them into new segments. e.g., $query->where('column->json_key[0]', 'foo')
- Loading branch information
Showing
3 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/Integration/Database/Postgres/DatabasePostgresConnectionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\Postgres; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
/** | ||
* @requires extension pdo_pgsql | ||
* requires OS Linux|Darwin | ||
*/ | ||
class DatabasePostgresConnectionTest extends PostgresTestCase | ||
{ | ||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
if (! Schema::hasTable('json_table')) { | ||
Schema::create('json_table', function (Blueprint $table) { | ||
$table->json('json_col')->nullable(); | ||
}); | ||
} | ||
} | ||
|
||
protected function destroyDatabaseMigrations() | ||
{ | ||
Schema::drop('json_table'); | ||
} | ||
|
||
/** | ||
* @dataProvider jsonWhereNullDataProvider | ||
*/ | ||
public function testJsonWhereNull($expected, $key, array $value = ['value' => 123]) | ||
{ | ||
DB::table('json_table')->insert(['json_col' => json_encode($value)]); | ||
|
||
$this->assertSame($expected, DB::table('json_table')->whereNull("json_col->$key")->exists()); | ||
} | ||
|
||
/** | ||
* @dataProvider jsonWhereNullDataProvider | ||
*/ | ||
public function testJsonWhereNotNull($expected, $key, array $value = ['value' => 123]) | ||
{ | ||
DB::table('json_table')->insert(['json_col' => json_encode($value)]); | ||
|
||
$this->assertSame(! $expected, DB::table('json_table')->whereNotNull("json_col->$key")->exists()); | ||
} | ||
|
||
public function jsonWhereNullDataProvider() | ||
{ | ||
return [ | ||
'key not exists' => [true, 'invalid'], | ||
'key exists and null' => [true, 'value', ['value' => null]], | ||
'key exists and "null"' => [false, 'value', ['value' => 'null']], | ||
'key exists and not null' => [false, 'value', ['value' => false]], | ||
'nested key not exists' => [true, 'nested->invalid'], | ||
'nested key exists and null' => [true, 'nested->value', ['nested' => ['value' => null]]], | ||
'nested key exists and "null"' => [false, 'nested->value', ['nested' => ['value' => 'null']]], | ||
'nested key exists and not null' => [false, 'nested->value', ['nested' => ['value' => false]]], | ||
'array index not exists' => [true, '[0]', [1 => 'invalid']], | ||
'array index exists and null' => [true, '[0]', [null]], | ||
'array index exists and "null"' => [false, '[0]', ['null']], | ||
'array index exists and not null' => [false, '[0]', [false]], | ||
'multiple array index not exists' => [true, '[0][0]', [1 => [1 => 'invalid']]], | ||
'multiple array index exists and null' => [true, '[0][0]', [[null]]], | ||
'multiple array index exists and "null"' => [false, '[0][0]', [['null']]], | ||
'multiple array index exists and not null' => [false, '[0][0]', [[false]]], | ||
'nested array index not exists' => [true, 'nested[0]', ['nested' => [1 => 'nested->invalid']]], | ||
'nested array index exists and null' => [true, 'nested->value[1]', ['nested' => ['value' => [0, null]]]], | ||
'nested array index exists and "null"' => [false, 'nested->value[1]', ['nested' => ['value' => [0, 'null']]]], | ||
'nested array index exists and not null' => [false, 'nested->value[1]', ['nested' => ['value' => [0, false]]]], | ||
]; | ||
} | ||
|
||
public function testJsonPathUpdate() | ||
{ | ||
DB::table('json_table')->insert([ | ||
['json_col' => '{"foo":["bar"]}'], | ||
['json_col' => '{"foo":["baz"]}'], | ||
['json_col' => '{"foo":[["array"]]}'], | ||
]); | ||
|
||
$updatedCount = DB::table('json_table')->where('json_col->foo[0]', 'baz')->update([ | ||
'json_col->foo[0]' => 'updated', | ||
]); | ||
$this->assertSame(1, $updatedCount); | ||
|
||
$updatedCount = DB::table('json_table')->where('json_col->foo[0][0]', 'array')->update([ | ||
'json_col->foo[0][0]' => 'updated', | ||
]); | ||
$this->assertSame(1, $updatedCount); | ||
} | ||
} |