Skip to content

Commit

Permalink
Fix: Handle TRUNCATE() function and TRUNCATE keyword. Issue greenlion…
Browse files Browse the repository at this point in the history
  • Loading branch information
Herz3h committed May 28, 2021
1 parent e6bb45c commit 2f01c54
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/PHPSQLParser/processors/SQLProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ public function process($tokens) {
case 'DELETE':
case 'ALTER':
case 'INSERT':
case 'TRUNCATE':
case 'OPTIMIZE':
case 'GRANT':
case 'REVOKE':
Expand All @@ -264,6 +263,17 @@ public function process($tokens) {
$out[$upper][0] = $trim;
continue 2;

case 'TRUNCATE':
if ($prev_category === '') {
// set the category in case these get subclauses in a future version of MySQL
$token_category = $upper;
$out[$upper][0] = $trim;
continue 2;
}
// part of the CREATE TABLE statement or a function
$out[$prev_category][] = $trim;
continue 2;

case 'REPLACE':
if ($prev_category === '') {
// set the category in case these get subclauses in a future version of MySQL
Expand Down
30 changes: 30 additions & 0 deletions tests/cases/parser/issue338Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace parser;

use PHPSQLParser\PHPSQLParser;

class issue338Test extends \PHPUnit_Framework_TestCase
{
public function testIssue338() {
$sql = "SELECT id, date, type as type, libelle as libelle, TRUNCATE(debit, 2) as debit, ROUND(COALESCE(credit, 0) - COALESCE(debit, 0), 2) as solde FROM compte_cp";

$parser = new PHPSQLParser();

$parser->parse($sql, true);
$parsed = $parser->parsed;

$this->assertNotFalse($parsed);
$this->assertTrue(is_array($parsed));
$this->assertTrue(!array_key_exists('TRUNCATE', $parsed));

$sql = "TRUNCATE TABLE truncate_table";
$parser->parse($sql, true);
$parsed = $parser->parsed;

$this->assertNotFalse($parsed);
$this->assertTrue(is_array($parsed));
$this->assertTrue(array_key_exists('TRUNCATE', $parsed));
}

}

0 comments on commit 2f01c54

Please sign in to comment.