Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Sep 19, 2019
1 parent 91fde05 commit 4eba9dc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
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)));
}

}

0 comments on commit 4eba9dc

Please sign in to comment.