Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resetting QBFrom part #2802

Merged
merged 2 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -998,9 +998,9 @@ 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;
}

return $this;
}

if ($values === null || (! is_array($values) && ! ($values instanceof Closure)))
Expand All @@ -1009,8 +1009,8 @@ 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;
Expand Down Expand Up @@ -3328,6 +3328,12 @@ protected function resetSelect()
{
$this->db->setAliasedTables([]);
}

// Reset QBFrom part
if (! empty($this->QBFrom))
{
$this->from(array_shift($this->QBFrom), true);
}
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -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;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/system/Database/Builder/FromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

//--------------------------------------------------------------------
}