Skip to content

Commit

Permalink
Do not allow CQL queries to contain null values
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed May 22, 2015
1 parent d32fa0c commit 50e6848
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Assembler/CQL/CqlAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use Packaged\QueryBuilder\Assembler\QueryAssembler;
use Packaged\QueryBuilder\Clause\CQL\AllowFilteringClause;
use Packaged\QueryBuilder\Clause\CQL\UsingClause;
use Packaged\QueryBuilder\Expression\FieldExpression;
use Packaged\QueryBuilder\Expression\TableExpression;
use Packaged\QueryBuilder\Expression\ValueExpression;
use Packaged\QueryBuilder\Exceptions\Assembler\CqlAssemblerException;
use Packaged\QueryBuilder\Predicate\BetweenPredicate;
use Packaged\QueryBuilder\Predicate\EqualPredicate;
use Packaged\QueryBuilder\Predicate\GreaterThanOrEqualPredicate;
use Packaged\QueryBuilder\Predicate\LessThanOrEqualPredicate;
use Packaged\QueryBuilder\Predicate\NotEqualPredicate;
use Packaged\QueryBuilder\Predicate\PredicateSet;
use Packaged\QueryBuilder\SelectExpression\AllSelectExpression;
use Packaged\QueryBuilder\SelectExpression\FieldSelectExpression;
Expand All @@ -18,6 +18,13 @@ class CqlAssembler extends QueryAssembler
{
public function assembleSegment($segment)
{
if(($segment instanceof EqualPredicate || $segment instanceof NotEqualPredicate)
&& $segment->isNullValue()
)
{
throw new CqlAssemblerException("Null is not available in CQL Queries");
}

if($segment instanceof FieldSelectExpression)
{
$segment->setAlias(null);
Expand Down
6 changes: 6 additions & 0 deletions src/Exceptions/Assembler/CqlAssemblerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Packaged\QueryBuilder\Exceptions\Assembler;

class CqlAssemblerException extends QueryBuilderAssemblerException
{
}
8 changes: 8 additions & 0 deletions src/Exceptions/Assembler/QueryBuilderAssemblerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Packaged\QueryBuilder\Exceptions\Assembler;

use Packaged\QueryBuilder\Exceptions\QueryBuilderException;

class QueryBuilderAssemblerException extends QueryBuilderException
{
}
6 changes: 6 additions & 0 deletions src/Exceptions/QueryBuilderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Packaged\QueryBuilder\Exceptions;

class QueryBuilderException extends \Exception
{
}
13 changes: 13 additions & 0 deletions tests/Assembler/CQL/CqlAssemblerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace Packaged\Tests\QueryBuilder\Assembler\CQL;

use Packaged\QueryBuilder\Assembler\CQL\CqlAssembler;
use Packaged\QueryBuilder\Builder\QueryBuilder;
use Packaged\QueryBuilder\Clause\WhereClause;
use Packaged\QueryBuilder\Exceptions\Assembler\CqlAssemblerException;
use Packaged\QueryBuilder\Expression\FieldExpression;
use Packaged\QueryBuilder\Expression\NumericExpression;
use Packaged\QueryBuilder\Expression\StringExpression;
Expand Down Expand Up @@ -66,6 +68,17 @@ public function testAllSelect()
$this->assertEquals('*', CqlAssembler::stringify($selector));
}

/**
* @expectedException \Packaged\QueryBuilder\Exceptions\Assembler\CqlAssemblerException
* @expectedExceptionMessage Null is not available in CQL Queries
*/
public function testEmptySelect()
{
CqlAssembler::stringify(
QueryBuilder::select()->from('table')->where(['id' => null])
);
}

public function testTableName()
{
$this->assertEquals(
Expand Down

0 comments on commit 50e6848

Please sign in to comment.