From dc9a9e2db31f9ef138e0288ac64d24ab748364ec Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Wed, 15 Feb 2023 10:18:26 +0100 Subject: [PATCH] fix parsing of nested cte's --- .../analysis/resolver/drift/cte_test.dart | 31 +++++++++++++++++++ sqlparser/lib/src/reader/parser.dart | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/drift_dev/test/analysis/resolver/drift/cte_test.dart b/drift_dev/test/analysis/resolver/drift/cte_test.dart index 7785058e..b7d4ec79 100644 --- a/drift_dev/test/analysis/resolver/drift/cte_test.dart +++ b/drift_dev/test/analysis/resolver/drift/cte_test.dart @@ -5,6 +5,37 @@ import 'package:test/test.dart'; import '../../test_utils.dart'; void main() { + test('parse nested CTE', () async { + final backend = TestBackend.inTest({ + 'a|lib/test.drift': ''' +test: +SELECT + * + FROM + ( + WITH cte AS ( + SELECT 1 AS val + ) + SELECT * from cte + ); +''' + }); + + final file = await backend.analyze('package:a/test.drift'); + backend.expectNoErrors(); + + final query = + file.fileAnalysis!.resolvedQueries.values.single as SqlSelectQuery; + + expect(query.variables, isEmpty); + expect(query.readsFrom, isEmpty); + + final resultSet = query.resultSet; + expect(resultSet.singleColumn, isTrue); + expect(resultSet.needsOwnClass, isFalse); + expect(resultSet.scalarColumns.map((c) => c.sqlType), [DriftSqlType.int]); + }); + test('recognizes CTE clause', () async { final backend = TestBackend.inTest({ 'a|lib/test.drift': ''' diff --git a/sqlparser/lib/src/reader/parser.dart b/sqlparser/lib/src/reader/parser.dart index 0fea4c57..349d1c1a 100644 --- a/sqlparser/lib/src/reader/parser.dart +++ b/sqlparser/lib/src/reader/parser.dart @@ -1350,7 +1350,7 @@ class Parser { return tableRef; } else if (_matchOne(TokenType.leftParen)) { final first = _previous; - final innerStmt = select()!; + final innerStmt = _fullSelect()!; _consume(TokenType.rightParen, 'Expected a right bracket to terminate the inner select');