Skip to content

Commit

Permalink
Merge pull request #5361 from kenjis/fix-prefixed-alias-bug
Browse files Browse the repository at this point in the history
fix: table alias is prefixed when LIKE clause
  • Loading branch information
kenjis authored Nov 22, 2021
2 parents 8b01f90 + 20411e6 commit be1908f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,8 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro
//
// NOTE: The ! empty() condition prevents this method
// from breaking when QB isn't enabled.
if (! empty($this->aliasedTables) && in_array($parts[0], $this->aliasedTables, true)) {
$firstSegment = trim($parts[0], $this->escapeChar);
if (! empty($this->aliasedTables) && in_array($firstSegment, $this->aliasedTables, true)) {
if ($protectIdentifiers === true) {
foreach ($parts as $key => $val) {
if (! in_array($val, $this->reservedIdentifiers, true)) {
Expand Down
16 changes: 16 additions & 0 deletions tests/system/Database/Builder/AliasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,20 @@ public function testAliasLeftJoinWithLongTableName()

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5360
*/
public function testAliasSimpleLikeWithDBPrefix()
{
$this->setPrivateProperty($this->db, 'DBPrefix', 'db_');
$builder = $this->db->table('jobs j');

$builder->like('j.name', 'veloper');

$expectedSQL = <<<'SQL'
SELECT * FROM "db_jobs" "j" WHERE "j"."name" LIKE '%veloper%' ESCAPE '!'
SQL;
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
}

0 comments on commit be1908f

Please sign in to comment.