Fixed regex function for postgres

This commit is contained in:
Jan Böhmer 2024-06-13 23:18:25 +02:00
parent c58ff5861d
commit 6e9b337b49
5 changed files with 58 additions and 3 deletions

View file

@ -43,7 +43,7 @@ doctrine:
dql: dql:
string_functions: string_functions:
regexp: DoctrineExtensions\Query\Mysql\Regexp regexp: App\Doctrine\Functions\Regexp
field: DoctrineExtensions\Query\Mysql\Field field: DoctrineExtensions\Query\Mysql\Field
field2: App\Doctrine\Functions\Field2 field2: App\Doctrine\Functions\Field2

View file

@ -113,7 +113,7 @@ class TextConstraint extends AbstractConstraint
//Regex is only supported on MySQL and needs a special function //Regex is only supported on MySQL and needs a special function
if ($this->operator === 'REGEX') { if ($this->operator === 'REGEX') {
$queryBuilder->andWhere(sprintf('REGEXP(%s, :%s) = 1', $this->property, $this->identifier)); $queryBuilder->andWhere(sprintf('REGEXP(%s, :%s) = TRUE', $this->property, $this->identifier));
$queryBuilder->setParameter($this->identifier, $this->value); $queryBuilder->setParameter($this->identifier, $this->value);
} }
} }

View file

@ -129,7 +129,7 @@ class PartSearchFilter implements FilterInterface
//Convert the fields to search to a list of expressions //Convert the fields to search to a list of expressions
$expressions = array_map(function (string $field): string { $expressions = array_map(function (string $field): string {
if ($this->regex) { if ($this->regex) {
return sprintf("REGEXP(%s, :search_query) = 1", $field); return sprintf("REGEXP(%s, :search_query) = TRUE", $field);
} }
return sprintf("%s LIKE :search_query", $field); return sprintf("%s LIKE :search_query", $field);

View file

@ -0,0 +1,52 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Doctrine\Functions;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\ORM\Query\SqlWalker;
/**
* Similar to the regexp function, but with support for multi platform.
*/
class Regexp extends \DoctrineExtensions\Query\Mysql\Regexp
{
public function getSql(SqlWalker $sqlWalker): string
{
$platform = $sqlWalker->getConnection()->getDatabasePlatform();
//
if ($platform instanceof AbstractMySQLPlatform || $platform instanceof SQLitePlatform) {
$operator = 'REGEXP';
} elseif ($platform instanceof PostgreSQLPlatform) {
//Use the case-insensitive operator, to have the same behavior as MySQL
$operator = '~*';
} else {
throw new \RuntimeException('Platform ' . gettype($platform) . ' does not support regular expressions.');
}
return '(' . $this->value->dispatch($sqlWalker) . ' ' . $operator . ' ' . $this->regexp->dispatch($sqlWalker) . ')';
}
}

View file

@ -99,6 +99,9 @@ class DatatablesAvailabilityTest extends WebTestCase
//Test using filters //Test using filters
yield ['/category/1/parts?part_filter%5Bname%5D%5Boperator%5D=%3D&part_filter%5Bname%5D%5Bvalue%5D=BC547&part_filter%5Bcategory%5D%5Boperator%5D=INCLUDING_CHILDREN&part_filter%5Btags%5D%5Boperator%5D=ANY&part_filter%5Btags%5D%5Bvalue%5D=Test&part_filter%5Bsubmit%5D=']; yield ['/category/1/parts?part_filter%5Bname%5D%5Boperator%5D=%3D&part_filter%5Bname%5D%5Bvalue%5D=BC547&part_filter%5Bcategory%5D%5Boperator%5D=INCLUDING_CHILDREN&part_filter%5Btags%5D%5Boperator%5D=ANY&part_filter%5Btags%5D%5Bvalue%5D=Test&part_filter%5Bsubmit%5D='];
yield ['/category/1/parts?part_filter%5Bcategory%5D%5Boperator%5D=INCLUDING_CHILDREN&part_filter%5Bstorelocation%5D%5Boperator%5D=%3D&part_filter%5Bstorelocation%5D%5Bvalue%5D=1&part_filter%5BattachmentsCount%5D%5Boperator%5D=%3D&part_filter%5BattachmentsCount%5D%5Bvalue1%5D=3&part_filter%5Bsubmit%5D=']; yield ['/category/1/parts?part_filter%5Bcategory%5D%5Boperator%5D=INCLUDING_CHILDREN&part_filter%5Bstorelocation%5D%5Boperator%5D=%3D&part_filter%5Bstorelocation%5D%5Bvalue%5D=1&part_filter%5BattachmentsCount%5D%5Boperator%5D=%3D&part_filter%5BattachmentsCount%5D%5Bvalue1%5D=3&part_filter%5Bsubmit%5D='];
//Test regex search
yield ['/parts/search?category=1&comment=1&description=1&ipn=1&keyword=test&mpn=1&name=1&ordernr=1&regex=1&storelocation=1&tags=1'];
} }
public function testOrdering(): void public function testOrdering(): void