Skip to content

Commit

Permalink
Merge branch 'QA'
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Aug 14, 2021
2 parents 184a803 + 5876ece commit e283bef
Show file tree
Hide file tree
Showing 9 changed files with 1,380 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/Statements/CreateStatement.php
Expand Up @@ -438,7 +438,7 @@ public function build()
return 'CREATE '
. OptionsArray::build($this->options) . ' '
. Expression::build($this->name) . ' '
. $fields . ' AS ' . ($this->select ? $this->select->build() : TokensList::build($this->body)) . ' '
. $fields . ' AS ' . ($this->select ? $this->select->build() : '') . (! empty($this->body) ? TokensList::build($this->body) : '') . ' '
. OptionsArray::build($this->entityOptions);
} elseif ($this->options->has('TRIGGER')) {
return 'CREATE '
Expand Down Expand Up @@ -677,15 +677,16 @@ public function parse(Parser $parser, TokensList $list)
// Parsing the SELECT expression with and without the `AS` keyword
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'SELECT') {
$this->select = new SelectStatement($parser, $list);
} elseif (
$token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'AS'
&& $list->tokens[$nextidx]->type === Token::TYPE_KEYWORD
&& $list->tokens[$nextidx]->value === 'SELECT'
) {
$list->idx = $nextidx;
$this->select = new SelectStatement($parser, $list);
} else {
if (
$token->type === Token::TYPE_KEYWORD
&& $token->keyword === 'AS'
&& $list->tokens[$nextidx]->type === Token::TYPE_KEYWORD
&& $list->tokens[$nextidx]->value === 'SELECT'
) {
$list->idx = $nextidx;
$this->select = new SelectStatement($parser, $list);
}
for (; $list->idx < $list->count; ++$list->idx) {
$token = $list->tokens[$list->idx];
if ($token->type === Token::TYPE_DELIMITER) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Builder/CreateStatementTest.php
Expand Up @@ -306,6 +306,22 @@ public function testBuilderView(): void
'SELECT id, first_name, FROMzz employee WHERE id = 1 ',
$stmt->build()
);

$parser = new Parser(
'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS ' .
'SELECT id, first_name, FROMzz employee WHERE id = 1 ' .
'UNION ' .
'SELECT id, first_name, FROMzz employee WHERE id = 2 '
);
$stmt = $parser->statements[0];

$this->assertEquals(
'CREATE OR REPLACE VIEW myView (vid, vfirstname) AS ' .
'SELECT id, first_name, FROMzz employee WHERE id = 1 ' .
'UNION ' .
'SELECT id, first_name, FROMzz employee WHERE id = 2 ',
$stmt->build()
);
}

public function testBuilderTrigger(): void
Expand Down
1 change: 1 addition & 0 deletions tests/Parser/CreateStatementTest.php
Expand Up @@ -55,6 +55,7 @@ public function createProvider(): array
['parser/parseCreateViewWithoutQuotes'],
['parser/parseCreateViewWithQuotes'],
['parser/parseCreateViewWithWrongSyntax'],
['parser/parseCreateViewWithUnion'],
];
}
}
8 changes: 6 additions & 2 deletions tests/data/parser/parseCreateView.out
Expand Up @@ -3496,7 +3496,11 @@
"table": null,
"return": null,
"parameters": null,
"body": [],
"body": [
{
"@type": "@286"
}
],
"CLAUSES": [],
"END_OPTIONS": [],
"options": {
Expand Down Expand Up @@ -3546,7 +3550,7 @@
}
},
"first": 0,
"last": 284
"last": 285
}
],
"brackets": 0,
Expand Down
8 changes: 6 additions & 2 deletions tests/data/parser/parseCreateViewWithQuotes.out
Expand Up @@ -1190,7 +1190,11 @@
"table": null,
"return": null,
"parameters": null,
"body": [],
"body": [
{
"@type": "@43"
}
],
"CLAUSES": [],
"END_OPTIONS": [],
"options": {
Expand Down Expand Up @@ -1240,7 +1244,7 @@
}
},
"first": 0,
"last": 41
"last": 42
}
],
"brackets": 0,
Expand Down
12 changes: 12 additions & 0 deletions tests/data/parser/parseCreateViewWithUnion.in
@@ -0,0 +1,12 @@
CREATE VIEW `employees_view` AS
SELECT *
FROM
`employees`
WHERE
`employees`.`gender` = 'M'
UNION
SELECT *
FROM
`employees`
WHERE
`employees`.`gender` = 'F';

0 comments on commit e283bef

Please sign in to comment.