Skip to content

Commit

Permalink
Add NotEqualComparison to allow simple NOT NULL conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlokorte committed Apr 25, 2012
1 parent 9c04d4c commit 891f3e4
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Dataphant/Adapters/SqlAdapterBase.php
Expand Up @@ -67,6 +67,7 @@ abstract class SqlAdapterBase extends AdapterBase implements SchematicDatabaseIn
*/
static protected $comparisons = array(
'Dataphant\Query\Comparisons\EqualToComparison' => ' = ',
'Dataphant\Query\Comparisons\NotEqualToComparison' => ' <> ',
'Dataphant\Query\Comparisons\LessThanOrEqualToComparison' => ' <= ',
'Dataphant\Query\Comparisons\LessThanComparison' => ' < ',
'Dataphant\Query\Comparisons\GreaterThanOrEqualToComparison' => ' >= ',
Expand All @@ -76,6 +77,12 @@ abstract class SqlAdapterBase extends AdapterBase implements SchematicDatabaseIn
);


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',
Expand Down Expand Up @@ -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();

Expand All @@ -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) . ')';

}

Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Dataphant/ComparableInterface.php
Expand Up @@ -25,6 +25,14 @@ interface ComparableInterface
*/
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
*
Expand Down
7 changes: 7 additions & 0 deletions src/Dataphant/Properties/PropertyBase.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions 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);
}

}
6 changes: 6 additions & 0 deletions src/Dataphant/Query/Path.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/Dataphant/Relationships/OneToOneRelationship.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Dataphant/Relationships/RelationshipBase.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/Dataphant/Tests/Properties/PropertyBaseTest.php
Expand Up @@ -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');
Expand Down
129 changes: 129 additions & 0 deletions 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.