Skip to content

added where::asLiteral #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}