-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathsqlparser_example.dart
More file actions
62 lines (55 loc) · 1.9 KB
/
sqlparser_example.dart
File metadata and controls
62 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'package:sqlparser/sqlparser.dart';
// Example that parses a select statement on some tables defined below and
// prints what columns would be returned by that statement.
void main() {
final engine = SqlEngine()
..registerTableFromSql('''
CREATE TABLE frameworks (
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
popularity REAL NOT NULL
);
''')
..registerTableFromSql('''
CREATE TABLE languages (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
''')
..registerTableFromSql('''
CREATE TABLE uses_language (
framework INTEGER NOT NULL REFERENCES frameworks (id),
language INTEGER NOT NULL REFERENCES languages (id),
PRIMARY KEY (framework, language)
);
''');
// Use SqlEngine.analyze to parse a single sql statement and analyze it.
// Analysis can be used to find semantic errors, lints and inferred types of
// expressions or result columns.
final result = engine.analyze('''
SELECT f.* FROM frameworks f
INNER JOIN uses_language ul ON ul.framework = f.id
INNER JOIN languages l ON l.id = ul.language
WHERE l.name = 'Dart'
ORDER BY f.name ASC, f.popularity DESC
LIMIT 5 OFFSET 5 * 3
''');
result.errors.forEach(print);
final select = result.root as SelectStatement;
final columns = select.resolvedColumns!;
print('the query returns ${columns.length} columns');
for (final column in columns) {
final type = result.typeOf(column);
print('${column.name}, which will be a $type');
}
}
extension on SqlEngine {
/// Utility function that parses a `CREATE TABLE` statement and registers the
/// created table to the engine.
void registerTableFromSql(String createTable) {
final stmt =
parse(ParserEntrypoint.statement, createTable).rootNode
as CreateTableStatement;
registerTable(schemaReader.read(stmt));
}
}