Skip to content
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
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1185,16 +1185,6 @@ parameters:
count: 1
path: tests/Parser/WithStatementTest.php

-
message: "#^Parameter \\#1 \\$statement of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Misc\\:\\:getAliases\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement, PhpMyAdmin\\\\SqlParser\\\\Statement\\|null given\\.$#"
count: 1
path: tests/Utils/MiscTest.php

-
message: "#^Parameter \\#2 \\$database of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Misc\\:\\:getAliases\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: tests/Utils/MiscTest.php

-
message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getClause\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#"
count: 9
Expand Down
20 changes: 3 additions & 17 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5177,6 +5177,9 @@
</PossiblyNullOperand>
</file>
<file src="src/Statements/SelectStatement.php">
<PossiblyNullArrayOffset>
<code>$tables[$thisDb]</code>
</PossiblyNullArrayOffset>
<PossiblyUnusedProperty>
<code>$endOptions</code>
<code>$groupOptions</code>
Expand Down Expand Up @@ -5419,14 +5422,6 @@
<code><![CDATA[$curr->type === Token::TYPE_KEYWORD]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Utils/Misc.php">
<DocblockTypeContradiction>
<code>! ($statement instanceof SelectStatement)</code>
</DocblockTypeContradiction>
<PossiblyNullArrayOffset>
<code>$tables[$thisDb]</code>
</PossiblyNullArrayOffset>
</file>
<file src="src/Utils/Query.php">
<InvalidNullableReturnType>
<code>int</code>
Expand Down Expand Up @@ -5735,15 +5730,6 @@
<code>setAccessible</code>
</UnusedMethodCall>
</file>
<file src="tests/Utils/MiscTest.php">
<ArgumentTypeCoercion>
<code>$statement</code>
</ArgumentTypeCoercion>
<PossiblyNullArgument>
<code>$db</code>
<code>$statement</code>
</PossiblyNullArgument>
</file>
<file src="tests/Utils/QueryTest.php">
<PossiblyNullArgument>
<code><![CDATA[$parser->list]]></code>
Expand Down
81 changes: 81 additions & 0 deletions src/Statements/SelectStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,85 @@ public function getClauses()

return static::$clauses;
}

/**
* Gets a list of all aliases and their original names.
*
* @param string $database the name of the database
*
* @return array<string, array<string, array<string, array<string, array<string, string>|string|null>>|null>>
*/
public function getAliases(string $database): array
{
if (empty($this->expr) || empty($this->from)) {
return [];
}

$retval = [];

$tables = [];

/**
* Expressions that may contain aliases.
* These are extracted from `FROM` and `JOIN` keywords.
*/
$expressions = $this->from;

// Adding expressions from JOIN.
if (! empty($this->join)) {
foreach ($this->join as $join) {
$expressions[] = $join->expr;
}
}

foreach ($expressions as $expr) {
if (! isset($expr->table) || ($expr->table === '')) {
continue;
}

$thisDb = isset($expr->database) && ($expr->database !== '') ?
$expr->database : $database;

if (! isset($retval[$thisDb])) {
$retval[$thisDb] = [
'alias' => null,
'tables' => [],
];
}

if (! isset($retval[$thisDb]['tables'][$expr->table])) {
$retval[$thisDb]['tables'][$expr->table] = [
'alias' => isset($expr->alias) && ($expr->alias !== '') ?
$expr->alias : null,
'columns' => [],
];
}

if (! isset($tables[$thisDb])) {
$tables[$thisDb] = [];
}

$tables[$thisDb][$expr->alias] = $expr->table;
}

foreach ($this->expr as $expr) {
if (! isset($expr->column, $expr->alias) || ($expr->column === '') || ($expr->alias === '')) {
continue;
}

$thisDb = isset($expr->database) && ($expr->database !== '') ?
$expr->database : $database;

if (isset($expr->table) && ($expr->table !== '')) {
$thisTable = $tables[$thisDb][$expr->table] ?? $expr->table;
$retval[$thisDb]['tables'][$thisTable]['columns'][$expr->column] = $expr->alias;
} else {
foreach ($retval[$thisDb]['tables'] as &$table) {
$table['columns'][$expr->column] = $expr->alias;
}
}
}

return $retval;
}
}
98 changes: 0 additions & 98 deletions src/Utils/Misc.php

This file was deleted.

118 changes: 118 additions & 0 deletions tests/Builder/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Components\Limit;
use PhpMyAdmin\SqlParser\Components\OptionsArray;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
use PhpMyAdmin\SqlParser\Tests\TestCase;

Expand Down Expand Up @@ -39,4 +40,121 @@ public function testBuilder(): void
(string) $stmt
);
}

/**
* @psalm-param array<string, array{
* alias: (string|null),
* tables: array<string, array{alias: (string|null), columns: array<string, string>}>
* }> $expected
*
* @dataProvider getAliasesProvider
*/
public function testGetAliases(string $query, string $db, array $expected): void
{
$parser = new Parser($query);
$this->assertInstanceOf(SelectStatement::class, $parser->statements[0]);
$this->assertEquals($expected, $parser->statements[0]->getAliases($db));
}

/**
* @psalm-return list<array{string, string, array<string, array{
* alias: (string|null),
* tables: array<string, array{alias: (string|null), columns: array<string, string>}>
* }>}>
*/
public static function getAliasesProvider(): array
{
return [
[
'select * from (select 1) tbl',
'mydb',
[],
],
[
'select i.name as `n`,abcdef gh from qwerty i',
'mydb',
[
'mydb' => [
'alias' => null,
'tables' => [
'qwerty' => [
'alias' => 'i',
'columns' => [
'name' => 'n',
'abcdef' => 'gh',
],
],
],
],
],
],
[
'select film_id id,title from film',
'sakila',
[
'sakila' => [
'alias' => null,
'tables' => [
'film' => [
'alias' => null,
'columns' => ['film_id' => 'id'],
],
],
],
],
],
[
'select `sakila`.`A`.`actor_id` as aid,`F`.`film_id` `fid`,'
. 'last_update updated from `sakila`.actor A join `film_actor` as '
. '`F` on F.actor_id = A.`actor_id`',
'sakila',
[
'sakila' => [
'alias' => null,
'tables' => [
'film_actor' => [
'alias' => 'F',
'columns' => [
'film_id' => 'fid',
'last_update' => 'updated',
],
],
'actor' => [
'alias' => 'A',
'columns' => [
'actor_id' => 'aid',
'last_update' => 'updated',
],
],
],
],
],
],
[
'SELECT film_id FROM (SELECT * FROM film) as f;',
'sakila',
[],
],
[
'SELECT 1',
'',
[],
],
[
'SELECT * FROM orders AS ord WHERE 1',
'db',
[
'db' => [
'alias' => null,
'tables' => [
'orders' => [
'alias' => 'ord',
'columns' => [],
],
],
],
],
],
];
}
}
Loading