From 2566e2c4f3e0f15cd8fa61f2eff267ae2f2f457e Mon Sep 17 00:00:00 2001 From: "cristian.almohalla" Date: Fri, 20 Aug 2021 14:22:20 +0200 Subject: [PATCH 1/2] add null operator support --- src/Criteria/DBALCriteriaVisitor.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Criteria/DBALCriteriaVisitor.php b/src/Criteria/DBALCriteriaVisitor.php index 3d1f1dd..3796809 100644 --- a/src/Criteria/DBALCriteriaVisitor.php +++ b/src/Criteria/DBALCriteriaVisitor.php @@ -5,6 +5,7 @@ use Doctrine\DBAL\Query\QueryBuilder; use Pccomponentes\Criteria\Domain\Criteria\AndFilter; use Pccomponentes\Criteria\Domain\Criteria\Criteria; +use Pccomponentes\Criteria\Domain\Criteria\Filter; use Pccomponentes\Criteria\Domain\Criteria\FilterInterface; use Pccomponentes\Criteria\Domain\Criteria\NullValueFilter; use Pccomponentes\Criteria\Domain\Criteria\FilterOperator; @@ -77,10 +78,21 @@ public function visitFilter(FilterInterface $filter): string return $this->mapFieldValue($filter->field()->value()) . ' ' . $this->mapOperator($filter) - . (\in_array($filter->operator()->value(), [FilterOperator::IN, FilterOperator::NOT_IN]) ? ' (' : ' ') - . ':' . $filter->field()->value() - . $this->countParams - . (\in_array($filter->operator()->value(), [FilterOperator::IN, FilterOperator::NOT_IN]) ? ')' : ''); + . ' ' + . $this->parameterExpression($filter); + } + + private function parameterExpression(Filter $filter): string + { + if (\in_array($filter->operator()->value(), [FilterOperator::IS_NULL, FilterOperator::IS_NOT_NULL], true)) { + return ''; + } + + if (\in_array($filter->operator()->value(), [FilterOperator::IN, FilterOperator::NOT_IN])) { + return '(:' . $filter->field()->value() . $this->countParams . ')'; + } + + return ':' . $filter->field()->value() . $this->countParams; } private function buildExpression(FilterInterface $filter) From 61be3966c6eee7ea68be8fed01566b0bc4d3d51a Mon Sep 17 00:00:00 2001 From: "cristian.almohalla" Date: Sat, 6 Nov 2021 16:11:00 +0100 Subject: [PATCH 2/2] add null operator support, refactor --- src/Criteria/DBALCriteriaVisitor.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Criteria/DBALCriteriaVisitor.php b/src/Criteria/DBALCriteriaVisitor.php index af9fd28..062497b 100644 --- a/src/Criteria/DBALCriteriaVisitor.php +++ b/src/Criteria/DBALCriteriaVisitor.php @@ -50,12 +50,12 @@ public function execute(Criteria $criteria): void public function visitAnd(AndFilter $filter): string { - return '( '. $this->buildExpression($filter->left()) .' AND '. $this->buildExpression($filter->right()) .' )'; + return '( ' . $this->buildExpression($filter->left()) . ' AND ' . $this->buildExpression($filter->right()) . ' )'; } public function visitOr(OrFilter $filter): string { - return '( '. $this->buildExpression($filter->left()) .' OR '. $this->buildExpression($filter->right()) .' )'; + return '( ' . $this->buildExpression($filter->left()) . ' OR ' . $this->buildExpression($filter->right()) . ' )'; } public function visitFilter(Filter $filter): string @@ -81,11 +81,13 @@ private function parameterExpression(Filter $filter): string return ''; } + $parameterName = ':' . $filter->field()->value() . $this->countParams; + if (\in_array($filter->operator()->value(), [FilterOperator::IN, FilterOperator::NOT_IN])) { - return '(:' . $filter->field()->value() . $this->countParams . ')'; + return '(' . $parameterName . ')'; } - return ':' . $filter->field()->value() . $this->countParams; + return $parameterName; } private function buildExpression(FilterInterface $filter)