Skip to content

Commit

Permalink
Merge branch 'QA'
Browse files Browse the repository at this point in the history
This merge includes phpcs fixes

Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Oct 28, 2019
2 parents 6de19ae + 49731d2 commit 1a073cd
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 20 deletions.
5 changes: 5 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion src/Components/AlterOperation.php
Expand Up @@ -339,7 +339,7 @@ private static function checkIfColumnDefinitionKeyword($tokenValue)
'COMMENT',
'DEFAULT',
'CHARACTER SET',
'COLLATE'
'COLLATE',
];
// Since these options can be used for
// both table as well as a specific column in the table
Expand Down
5 changes: 5 additions & 0 deletions src/Components/CreateDefinition.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Expression.php
Expand Up @@ -433,7 +433,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
public static function build($component, array $options = [])
{
if (is_array($component)) {
return implode($component, ', ');
return implode(', ', $component);
}

if ($component->expr !== '' && ! is_null($component->expr)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Components/ExpressionArray.php
Expand Up @@ -122,6 +122,6 @@ public static function build($component, array $options = [])
$ret[] = $frag::build($frag);
}

return implode($ret, ', ');
return implode(', ', $ret);
}
}
14 changes: 9 additions & 5 deletions src/Statement.php
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
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 . ';';
}
}
9 changes: 5 additions & 4 deletions src/Utils/CLI.php
Expand Up @@ -78,7 +78,7 @@ public function runHighlight()

return 0;
}
if (!isset($params['q'])) {
if (! isset($params['q'])) {
if ($stdIn = $this->readStdin()) {
$params['q'] = $stdIn;
}
Expand Down Expand Up @@ -134,7 +134,7 @@ public function runLint()
if (isset($params['c'])) {
Context::load($params['c']);
}
if (!isset($params['q'])) {
if (! isset($params['q'])) {
if ($stdIn = $this->readStdin()) {
$params['q'] = $stdIn;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ public function runTokenize()

return 0;
}
if (!isset($params['q'])) {
if (! isset($params['q'])) {
if ($stdIn = $this->readStdin()) {
$params['q'] = $stdIn;
}
Expand Down Expand Up @@ -218,7 +218,8 @@ public function runTokenize()
return 1;
}

private function readStdin() {
private function readStdin()
{
stream_set_blocking(STDIN, false);
$stdin = stream_get_contents(STDIN);
// restore-default block-mode setting
Expand Down
16 changes: 16 additions & 0 deletions tests/Components/CreateDefinitionTest.php
Expand Up @@ -64,4 +64,20 @@ public function testBuild()
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])
);
}
}
34 changes: 26 additions & 8 deletions tests/Utils/CLITest.php
Expand Up @@ -220,21 +220,39 @@ public function tokenizeParams()
*/
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,
],
];
}
}

0 comments on commit 1a073cd

Please sign in to comment.