From bf621e49c20f9e0a9736ba9c77289efbeca98fea Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 23 Sep 2026 11:17:34 +0200 Subject: [PATCH] QueryBuilder: Also support expressions as filter value The previous support only covered the case when working with ipl-sql directly and using expressions as the actual condition and not only as value. This change now allows to use expressions as part of every other filter rule in place of the value being compared. --- src/QueryBuilder.php | 7 ++++++- tests/FilterProcessorTest.php | 11 +++++++++++ tests/WhereTest.php | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index e799681..fc158c6 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -445,7 +445,12 @@ public function buildCondition(array $condition, array &$values): string } } else { if ($value instanceof ExpressionInterface) { - $sql[] = $this->buildExpression($value, $values); + $stmt = $this->buildExpression($value, $values); + if (is_int($expression)) { + $sql[] = $stmt; + } else { + $sql[] = str_replace('?', $stmt, $expression); + } } elseif ($value instanceof Select) { $stmt = '(' . $this->assembleSelect($value, $values)[0] . ')'; if (is_int($expression)) { diff --git a/tests/FilterProcessorTest.php b/tests/FilterProcessorTest.php index 40d2acf..8174dad 100644 --- a/tests/FilterProcessorTest.php +++ b/tests/FilterProcessorTest.php @@ -3,6 +3,7 @@ namespace ipl\Tests\Sql; use ipl\Sql\Compat\FilterProcessor; +use ipl\Sql\Expression; use ipl\Sql\Filter\Exists; use ipl\Sql\Filter\In; use ipl\Sql\Filter\NotExists; @@ -116,4 +117,14 @@ public function testNotExistsToSql() FilterProcessor::assemblePredicate(new NotExists($select)) ); } + + public function testFilterWithExpressionToSql(): void + { + $expression = new Expression('NOW()'); + + $this->assertSame( + ['foo = ?' => $expression], + FilterProcessor::assemblePredicate(Filter::equal('foo', $expression)) + ); + } } diff --git a/tests/WhereTest.php b/tests/WhereTest.php index 0ac9aac..0a76aa6 100644 --- a/tests/WhereTest.php +++ b/tests/WhereTest.php @@ -198,6 +198,11 @@ public function testWhereWithExpression() $this->query->where($expression); $this->assertSql('WHERE c2 = ?', $this->query, [1]); + + $expression = new Expression('NOW()'); + $this->query->where(['c3 = ?' => $expression]); + + $this->assertSql('WHERE (c2 = ?) AND (c3 = NOW())', $this->query, [1]); } public function testWhereWithSelect()