Skip to content

Commit

Permalink
Merge pull request #6390 from iRedds/fix/alias-subquery
Browse files Browse the repository at this point in the history
Fix: The subquery adds a prefix for the table alias.
  • Loading branch information
kenjis authored Aug 22, 2022
2 parents aed7114 + 99f0dd5 commit 4727025
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 2 additions & 2 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ public function fromSubquery(BaseBuilder $from, string $alias): self
{
$table = $this->buildSubquery($from, true, $alias);

$this->trackAliases($table);
$this->db->addTableAlias($alias);
$this->QBFrom[] = $table;

return $this;
Expand Down Expand Up @@ -2952,7 +2952,7 @@ protected function buildSubquery($builder, bool $wrapped = false, string $alias
throw new DatabaseException('The subquery cannot be the same object as the main query object.');
}

$subquery = strtr($builder->getCompiledSelect(), "\n", ' ');
$subquery = strtr($builder->getCompiledSelect(false), "\n", ' ');

if ($wrapped) {
$subquery = '(' . $subquery . ')';
Expand Down
7 changes: 6 additions & 1 deletion system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,12 @@ public function table($tableName)
*/
public function newQuery(): BaseBuilder
{
return $this->table(',')->from([], true);
// save table aliases
$tempAliases = $this->aliasedTables;
$builder = $this->table(',')->from([], true);
$this->aliasedTables = $tempAliases;

return $builder;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/system/Database/Builder/PrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Database\Builder;

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;

Expand Down Expand Up @@ -49,4 +50,40 @@ public function testPrefixesSetOnTableNamesWithWhereClause()
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$this->assertSame($expectedBinds, $builder->getBinds());
}

public function testPrefixWithSubquery(): void
{
$expected = <<<'NOWDOC'
SELECT "u"."id", "u"."name", (SELECT 1 FROM "ci_users" "sub" WHERE "sub"."id" = "u"."id") "one"
FROM "ci_users" "u"
WHERE "u"."id" = 1
NOWDOC;

$subquery = $this->db->table('users sub')
->select('1', false)
->where('sub.id = u.id');

$builder = $this->db->table('users u')
->select('u.id, u.name')
->selectSubquery($subquery, 'one')
->where('u.id', 1);

$this->assertSame($expected, $builder->getCompiledSelect());
}

public function testPrefixWithNewQuery(): void
{
$expectedSQL = <<<'NOWDOC'
SELECT "users_1"."id", "name"
FROM (SELECT "u"."id", "u"."name" FROM "ci_users" "u") "users_1"
WHERE "users_1"."id" > 10
NOWDOC;

$subquery = (new BaseBuilder('users u', $this->db))->select('u.id, u.name');
$builder = $this->db->newQuery()->fromSubquery($subquery, 'users_1')
->select('users_1.id, name')
->where('users_1.id > ', 10);

$this->assertSame($expectedSQL, $builder->getCompiledSelect());
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ none.

Bugs Fixed
**********
- When using subqueries in the main query, prefixes are added to the table alias.

See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.

0 comments on commit 4727025

Please sign in to comment.