|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* `WHERE` keyword parser. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
|
8
|
|
|
|
|
9
|
|
|
use PhpMyAdmin\SqlParser\Component; |
|
10
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
|
11
|
|
|
use PhpMyAdmin\SqlParser\Token; |
|
12
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* `WHERE` keyword parser. |
|
16
|
|
|
* |
|
17
|
|
|
* @category Keywords |
|
18
|
|
|
* |
|
19
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ |
|
20
|
|
|
*/ |
|
21
|
|
|
class Condition extends Component |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Logical operators that can be used to delimit expressions. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
public static $DELIMITERS = array('&&', '||', 'AND', 'OR', 'XOR'); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List of allowed reserved keywords in conditions. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
public static $ALLOWED_KEYWORDS = array( |
|
36
|
|
|
'ALL' => 1, |
|
37
|
|
|
'AND' => 1, |
|
38
|
|
|
'BETWEEN' => 1, |
|
39
|
|
|
'EXISTS' => 1, |
|
40
|
|
|
'IF' => 1, |
|
41
|
|
|
'IN' => 1, |
|
42
|
|
|
'INTERVAL' => 1, |
|
43
|
|
|
'IS' => 1, |
|
44
|
|
|
'LIKE' => 1, |
|
45
|
|
|
'MATCH' => 1, |
|
46
|
|
|
'NOT IN' => 1, |
|
47
|
|
|
'NOT NULL' => 1, |
|
48
|
|
|
'NOT' => 1, |
|
49
|
|
|
'NULL' => 1, |
|
50
|
|
|
'OR' => 1, |
|
51
|
|
|
'REGEXP' => 1, |
|
52
|
|
|
'RLIKE' => 1, |
|
53
|
|
|
'XOR' => 1, |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Identifiers recognized. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
public $identifiers = array(); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Whether this component is an operator. |
|
65
|
|
|
* |
|
66
|
|
|
* @var bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public $isOperator = false; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The condition. |
|
72
|
|
|
* |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
public $expr; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Constructor. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $expr the condition or the operator |
|
|
|
|
|
|
81
|
|
|
*/ |
|
82
|
61 |
|
public function __construct($expr = null) |
|
83
|
|
|
{ |
|
84
|
61 |
|
$this->expr = trim($expr); |
|
85
|
61 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Parser $parser the parser that serves as context |
|
89
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
|
90
|
|
|
* @param array $options parameters for parsing |
|
91
|
|
|
* |
|
92
|
|
|
* @return Condition[] |
|
93
|
|
|
*/ |
|
94
|
60 |
|
public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
95
|
|
|
{ |
|
96
|
60 |
|
$ret = array(); |
|
97
|
|
|
|
|
98
|
60 |
|
$expr = new self(); |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Counts brackets. |
|
102
|
|
|
* |
|
103
|
|
|
* @var int |
|
104
|
|
|
*/ |
|
105
|
60 |
|
$brackets = 0; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Whether there was a `BETWEEN` keyword before or not. |
|
109
|
|
|
* |
|
110
|
|
|
* It is required to keep track of them because their structure contains |
|
111
|
|
|
* the keyword `AND`, which is also an operator that delimits |
|
112
|
|
|
* expressions. |
|
113
|
|
|
* |
|
114
|
|
|
* @var bool |
|
115
|
|
|
*/ |
|
116
|
60 |
|
$betweenBefore = false; |
|
117
|
|
|
|
|
118
|
60 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
|
119
|
|
|
/** |
|
120
|
|
|
* Token parsed at this moment. |
|
121
|
|
|
* |
|
122
|
|
|
* @var Token |
|
123
|
|
|
*/ |
|
124
|
60 |
|
$token = $list->tokens[$list->idx]; |
|
125
|
|
|
|
|
126
|
|
|
// End of statement. |
|
127
|
60 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
|
128
|
31 |
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Skipping whitespaces and comments. |
|
132
|
60 |
|
if ($token->type === Token::TYPE_COMMENT) { |
|
133
|
5 |
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// Replacing all whitespaces (new lines, tabs, etc.) with a single |
|
137
|
|
|
// space character. |
|
138
|
60 |
|
if ($token->type === Token::TYPE_WHITESPACE) { |
|
139
|
59 |
|
$expr->expr .= ' '; |
|
140
|
59 |
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Conditions are delimited by logical operators. |
|
144
|
60 |
|
if (in_array($token->value, static::$DELIMITERS, true)) { |
|
145
|
10 |
|
if (($betweenBefore) && ($token->value === 'AND')) { |
|
146
|
|
|
// The syntax of keyword `BETWEEN` is hard-coded. |
|
147
|
1 |
|
$betweenBefore = false; |
|
148
|
1 |
|
} else { |
|
149
|
|
|
// The expression ended. |
|
150
|
10 |
|
$expr->expr = trim($expr->expr); |
|
151
|
10 |
|
if (!empty($expr->expr)) { |
|
152
|
10 |
|
$ret[] = $expr; |
|
153
|
10 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
// Adding the operator. |
|
156
|
10 |
|
$expr = new self($token->value); |
|
157
|
10 |
|
$expr->isOperator = true; |
|
158
|
10 |
|
$ret[] = $expr; |
|
159
|
|
|
|
|
160
|
|
|
// Preparing to parse another condition. |
|
161
|
10 |
|
$expr = new self(); |
|
162
|
10 |
|
continue; |
|
163
|
|
|
} |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
60 |
|
if (($token->type === Token::TYPE_KEYWORD) |
|
167
|
60 |
|
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED) |
|
168
|
60 |
|
&& !($token->flags & Token::FLAG_KEYWORD_FUNCTION) |
|
169
|
60 |
|
) { |
|
170
|
33 |
|
if ($token->value === 'BETWEEN') { |
|
171
|
1 |
|
$betweenBefore = true; |
|
172
|
1 |
|
} |
|
173
|
33 |
|
if (($brackets === 0) && (empty(static::$ALLOWED_KEYWORDS[$token->value]))) { |
|
174
|
31 |
|
break; |
|
175
|
|
|
} |
|
176
|
3 |
|
} |
|
177
|
|
|
|
|
178
|
59 |
View Code Duplication |
if ($token->type === Token::TYPE_OPERATOR) { |
|
179
|
57 |
|
if ($token->value === '(') { |
|
180
|
11 |
|
++$brackets; |
|
181
|
57 |
|
} elseif ($token->value === ')') { |
|
182
|
12 |
|
if ($brackets == 0) { |
|
183
|
1 |
|
break; |
|
184
|
|
|
} |
|
185
|
11 |
|
--$brackets; |
|
186
|
11 |
|
} |
|
187
|
57 |
|
} |
|
188
|
|
|
|
|
189
|
59 |
|
$expr->expr .= $token->token; |
|
190
|
59 |
|
if (($token->type === Token::TYPE_NONE) |
|
191
|
59 |
|
|| (($token->type === Token::TYPE_KEYWORD) |
|
192
|
59 |
|
&& (!($token->flags & Token::FLAG_KEYWORD_RESERVED))) |
|
193
|
59 |
|
|| ($token->type === Token::TYPE_STRING) |
|
194
|
59 |
|
|| ($token->type === Token::TYPE_SYMBOL) |
|
195
|
59 |
|
) { |
|
196
|
41 |
|
if (!in_array($token->value, $expr->identifiers)) { |
|
197
|
41 |
|
$expr->identifiers[] = $token->value; |
|
198
|
41 |
|
} |
|
199
|
41 |
|
} |
|
200
|
59 |
|
} |
|
201
|
|
|
|
|
202
|
|
|
// Last iteration was not processed. |
|
203
|
60 |
|
$expr->expr = trim($expr->expr); |
|
204
|
60 |
|
if (!empty($expr->expr)) { |
|
205
|
59 |
|
$ret[] = $expr; |
|
206
|
59 |
|
} |
|
207
|
|
|
|
|
208
|
60 |
|
--$list->idx; |
|
209
|
|
|
|
|
210
|
60 |
|
return $ret; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param Condition[] $component the component to be built |
|
215
|
|
|
* @param array $options parameters for building |
|
216
|
|
|
* |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
11 |
|
public static function build($component, array $options = array()) |
|
220
|
|
|
{ |
|
221
|
11 |
|
if (is_array($component)) { |
|
222
|
11 |
|
return implode(' ', $component); |
|
223
|
|
|
} else { |
|
224
|
11 |
|
return $component->expr; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.