|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Miscellaneous utilities. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace PhpMyAdmin\SqlParser\Utils; |
|
8
|
|
|
|
|
9
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
|
10
|
|
|
use PhpMyAdmin\SqlParser\Statements\SelectStatement; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Miscellaneous utilities. |
|
14
|
|
|
* |
|
15
|
|
|
* @category Misc |
|
16
|
|
|
* |
|
17
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ |
|
18
|
|
|
*/ |
|
19
|
|
|
class Misc |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Gets a list of all aliases and their original names. |
|
23
|
|
|
* |
|
24
|
|
|
* @param SelectStatement $statement the statement to be processed |
|
25
|
|
|
* @param string $database the name of the database |
|
26
|
|
|
* |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public static function getAliases($statement, $database) |
|
30
|
|
|
{ |
|
31
|
8 |
|
if (!($statement instanceof SelectStatement) |
|
32
|
8 |
|
|| (empty($statement->expr)) |
|
33
|
7 |
|
|| (empty($statement->from)) |
|
34
|
8 |
|
) { |
|
35
|
2 |
|
return array(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
$retval = array(); |
|
39
|
|
|
|
|
40
|
6 |
|
$tables = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Expressions that may contain aliases. |
|
44
|
|
|
* These are extracted from `FROM` and `JOIN` keywords. |
|
45
|
|
|
* |
|
46
|
|
|
* @var Expression[] |
|
47
|
|
|
*/ |
|
48
|
6 |
|
$expressions = $statement->from; |
|
49
|
|
|
|
|
50
|
|
|
// Adding expressions from JOIN. |
|
51
|
6 |
|
if (!empty($statement->join)) { |
|
52
|
1 |
|
foreach ($statement->join as $join) { |
|
53
|
1 |
|
$expressions[] = $join->expr; |
|
54
|
1 |
|
} |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
foreach ($expressions as $expr) { |
|
58
|
6 |
|
if ((!isset($expr->table)) || ($expr->table === '')) { |
|
59
|
2 |
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
$thisDb = ((isset($expr->database)) && ($expr->database !== '')) ? |
|
63
|
4 |
|
$expr->database : $database; |
|
64
|
|
|
|
|
65
|
4 |
|
if (!isset($retval[$thisDb])) { |
|
66
|
4 |
|
$retval[$thisDb] = array( |
|
67
|
4 |
|
'alias' => null, |
|
68
|
4 |
|
'tables' => array(), |
|
69
|
|
|
); |
|
70
|
4 |
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
if (!isset($retval[$thisDb]['tables'][$expr->table])) { |
|
73
|
4 |
|
$retval[$thisDb]['tables'][$expr->table] = array( |
|
74
|
4 |
|
'alias' => ((isset($expr->alias)) && ($expr->alias !== '')) ? |
|
75
|
4 |
|
$expr->alias : null, |
|
76
|
4 |
|
'columns' => array(), |
|
77
|
|
|
); |
|
78
|
4 |
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
if (!isset($tables[$thisDb])) { |
|
81
|
4 |
|
$tables[$thisDb] = array(); |
|
82
|
4 |
|
} |
|
83
|
4 |
|
$tables[$thisDb][$expr->alias] = $expr->table; |
|
84
|
6 |
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
foreach ($statement->expr as $expr) { |
|
87
|
6 |
|
if ((!isset($expr->column)) || ($expr->column === '') |
|
88
|
4 |
|
|| (!isset($expr->alias)) || ($expr->alias === '') |
|
89
|
6 |
|
) { |
|
90
|
4 |
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
$thisDb = ((isset($expr->database)) && ($expr->database !== '')) ? |
|
94
|
3 |
|
$expr->database : $database; |
|
95
|
|
|
|
|
96
|
3 |
|
if ((isset($expr->table)) && ($expr->table !== '')) { |
|
97
|
2 |
|
$thisTable = isset($tables[$thisDb][$expr->table]) ? |
|
98
|
2 |
|
$tables[$thisDb][$expr->table] : $expr->table; |
|
99
|
2 |
|
$retval[$thisDb]['tables'][$thisTable]['columns'][$expr->column] = $expr->alias; |
|
100
|
2 |
|
} else { |
|
101
|
3 |
|
foreach ($retval[$thisDb]['tables'] as &$table) { |
|
102
|
3 |
|
$table['columns'][$expr->column] = $expr->alias; |
|
103
|
3 |
|
} |
|
104
|
|
|
} |
|
105
|
6 |
|
} |
|
106
|
|
|
|
|
107
|
6 |
|
return $retval; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|