Skip to content

Commit 9ff74d9

Browse files
Herz3hgreenlion
andauthored
Handle TRUNCATE() function and TRUNCATE keyword. Issue #338. (#339)
* 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>
1 parent 8339ac3 commit 9ff74d9

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

src/PHPSQLParser/processors/SQLProcessor.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ public function process($tokens) {
242242
case 'DELETE':
243243
case 'ALTER':
244244
case 'INSERT':
245-
case 'TRUNCATE':
246245
case 'OPTIMIZE':
247246
case 'GRANT':
248247
case 'REVOKE':
@@ -264,6 +263,17 @@ public function process($tokens) {
264263
$out[$upper][0] = $trim;
265264
continue 2;
266265

266+
case 'TRUNCATE':
267+
if ($prev_category === '') {
268+
// set the category in case these get subclauses in a future version of MySQL
269+
$token_category = $upper;
270+
$out[$upper][0] = $trim;
271+
continue 2;
272+
}
273+
// part of the CREATE TABLE statement or a function
274+
$out[$prev_category][] = $trim;
275+
continue 2;
276+
267277
case 'REPLACE':
268278
if ($prev_category === '') {
269279
// set the category in case these get subclauses in a future version of MySQL

tests/cases/creator/leftTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ public function testLeft() {
5656
$this->assertSame($expected, $created, 'left joins and table-expression');
5757

5858
}
59-
60-
/**
61-
* @doesNotPerformAssertions
62-
*/
59+
/**
60+
* @doesNotPerformAssertions
61+
*/
6362
public function testLeftIn() {
6463
$sql = 'SELECT *
6564
FROM (t1 LEFT JOIN t2 ON t1.a=t2.a)

tests/cases/parser/issue322Test.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@
4444

4545
class issue322Test extends \PHPUnit\Framework\TestCase
4646
{
47-
/**
48-
* @doesNotPerformAssertions
49-
*/
47+
/**
48+
* @doesNotPerformAssertions
49+
*/
50+
5051
public function testIssue322()
5152
{
52-
$sql = "SELECT IF(createdAt >= CURRENT_DATE(), '1', '0') FROM f_another_table WHERE id in(SELECT id FROM f_table WHERE createdAt > NOW())";
53+
$sql = "SELECT IF(createdAt >= CURRENT_DATE(), '1', '0') FROM f_another_table WHERE id in(SELECT id FROM f_table WHERE createdAt > NOW())";
5354
$parser = new PHPSQLParser();
5455
$parsed = $parser->parse($sql, true);
5556
$creator = new PHPSQLCreator();
5657
$sql = $creator->create($parsed);
57-
//echo $sql;
58-
}
58+
59+
}
5960
}
6061

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace parser;
4+
5+
use PHPSQLParser\PHPSQLParser;
6+
7+
class issue338Test extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testIssue338() {
10+
$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";
11+
12+
$parser = new PHPSQLParser();
13+
14+
$parser->parse($sql, true);
15+
$parsed = $parser->parsed;
16+
17+
$this->assertNotFalse($parsed);
18+
$this->assertTrue(is_array($parsed));
19+
$this->assertTrue(!array_key_exists('TRUNCATE', $parsed));
20+
21+
$sql = "TRUNCATE TABLE truncate_table";
22+
$parser->parse($sql, true);
23+
$parsed = $parser->parsed;
24+
25+
$this->assertNotFalse($parsed);
26+
$this->assertTrue(is_array($parsed));
27+
$this->assertTrue(array_key_exists('TRUNCATE', $parsed));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)