From f05ee254c183d78828bf0943b46a4cb4c1586c05 Mon Sep 17 00:00:00 2001 From: michalsn Date: Sun, 5 Apr 2020 16:59:25 +0200 Subject: [PATCH 1/2] fix from reset #2800 --- system/Database/BaseBuilder.php | 26 +++++++++++-------- tests/system/Database/Builder/FromTest.php | 29 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index a49a061b28d2..61e2a5704c87 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -981,12 +981,12 @@ public function orHavingNotIn(string $key = null, $values = null, bool $escape = * @used-by whereNotIn() * @used-by orWhereNotIn() * - * @param string $key The field to search - * @param array|Closure $values The values searched on, or anonymous function with subquery - * @param boolean $not If the statement would be IN or NOT IN - * @param string $type - * @param boolean $escape - * @param string $clause (Internal use only) + * @param string $key The field to search + * @param array|Closure $values The values searched on, or anonymous function with subquery + * @param boolean $not If the statement would be IN or NOT IN + * @param string $type + * @param boolean $escape + * @param string $clause (Internal use only) * @throws InvalidArgumentException * * @return BaseBuilder @@ -998,8 +998,8 @@ protected function _whereIn(string $key = null, $values = null, bool $not = fals if (CI_DEBUG) { throw new InvalidArgumentException(sprintf('%s() expects $key to be a non-empty string', debug_backtrace(0, 2)[1]['function'])); - } - + } + return this; } @@ -1009,7 +1009,7 @@ protected function _whereIn(string $key = null, $values = null, bool $not = fals { throw new InvalidArgumentException(sprintf('%s() expects $values to be of type array or closure', debug_backtrace(0, 2)[1]['function'])); } - + return this; } @@ -3328,6 +3328,12 @@ protected function resetSelect() { $this->db->setAliasedTables([]); } + + // Reset QBFrom part + if (! empty($this->QBFrom)) + { + $this->from(array_shift($this->QBFrom), true); + } } //-------------------------------------------------------------------- @@ -3426,7 +3432,7 @@ protected function setBind(string $key, $value = null, bool $escape = true): str return $key; } - if (!array_key_exists($key, $this->bindsKeyCount)) + if (! array_key_exists($key, $this->bindsKeyCount)) { $this->bindsKeyCount[$key] = 0; } diff --git a/tests/system/Database/Builder/FromTest.php b/tests/system/Database/Builder/FromTest.php index fb5570ef5e8b..e2c3866278d7 100644 --- a/tests/system/Database/Builder/FromTest.php +++ b/tests/system/Database/Builder/FromTest.php @@ -69,4 +69,33 @@ public function testFromWithMultipleTablesAsString() } //-------------------------------------------------------------------- + + public function testFromReset() + { + $builder = new BaseBuilder('user', $this->db); + + $builder->from(['jobs', 'roles']); + + $expectedSQL = 'SELECT * FROM "user", "jobs", "roles"'; + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + + $expectedSQL = 'SELECT * FROM "user"'; + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + + $expectedSQL = 'SELECT *'; + + $builder->from(null, true); + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + + $expectedSQL = 'SELECT * FROM "jobs"'; + + $builder->from('jobs'); + + $this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); + } + + //-------------------------------------------------------------------- } From 654b9156692e8f992b9e7436a2712961dda8dce9 Mon Sep 17 00:00:00 2001 From: michalsn Date: Sun, 5 Apr 2020 17:11:33 +0200 Subject: [PATCH 2/2] fix conflicts --- system/Database/BaseBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 61e2a5704c87..2b722fe7e966 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -1000,7 +1000,7 @@ protected function _whereIn(string $key = null, $values = null, bool $not = fals throw new InvalidArgumentException(sprintf('%s() expects $key to be a non-empty string', debug_backtrace(0, 2)[1]['function'])); } - return this; + return $this; } if ($values === null || (! is_array($values) && ! ($values instanceof Closure))) @@ -1010,7 +1010,7 @@ protected function _whereIn(string $key = null, $values = null, bool $not = fals throw new InvalidArgumentException(sprintf('%s() expects $values to be of type array or closure', debug_backtrace(0, 2)[1]['function'])); } - return this; + return $this; } is_bool($escape) || $escape = $this->db->protectIdentifiers;