Skip to content

Commit

Permalink
Removed explicit escaping for pgsql driver in FilterPartial#maybeSpec…
Browse files Browse the repository at this point in the history
…ifyEscapeChar. Fixes #941 (#968)

* Fix styling

* Removed explicit escaping for pgsql driver in FilterPartial#maybeSpecifyEscapeChar. Fixes #941

Added mariadb driver in FilterPartial#maybeSpecifyEscapeChar phpdoc for param $driver. Also adjusted test in order to run with mariadb driver only if the installed version of illuminate/database dependency supports the driver.

* Fix styling

---------

Co-authored-by: Talpx1 <[email protected]>
Co-authored-by: Alex Vanderbist <[email protected]>
  • Loading branch information
3 people authored Oct 3, 2024
1 parent 46018b3 commit bc2daf1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Filters/FiltersPartial.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ protected static function escapeLike(string $value): string
}

/**
* @param 'sqlite'|'pgsql'|'sqlsrc'|'mysql' $driver
* @param 'sqlite'|'pgsql'|'sqlsrc'|'mysql'|'mariadb' $driver
* @return string
*/
protected static function maybeSpecifyEscapeChar(string $driver): string
{
if (! in_array($driver, ['sqlite','pgsql','sqlsrv'])) {
if (! in_array($driver, ['sqlite','sqlsrv'])) {
return '';
}

Expand Down
11 changes: 8 additions & 3 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
});

it('specifies escape character in supported databases', function (string $dbDriver) {
if ($dbDriver === 'mariadb' && ! in_array('mariadb', DB::supportedDrivers())) {
$this->markTestSkipped('mariadb driver not supported in the installed version of illuminate/database dependency');
}

$fakeConnection = "test_{$dbDriver}";

DB::connectUsing($fakeConnection, [
Expand All @@ -100,6 +104,7 @@
]);

DB::usingConnection($fakeConnection, function () use ($dbDriver) {

$request = new Request([
'filter' => ['name' => 'to_find'],
]);
Expand All @@ -108,10 +113,10 @@
->allowedFilters('name', 'id')
->toSql();

expect($queryBuilderSql)->when(in_array($dbDriver, ["sqlite","pgsql","sqlsrv"]), fn (Expectation $query) => $query->toContain("ESCAPE '\'"));
expect($queryBuilderSql)->when($dbDriver === 'mysql', fn (Expectation $query) => $query->not->toContain("ESCAPE '\'"));
expect($queryBuilderSql)->when(in_array($dbDriver, ["sqlite","sqlsrv"]), fn (Expectation $query) => $query->toContain("ESCAPE '\'"));
expect($queryBuilderSql)->when(in_array($dbDriver, ["mysql","mariadb", "pgsql"]), fn (Expectation $query) => $query->not->toContain("ESCAPE '\'"));
});
})->with(['sqlite', 'mysql', 'pgsql', 'sqlsrv']);
})->with(['sqlite', 'mysql', 'pgsql', 'sqlsrv', 'mariadb']);

it('can filter results based on the existence of a property in an array', function () {
$results = createQueryFromFilterRequest([
Expand Down

0 comments on commit bc2daf1

Please sign in to comment.