From bc2daf1f854516b49cacceaf21b5dbfab1f0556d Mon Sep 17 00:00:00 2001 From: Talpx1 <69934344+Talpx1@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:07:28 +0200 Subject: [PATCH] Removed explicit escaping for pgsql driver in FilterPartial#maybeSpecifyEscapeChar. 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 Co-authored-by: Alex Vanderbist --- src/Filters/FiltersPartial.php | 4 ++-- tests/FilterTest.php | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Filters/FiltersPartial.php b/src/Filters/FiltersPartial.php index 77db819..31d7710 100644 --- a/src/Filters/FiltersPartial.php +++ b/src/Filters/FiltersPartial.php @@ -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 ''; } diff --git a/tests/FilterTest.php b/tests/FilterTest.php index e4cea38..5648603 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -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, [ @@ -100,6 +104,7 @@ ]); DB::usingConnection($fakeConnection, function () use ($dbDriver) { + $request = new Request([ 'filter' => ['name' => 'to_find'], ]); @@ -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([