-
Notifications
You must be signed in to change notification settings - Fork 153
/
AbstractProcessor.php
331 lines (289 loc) · 10.2 KB
/
AbstractProcessor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* AbstractProcessor.php
*
* This file implements an abstract processor, which implements some helper functions.
*
* PHP version 5
*
* LICENSE:
* Copyright (c) 2010-2014 Justin Swanhart and André Rothe
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author André Rothe <andre.rothe@phosco.info>
* @copyright 2010-2014 Justin Swanhart and André Rothe
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version SVN: $Id$
*
*/
namespace PHPSQLParser\processors;
use PHPSQLParser\lexer\PHPSQLLexer;
use PHPSQLParser\Options;
use PHPSQLParser\utils\ExpressionType;
/**
* This class contains some general functions for a processor.
*
* @author André Rothe <andre.rothe@phosco.info>
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
*
*/
abstract class AbstractProcessor {
/**
* @var Options
*/
protected $options;
/**
* AbstractProcessor constructor.
*
* @param Options $options
*/
public function __construct(Options $options)
{
$this->options = $options;
}
/**
* This function implements the main functionality of a processor class.
* Always use default valuses for additional parameters within overridden functions.
*/
public abstract function process($tokens);
/**
* this function splits up a SQL statement into easy to "parse"
* tokens for the SQL processor
*/
public function splitSQLIntoTokens($sql) {
$lexer = new PHPSQLLexer();
return $lexer->split($sql);
}
/**
* Revokes the quoting characters from an expression
* Possibibilies:
* `a`
* 'a'
* "a"
* `a`.`b`
* `a.b`
* a.`b`
* `a`.b
* It is also possible to have escaped quoting characters
* within an expression part:
* `a``b` => a`b
* And you can use whitespace between the parts:
* a . `b` => [a,b]
*/
protected function revokeQuotation($sql) {
$tmp = trim($sql);
$result = array();
$quote = false;
$start = 0;
$i = 0;
$len = strlen($tmp);
while ($i < $len) {
$char = $tmp[$i];
switch ($char) {
case '`':
case '\'':
case '"':
if ($quote === false) {
// start
$quote = $char;
$start = $i + 1;
break;
}
if ($quote !== $char) {
break;
}
if (isset($tmp[$i + 1]) && ($quote === $tmp[$i + 1])) {
// escaped
$i++;
break;
}
// end
$char = substr($tmp, $start, $i - $start);
$result[] = str_replace($quote . $quote, $quote, $char);
$start = $i + 1;
$quote = false;
break;
case '.':
if ($quote === false) {
// we have found a separator
$char = trim(substr($tmp, $start, $i - $start));
if ($char !== '') {
$result[] = $char;
}
$start = $i + 1;
}
break;
default:
// ignore
break;
}
$i++;
}
if ($quote === false && ($start < $len)) {
$char = trim(substr($tmp, $start, $i - $start));
if ($char !== '') {
$result[] = $char;
}
}
return array('delim' => (count($result) === 1 ? false : '.'), 'parts' => $result);
}
/**
* This method removes parenthesis from start of the given string.
* It removes also the associated closing parenthesis.
*/
protected function removeParenthesisFromStart($token) {
$parenthesisRemoved = 0;
$trim = trim($token);
if ($trim !== '' && $trim[0] === '(') { // remove only one parenthesis pair now!
$parenthesisRemoved++;
$trim[0] = ' ';
$trim = trim($trim);
}
$parenthesis = $parenthesisRemoved;
$i = 0;
$string = 0;
// Whether a string was opened or not, and with which character it was open (' or ")
$stringOpened = '';
while ($i < strlen($trim)) {
if ($trim[$i] === "\\") {
$i += 2; // an escape character, the next character is irrelevant
continue;
}
if ($trim[$i] === "'") {
if ($stringOpened === '') {
$stringOpened = "'";
} elseif ($stringOpened === "'") {
$stringOpened = '';
}
}
if ($trim[$i] === '"') {
if ($stringOpened === '') {
$stringOpened = '"';
} elseif ($stringOpened === '"') {
$stringOpened = '';
}
}
if (($stringOpened === '') && ($trim[$i] === '(')) {
$parenthesis++;
}
if (($stringOpened === '') && ($trim[$i] === ')')) {
if ($parenthesis == $parenthesisRemoved) {
$trim[$i] = ' ';
$parenthesisRemoved--;
}
$parenthesis--;
}
$i++;
}
return trim($trim);
}
protected function getVariableType($expression) {
// $expression must contain only upper-case characters
if ($expression[1] !== '@') {
return ExpressionType::USER_VARIABLE;
}
$type = substr($expression, 2, strpos($expression, '.', 2));
switch ($type) {
case 'GLOBAL':
$type = ExpressionType::GLOBAL_VARIABLE;
break;
case 'LOCAL':
$type = ExpressionType::LOCAL_VARIABLE;
break;
case 'SESSION':
default:
$type = ExpressionType::SESSION_VARIABLE;
break;
}
return $type;
}
protected function isCommaToken($token) {
return (trim($token) === ',');
}
protected function isWhitespaceToken($token) {
return (trim($token) === '');
}
protected function isCommentToken($token) {
return isset($token[0]) && isset($token[1])
&& (($token[0] === '-' && $token[1] === '-') || ($token[0] === '/' && $token[1] === '*'));
}
protected function isColumnReference($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::COLREF);
}
protected function isReserved($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::RESERVED);
}
protected function isConstant($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::CONSTANT);
}
protected function isAggregateFunction($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::AGGREGATE_FUNCTION);
}
protected function isCustomFunction($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::CUSTOM_FUNCTION);
}
protected function isFunction($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::SIMPLE_FUNCTION);
}
protected function isExpression($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::EXPRESSION);
}
protected function isBracketExpression($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::BRACKET_EXPRESSION);
}
protected function isSubQuery($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::SUBQUERY);
}
protected function isComment($out) {
return (isset($out['expr_type']) && $out['expr_type'] === ExpressionType::COMMENT);
}
public function processComment($expression) {
$result = array();
$result['expr_type'] = ExpressionType::COMMENT;
$result['value'] = $expression;
return $result;
}
/**
* translates an array of objects into an associative array
*/
public function toArray($tokenList) {
$expr = array();
foreach ($tokenList as $token) {
if ($token instanceof \PHPSQLParser\utils\ExpressionToken) {
$expr[] = $token->toArray();
} else {
$expr[] = $token;
}
}
return $expr;
}
protected function array_insert_after($array, $key, $entry) {
$idx = array_search($key, array_keys($array));
$array = array_slice($array, 0, $idx + 1, true) + $entry
+ array_slice($array, $idx + 1, count($array) - 1, true);
return $array;
}
}
?>