diff options
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | src/Components/AlterOperation.php | 2 | ||||
-rw-r--r-- | src/Components/CreateDefinition.php | 5 | ||||
-rw-r--r-- | src/Components/Expression.php | 2 | ||||
-rw-r--r-- | src/Components/ExpressionArray.php | 2 | ||||
-rw-r--r-- | src/Statement.php | 14 | ||||
-rw-r--r-- | src/Statements/TruncateStatement.php | 10 | ||||
-rw-r--r-- | src/Utils/CLI.php | 9 | ||||
-rw-r--r-- | tests/Components/CreateDefinitionTest.php | 16 | ||||
-rw-r--r-- | tests/Utils/CLITest.php | 34 |
10 files changed, 79 insertions, 20 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 95a986f..bf72c73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## [Unreleased] - +* Fix for PHP deprecations messages about implode for php 7.4+ (#258) +* Parse CHECK keyword on table definition (#264) +* Parse truncate statement (#221) + ## [5.0.0] - 2019-05-09 * Drop support for PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, PHP 7.0 and HHVM diff --git a/src/Components/AlterOperation.php b/src/Components/AlterOperation.php index 2b83ae5..2540521 100644 --- a/src/Components/AlterOperation.php +++ b/src/Components/AlterOperation.php @@ -339,7 +339,7 @@ class AlterOperation extends Component 'COMMENT', 'DEFAULT', 'CHARACTER SET', - 'COLLATE' + 'COLLATE', ]; // Since these options can be used for // both table as well as a specific column in the table diff --git a/src/Components/CreateDefinition.php b/src/Components/CreateDefinition.php index 388a080..03f8225 100644 --- a/src/Components/CreateDefinition.php +++ b/src/Components/CreateDefinition.php @@ -80,6 +80,11 @@ class CreateDefinition extends Component 'VIRTUAL' => 10, 'PERSISTENT' => 11, 'STORED' => 11, + 'CHECK' => [ + 12, + 'expr', + ['parenthesesDelimited' => true], + ] // Common entries. // // NOTE: Some of the common options are not in the same order which diff --git a/src/Components/Expression.php b/src/Components/Expression.php index 9c21def..108f335 100644 --- a/src/Components/Expression.php +++ b/src/Components/Expression.php @@ -433,7 +433,7 @@ class Expression extends Component public static function build($component, array $options = []) { if (is_array($component)) { - return implode($component, ', '); + return implode(', ', $component); } if ($component->expr !== '' && ! is_null($component->expr)) { diff --git a/src/Components/ExpressionArray.php b/src/Components/ExpressionArray.php index 2f44d7a..eb05aac 100644 --- a/src/Components/ExpressionArray.php +++ b/src/Components/ExpressionArray.php @@ -122,6 +122,6 @@ class ExpressionArray extends Component $ret[] = $frag::build($frag); } - return implode($ret, ', '); + return implode(', ', $ret); } } diff --git a/src/Statement.php b/src/Statement.php index de94951..d2d0ed2 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -320,11 +320,15 @@ abstract class Statement } // 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']; } } diff --git a/src/Statements/TruncateStatement.php b/src/Statements/TruncateStatement.php index 52f6347..519ffaf 100644 --- a/src/Statements/TruncateStatement.php +++ b/src/Statements/TruncateStatement.php @@ -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 . ';'; + } } diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php index 4881c5b..cd27908 100644 --- a/src/Utils/CLI.php +++ b/src/Utils/CLI.php @@ -78,7 +78,7 @@ class CLI return 0; } - if (!isset($params['q'])) { + if (! isset($params['q'])) { if ($stdIn = $this->readStdin()) { $params['q'] = $stdIn; } @@ -134,7 +134,7 @@ class CLI if (isset($params['c'])) { Context::load($params['c']); } - if (!isset($params['q'])) { + if (! isset($params['q'])) { if ($stdIn = $this->readStdin()) { $params['q'] = $stdIn; } @@ -190,7 +190,7 @@ class CLI return 0; } - if (!isset($params['q'])) { + if (! isset($params['q'])) { if ($stdIn = $this->readStdin()) { $params['q'] = $stdIn; } @@ -218,7 +218,8 @@ class CLI return 1; } - private function readStdin() { + private function readStdin() + { stream_set_blocking(STDIN, false); $stdin = stream_get_contents(STDIN); // restore-default block-mode setting diff --git a/tests/Components/CreateDefinitionTest.php b/tests/Components/CreateDefinitionTest.php index 6b97cb1..a1f50b8 100644 --- a/tests/Components/CreateDefinitionTest.php +++ b/tests/Components/CreateDefinitionTest.php @@ -64,4 +64,20 @@ class CreateDefinitionTest extends TestCase CreateDefinition::build($parser->statements[0]->fields[1]) ); } + + public function testBuild2() + { + $parser = new Parser( + 'CREATE TABLE `payment` (' . + '-- snippet' . "\n" . + '`customer_id` smallint(5) unsigned NOT NULL,' . + '`customer_data` longtext CHARACTER SET utf8mb4 CHARSET utf8mb4_bin NOT NULL CHECK (json_valid(customer_data)),' . + 'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE' . + ') ENGINE=InnoDB"' + ); + $this->assertEquals( + 'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE', + CreateDefinition::build($parser->statements[0]->fields[2]) + ); + } } diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php index 0f3b9e6..ec69ad9 100644 --- a/tests/Utils/CLITest.php +++ b/tests/Utils/CLITest.php @@ -220,21 +220,39 @@ class CLITest extends TestCase */ public function testStdinPipe($cmd, $result) { - exec ($cmd, $out, $ret); + exec($cmd, $out, $ret); $this->assertSame($result, $ret); } public function stdinParams() { - $binPath = PHP_BINARY .' '. dirname(__DIR__,2 ). '/bin/'; + $binPath = PHP_BINARY . ' ' . dirname(__DIR__, 2) . '/bin/'; return [ - ['echo "SELECT 1" | '. $binPath .'highlight-query', 0], - ['echo "invalid query" | '. $binPath .'highlight-query', 0], - ['echo "SELECT 1" | '. $binPath .'lint-query', 0], - ['echo "invalid query" | '. $binPath .'lint-query', 10], - ['echo "SELECT 1" | '. $binPath .'tokenize-query', 0], - ['echo "invalid query" | '. $binPath .'tokenize-query', 0], + [ + 'echo "SELECT 1" | ' . $binPath . 'highlight-query', + 0, + ], + [ + 'echo "invalid query" | ' . $binPath . 'highlight-query', + 0, + ], + [ + 'echo "SELECT 1" | ' . $binPath . 'lint-query', + 0, + ], + [ + 'echo "invalid query" | ' . $binPath . 'lint-query', + 10, + ], + [ + 'echo "SELECT 1" | ' . $binPath . 'tokenize-query', + 0, + ], + [ + 'echo "invalid query" | ' . $binPath . 'tokenize-query', + 0, + ], ]; } } |