Browse files

Add NotEqualComparison to allow simple NOT NULL conditions

  • Loading branch information...
1 parent 9c04d4c commit 891f3e497fa56823f053ee55de90af358a1fc2fa @laszlokorte committed Apr 25, 2012
View
22 src/Dataphant/Adapters/SqlAdapterBase.php
@@ -67,6 +67,7 @@
*/
static protected $comparisons = array(
'Dataphant\Query\Comparisons\EqualToComparison' => ' = ',
+ 'Dataphant\Query\Comparisons\NotEqualToComparison' => ' <> ',
'Dataphant\Query\Comparisons\LessThanOrEqualToComparison' => ' <= ',
'Dataphant\Query\Comparisons\LessThanComparison' => ' < ',
'Dataphant\Query\Comparisons\GreaterThanOrEqualToComparison' => ' >= ',
@@ -76,6 +77,12 @@
);
+ static protected $nullComparisons = array(
+ 'Dataphant\Query\Comparisons\EqualToComparison' => ' IS ',
+ 'Dataphant\Query\Comparisons\NotEqualToComparison' => ' IS NOT '
+ );
+
+
static protected $aggregators = array(
'Dataphant\\Query\\Aggregators\\CountAggregator' => 'COUNT',
'Dataphant\\Query\\Aggregators\\MaximumAggregator' => 'MAX',
@@ -626,14 +633,15 @@ protected function operationStatement($operation)
*/
protected function comparisonStatement($comparison)
{
- if ($comparison instanceof InEnumComparison && count($comparison->getValue()) < 1)
+ $value = $comparison->getValue();
+
+ if ($comparison instanceof InEnumComparison && count($value) < 1)
{
return 'NULL';
}
elseif($comparison->isComparingRelationship())
{
$relationship = $comparison->getSubject();
- $value = $comparison->getValue();
$targetKeys = $relationship->getInverse()->getTargetKeys();
@@ -647,8 +655,12 @@ protected function comparisonStatement($comparison)
return $this->operationStatement($conditions);
}
-
- return '(' . $this->operand($comparison->getSubject()) . static::$comparisons[get_class($comparison)] . $this->operand($comparison->getValue()) . ')';
+ if($value === NULL) {
+ $operand = static::$nullComparisons[get_class($comparison)];
+ } else {
+ $operand = static::$comparisons[get_class($comparison)];
+ }
+ return '(' . $this->operand($comparison->getSubject()) . $operand . $this->operand($value) . ')';
}
@@ -771,7 +783,7 @@ protected function valueList($values)
*/
protected function operand($val)
{
- if(is_scalar($val))
+ if(is_scalar($val) || $val === NULL)
{
return $this->quote($val);
}
View
8 src/Dataphant/ComparableInterface.php
@@ -26,6 +26,14 @@
public function eq($value);
/**
+ * Not Equals
+ *
+ * @param mixed $value the value to compare with.
+ * @return NotEqualToComparison A comparison object containing the comparable object and the given value.
+ */
+ public function notEq($value);
+
+ /**
* Greater than
*
* @param integer $value the value to compare with
View
7 src/Dataphant/Properties/PropertyBase.php
@@ -22,6 +22,7 @@
use Dataphant\Query\Aggregators\CountAggregator;
use Dataphant\Query\Comparisons\EqualToComparison;
+use Dataphant\Query\Comparisons\NotEqualToComparison;
use Dataphant\Query\Comparisons\GreaterThanComparison;
use Dataphant\Query\Comparisons\GreaterThanOrEqualToComparison;
use Dataphant\Query\Comparisons\LessThanComparison;
@@ -468,6 +469,12 @@ public function eq($value)
}
+ public function notEq($value)
+ {
+ return new NotEqualToComparison($this, $value);
+ }
+
+
public function gt($value)
{
return new GreaterThanComparison($this, $value);
View
21 src/Dataphant/Query/Comparisons/NotEqualToComparison.php
@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of Dataphant.
+ *
+ * (c) Laszlo Korte <me@laszlokorte.de>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * and AUTHORS files that was distributed with this source code.
+ */
+
+namespace Dataphant\Query\Comparisons;
+
+class NotEqualToComparison extends ComparisonBase
+{
+ public function match($record)
+ {
+ return $this->getGivenValueFor($record) != $this->getNeededValueFor($record);
+ }
+
+}
View
6 src/Dataphant/Query/Path.php
@@ -12,6 +12,7 @@
namespace Dataphant\Query;
use Dataphant\Query\Comparisons\EqualToComparison;
+use Dataphant\Query\Comparisons\NotEqualToComparison;
use Dataphant\Query\Comparisons\GreaterThanComparison;
use Dataphant\Query\Comparisons\GreaterThanOrEqualToComparison;
use Dataphant\Query\Comparisons\LessThanComparison;
@@ -218,6 +219,11 @@ public function eq($value)
return new EqualToComparison($this, $value);
}
+ public function notEq($value)
+ {
+ return new NotEqualToComparison($this, $value);
+ }
+
public function gt($value)
{
return new GreaterThanComparison($this, $value);
View
6 src/Dataphant/Relationships/OneToOneRelationship.php
@@ -12,6 +12,7 @@
namespace Dataphant\Relationships;
use Dataphant\Query\Comparisons\EqualToComparison;
+use Dataphant\Query\Comparisons\NotEqualToComparison;
use Dataphant\Query\Comparisons\GreaterThanComparison;
use Dataphant\Query\Comparisons\GreaterThanOrEqualToComparison;
use Dataphant\Query\Comparisons\LessThanComparison;
@@ -151,6 +152,11 @@ public function eq($value)
return new EqualToComparison($this, $value);
}
+ public function notEq($value)
+ {
+ return new NotEqualToComparison($this, $value);
+ }
+
public function gt($value)
{
View
7 src/Dataphant/Relationships/RelationshipBase.php
@@ -19,6 +19,7 @@
use Dataphant\CollectionInterface;
use Dataphant\Query\Comparisons\EqualToComparison;
+use Dataphant\Query\Comparisons\NotEqualToComparison;
use Dataphant\Query\Comparisons\GreaterThanComparison;
use Dataphant\Query\Comparisons\GreaterThanOrEqualToComparison;
use Dataphant\Query\Comparisons\LessThanComparison;
@@ -472,6 +473,12 @@ public function eq($value)
}
+ public function notEq($value)
+ {
+ return new NotEqualToComparison($this, $value);
+ }
+
+
public function gt($value)
{
return new GreaterThanComparison($this, $value);
View
11 tests/Dataphant/Tests/Properties/PropertyBaseTest.php
@@ -158,6 +158,17 @@ public function testEqualityComparisonCanBePerformed()
$this->assertSame($value, $operator->getValue());
}
+ public function testInequalityComparisonCanBePerformed()
+ {
+ $property = $this->getPropertyMock('User', 'password');
+
+ $value = 'geheim';
+ $operator = $property->notEq($value);
+ $this->assertInstanceOf('Dataphant\Query\Comparisons\NotEqualToComparison', $operator);
+ $this->assertSame($property, $operator->getSubject());
+ $this->assertSame($value, $operator->getValue());
+ }
+
public function testGreaterThanComparisonCanBePerformed()
{
$property = $this->getPropertyMock('User', 'age');
View
129 tests/Dataphant/Tests/Query/Comparisons/NotEqualToComparisonTest.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace Dataphant\Tests\Query\Comparisons;
+
+use Dataphant\Tests\Query\QueryBaseTestCase;
+
+use Dataphant\Query\Comparisons\NotEqualToComparison;
+use Dataphant\Query\Operations;
+
+class NotEqualToComparisonTest extends QueryBaseTestCase
+{
+ public function testRecordsPropertyMatchesNumericValueIfEqual()
+ {
+ $compareValue = 23;
+ $realValue = 23;
+ $this->property->expects($this->any())
+ ->method('getValueFor')
+ ->will($this->returnValue($realValue));
+
+ $comparison = new NotEqualToComparison($this->property, $compareValue);
+
+ $this->assertFalse($comparison->match($this->record));
+ }
+
+ public function testRecordsPropertyMatchesStringValueIfEqual()
+ {
+ $compareValue = 'FailWhale';
+ $realValue = 'FailWhale';
+ $this->property->expects($this->any())
+ ->method('getValueFor')
+ ->will($this->returnValue($realValue));
+
+ $comparison = new NotEqualToComparison($this->property, $compareValue);
+
+ $this->assertFalse($comparison->match($this->record));
+ }
+
+ public function testRecordsPropertyDoesNotMatchNumericValueIfGreater()
+ {
+ $compareValue = 23;
+ $realValue = 42;
+ $this->property->expects($this->any())
+ ->method('getValueFor')
+ ->will($this->returnValue($realValue));
+
+ $comparison = new NotEqualToComparison($this->property, $compareValue);
+
+ $this->assertTrue($comparison->match($this->record));
+ }
+
+ public function testRecordsPropertyDoesNotMatchNumericValueIfLess()
+ {
+ $compareValue = 8;
+ $realValue = 4;
+ $this->property->expects($this->any())
+ ->method('getValueFor')
+ ->will($this->returnValue($realValue));
+
+ $comparison = new NotEqualToComparison($this->property, $compareValue);
+
+ $this->assertTrue($comparison->match($this->record));
+ }
+
+ public function testRecordsPropertyDoesNotMatchStringValueIfNotEqual()
+ {
+ $compareValue = 'RapidRaptor';
+ $realValue = 'FailWhale';
+ $this->property->expects($this->any())
+ ->method('getValueFor')
+ ->will($this->returnValue($realValue));
+
+ $comparison = new NotEqualToComparison($this->property, $compareValue);
+
+ $this->assertTrue($comparison->match($this->record));
+ }
+
+
+ public function testIsValidByDefault()
+ {
+ $comparison = new NotEqualToComparison($this->property, 16);
+ $this->assertTrue($comparison->isValid());
+ }
+
+ public function testLogicalAndCompositionWithOtherConditionCanBeBuilt()
+ {
+ $model = $this->getFakeModel();
+ $query = $this->adapter->getNewQuery($this->dataSource, $model);
+ $comp = new NotEqualToComparison('3', '4');
+ $op = new Operations\AndOperation(array($comp, $comp));
+ $query->setConditions($op);
+
+ $this->adapter->read($query);
+ $sql = "SELECT ";
+ $sql .= "\"users\".\"id\" AS \"id\", \"users\".\"nickname\" AS \"nickname\" FROM \"users\" ";
+ $sql .= "WHERE (('3' <> '4') AND ('3' <> '4'))";
+ $this->assertSame($sql, $this->adapter->getLastStatement());
+ }
+
+ public function testLogicalOrCompositionWithOtherConditionCanBeBuilt()
+ {
+ $model = $this->getFakeModel();
+ $query = $this->adapter->getNewQuery($this->dataSource, $model);
+ $comp = new NotEqualToComparison('3', '4');
+ $op = new Operations\OrOperation(array($comp, $comp));
+ $query->setConditions($op);
+
+ $this->adapter->read($query);
+ $sql = "SELECT ";
+ $sql .= "\"users\".\"id\" AS \"id\", \"users\".\"nickname\" AS \"nickname\" FROM \"users\" ";
+ $sql .= "WHERE (('3' <> '4') OR ('3' <> '4'))";
+ $this->assertSame($sql, $this->adapter->getLastStatement());
+ }
+
+ public function testLogicalAndNotCompositionWithOtherConditionCanBeBuilt()
+ {
+ $model = $this->getFakeModel();
+ $query = $this->adapter->getNewQuery($this->dataSource, $model);
+ $comp = new NotEqualToComparison('3', '4');
+ $notop = new Operations\NotOperation($comp);
+ $op = new Operations\AndOperation(array($comp, $notop));
+ $query->setConditions($op);
+
+ $this->adapter->read($query);
+ $sql = "SELECT ";
+ $sql .= "\"users\".\"id\" AS \"id\", \"users\".\"nickname\" AS \"nickname\" FROM \"users\" ";
+ $sql .= "WHERE (('3' <> '4') AND (NOT ('3' <> '4')))";
+ $this->assertSame($sql, $this->adapter->getLastStatement());
+ }
+}

0 comments on commit 891f3e4

Please sign in to comment.