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()