diff --git a/src/Builder/Syntax/WhereWriter.php b/src/Builder/Syntax/WhereWriter.php index e98fb3a..d2a3223 100644 --- a/src/Builder/Syntax/WhereWriter.php +++ b/src/Builder/Syntax/WhereWriter.php @@ -196,6 +196,10 @@ protected function writeWhereComparisons(Where $where, array &$whereArray) $comparisons, function (&$comparison) { + if (!is_array($comparison)) { + return; + } + $str = $this->writeWherePartialCondition($comparison['subject']); $str .= $this->writer->writeConjunction($comparison['conjunction']); $str .= $this->writeWherePartialCondition($comparison['target']); diff --git a/src/Syntax/Where.php b/src/Syntax/Where.php index 0897dbe..5cc4558 100644 --- a/src/Syntax/Where.php +++ b/src/Syntax/Where.php @@ -382,6 +382,18 @@ protected function genericMatch(array &$columns, array &$values, $mode) return $this; } + /** + * @param string $literal + * + * @return $this + */ + public function asLiteral($literal) + { + $this->comparisons[] = $literal; + + return $this; + } + /** * @param string[] $columns * @param mixed[] $values diff --git a/tests/Builder/Syntax/WhereWriterTest.php b/tests/Builder/Syntax/WhereWriterTest.php index 949c3c5..5af2dd7 100644 --- a/tests/Builder/Syntax/WhereWriterTest.php +++ b/tests/Builder/Syntax/WhereWriterTest.php @@ -520,4 +520,23 @@ public function itShouldBeAbleToDoWhereNotExists() $expected = array(':v1' => 'Nil', ':v2' => 1); $this->assertEquals($expected, $this->writer->getValues()); } + + /** + * @test + */ + public function itShouldAllowWhereConditionAsLiteral() + { + $this->query + ->setTable('user') + ->where() + ->asLiteral("(username is not null and status=:status)") + ->notEquals('name', '%N%'); + + $expected = "SELECT user.* FROM user WHERE (username is not null and status=:status) AND (user.name <> :v1)"; + + $this->assertSame($expected, $this->writer->write($this->query)); + + $expected = array(':v1' => '%N%'); + $this->assertEquals($expected, $this->writer->getValues()); + } } diff --git a/tests/Syntax/WhereTest.php b/tests/Syntax/WhereTest.php index 3329ab8..0f74767 100644 --- a/tests/Syntax/WhereTest.php +++ b/tests/Syntax/WhereTest.php @@ -407,4 +407,13 @@ public function itShouldSetNotExistsCondition() $this->assertEquals(array($select1), $result); } + + /** + * @test + */ + public function itShouldReturnLiterals() + { + $result = $this->where->asLiteral("(username is not null and status=:status)")->getComparisons(); + $this->assertSame("(username is not null and status=:status)", $result[0]); + } }