Skip to content

Commit da21535

Browse files
committed
Merge pull request #124 from norbe/kh-alias-improvement
Improved unique table name detection
2 parents 1329476 + b389723 commit da21535

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/Database/Table/SqlBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ public function parseJoinsCb(& $joins, $match)
551551
$tableAlias = preg_replace('#^(.*\.)?(.*)$#', '$2', $table);
552552
}
553553

554-
$tableChain .= $keyMatch['del'] . $tableAlias;
554+
$tableChain .= $keyMatch[0];
555555
if (!$isLast || !$this->currentAlias) {
556556
$this->checkUniqueTableName($tableAlias, $tableChain);
557557
}

tests/Database/Table/SqlBuilder.addAlias().phpt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ $driver = $connection->getSupplementalDriver();
2929

3030

3131
test(function() use ($context, $driver) { // test duplicated table names throw exception
32-
if ($driver->isSupported(ISupplementalDriver::SUPPORT_SCHEMA)) {
33-
$sqlBuilder = new SqlBuilderMock('public.author', $context);
34-
} else {
35-
$sqlBuilder = new SqlBuilderMock('author', $context);
36-
}
32+
$authorTable = ($driver->isSupported(ISupplementalDriver::SUPPORT_SCHEMA) ? 'public.' : '' ) . 'author';
33+
$sqlBuilder = new SqlBuilderMock($authorTable, $context);
3734
$sqlBuilder->addAlias(':book(translator)', 'book1');
3835
$sqlBuilder->addAlias(':book:book_tag', 'book2');
3936
Assert::exception(function() use ($sqlBuilder) {
@@ -42,7 +39,7 @@ test(function() use ($context, $driver) { // test duplicated table names throw e
4239

4340
Assert::exception(function() use ($sqlBuilder) { // reserved by base table name
4441
$sqlBuilder->addAlias(':book', 'author');
45-
}, Nette\InvalidArgumentException::class, "Table alias 'author' from chain ':book' is already in use by chain 'author'. Please add/change alias for one of them.");
42+
}, Nette\InvalidArgumentException::class, "Table alias 'author' from chain ':book' is already in use by chain '$authorTable'. Please add/change alias for one of them.");
4643

4744
Assert::exception(function() use ($sqlBuilder) {
4845
$sqlBuilder->addAlias(':book', 'book1');
@@ -54,6 +51,12 @@ test(function() use ($context, $driver) { // test duplicated table names throw e
5451
$joins = [];
5552
$sqlBuilder->parseJoins($joins, $query);
5653
}, Nette\InvalidArgumentException::class, "Table alias 'tag' from chain '.book1:book_tag.tag' is already in use by chain ':book'. Please add/change alias for one of them.");
54+
55+
Assert::exception(function() use ($sqlBuilder) {
56+
$query = 'WHERE :book(translator).id IS NULL AND :book.id IS NULL';
57+
$joins = [];
58+
$sqlBuilder->parseJoins($joins, $query);
59+
}, Nette\InvalidArgumentException::class, "Table alias 'book' from chain ':book' is already in use by chain ':book(translator)'. Please add/change alias for one of them.");
5760
});
5861

5962

0 commit comments

Comments
 (0)