Skip to content

Commit 1b3d7db

Browse files
authored
V5: fix searching with Postgres (#130)
* Fixed searching SQL request for PostgreSQL by casting every values into text (fixes #129)
1 parent 9cd555c commit 1b3d7db

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050

5151
- name: PHPUnit without code coverage
5252
if: matrix.php != '8.2' || matrix.laravel != '10.*'
53-
run: vendor/bin/testbench package:test --parallel --no-coverage
53+
run: vendor/bin/phpunit --no-coverage
5454

5555
# Last PHP and laravel versions.
5656

@@ -65,7 +65,7 @@ jobs:
6565
if: matrix.php == '8.2' && matrix.laravel == '10.*'
6666
run: |
6767
mkdir -p build/logs
68-
vendor/bin/testbench package:test --parallel --coverage-text --coverage-clover build/logs/clover.xml
68+
vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml
6969
7070
- name: Code coverage upload to Coveralls
7171
if: env.COVERALLS_REPO_TOKEN && matrix.php == '8.2' && matrix.laravel == '10.*'

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this package will be documented in this file.
44

5+
## [5.3.1](https://github.com/Okipa/laravel-table/compare/5.3.0...5.3.1)
6+
7+
2023-04-02
8+
9+
* Fixed searching SQL request for PostgreSQL by casting every values into text (fixes #129)
10+
511
## [5.3.0](https://github.com/Okipa/laravel-table/compare/5.2.2...5.3.0)
612

713
2023-01-21

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"pint": ["vendor/bin/pint"],
5555
"phpmd": "vendor/bin/phpmd config,src,tests text phpmd.xml",
5656
"phpstan": "vendor/bin/phpstan analyse --memory-limit=2G",
57-
"phpunit" : "vendor/bin/testbench package:test --parallel --no-coverage",
57+
"phpunit" : "vendor/bin/phpunit",
5858
"test": ["@pint", "@phpmd", "@phpstan", "@phpunit"]
5959
},
6060
"extra": {

src/Table.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ public function prepareQuery(
178178
$searchBy
179179
))
180180
: $subSearchQuery->orWhereRaw(
181-
'LOWER(' . $searchableColumn->getAttribute() . ') '
182-
. $this->getCaseInsensitiveSearchingLikeOperator() . ' ?',
181+
$this->getSearchSqlStatement($searchableColumn->getAttribute()),
183182
['%' . Str::of($searchBy)->trim()->lower() . '%']
184183
);
185184
});
@@ -201,11 +200,22 @@ protected function getSearchableColumns(): Collection
201200
return $this->getColumns()->filter(fn (Column $column) => $column->isSearchable());
202201
}
203202

204-
protected function getCaseInsensitiveSearchingLikeOperator(): string
203+
protected function getSearchSqlStatement(string $attribute): string
205204
{
206205
$connection = config('database.default');
207206
$driver = config('database.connections.' . $connection . '.driver');
208207

208+
return $this->getSqlLowerFunction($driver, $attribute) . ' '
209+
. $this->getSqlCaseInsensitiveSearchingLikeOperator($driver) . ' ?';
210+
}
211+
212+
protected function getSqlLowerFunction(string $driver, string $attribute): string
213+
{
214+
return $driver === 'pgsql' ? 'LOWER(CAST(' . $attribute . ' AS TEXT))' : 'LOWER(' . $attribute . ')';
215+
}
216+
217+
protected function getSqlCaseInsensitiveSearchingLikeOperator(string $driver): string
218+
{
209219
return $driver === 'pgsql' ? 'ILIKE' : 'LIKE';
210220
}
211221

tests/Unit/Bootstrap5/ColumnSearchableTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ protected function columns(): array
403403
}
404404

405405
/** @test */
406-
public function it_can_search_with_insensitive_case_with_postgres(): void
406+
public function it_can_execute_adapted_search_sql_statement_with_postgres(): void
407407
{
408408
$this->expectException(PDOException::class);
409-
$this->expectExceptionMessageMatches('/select count\(\*\) as aggregate from "users" where \(LOWER\(name\) ILIKE %test%\)\)/');
409+
$this->expectExceptionMessageMatches('/select count\(\*\) as aggregate from "users" where \(LOWER\(CAST\(name AS TEXT\)\) ILIKE %test%\)\)/');
410410
$config = new class extends AbstractTableConfiguration
411411
{
412412
protected function table(): Table

0 commit comments

Comments
 (0)