Skip to content

Commit

Permalink
Handle TRUNCATE() function and TRUNCATE keyword. Issue #338. (#339)
Browse files Browse the repository at this point in the history
* Fix: Tests.

* Fix issue #322.

* Fix: Handle SubQuery in ref_clause.

* Fix: Handle TRUNCATE() function and TRUNCATE keyword. Issue #338.

---------

Co-authored-by: Justin Swanhart <greenlion@gmail.com>
  • Loading branch information
Herz3h and greenlion committed Mar 12, 2023
1 parent 8339ac3 commit 9ff74d9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/PHPSQLParser/processors/SQLProcessor.php
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
7 changes: 3 additions & 4 deletions tests/cases/creator/leftTest.php
Expand Up @@ -56,10 +56,9 @@ public function testLeft() {
$this->assertSame($expected, $created, 'left joins and table-expression');

}

/**
* @doesNotPerformAssertions
*/
/**
* @doesNotPerformAssertions
*/
public function testLeftIn() {
$sql = 'SELECT *
FROM (t1 LEFT JOIN t2 ON t1.a=t2.a)
Expand Down
13 changes: 7 additions & 6 deletions tests/cases/parser/issue322Test.php
Expand Up @@ -44,17 +44,18 @@

class issue322Test extends \PHPUnit\Framework\TestCase
{
/**
* @doesNotPerformAssertions
*/
/**
* @doesNotPerformAssertions
*/

public function testIssue322()
{
$sql = "SELECT IF(createdAt >= CURRENT_DATE(), '1', '0') FROM f_another_table WHERE id in(SELECT id FROM f_table WHERE createdAt > NOW())";
$sql = "SELECT IF(createdAt >= CURRENT_DATE(), '1', '0') FROM f_another_table WHERE id in(SELECT id FROM f_table WHERE createdAt > NOW())";
$parser = new PHPSQLParser();
$parsed = $parser->parse($sql, true);
$creator = new PHPSQLCreator();
$sql = $creator->create($parsed);
//echo $sql;
}

}
}

30 changes: 30 additions & 0 deletions tests/cases/parser/issue338Test.php
@@ -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 9ff74d9

Please sign in to comment.