Test Failed
Pull Request — master (#146)
by Raghuram
03:39
created

BufferedQuery::setDelimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Buffered query utilities.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Utils;
8
9
use PhpMyAdmin\SqlParser\Context;
10
11
/**
12
 * Buffer query utilities.
13
 *
14
 * Implements a specialized lexer used to extract statements from large inputs
15
 * that are being buffered. After each statement has been extracted, a lexer or
16
 * a parser may be used.
17
 *
18
 * All comments are skipped, with one exception: MySQL commands inside `/*!`.
19
 *
20
 * @category   Lexer
21
 *
22
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
23
 */
24
class BufferedQuery
25
{
26
    // Constants that describe the current status of the parser.
27
28
    // A string is being parsed.
29
    const STATUS_STRING = 16; // 0001 0000
30
    const STATUS_STRING_SINGLE_QUOTES = 17; // 0001 0001
31
    const STATUS_STRING_DOUBLE_QUOTES = 18; // 0001 0010
32
    const STATUS_STRING_BACKTICK = 20; // 0001 0100
33
34
    // A comment is being parsed.
35
    const STATUS_COMMENT = 32; // 0010 0000
36
    const STATUS_COMMENT_BASH = 33; // 0010 0001
37
    const STATUS_COMMENT_C = 34; // 0010 0010
38
    const STATUS_COMMENT_SQL = 36; // 0010 0100
39
40
    /**
41
     * The query that is being processed.
42
     *
43
     * This field can be modified just by appending to it!
44
     *
45
     * @var string
46
     */
47
    public $query = '';
48
49
    /**
50
     * The options of this parser.
51
     *
52
     * @var array
53
     */
54
    public $options = array();
55
56
    /**
57
     * The last delimiter used.
58
     *
59
     * @var string
60
     */
61
    public $delimiter;
62
63
    /**
64
     * The length of the delimiter.
65
     *
66
     * @var int
67
     */
68
    public $delimiterLen;
69
70
    /**
71
     * The current status of the parser.
72
     *
73
     * @var int
74
     */
75
    public $status;
76
77
    /**
78
     * The last incomplete query that was extracted.
79
     *
80
     * @var string
81
     */
82
    public $current = '';
83
84
    /**
85
     * Constructor.
86
     *
87
     * @param string $query   the query to be parsed
88
     * @param array  $options the options of this parser
89
     */
90 7
    public function __construct($query = '', array $options = array())
91
    {
92
        // Merges specified options with defaults.
93 7
        $this->options = array_merge(
94
            array(
95
                /*
96
                 * The starting delimiter.
97
                 *
98
                 * @var string
99
                 */
100 7
                'delimiter' => ';',
101
102
                /*
103
                 * Whether `DELIMITER` statements should be parsed.
104
                 *
105
                 * @var bool
106
                 */
107
                'parse_delimiter' => false,
108
109
                /*
110
                 * Whether a delimiter should be added at the end of the
111
                 * statement.
112
                 *
113
                 * @var bool
114
                 */
115
                'add_delimiter' => false,
116
            ),
117
            $options
118
        );
119
120 7
        $this->query = $query;
121 7
        $this->setDelimiter($this->options['delimiter']);
122 7
    }
123
124
    /**
125
     * Sets the delimiter.
126
     *
127
     * Used to update the length of it too.
128
     *
129
     * @param string $delimiter
130
     */
131 7
    public function setDelimiter($delimiter)
132
    {
133 7
        $this->delimiter = $delimiter;
134 7
        $this->delimiterLen = strlen($delimiter);
135 7
    }
136
137
    /**
138
     * Extracts a statement from the buffer.
139
     *
140
     * @param bool $end whether the end of the buffer was reached
141
     *
142
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
143
     */
144 7
    public function extract($end = false)
145
    {
146
        /**
147
         * The last parsed position.
148
         *
149
         * This is statically defined because it is not used outside anywhere
150
         * outside this method and there is probably a (minor) performance
151
         * improvement to it.
152
         *
153
         * @var int
154
         */
155 7
        static $i = 0;
156
157 7
        if (empty($this->query)) {
158 7
            return false;
159
        }
160
161
        /**
162
         * The length of the buffer.
163
         *
164
         * @var int
165
         */
166 7
        $len = strlen($this->query);
167
168
        /**
169
         * The last index of the string that is going to be parsed.
170
         *
171
         * There must be a few characters left in the buffer so the parser can
172
         * avoid confusing some symbols that may have multiple meanings.
173
         *
174
         * For example, if the buffer ends in `-` that may be an operator or the
175
         * beginning of a comment.
176
         *
177
         * Another example if the buffer ends in `DELIMITE`. The parser is going
178
         * to require a few more characters because that may be a part of the
179
         * `DELIMITER` keyword or just a column named `DELIMITE`.
180
         *
181
         * Those extra characters are required only if there is more data
182
         * expected (the end of the buffer was not reached).
183
         *
184
         * @var int
185
         */
186 7
        $loopLen = $end ? $len : $len - 16;
187
188 7
        for (; $i < $loopLen; ++$i) {
189
            /*
190
             * Handling backslash.
191
             *
192
             * Even if the next character is a special character that should be
193
             * treated differently, because of the preceding backslash, it will
194
             * be ignored.
195
             */
196 7
            if ((($this->status & static::STATUS_COMMENT) == 0) && ($this->query[$i] === '\\')) {
197 3
                $this->current .= $this->query[$i] . $this->query[++$i];
198 3
                continue;
199
            }
200
201
            /*
202
             * Handling special parses statuses.
203
             */
204 7
            if ($this->status === static::STATUS_STRING_SINGLE_QUOTES) {
205
                // Single-quoted strings like 'foo'.
206 5
                if ($this->query[$i] === '\'') {
207 5
                    $this->status = 0;
208
                }
209 5
                $this->current .= $this->query[$i];
210 5
                continue;
211 7 View Code Duplication
            } elseif ($this->status === static::STATUS_STRING_DOUBLE_QUOTES) {
212
                // Double-quoted strings like "bar".
213 4
                if ($this->query[$i] === '"') {
214 4
                    $this->status = 0;
215
                }
216 4
                $this->current .= $this->query[$i];
217 4
                continue;
218 7
            } elseif ($this->status === static::STATUS_STRING_BACKTICK) {
219 4
                if ($this->query[$i] === '`') {
220 4
                    $this->status = 0;
221
                }
222 4
                $this->current .= $this->query[$i];
223 4
                continue;
224 7
            } elseif (($this->status === static::STATUS_COMMENT_BASH)
225 7
                || ($this->status === static::STATUS_COMMENT_SQL)
226
            ) {
227
                // Bash-like (#) or SQL-like (-- ) comments end in new line.
228 3
                if ($this->query[$i] === "\n") {
229 3
                    $this->status = 0;
230
                }
231 3
            } elseif ($this->status === static::STATUS_COMMENT_C) {
232 7
                // C-like comments end in */.
233
                if (($this->query[$i - 1] === '*') && ($this->query[$i] === '/')) {
234 3
                    $this->status = 0;
235 3
                }
236
            }
237 3
238
            /*
239
             * Checking if a string started.
240
             */
241
            if ($this->query[$i] === '\'') {
242
                $this->status = static::STATUS_STRING_SINGLE_QUOTES;
243 7
                $this->current .= $this->query[$i];
244 5
                continue;
245 5 View Code Duplication
            } elseif ($this->query[$i] === '"') {
246 5
                $this->status = static::STATUS_STRING_DOUBLE_QUOTES;
247 7
                $this->current .= $this->query[$i];
248 4
                continue;
249 4
            } elseif ($this->query[$i] === '`') {
250 4
                $this->status = static::STATUS_STRING_BACKTICK;
251 7
                $this->current .= $this->query[$i];
252 4
                continue;
253 4
            }
254 4
255
            /*
256
             * Checking if a comment started.
257
             */
258
            if ($this->query[$i] === '#') {
259
                $this->status = static::STATUS_COMMENT_BASH;
260 7
            } elseif (($i + 2 < $len)
261 3
                && ($this->query[$i] === '-')
262 3
                && ($this->query[$i + 1] === '-')
263 7
                && (Context::isWhitespace($this->query[$i + 2]))
264 7
            ) {
265 7
                $this->status = static::STATUS_COMMENT_SQL;
266 7
            } elseif (($i + 2 < $len)
267
                && ($this->query[$i] === '/')
268 3
                && ($this->query[$i + 1] === '*')
269 3
                && ($this->query[$i + 2] !== '!')
270 7
            ) {
271 7
                $this->status = static::STATUS_COMMENT_C;
272 7
            }
273 7
274
            /*
275 3
             * Handling `DELIMITER` statement.
276 3
             *
277
             * The code below basically checks for
278
             *     `strtoupper(substr($this->query, $i, 9)) === 'DELIMITER'`
279
             *
280
             * This optimization makes the code about 3 times faster.
281
             *
282
             * `DELIMITER` is not being considered a keyword. The only context
283
             * it has a special meaning is when it is the beginning of a
284
             * statement. This is the reason for the last condition.
285
             */
286
            if (($i + 9 < $len)
287
                && (($this->query[$i] === 'D') || ($this->query[$i] === 'd'))
288
                && (($this->query[$i + 1] === 'E') || ($this->query[$i + 1] === 'e'))
289
                && (($this->query[$i + 2] === 'L') || ($this->query[$i + 2] === 'l'))
290
                && (($this->query[$i + 3] === 'I') || ($this->query[$i + 3] === 'i'))
291 7
                && (($this->query[$i + 4] === 'M') || ($this->query[$i + 4] === 'm'))
292 7
                && (($this->query[$i + 5] === 'I') || ($this->query[$i + 5] === 'i'))
293 7
                && (($this->query[$i + 6] === 'T') || ($this->query[$i + 6] === 't'))
294 7
                && (($this->query[$i + 7] === 'E') || ($this->query[$i + 7] === 'e'))
295 7
                && (($this->query[$i + 8] === 'R') || ($this->query[$i + 8] === 'r'))
296 7
                && (Context::isWhitespace($this->query[$i + 9]))
297 7
                && (trim($this->current) === '')
298 7
            ) {
299 7
                // Saving the current index to be able to revert any parsing
300 7
                // done in this block.
301 7
                $iBak = $i;
302 7
                $i += 9; // Skipping `DELIMITER`.
303
304
                // Skipping whitespaces.
305
                while (($i < $len) && (Context::isWhitespace($this->query[$i]))) {
306 4
                    ++$i;
307 4
                }
308
309
                // Parsing the delimiter.
310 4
                $delimiter = '';
311 4
                while (($i < $len) && (!Context::isWhitespace($this->query[$i]))) {
312
                    $delimiter .= $this->query[$i++];
313
                }
314
315 4
                // Checking if the delimiter definition ended.
316 4
                if (($delimiter != '')
317 4
                    && ((($i < $len) && (Context::isWhitespace($this->query[$i])))
318
                    || (($i === $len) && ($end)))
319
                ) {
320
                    // Saving the delimiter.
321 4
                    $this->setDelimiter($delimiter);
322 4
323 4
                    // Whether this statement should be returned or not.
324
                    $ret = '';
325
                    if (!empty($this->options['parse_delimiter'])) {
326 4
                        // Appending the `DELIMITER` statement that was just
327
                        // found to the current statement.
328
                        $ret = trim(
329 4
                            $this->current . ' ' . substr($this->query, $iBak, $i - $iBak)
330 4
                        );
331
                    }
332
333 2
                    // Removing the statement that was just extracted from the
334 2
                    // query.
335
                    $this->query = substr($this->query, $i);
336
                    $i = 0;
337
338
                    // Resetting the current statement.
339
                    $this->current = '';
340 4
341 4
                    return $ret;
342
                }
343
344 4
                // Incomplete statement. Reverting
345
                $i = $iBak;
346 4
347
                return false;
348
            }
349
350 1
            /*
351
             * Checking if the current statement finished.
352 1
             *
353
             * The first letter of the delimiter is being checked as an
354
             * optimization. This code is almost as fast as the one above.
355
             *
356
             * There is no point in checking if two strings match if not even
357
             * the first letter matches.
358
             */
359
            if (($this->query[$i] === $this->delimiter[0])
360
                && (($this->delimiterLen === 1)
361
                || (substr($this->query, $i, $this->delimiterLen) === $this->delimiter))
362
            ) {
363
                // Saving the statement that just ended.
364 7
                $ret = $this->current;
365 7
366 7
                // If needed, adds a delimiter at the end of the statement.
367
                if (!empty($this->options['add_delimiter'])) {
368
                    $ret .= $this->delimiter;
369 7
                }
370
371
                // Removing the statement that was just extracted from the
372 7
                // query.
373 5
                $this->query = substr($this->query, $i + $this->delimiterLen);
374
                $i = 0;
375
376
                // Resetting the current statement.
377
                $this->current = '';
378 7
379 7
                // Returning the statement.
380
                return trim($ret);
381
            }
382 7
383
            /*
384
             * Appending current character to current statement.
385 7
             */
386
            $this->current .= $this->query[$i];
387
        }
388
389
        if (($end) && ($i === $len)) {
390
            // If the end of the buffer was reached, the buffer is emptied and
391 7
            // the current statement that was extracted is returned.
392
            $ret = $this->current;
393
394 7
            // Emptying the buffer.
395
            $this->query = '';
396
            $i = 0;
397 5
398
            // Resetting the current statement.
399
            $this->current = '';
400 5
401 5
            // Returning the statement.
402
            return trim($ret);
403
        }
404 5
405
        return '';
406
    }
407
}
408