From 1b3d7db455f51156bc91cce55d5c74bdd98aa37b Mon Sep 17 00:00:00 2001 From: Arthur LORENT Date: Sun, 2 Apr 2023 21:40:41 +0200 Subject: [PATCH] V5: fix searching with Postgres (#130) * Fixed searching SQL request for PostgreSQL by casting every values into text (fixes #129) --- .github/workflows/ci.yml | 4 ++-- CHANGELOG.md | 6 ++++++ composer.json | 2 +- src/Table.php | 16 +++++++++++++--- tests/Unit/Bootstrap5/ColumnSearchableTest.php | 4 ++-- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1bf8583..cde31ac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,7 +50,7 @@ jobs: - name: PHPUnit without code coverage if: matrix.php != '8.2' || matrix.laravel != '10.*' - run: vendor/bin/testbench package:test --parallel --no-coverage + run: vendor/bin/phpunit --no-coverage # Last PHP and laravel versions. @@ -65,7 +65,7 @@ jobs: if: matrix.php == '8.2' && matrix.laravel == '10.*' run: | mkdir -p build/logs - vendor/bin/testbench package:test --parallel --coverage-text --coverage-clover build/logs/clover.xml + vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml - name: Code coverage upload to Coveralls if: env.COVERALLS_REPO_TOKEN && matrix.php == '8.2' && matrix.laravel == '10.*' diff --git a/CHANGELOG.md b/CHANGELOG.md index eb69bee..aaad8bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this package will be documented in this file. +## [5.3.1](https://github.com/Okipa/laravel-table/compare/5.3.0...5.3.1) + +2023-04-02 + +* Fixed searching SQL request for PostgreSQL by casting every values into text (fixes #129) + ## [5.3.0](https://github.com/Okipa/laravel-table/compare/5.2.2...5.3.0) 2023-01-21 diff --git a/composer.json b/composer.json index 06871e0..275598c 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,7 @@ "pint": ["vendor/bin/pint"], "phpmd": "vendor/bin/phpmd config,src,tests text phpmd.xml", "phpstan": "vendor/bin/phpstan analyse --memory-limit=2G", - "phpunit" : "vendor/bin/testbench package:test --parallel --no-coverage", + "phpunit" : "vendor/bin/phpunit", "test": ["@pint", "@phpmd", "@phpstan", "@phpunit"] }, "extra": { diff --git a/src/Table.php b/src/Table.php index ad4dc30..1f57610 100644 --- a/src/Table.php +++ b/src/Table.php @@ -178,8 +178,7 @@ public function prepareQuery( $searchBy )) : $subSearchQuery->orWhereRaw( - 'LOWER(' . $searchableColumn->getAttribute() . ') ' - . $this->getCaseInsensitiveSearchingLikeOperator() . ' ?', + $this->getSearchSqlStatement($searchableColumn->getAttribute()), ['%' . Str::of($searchBy)->trim()->lower() . '%'] ); }); @@ -201,11 +200,22 @@ protected function getSearchableColumns(): Collection return $this->getColumns()->filter(fn (Column $column) => $column->isSearchable()); } - protected function getCaseInsensitiveSearchingLikeOperator(): string + protected function getSearchSqlStatement(string $attribute): string { $connection = config('database.default'); $driver = config('database.connections.' . $connection . '.driver'); + return $this->getSqlLowerFunction($driver, $attribute) . ' ' + . $this->getSqlCaseInsensitiveSearchingLikeOperator($driver) . ' ?'; + } + + protected function getSqlLowerFunction(string $driver, string $attribute): string + { + return $driver === 'pgsql' ? 'LOWER(CAST(' . $attribute . ' AS TEXT))' : 'LOWER(' . $attribute . ')'; + } + + protected function getSqlCaseInsensitiveSearchingLikeOperator(string $driver): string + { return $driver === 'pgsql' ? 'ILIKE' : 'LIKE'; } diff --git a/tests/Unit/Bootstrap5/ColumnSearchableTest.php b/tests/Unit/Bootstrap5/ColumnSearchableTest.php index 86840a6..d4edfa2 100644 --- a/tests/Unit/Bootstrap5/ColumnSearchableTest.php +++ b/tests/Unit/Bootstrap5/ColumnSearchableTest.php @@ -403,10 +403,10 @@ protected function columns(): array } /** @test */ - public function it_can_search_with_insensitive_case_with_postgres(): void + public function it_can_execute_adapted_search_sql_statement_with_postgres(): void { $this->expectException(PDOException::class); - $this->expectExceptionMessageMatches('/select count\(\*\) as aggregate from "users" where \(LOWER\(name\) ILIKE %test%\)\)/'); + $this->expectExceptionMessageMatches('/select count\(\*\) as aggregate from "users" where \(LOWER\(CAST\(name AS TEXT\)\) ILIKE %test%\)\)/'); $config = new class extends AbstractTableConfiguration { protected function table(): Table