Skip to content

Commit cf7fc7b

Browse files
committed
Merge pull request #68 from malteriesch/master
added where::asLiteral
2 parents e3d8c7c + 65ae980 commit cf7fc7b

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/Builder/Syntax/WhereWriter.php

+4
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ protected function writeWhereComparisons(Where $where, array &$whereArray)
196196
$comparisons,
197197
function (&$comparison) {
198198

199+
if (!is_array($comparison)) {
200+
return;
201+
}
202+
199203
$str = $this->writeWherePartialCondition($comparison['subject']);
200204
$str .= $this->writer->writeConjunction($comparison['conjunction']);
201205
$str .= $this->writeWherePartialCondition($comparison['target']);

src/Syntax/Where.php

+12
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,18 @@ protected function genericMatch(array &$columns, array &$values, $mode)
382382
return $this;
383383
}
384384

385+
/**
386+
* @param string $literal
387+
*
388+
* @return $this
389+
*/
390+
public function asLiteral($literal)
391+
{
392+
$this->comparisons[] = $literal;
393+
394+
return $this;
395+
}
396+
385397
/**
386398
* @param string[] $columns
387399
* @param mixed[] $values

tests/Builder/Syntax/WhereWriterTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,23 @@ public function itShouldBeAbleToDoWhereNotExists()
520520
$expected = array(':v1' => 'Nil', ':v2' => 1);
521521
$this->assertEquals($expected, $this->writer->getValues());
522522
}
523+
524+
/**
525+
* @test
526+
*/
527+
public function itShouldAllowWhereConditionAsLiteral()
528+
{
529+
$this->query
530+
->setTable('user')
531+
->where()
532+
->asLiteral("(username is not null and status=:status)")
533+
->notEquals('name', '%N%');
534+
535+
$expected = "SELECT user.* FROM user WHERE (username is not null and status=:status) AND (user.name <> :v1)";
536+
537+
$this->assertSame($expected, $this->writer->write($this->query));
538+
539+
$expected = array(':v1' => '%N%');
540+
$this->assertEquals($expected, $this->writer->getValues());
541+
}
523542
}

tests/Syntax/WhereTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,13 @@ public function itShouldSetNotExistsCondition()
407407

408408
$this->assertEquals(array($select1), $result);
409409
}
410+
411+
/**
412+
* @test
413+
*/
414+
public function itShouldReturnLiterals()
415+
{
416+
$result = $this->where->asLiteral("(username is not null and status=:status)")->getComparisons();
417+
$this->assertSame("(username is not null and status=:status)", $result[0]);
418+
}
410419
}

0 commit comments

Comments
 (0)