Skip to content

Commit 3a0b8df

Browse files
authored
Merge pull request #2232 from michalsn/feature/fix_getWhere
Fixes BaseBuilder getWhere() bug
2 parents 23fd6bb + 5d8ab18 commit 3a0b8df

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

system/Database/BaseBuilder.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,13 +1894,15 @@ public function getCompiledQBWhere()
18941894
*
18951895
* Allows the where clause, limit and offset to be added directly
18961896
*
1897-
* @param string|array $where
1898-
* @param integer $limit
1899-
* @param integer $offset
1897+
* @param string|array $where Where condition
1898+
* @param integer $limit Limit value
1899+
* @param integer $offset Offset value
1900+
* @param boolean $returnSQL If true, returns the generate SQL, otherwise executes the query.
1901+
* @param boolean $reset Are we want to clear query builder values?
19001902
*
19011903
* @return ResultInterface
19021904
*/
1903-
public function getWhere($where = null, int $limit = null, int $offset = null)
1905+
public function getWhere($where = null, int $limit = null, ?int $offset = 0, bool $returnSQL = false, bool $reset = true)
19041906
{
19051907
if ($where !== null)
19061908
{
@@ -1912,8 +1914,17 @@ public function getWhere($where = null, int $limit = null, int $offset = null)
19121914
$this->limit($limit, $offset);
19131915
}
19141916

1915-
$result = $this->db->query($this->compileSelect(), $this->binds, false);
1916-
$this->resetSelect();
1917+
$result = $returnSQL
1918+
? $this->getCompiledSelect($reset)
1919+
: $this->db->query($this->compileSelect(), $this->binds, false);
1920+
1921+
if ($reset === true)
1922+
{
1923+
$this->resetSelect();
1924+
1925+
// Clear our binds so we don't eat up memory
1926+
$this->binds = [];
1927+
}
19171928

19181929
return $result;
19191930
}

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
}

user_guide_src/source/database/query_builder.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,11 +1256,13 @@ Class Reference
12561256
Compiles and runs SELECT statement based on the already
12571257
called Query Builder methods.
12581258

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

0 commit comments

Comments
 (0)