Skip to content

Commit

Permalink
Additional edge case for yiisoft#14150
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed May 12, 2017
1 parent 8a087c8 commit 625d554
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
41 changes: 36 additions & 5 deletions framework/db/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,17 +813,48 @@ public function getTablesUsedInFrom()
$cleanedUpTableNames = [];
foreach ($tableNames as $alias => $tableName) {
if (!is_string($alias)) {
if (preg_match('~\s*({{.*?}})\s*~', $tableName, $matches)) {
$alias = $tableName = $matches[1];
} elseif (preg_match('~^\s*([\'"`\[].*?[\'"`\]]|\w+)(?:(?:\s+(?:as)?\s*)([\'"`\[].*?[\'"`\]]|\w+))?\s*$~iu', $tableName, $matches)) {
$pattern = <<<PATTERN
~
^
\s*
(
(?:['"`\[]|{{)
.*?
(?:['"`\]]|}})
|
\w+
)
(?:
(?:
\s+
(?:as)?
\s*
)
(
(?:['"`\[]|{{)
.*?
(?:['"`\]]|}})
|
\w+
)
)?
\s*
$
~iux
PATTERN;
if (preg_match($pattern, $tableName, $matches)) {
if (isset($matches[1])) {
if (isset($matches[2])) {
list(, $tableName, $alias) = $matches;
} else {
$tableName = $alias = $matches[1];
}
$alias = '{{' . $alias . '}}';
$tableName = '{{' . $tableName . '}}';
if (strncmp($alias, '{{', 2) !== 0) {
$alias = '{{' . $alias . '}}';
}
if (strncmp($tableName, '{{', 2) !== 0) {
$tableName = '{{' . $tableName . '}}';
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/framework/db/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function testGetTablesAlias_isFromArray()
public function testGetTablesAlias_isFromString()
{
$query = new ActiveQuery(null);
$query->from = 'profile AS \'prf\', user "usr", service srv, order, [a b] [c d]';
$query->from = 'profile AS \'prf\', user "usr", service srv, order, [a b] [c d], {{something}} AS myalias';

$tables = $query->getTablesUsedInFrom();

Expand All @@ -329,6 +329,7 @@ public function testGetTablesAlias_isFromString()
'{{srv}}' => '{{service}}',
'{{order}}' => '{{order}}',
'{{c d}}' => '{{a b}}',
'{{myalias}}' => '{{something}}'
], $tables);
}

Expand Down

0 comments on commit 625d554

Please sign in to comment.