Skip to content
Permalink
Browse files

Translating IS operator to the best possible operator depending on value

  • Loading branch information...
lorenzo committed Jul 23, 2014
1 parent d227e15 commit f15a6f58d52d46316081e3228fdb0d15f10618c1
Showing with 33 additions and 1 deletion.
  1. +10 −1 src/Database/Expression/QueryExpression.php
  2. +23 −0 tests/TestCase/Database/QueryTest.php
@@ -488,9 +488,10 @@ protected function _parseCondition($field, $value) {
$type = $this->typeMap()->type($expression);
$multi = false;
$operator = strtolower(trim($operator));
$typeMultiple = strpos($type, '[]') !== false;
if (in_array(strtolower(trim($operator)), ['in', 'not in']) || $typeMultiple) {
if (in_array($operator, ['in', 'not in']) || $typeMultiple) {
$type = $type ?: 'string';
$type .= $typeMultiple ? null : '[]';
$operator = $operator === '=' ? 'IN' : $operator;
@@ -502,6 +503,14 @@ protected function _parseCondition($field, $value) {
$value = $value instanceof ExpressionInterface ? $value : (array)$value;
}
if ($operator === 'is' && $value === null) {
return new UnaryExpression('IS NULL', $expression, UnaryExpression::POSTFIX);
}
if ($operator === 'is' && $value !== null) {
$operator = '=';
}
return new Comparison($expression, $value, $type, $operator);
}
@@ -2693,6 +2693,29 @@ public function testIsNullAutoQuoting() {
$this->assertQuotedQuery('WHERE \(<field>\) IS NOT NULL', $query->sql());
}
/**
* Tests that using the IS operator will automatically translate to the best
* possible operator depending on the passed value
*
* @return void
*/
public function testDirectIsNull() {
$sql = (new Query($this->connection))
->select(['name'])
->from(['authors'])
->where(['name IS' => null])
->sql();
$this->assertQuotedQuery('WHERE \(<name>\) IS NULL', $sql, true);
$results = (new Query($this->connection))
->select(['name'])
->from(['authors'])
->where(['name IS' => 'larry'])
->execute();
$this->assertCount(1, $results);
$this->assertEquals(['name' => 'larry'], $results->fetch('assoc'));
}
/**
* Assertion for comparing a table's contents with what is in it.
*

0 comments on commit f15a6f5

Please sign in to comment.
You can’t perform that action at this time.