Skip to content
Permalink
Browse files

Add whereIn() and whereNotIn() and allow to behave as intuitively exp…

…ected.
  • Loading branch information...
dereuromark committed Mar 2, 2018
1 parent 48b500e commit 6075bc4bc498e3fe7704bcfcef51629d20f9b26a
Showing with 150 additions and 0 deletions.
  1. +40 −0 src/Database/Query.php
  2. +110 −0 tests/TestCase/Database/QueryTest.php
@@ -889,6 +889,46 @@ public function where($conditions = null, $types = [], $overwrite = false)
return $this;
}
/**
* Adds an IN condition or set of conditions to be used in the WHERE clause for this
* query.
*
* This method does allow empty inputs in contrast to where().
* Be careful about using it without proper sanity checks.
*
* @param string $field Field
* @param array $values Array of values
* @return $this
*/
public function whereIn($field, array $values)
{
if (!$values) {
return $this->where('1=0');
}
return $this->where([$field . ' IN' => $values]);
}
/**
* Adds a NOT IN condition or set of conditions to be used in the WHERE clause for this
* query.
*
* This method does allow empty inputs in contrast to where().
* Be careful about using it without proper sanity checks.
*
* @param string $field Field
* @param array $values Array of values
* @return $this
*/
public function whereNotIn($field, array $values)
{
if (!$values) {
return $this->where('1=1');
}
return $this->where([$field . ' NOT IN' => $values]);
}
/**
* Connects any previously defined set of conditions to the provided list
* using the AND operator. This function accepts the conditions list in the same
@@ -36,6 +36,16 @@ class QueryTest extends TestCase
const AUTHOR_COUNT = 4;
const COMMENT_COUNT = 6;
/**
* @var \Cake\Database\Connection
*/
protected $connection;
/**
* @var bool
*/
protected $autoQuote;
public function setUp()
{
parent::setUp();
@@ -1703,6 +1713,106 @@ public function testSelectWhereNot2()
$result->closeCursor();
}
/**
* Tests whereIn() and its input types.
*
* @return void
*/
public function testWhereIn()
{
$this->loadFixtures('Articles');
$query = new Query($this->connection);
$query->select(['id'])
->from('articles')
->whereIn('id', [2, 3])
->execute();
$sql = $query->sql();
$result = $query->execute();
$this->assertEquals(['id' => '2'], $result->fetch('assoc'));
$this->assertQuotedQuery(
'SELECT <id> FROM <articles> WHERE <id> in \\(:c0,:c1\\)',
$sql,
!$this->autoQuote
);
}
/**
* Tests whereIn() and empty array input.
*
* @return void
*/
public function testWhereInEmpty()
{
$this->loadFixtures('Articles');
$query = new Query($this->connection);
$query->select(['id'])
->from('articles')
->whereIn('id', [])
->execute();
$sql = $query->sql();
$result = $query->execute();
$this->assertFalse($result->fetch('assoc'));
$this->assertQuotedQuery(
'SELECT <id> FROM <articles> WHERE 1=0',
$sql,
!$this->autoQuote
);
}
/**
* Tests whereNotIn() and its input types.
*
* @return void
*/
public function testWhereNotIn()
{
$this->loadFixtures('Articles');
$query = new Query($this->connection);
$query->select(['id'])
->from('articles')
->whereNotIn('id', [1, 3])
->execute();
$sql = $query->sql();
$result = $query->execute();
$this->assertEquals(['id' => '2'], $result->fetch('assoc'));
$this->assertQuotedQuery(
'SELECT <id> FROM <articles> WHERE <id> not in \\(:c0,:c1\\)',
$sql,
!$this->autoQuote
);
}
/**
* Tests whereNotIn() and empty array input.
*
* @return void
*/
public function testWhereNotInEmpty()
{
$this->loadFixtures('Articles');
$query = new Query($this->connection);
$query->select(['id'])
->from('articles')
->whereNotIn('id', [])
->execute();
$sql = $query->sql();
$result = $query->execute();
$this->assertEquals(['id' => '1'], $result->fetch('assoc'));
$this->assertQuotedQuery(
'SELECT <id> FROM <articles> WHERE 1=1',
$sql,
!$this->autoQuote
);
}
/**
* Tests order() method both with simple fields and expressions
*

0 comments on commit 6075bc4

Please sign in to comment.
You can’t perform that action at this time.