Skip to content

Commit

Permalink
Merge pull request #2232 from michalsn/feature/fix_getWhere
Browse files Browse the repository at this point in the history
Fixes BaseBuilder getWhere() bug
  • Loading branch information
MGatner authored Sep 24, 2019
2 parents 23fd6bb + 5d8ab18 commit 3a0b8df
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
23 changes: 17 additions & 6 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1894,13 +1894,15 @@ public function getCompiledQBWhere()
*
* Allows the where clause, limit and offset to be added directly
*
* @param string|array $where
* @param integer $limit
* @param integer $offset
* @param string|array $where Where condition
* @param integer $limit Limit value
* @param integer $offset Offset value
* @param boolean $returnSQL If true, returns the generate SQL, otherwise executes the query.
* @param boolean $reset Are we want to clear query builder values?
*
* @return ResultInterface
*/
public function getWhere($where = null, int $limit = null, int $offset = null)
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $returnSQL = false, bool $reset = true)
{
if ($where !== null)
{
Expand All @@ -1912,8 +1914,17 @@ public function getWhere($where = null, int $limit = null, int $offset = null)
$this->limit($limit, $offset);
}

$result = $this->db->query($this->compileSelect(), $this->binds, false);
$this->resetSelect();
$result = $returnSQL
? $this->getCompiledSelect($reset)
: $this->db->query($this->compileSelect(), $this->binds, false);

if ($reset === true)
{
$this->resetSelect();

// Clear our binds so we don't eat up memory
$this->binds = [];
}

return $result;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/system/Database/Builder/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,60 @@ public function testGetWithReset()
$this->assertEquals($expectedSQLafterreset, str_replace("\n", ' ', $builder->get(0, 50, true, true)));
}

//--------------------------------------------------------------------

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2143
*/
public function testGetWhereWithLimit()
{
$builder = $this->db->table('users');

$expectedSQL = 'SELECT * FROM "users" WHERE "username" = \'bogus\' LIMIT 5';
$expectedSQLWithoutReset = 'SELECT * FROM "users" WHERE "username" = \'bogus\' AND "username" = \'bogus\' LIMIT 5';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, null, true, false)));
$this->assertEquals($expectedSQLWithoutReset, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 0, true, true)));
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, null, true, true)));
}

//--------------------------------------------------------------------

public function testGetWhereWithLimitAndOffset()
{
$builder = $this->db->table('users');

$expectedSQL = 'SELECT * FROM "users" WHERE "username" = \'bogus\' LIMIT 10, 5';
$expectedSQLWithoutReset = 'SELECT * FROM "users" WHERE "username" = \'bogus\' AND "username" = \'bogus\' LIMIT 10, 5';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 10, true, false)));
$this->assertEquals($expectedSQLWithoutReset, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 10, true, true)));
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 10, true, true)));
}

//--------------------------------------------------------------------

public function testGetWhereWithWhereConditionOnly()
{
$builder = $this->db->table('users');

$expectedSQL = 'SELECT * FROM "users" WHERE "username" = \'bogus\'';
$expectedSQLWithoutReset = 'SELECT * FROM "users" WHERE "username" = \'bogus\' AND "username" = \'bogus\'';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], null, null, true, false)));
$this->assertEquals($expectedSQLWithoutReset, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], null, null, true, true)));
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], null, null, true, true)));
}

//--------------------------------------------------------------------

public function testGetWhereWithoutArgs()
{
$builder = $this->db->table('users');

$expectedSQL = 'SELECT * FROM "users"';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(null, null, null, true, true)));
}

}
4 changes: 3 additions & 1 deletion user_guide_src/source/database/query_builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1256,11 +1256,13 @@ Class Reference
Compiles and runs SELECT statement based on the already
called Query Builder methods.

.. php:method:: getWhere([$where = NULL[, $limit = NULL[, $offset = NULL]]])
.. php:method:: getWhere([$where = NULL[, $limit = NULL[, $offset = NULL[, $returnSQL = FALSE[, $reset = TRUE]]]]])
:param string $where: The WHERE clause
:param int $limit: The LIMIT clause
:param int $offset: The OFFSET clause
:param bool $returnSQL: If true, returns the generate SQL, otherwise executes the query.
:param bool $reset: Do we want to clear query builder values?
:returns: \CodeIgniter\Database\ResultInterface instance (method chaining)
:rtype: \CodeIgniter\Database\ResultInterface

Expand Down

0 comments on commit 3a0b8df

Please sign in to comment.