Skip to content

Commit 2737d05

Browse files
Added tests and solution for ignoring empty statements
1 parent 8ff8c1d commit 2737d05

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/Tokenizer.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,23 @@ protected function statements()
5555
{
5656
$statements = [];
5757

58-
while ($result = $this->statement()) {
59-
$statements[] = $result;
60-
}
58+
do {
59+
$result = $this->statement();
60+
if ($result !== null && count($result) > 0) {
61+
$statements[] = $result;
62+
}
63+
} while ($result !== null);
6164

6265
return $statements;
6366
}
6467

6568
/**
69+
* Returns an array containing the tokens in the next statement of the input.
70+
*
71+
* Returns empty array on empty statements (i.e. repeated delimiters, like: ;;;).
72+
*
73+
* Returns null if End-Of-File reached.
74+
*
6675
* @return string[]|null
6776
*/
6877
protected function statement()
@@ -76,6 +85,10 @@ protected function statement()
7685
$tokens = [];
7786

7887
while ("" !== $token = $this->token()) {
88+
/**
89+
* DEV NOTE: This checks for DELIMITER statement, that changes delimiter from ; to something else.
90+
* If detected, it will extract the new DELIMITER and reassign $this->delimiter_pattern.
91+
*/
7992
if (is_string($token) && preg_match('/^delimiter$/i', $token) === 1) {
8093
$this->consume('[ ]*');
8194

@@ -218,7 +231,7 @@ protected function grouped()
218231
if ($this->is($closing)) {
219232
$tokens[] = $closing;
220233

221-
$this->offset +=1;
234+
$this->offset += 1;
222235

223236
return $tokens;
224237
}

test/test.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ function () {
4040
}
4141
);
4242

43+
test(
44+
'Empty statements',
45+
function () {
46+
eq(Tokenizer::tokenize("SELECT 1;; SELECT 2"), [["SELECT", " ", "1"], ["SELECT", " ", "2"]], "Empty statement - in between");
47+
eq(Tokenizer::tokenize(";;SELECT 1; SELECT 2;"), [["SELECT", " ", "1"], ["SELECT", " ", "2"]], "Empty statements - first");
48+
eq(Tokenizer::tokenize("SELECT 1; SELECT 2;;;"), [["SELECT", " ", "1"], ["SELECT", " ", "2"]], "Empty statements - last");
49+
eq(Tokenizer::tokenize(";;;;SELECT 1;;;; SELECT 2;;;"), [["SELECT", " ", "1"], ["SELECT", " ", "2"]], "Empty statements - all over");
50+
}
51+
);
52+
4353
test(
4454
'comments',
4555
function () {

0 commit comments

Comments
 (0)