Skip to content

Commit

Permalink
Add whereIn() and whereNotIn() and allow to behave as intuitively exp…
Browse files Browse the repository at this point in the history
…ected.
  • Loading branch information
dereuromark committed Mar 2, 2018
1 parent 48b500e commit 6075bc4
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Database/Query.php
Expand Up @@ -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
Expand Down
110 changes: 110 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 6075bc4

Please sign in to comment.