mirror of https://github.com/AMT-Cheif/drift.git
Parse nested star columns
This commit is contained in:
parent
aed9b40c30
commit
d5ad3c6d34
|
@ -27,6 +27,7 @@ part 'moor/declared_statement.dart';
|
|||
part 'moor/import_statement.dart';
|
||||
part 'moor/inline_dart.dart';
|
||||
part 'moor/moor_file.dart';
|
||||
part 'moor/nested_star_result_column.dart';
|
||||
part 'schema/column_definition.dart';
|
||||
part 'schema/table_definition.dart';
|
||||
part 'statements/block.dart';
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
part of '../ast.dart';
|
||||
|
||||
/// A nested star result column, denoted by `**` in user queries.
|
||||
///
|
||||
/// Nested star result columns behave similar to a regular [StarResultColumn]
|
||||
/// when the query is actually run. However, they will affect generated code
|
||||
/// when using moor.
|
||||
class NestedStarResultColumn extends StarResultColumn {
|
||||
NestedStarResultColumn(String tableName) : super(tableName);
|
||||
}
|
|
@ -209,11 +209,16 @@ mixin CrudParser on ParserBase {
|
|||
// we have a star result column. If it's followed by anything else, it can
|
||||
// still refer to a column in a table as part of a expression
|
||||
// result column
|
||||
final identifier = _previous;
|
||||
final identifier = _previous as IdentifierToken;
|
||||
|
||||
if (_match(const [TokenType.dot]) && _match(const [TokenType.star])) {
|
||||
return StarResultColumn((identifier as IdentifierToken).identifier)
|
||||
if (_matchOne(TokenType.dot)) {
|
||||
if (_matchOne(TokenType.star)) {
|
||||
return StarResultColumn(identifier.identifier)
|
||||
..setSpan(identifier, _previous);
|
||||
} else if (enableMoorExtensions && _matchOne(TokenType.doubleStar)) {
|
||||
return NestedStarResultColumn(identifier.identifier)
|
||||
..setSpan(identifier, _previous);
|
||||
}
|
||||
}
|
||||
|
||||
// not a star result column. go back and parse the expression.
|
||||
|
|
|
@ -78,6 +78,9 @@ class Scanner {
|
|||
}
|
||||
break;
|
||||
case charStar:
|
||||
if (scanMoorTokens && _match(charStar)) {
|
||||
_addToken(TokenType.doubleStar);
|
||||
}
|
||||
_addToken(TokenType.star);
|
||||
break;
|
||||
case charSlash:
|
||||
|
|
|
@ -9,6 +9,9 @@ enum TokenType {
|
|||
$do,
|
||||
doublePipe,
|
||||
star,
|
||||
|
||||
/// A `**` token. This is only scanned when scanning for moor tokens.
|
||||
doubleStar,
|
||||
slash,
|
||||
percent,
|
||||
plus,
|
||||
|
|
|
@ -16,6 +16,7 @@ CREATE TABLE tbl (
|
|||
all: SELECT /* COUNT(*), */ * FROM tbl WHERE $predicate;
|
||||
@special: SELECT * FROM tbl;
|
||||
typeHints(:foo AS TEXT): SELECT :foo;
|
||||
nested: SELECT foo.** FROM tbl foo;
|
||||
''';
|
||||
|
||||
void main() {
|
||||
|
@ -87,6 +88,13 @@ void main() {
|
|||
)
|
||||
],
|
||||
),
|
||||
DeclaredStatement(
|
||||
SimpleName('nested'),
|
||||
SelectStatement(
|
||||
columns: [NestedStarResultColumn('foo')],
|
||||
from: TableReference('tbl', 'foo'),
|
||||
),
|
||||
),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'package:sqlparser/src/reader/tokenizer/token.dart';
|
|||
|
||||
void main() {
|
||||
test('parses moor specific tokens', () {
|
||||
const part = 'c INTEGER MAPPED BY `const Mapper()` NOT NULL';
|
||||
const part = 'c INTEGER MAPPED BY `const Mapper()` NOT NULL **';
|
||||
final scanner = Scanner(part, scanMoorTokens: true);
|
||||
final tokens = scanner.scanTokens();
|
||||
|
||||
|
@ -17,6 +17,7 @@ void main() {
|
|||
TokenType.inlineDart, // `const Mapper()`
|
||||
TokenType.not,
|
||||
TokenType.$null,
|
||||
TokenType.doubleStar,
|
||||
TokenType.eof,
|
||||
]);
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
import 'package:sqlparser/sqlparser.dart';
|
||||
import 'package:sqlparser/src/reader/tokenizer/scanner.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test('parses ** as two tokens when not using moor mode', () {
|
||||
final tokens = Scanner('**').scanTokens();
|
||||
expect(tokens.map((e) => e.type),
|
||||
containsAllInOrder([TokenType.star, TokenType.star]));
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue