Skip to content

Commit be1908f

Browse files
authored
Merge pull request #5361 from kenjis/fix-prefixed-alias-bug
fix: table alias is prefixed when LIKE clause
2 parents 8b01f90 + 20411e6 commit be1908f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

system/Database/BaseConnection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,8 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro
10141014
//
10151015
// NOTE: The ! empty() condition prevents this method
10161016
// from breaking when QB isn't enabled.
1017-
if (! empty($this->aliasedTables) && in_array($parts[0], $this->aliasedTables, true)) {
1017+
$firstSegment = trim($parts[0], $this->escapeChar);
1018+
if (! empty($this->aliasedTables) && in_array($firstSegment, $this->aliasedTables, true)) {
10181019
if ($protectIdentifiers === true) {
10191020
foreach ($parts as $key => $val) {
10201021
if (! in_array($val, $this->reservedIdentifiers, true)) {

tests/system/Database/Builder/AliasTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,20 @@ public function testAliasLeftJoinWithLongTableName()
8484

8585
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
8686
}
87+
88+
/**
89+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5360
90+
*/
91+
public function testAliasSimpleLikeWithDBPrefix()
92+
{
93+
$this->setPrivateProperty($this->db, 'DBPrefix', 'db_');
94+
$builder = $this->db->table('jobs j');
95+
96+
$builder->like('j.name', 'veloper');
97+
98+
$expectedSQL = <<<'SQL'
99+
SELECT * FROM "db_jobs" "j" WHERE "j"."name" LIKE '%veloper%' ESCAPE '!'
100+
SQL;
101+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
102+
}
87103
}

0 commit comments

Comments
 (0)