Skip to content

Commit 04da59b

Browse files
sinriwilliamdes
authored andcommitted
Fix #221 - parse truncate statement
Fixes: #221 Pull-request: #222 Co-authored-by: Sinri Edogawa <e.joshua.s.e@gmail.com> Signed-off-by: William Desportes <williamdes@wdes.fr>
1 parent dedb5d5 commit 04da59b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

‎src/Statement.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,15 @@ public function parse(Parser $parser, TokensList $list)
320320
}
321321

322322
// Checking if this is the beginning of a clause.
323-
if (! empty(Parser::$KEYWORD_PARSERS[$token->value]) && $list->idx < $list->count) {
324-
$class = Parser::$KEYWORD_PARSERS[$token->value]['class'];
325-
$field = Parser::$KEYWORD_PARSERS[$token->value]['field'];
326-
if (! empty(Parser::$KEYWORD_PARSERS[$token->value]['options'])) {
327-
$options = Parser::$KEYWORD_PARSERS[$token->value]['options'];
323+
// Fix Issue #221: As `truncate` is not a keyword
324+
// but it might be the beginning of a statement of truncate,
325+
// so let the value use the keyword field for truncate type.
326+
$token_value = in_array($token->keyword, ['TRUNCATE']) ? $token->keyword : $token->value;
327+
if (! empty(Parser::$KEYWORD_PARSERS[$token_value]) && $list->idx < $list->count) {
328+
$class = Parser::$KEYWORD_PARSERS[$token_value]['class'];
329+
$field = Parser::$KEYWORD_PARSERS[$token_value]['field'];
330+
if (! empty(Parser::$KEYWORD_PARSERS[$token_value]['options'])) {
331+
$options = Parser::$KEYWORD_PARSERS[$token_value]['options'];
328332
}
329333
}
330334

‎src/Statements/TruncateStatement.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ class TruncateStatement extends Statement
3333
* @var Expression
3434
*/
3535
public $table;
36+
37+
/**
38+
* Special build method for truncate statement as Statement::build would return empty string.
39+
*
40+
* @return string
41+
*/
42+
public function build()
43+
{
44+
return 'TRUNCATE TABLE ' . $this->table . ';';
45+
}
3646
}

0 commit comments

Comments
 (0)