Skip to content

Commit

Permalink
Merge pull request #2533 from musmanikram/2532-fix-multi-column-where…
Browse files Browse the repository at this point in the history
…-prefix-issue

Fix multi-column WHERE not prefixed with DBPrefix
  • Loading branch information
MGatner authored Feb 9, 2020
2 parents 1514be2 + ffe6db9 commit 8597be3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3045,6 +3045,17 @@ protected function compileWhereHaving(string $qb_key): string

if (! empty($matches[4]))
{
$protectIdentifiers = false;
if (strpos($matches[4], '.') !== false)
{
$protectIdentifiers = true;
}

if (strpos($matches[4], ':') === false)
{
$matches[4] = $this->db->protectIdentifiers(trim($matches[4]), false, $protectIdentifiers);
}

$matches[4] = ' ' . $matches[4];
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ public function protectIdentifiers($item, bool $prefixSingle = false, bool $prot
// This can happen when this function is being called from a JOIN.
if ($fieldExists === false)
{
$i ++;
$i++;
}

// Verify table prefix and replace if necessary
Expand Down
15 changes: 15 additions & 0 deletions tests/system/Database/Builder/PrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,19 @@ public function testPrefixesSetOnTableNames()

//--------------------------------------------------------------------

public function testPrefixesSetOnTableNamesWithWhereClause()
{
$builder = $this->db->table('users');

$where = 'users.created_at < users.updated_at';

$expectedSQL = 'SELECT * FROM "ci_users" WHERE "ci_users"."created_at" < "ci_users"."updated_at"';
$expectedBinds = [];

$builder->where($where);

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

}

0 comments on commit 8597be3

Please sign in to comment.