Skip to content

Fix: The subquery adds a prefix for the table alias. #6390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.