Skip to content

Commit

Permalink
implementing and_ and or_ in QueryExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 1, 2013
1 parent 8a44c05 commit a0206f6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Cake/Model/Datasource/Database/Expression/QueryExpression.php
Expand Up @@ -98,6 +98,20 @@ public function notIn($field, $values, $type = null) {
return $this->add([$field . ' NOT IN' => $values], $type ? [$field => $type] : []);
}

public function and_($conditions, $types = []) {
if (is_callable($conditions)) {
return $conditions(new self);
}
return new self($conditions, $types);
}

public function or_($conditions, $types = []) {

This comment has been minimized.

Copy link
@ADmad

ADmad Dec 28, 2013

Member

Needs api docblock.

if (is_callable($conditions)) {
return $conditions(new self([], [], 'OR'));
}
return new self($conditions, $types, 'OR');
}

/**
* Associates a query placeholder to a value and a type for next execution
*
Expand Down
73 changes: 73 additions & 0 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -735,4 +735,77 @@ public function testSelectWhereOperatorMethods() {
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
}

/**
* Tests nesting query expressions both using arrays and closures
*
* @return void
**/
public function testSelectExpressionComposition() {
$this->_insertDateRecords();
$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) {
$and = $exp->and_(['id' => 2, 'id >' => 1]);
return $exp->add($and);
})
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 2], $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) {
$and = $exp->and_(['id' => 2, 'id <' => 2]);
return $exp->add($and);
})
->execute();
$this->assertCount(0, $result);

$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) {
$and = $exp->and_(function($and) {
return $and->eq('id', 1)->gt('id', 0);
});
return $exp->add($and);
})
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) {
$or = $exp->or_(['id' => 1]);
$and = $exp->and_(['id >' => 2, 'id <' => 4]);
return $or->add($and);
})
->execute();
$this->assertCount(2, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) {
$or = $exp->or_(function($or) {
return $or->eq('id', 1)->eq('id', 2);
});
return $or;
})
->execute();
$this->assertCount(2, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
}
}

0 comments on commit a0206f6

Please sign in to comment.