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

fix: table name is double prefixed when LIKE clause #5778

Merged
merged 3 commits into from
Mar 8, 2022
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
4 changes: 2 additions & 2 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ protected function _like($field, string $match = '', string $type = 'AND ', stri
$bind = $this->setBind($k, "%{$v}%", $escape);
}

$likeStatement = $this->_like_statement($prefix, $this->db->protectIdentifiers($k, false, $escape), $not, $bind, $insensitiveSearch);
$likeStatement = $this->_like_statement($prefix, $k, $not, $bind, $insensitiveSearch);

// some platforms require an escape sequence definition for LIKE wildcards
if ($escape === true && $this->db->likeEscapeStr !== '') {
Expand All @@ -1073,7 +1073,7 @@ protected function _like($field, string $match = '', string $type = 'AND ', stri
protected function _like_statement(?string $prefix, string $column, ?string $not, string $bind, bool $insensitiveSearch = false): string
{
if ($insensitiveSearch === true) {
return "{$prefix} LOWER({$column}) {$not} LIKE :{$bind}:";
return "{$prefix} LOWER(" . $this->db->escapeIdentifiers($column) . ") {$not} LIKE :{$bind}:";
}

return "{$prefix} {$column} {$not} LIKE :{$bind}:";
Expand Down
8 changes: 4 additions & 4 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,9 @@ public function getConnectDuration(int $decimals = 6): string
* the correct identifiers.
*
* @param array|string $item
* @param bool $prefixSingle Prefix an item with no segments?
* @param bool $fieldExists Supplied $item contains a field name?
* @param bool $prefixSingle Prefix a table name with no segments?
* @param bool $protectIdentifiers Protect table or column names?
* @param bool $fieldExists Supplied $item contains a column name?
*
* @return array|string
*/
Expand Down Expand Up @@ -1021,8 +1022,7 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro
//
// NOTE: The ! empty() condition prevents this method
// from breaking when QB isn't enabled.
$firstSegment = trim($parts[0], $this->escapeChar);
if (! empty($this->aliasedTables) && in_array($firstSegment, $this->aliasedTables, true)) {
if (! empty($this->aliasedTables) && in_array($parts[0], $this->aliasedTables, true)) {
if ($protectIdentifiers === true) {
foreach ($parts as $key => $val) {
if (! in_array($val, $this->reservedIdentifiers, true)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/system/Database/Builder/LikeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,27 @@ public function testCaseInsensitiveLike()
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$this->assertSame($expectedBinds, $builder->getBinds());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5775
*/
public function testDBPrefixAndCoulmnWithTablename()
{
$this->db = new MockConnection(['DBPrefix' => 'db_']);
$builder = new BaseBuilder('test', $this->db);

$builder->like('test.field', 'string');

$expectedSQL = <<<'SQL'
SELECT * FROM "db_test" WHERE "db_test"."field" LIKE '%string%' ESCAPE '!'
SQL;
$expectedBinds = [
'test.field' => [
'%string%',
true,
],
];
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$this->assertSame($expectedBinds, $builder->getBinds());
}
}