Skip to content

Commit 8597be3

Browse files
authored
Merge pull request #2533 from musmanikram/2532-fix-multi-column-where-prefix-issue
Fix multi-column WHERE not prefixed with DBPrefix
2 parents 1514be2 + ffe6db9 commit 8597be3

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

system/Database/BaseBuilder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,17 @@ protected function compileWhereHaving(string $qb_key): string
30453045

30463046
if (! empty($matches[4]))
30473047
{
3048+
$protectIdentifiers = false;
3049+
if (strpos($matches[4], '.') !== false)
3050+
{
3051+
$protectIdentifiers = true;
3052+
}
3053+
3054+
if (strpos($matches[4], ':') === false)
3055+
{
3056+
$matches[4] = $this->db->protectIdentifiers(trim($matches[4]), false, $protectIdentifiers);
3057+
}
3058+
30483059
$matches[4] = ' ' . $matches[4];
30493060
}
30503061

system/Database/BaseConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ public function protectIdentifiers($item, bool $prefixSingle = false, bool $prot
12151215
// This can happen when this function is being called from a JOIN.
12161216
if ($fieldExists === false)
12171217
{
1218-
$i ++;
1218+
$i++;
12191219
}
12201220

12211221
// Verify table prefix and replace if necessary

tests/system/Database/Builder/PrefixTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,19 @@ public function testPrefixesSetOnTableNames()
2626

2727
//--------------------------------------------------------------------
2828

29+
public function testPrefixesSetOnTableNamesWithWhereClause()
30+
{
31+
$builder = $this->db->table('users');
32+
33+
$where = 'users.created_at < users.updated_at';
34+
35+
$expectedSQL = 'SELECT * FROM "ci_users" WHERE "ci_users"."created_at" < "ci_users"."updated_at"';
36+
$expectedBinds = [];
37+
38+
$builder->where($where);
39+
40+
$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
41+
$this->assertSame($expectedBinds, $builder->getBinds());
42+
}
43+
2944
}

0 commit comments

Comments
 (0)