Skip to content

Commit 4eba9dc

Browse files
committed
fix #2143
1 parent 91fde05 commit 4eba9dc

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,13 +1647,15 @@ public function getCompiledQBWhere()
16471647
*
16481648
* Allows the where clause, limit and offset to be added directly
16491649
*
1650-
* @param string|array $where
1651-
* @param integer $limit
1652-
* @param integer $offset
1650+
* @param string|array $where Where condition
1651+
* @param integer $limit Limit value
1652+
* @param integer $offset Offset value
1653+
* @param boolean $returnSQL If true, returns the generate SQL, otherwise executes the query.
1654+
* @param boolean $reset Are we want to clear query builder values?
16531655
*
16541656
* @return ResultInterface
16551657
*/
1656-
public function getWhere($where = null, int $limit = null, int $offset = null)
1658+
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $returnSQL = false, bool $reset = true)
16571659
{
16581660
if ($where !== null)
16591661
{
@@ -1665,8 +1667,17 @@ public function getWhere($where = null, int $limit = null, int $offset = null)
16651667
$this->limit($limit, $offset);
16661668
}
16671669

1668-
$result = $this->db->query($this->compileSelect(), $this->binds, false);
1669-
$this->resetSelect();
1670+
$result = $returnSQL
1671+
? $this->getCompiledSelect($reset)
1672+
: $this->db->query($this->compileSelect(), $this->binds, false);
1673+
1674+
if ($reset === true)
1675+
{
1676+
$this->resetSelect();
1677+
1678+
// Clear our binds so we don't eat up memory
1679+
$this->binds = [];
1680+
}
16701681

16711682
return $result;
16721683
}

tests/system/Database/Builder/GetTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,60 @@ public function testGetWithReset()
4444
$this->assertEquals($expectedSQLafterreset, str_replace("\n", ' ', $builder->get(0, 50, true, true)));
4545
}
4646

47+
//--------------------------------------------------------------------
48+
49+
/**
50+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2143
51+
*/
52+
public function testGetWhereWithLimit()
53+
{
54+
$builder = $this->db->table('users');
55+
56+
$expectedSQL = 'SELECT * FROM "users" WHERE "username" = \'bogus\' LIMIT 5';
57+
$expectedSQLWithoutReset = 'SELECT * FROM "users" WHERE "username" = \'bogus\' AND "username" = \'bogus\' LIMIT 5';
58+
59+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, null, true, false)));
60+
$this->assertEquals($expectedSQLWithoutReset, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 0, true, true)));
61+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, null, true, true)));
62+
}
63+
64+
//--------------------------------------------------------------------
65+
66+
public function testGetWhereWithLimitAndOffset()
67+
{
68+
$builder = $this->db->table('users');
69+
70+
$expectedSQL = 'SELECT * FROM "users" WHERE "username" = \'bogus\' LIMIT 10, 5';
71+
$expectedSQLWithoutReset = 'SELECT * FROM "users" WHERE "username" = \'bogus\' AND "username" = \'bogus\' LIMIT 10, 5';
72+
73+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 10, true, false)));
74+
$this->assertEquals($expectedSQLWithoutReset, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 10, true, true)));
75+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], 5, 10, true, true)));
76+
}
77+
78+
//--------------------------------------------------------------------
79+
80+
public function testGetWhereWithWhereConditionOnly()
81+
{
82+
$builder = $this->db->table('users');
83+
84+
$expectedSQL = 'SELECT * FROM "users" WHERE "username" = \'bogus\'';
85+
$expectedSQLWithoutReset = 'SELECT * FROM "users" WHERE "username" = \'bogus\' AND "username" = \'bogus\'';
86+
87+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], null, null, true, false)));
88+
$this->assertEquals($expectedSQLWithoutReset, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], null, null, true, true)));
89+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(['username' => 'bogus'], null, null, true, true)));
90+
}
91+
92+
//--------------------------------------------------------------------
93+
94+
public function testGetWhereWithoutArgs()
95+
{
96+
$builder = $this->db->table('users');
97+
98+
$expectedSQL = 'SELECT * FROM "users"';
99+
100+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getWhere(null, null, null, true, true)));
101+
}
102+
47103
}

0 commit comments

Comments
 (0)