mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Fix pgsql json search
This commit is contained in:
@@ -170,6 +170,20 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) {
|
||||
};
|
||||
}
|
||||
|
||||
$column = match ($driverName) {
|
||||
'pgsql' => str($column)->contains('->')
|
||||
? str($column)
|
||||
->beforeLast('->')
|
||||
->append('->>')
|
||||
->append("'")
|
||||
->append(str($column)->afterLast('->'))
|
||||
->append("'")
|
||||
: $column,
|
||||
default => $column,
|
||||
};
|
||||
|
||||
echo $column;
|
||||
|
||||
$column = match ($driverName) {
|
||||
'pgsql' => "{$column}::text",
|
||||
default => $column,
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\Query\Grammars\MySqlGrammar;
|
||||
use Illuminate\Database\Query\Grammars\PostgresGrammar;
|
||||
use Illuminate\View\ComponentAttributeBag;
|
||||
|
||||
use function Filament\Support\generate_search_column_expression;
|
||||
use function Filament\Support\prepare_inherited_attributes;
|
||||
|
||||
it('will prepare attributes', function () {
|
||||
@@ -39,3 +44,37 @@ it('will prepare data attributes', function () {
|
||||
'data-foo' => 'bar',
|
||||
]);
|
||||
});
|
||||
|
||||
it('will generate json search column expression for mysql', function () {
|
||||
$column = 'data->name';
|
||||
$isSearchForcedCaseInsensitive = true;
|
||||
|
||||
$databaseConnection = Mockery::mock(Connection::class);
|
||||
$databaseConnection->shouldReceive('getDriverName')->andReturn('mysql');
|
||||
$databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null);
|
||||
|
||||
$grammar = new MySqlGrammar($databaseConnection);
|
||||
|
||||
$databaseConnection->shouldReceive('getQueryGrammar')->andReturn($grammar);
|
||||
|
||||
$expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection);
|
||||
|
||||
expect($expression->getValue($grammar))
|
||||
->toBe("lower(json_extract(`data`, '$.\"name\"'))");
|
||||
});
|
||||
|
||||
it('will generate json search column expression for pgsql', function () {
|
||||
$column = 'data->name';
|
||||
$isSearchForcedCaseInsensitive = true;
|
||||
|
||||
$databaseConnection = Mockery::mock(Connection::class);
|
||||
$databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql');
|
||||
$databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null);
|
||||
|
||||
$grammar = new PostgresGrammar($databaseConnection);
|
||||
|
||||
$expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection);
|
||||
|
||||
expect($expression->getValue($grammar))
|
||||
->toBe("lower(data->>'name'::text)");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user