-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathExpression.php
119 lines (100 loc) · 2.97 KB
/
Expression.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Components;
use AllowDynamicProperties;
use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Context;
use function implode;
/**
* Parses a reference to an expression (column, table or database name, function
* call, mathematical expression, etc.).
*/
#[AllowDynamicProperties]
final class Expression implements Component
{
/**
* The name of this database.
*/
public string|null $database = null;
/**
* The name of this table.
*/
public string|null $table = null;
/**
* The name of the column.
*/
public string|null $column = null;
/**
* The sub-expression.
*/
public string|null $expr = '';
/**
* The alias of this expression.
*/
public string|null $alias = null;
/**
* The name of the function.
*/
public string|null $function = null;
/**
* The type of subquery.
*/
public string|null $subquery = null;
/**
* Syntax:
* new Expression('expr')
* new Expression('expr', 'alias')
* new Expression('database', 'table', 'column')
* new Expression('database', 'table', 'column', 'alias')
*
* If the database, table or column name is not required, pass an empty
* string.
*
* @param string|null $database The name of the database or the expression.
* @param string|null $table The name of the table or the alias of the expression.
* @param string|null $column the name of the column
* @param string|null $alias the name of the alias
*/
public function __construct(
string|null $database = null,
string|null $table = null,
string|null $column = null,
string|null $alias = null,
) {
if (($column === null) && ($alias === null)) {
$this->expr = $database; // case 1
$this->alias = $table; // case 2
} else {
$this->database = $database; // case 3
$this->table = $table; // case 3
$this->column = $column; // case 3
$this->alias = $alias; // case 4
}
}
public function build(): string
{
if ($this->expr !== '' && $this->expr !== null) {
$ret = $this->expr;
} else {
$fields = [];
if (isset($this->database) && ($this->database !== '')) {
$fields[] = $this->database;
}
if (isset($this->table) && ($this->table !== '')) {
$fields[] = $this->table;
}
if (isset($this->column) && ($this->column !== '')) {
$fields[] = $this->column;
}
$ret = implode('.', Context::escapeAll($fields));
}
if (! empty($this->alias)) {
$ret .= ' AS ' . Context::escape($this->alias);
}
return $ret;
}
public function __toString(): string
{
return $this->build();
}
}