Skip to content

Commit

Permalink
Added methods to programatically generate conditions in QueryExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 1, 2013
1 parent 34ff255 commit 2d0182f
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 23 deletions.
Expand Up @@ -45,10 +45,50 @@ public function add($conditions, $types = []) {
return $this;
}

public function equals($field, $value, $type = null) {
public function eq($field, $value, $type = null) {
return $this->add([$field => $value], $type ? [$field => $type] : []);
}

public function notEq($field, $value, $type = null) {
return $this->add([$field . ' !=' => $value], $type ? [$field => $type] : []);
}

public function gt($field, $value, $type = null) {
return $this->add([$field . ' >' => $value], $type ? [$field => $type] : []);
}

public function lt($field, $value, $type = null) {
return $this->add([$field . ' <' => $value], $type ? [$field => $type] : []);
}

public function gte($field, $value, $type = null) {
return $this->add([$field . ' >=' => $value], $type ? [$field => $type] : []);
}

public function lte($field, $value, $type = null) {
return $this->add([$field . ' <=' => $value], $type ? [$field => $type] : []);
}

public function isNull($field) {
return $this->add($field . ' IS NULL');
}

public function isNotNull($field) {
return $this->add($field . ' IS NOT NULL');
}

public function like($field, $value, $type = null) {
return $this->add([$field . ' LIKE' => $value], $type ? [$field => $type] : []);
}

public function notLike($field, $value, $type = null) {
return $this->add([$field . ' NOT LIKE' => $value], $type ? [$field => $type] : []);
}

public function in($field, $values, $type = null) {
return $this->add([$field . ' IN' => $values], $type ? [$field => $type] : []);
}

/**
* Associates a query placeholder to a value and a type for next execution
*
Expand Down
140 changes: 118 additions & 22 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -194,15 +194,6 @@ public function testSelectWhereOperators() {
$this->assertCount(1, $result);
$this->assertEquals(array('title' => 'a title'), $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(['id <' => 2])
->execute();
$this->assertCount(1, $result);
$this->assertEquals(array('title' => 'a title'), $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['title'])
Expand Down Expand Up @@ -268,27 +259,30 @@ public function testSelectWhereOperators() {
* @return void
**/
protected function _insertDateRecords() {
$table = 'CREATE TEMPORARY TABLE dates(id int, name varchar(50), posted datetime)';
$table = 'CREATE TEMPORARY TABLE dates(id int, name varchar(50), posted datetime, visible char(1))';
$this->connection->execute($table);
$data = [
'id' => '1',
'name' => 'Chuck Norris',
'posted' => new \DateTime('2012-12-21 12:00')
'posted' => new \DateTime('2012-12-21 12:00'),
'visible' => 'Y'
];
$result = $this->connection->insert(
'dates',
$data,
['id' => 'integer', 'name' => 'string', 'posted' => 'datetime']
['id' => 'integer', 'name' => 'string', 'posted' => 'datetime', 'visible' => 'string']
);

$result->bindValue(1, '2', 'integer');
$result->bindValue(2, 'Bruce Lee');
$result->bindValue(3, new \DateTime('2012-12-22 12:00'), 'datetime');
$result->bindValue(4, 'N');
$result->execute();

$result->bindValue(1, 3, 'integer');
$result->bindValue(2, 'Jet Li');
$result->bindValue(3, new \DateTime('2012-12-25 12:00'), 'datetime');
$result->bindValue(4, null);
$result->execute();

return $result;
Expand Down Expand Up @@ -498,7 +492,7 @@ public function testSelectWhereUsingClosure() {
->select(['id'])
->from('dates')
->where(function($exp) {
return $exp->equals('id', 1);
return $exp->eq('id', 1);
})
->execute();
$this->assertCount(1, $result);
Expand All @@ -510,8 +504,8 @@ public function testSelectWhereUsingClosure() {
->from('dates')
->where(function($exp) {
return $exp
->equals('id', 1)
->equals('posted', new \DateTime('2012-12-21 12:00'), 'datetime');
->eq('id', 1)
->eq('posted', new \DateTime('2012-12-21 12:00'), 'datetime');
})
->execute();
$this->assertCount(1, $result);
Expand All @@ -523,8 +517,8 @@ public function testSelectWhereUsingClosure() {
->from('dates')
->where(function($exp) {
return $exp
->equals('id', 1)
->equals('posted', new \DateTime('2021-12-30 15:00'), 'datetime');
->eq('id', 1)
->eq('posted', new \DateTime('2021-12-30 15:00'), 'datetime');
})
->execute();
$this->assertCount(0, $result);
Expand All @@ -544,7 +538,7 @@ public function testSelectAndWhereUsingClosure() {
->from('dates')
->where(['id' => '1'])
->andWhere(function($exp) {
return $exp->equals('posted', new \DateTime('2012-12-21 12:00'), 'datetime');
return $exp->eq('posted', new \DateTime('2012-12-21 12:00'), 'datetime');
})
->execute();
$this->assertCount(1, $result);
Expand All @@ -556,7 +550,7 @@ public function testSelectAndWhereUsingClosure() {
->from('dates')
->where(['id' => '1'])
->andWhere(function($exp) {
return $exp->equals('posted', new \DateTime('2022-12-21 12:00'), 'datetime');
return $exp->eq('posted', new \DateTime('2022-12-21 12:00'), 'datetime');
})
->execute();
$this->assertCount(0, $result);
Expand All @@ -576,7 +570,7 @@ public function testSelectOrWhereUsingClosure() {
->from('dates')
->where(['id' => '1'])
->orWhere(function($exp) {
return $exp->equals('posted', new \DateTime('2012-12-22 12:00'), 'datetime');
return $exp->eq('posted', new \DateTime('2012-12-22 12:00'), 'datetime');
})
->execute();
$this->assertCount(2, $result);
Expand All @@ -590,12 +584,114 @@ public function testSelectOrWhereUsingClosure() {
->where(['id' => '1'])
->orWhere(function($exp) {
return $exp
->equals('posted', new \DateTime('2012-12-22 12:00'), 'datetime')
->equals('id', 3);
->eq('posted', new \DateTime('2012-12-22 12:00'), 'datetime')
->eq('id', 3);
})
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['id' => 1], $result->fetch('assoc'));
}

/**
* Tests using where conditions with operator methods
*
* @return void
**/
public function testSelectWhereOperatorMethods() {
$this->_insertTwoRecords();
$this->_insertDateRecords();

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->gt('id', 1); })
->execute();
$this->assertCount(1, $result);
$this->assertEquals(array('title' => 'another title'), $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->lt('id', 2); })
->execute();
$this->assertCount(1, $result);
$this->assertEquals(array('title' => 'a title'), $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->lte('id', 2); })
->execute();
$this->assertCount(2, $result);

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->gte('id', 1); })
->execute();
$this->assertCount(2, $result);

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->lte('id', 1); })
->execute();
$this->assertCount(1, $result);

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->notEq('id', 2); })
->execute();
$this->assertCount(1, $result);
$this->assertEquals(array('title' => 'a title'), $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->like('title', 'a title'); })
->execute();
$this->assertCount(1, $result);
$this->assertEquals(array('title' => 'a title'), $result->fetch('assoc'));

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->like('title', '%title%'); })
->execute();
$this->assertCount(2, $result);

$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(function($exp) { return $exp->notLike('title', '%title%'); })
->execute();
$this->assertCount(0, $result);

$query = new Query($this->connection);
$result = $query
->select(['id'])
->from('dates')
->where(function($exp) { return $exp->isNull('visible'); })
->execute();
$this->assertCount(1, $result);

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

}

0 comments on commit 2d0182f

Please sign in to comment.