Skip to content

Commit

Permalink
Fix #221 - parse truncate statement
Browse files Browse the repository at this point in the history
Fixes: #221
Pull-request: #222

Co-authored-by: Sinri Edogawa <[email protected]>
Signed-off-by: William Desportes <[email protected]>
  • Loading branch information
sinri authored and williamdes committed Oct 28, 2019
1 parent dedb5d5 commit 04da59b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,15 @@ public function parse(Parser $parser, TokensList $list)
}

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

Expand Down
10 changes: 10 additions & 0 deletions src/Statements/TruncateStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ class TruncateStatement extends Statement
* @var Expression
*/
public $table;

/**
* Special build method for truncate statement as Statement::build would return empty string.
*
* @return string
*/
public function build()
{
return 'TRUNCATE TABLE ' . $this->table . ';';
}
}

0 comments on commit 04da59b

Please sign in to comment.