Skip to content

Commit

Permalink
V5: fix searching with Postgres (#130)
Browse files Browse the repository at this point in the history
* Fixed searching SQL request for PostgreSQL by casting every values into text (fixes #129)
  • Loading branch information
Okipa committed Apr 2, 2023
1 parent 9cd555c commit 1b3d7db
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.*'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
16 changes: 13 additions & 3 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public function prepareQuery(
$searchBy
))
: $subSearchQuery->orWhereRaw(
'LOWER(' . $searchableColumn->getAttribute() . ') '
. $this->getCaseInsensitiveSearchingLikeOperator() . ' ?',
$this->getSearchSqlStatement($searchableColumn->getAttribute()),
['%' . Str::of($searchBy)->trim()->lower() . '%']
);
});
Expand All @@ -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';
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Bootstrap5/ColumnSearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1b3d7db

Please sign in to comment.