From 4eba9dc0598a2dd746fc9fa5105e0b84514737d9 Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 19 Sep 2019 17:17:45 +0200 Subject: [PATCH] fix #2143 --- system/Database/BaseBuilder.php | 23 +++++++--- tests/system/Database/Builder/GetTest.php | 56 +++++++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index ea3488d9e9aa..91a490d3b5cf 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -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) { @@ -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; } diff --git a/tests/system/Database/Builder/GetTest.php b/tests/system/Database/Builder/GetTest.php index 6eee4924c56a..f4b0a4896d76 100644 --- a/tests/system/Database/Builder/GetTest.php +++ b/tests/system/Database/Builder/GetTest.php @@ -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))); + } + }