Skip to content

Commit

Permalink
Merge pull request #68 from malteriesch/master
Browse files Browse the repository at this point in the history
added where::asLiteral
  • Loading branch information
nilportugues committed Nov 16, 2015
2 parents e3d8c7c + 65ae980 commit cf7fc7b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Builder/Syntax/WhereWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
12 changes: 12 additions & 0 deletions src/Syntax/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/Builder/Syntax/WhereWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
9 changes: 9 additions & 0 deletions tests/Syntax/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit cf7fc7b

Please sign in to comment.