Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes BaseBuilder getWhere() bug #2232

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1647,13 +1647,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 @@ -1665,8 +1667,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 @@ -1084,11 +1084,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