Skip to content

Commit

Permalink
Update the with Build statement to build the cteStatement
Browse files Browse the repository at this point in the history
Signed-off-by: Fawzi E. Abdulfattah <iifawzie@gmail.com>
  • Loading branch information
iifawzi committed Dec 7, 2021
1 parent cd49677 commit 6b1a67b
Show file tree
Hide file tree
Showing 20 changed files with 6,869 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/Statements/WithStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ final class WithStatement extends Statement
/** @var WithKeyword[] */
public $withers = [];

/**
* holds the CTE statement.
*
* @var Statement
*/
public $cteStatement;

/**
* @param Parser $parser the instance that requests parsing
* @param TokensList $list the list of tokens to be parsed
Expand Down Expand Up @@ -243,8 +250,12 @@ public function parse(Parser $parser, TokensList $list)
foreach ($subParser->errors as $error) {
$parser->errors[] = $error;
}

break;
}

$this->cteStatement = $subParser;

$list->idx = $idxOfLastParsedToken;
break;
}
Expand Down Expand Up @@ -277,6 +288,12 @@ public function build()
$str .= WithKeyword::build($wither);
}

$str .= ' ';

foreach ($this->cteStatement->statements as $statement) {
$str .= $statement->build();
}

return $str;
}

Expand Down
17 changes: 12 additions & 5 deletions tests/Builder/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public function testBuilderTable(): void

$this->assertEquals(
'CREATE TABLE table_name WITH' .
' cte(col1) AS (SELECT 1 UNION ALL SELECT 2)',
' cte(col1) AS (SELECT 1 UNION ALL SELECT 2)' .
' SELECT col1 FROM cte',
$stmt->build()
);
}
Expand Down Expand Up @@ -370,9 +371,12 @@ public function testBuilderViewComplex(): void
$this->assertEquals(
'CREATE VIEW withclause AS '
. 'WITH cte AS ('
. 'SELECT p.name, p.shape'
. ' FROM gis_all AS `p`'
. ') ',
. 'SELECT p.name, p.shape '
. 'FROM gis_all AS `p`'
. ') '
. 'SELECT cte.* '
. 'FROM cte '
. 'CROSS JOIN gis_all ',
$stmt->build()
);
$parser = new Parser(
Expand Down Expand Up @@ -400,7 +404,10 @@ public function testBuilderViewComplex(): void
. '), cte2 AS ('
. 'SELECT p.name AS `n2`, p.shape AS `sh2`'
. ' FROM gis_all AS `p`'
. ') ',
. ')'
. ' SELECT cte.*, cte2.* '
. 'FROM cte, cte2'
. ' CROSS JOIN gis_all ',
$stmt->build()
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Parser/WithStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testWith(): void

// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<SQL
WITH categories(identifier, name, parent_id) AS (SELECT c.identifier, c.name, c.parent_id FROM category AS `c` WHERE c.identifier = 'a' UNION ALL SELECT c.identifier, c.name, c.parent_id FROM categories, category AS `c` WHERE c.identifier = categories.parent_id), foo AS (SELECT * FROM test)
WITH categories(identifier, name, parent_id) AS (SELECT c.identifier, c.name, c.parent_id FROM category AS `c` WHERE c.identifier = 'a' UNION ALL SELECT c.identifier, c.name, c.parent_id FROM categories, category AS `c` WHERE c.identifier = categories.parent_id), foo AS (SELECT * FROM test) SELECT * FROM categories
SQL;
// phpcs:enable
$this->assertEquals($expected, $parser->statements[0]->build());
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testWithEmbedParenthesis(): void

// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<SQL
WITH categories AS (SELECT * FROM (SELECT * FROM foo))
WITH categories AS (SELECT * FROM (SELECT * FROM foo)) SELECT * FROM categories
SQL;
// phpcs:enable
$this->assertEquals($expected, $parser->statements[0]->build());
Expand Down
3,157 changes: 3,157 additions & 0 deletions tests/data/parser/parseCreateViewAsWithAs.out

Large diffs are not rendered by default.

555 changes: 555 additions & 0 deletions tests/data/parser/parseWithStatement.out

Large diffs are not rendered by default.

555 changes: 555 additions & 0 deletions tests/data/parser/parseWithStatement1.out

Large diffs are not rendered by default.

555 changes: 555 additions & 0 deletions tests/data/parser/parseWithStatement2.out

Large diffs are not rendered by default.

555 changes: 555 additions & 0 deletions tests/data/parser/parseWithStatement3.out

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/data/parser/parseWithStatement4.out
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,7 @@
}
}
},
"cteStatement": null,
"END_OPTIONS": [],
"options": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
Expand Down
555 changes: 555 additions & 0 deletions tests/data/parser/parseWithStatement5.out

Large diffs are not rendered by default.

Loading

0 comments on commit 6b1a67b

Please sign in to comment.