2019-06-15 14:01:10 -07:00
|
|
|
# sqlparser
|
|
|
|
|
2019-06-29 03:48:09 -07:00
|
|
|
An sql parser and static analyzer, written in pure Dart. At the moment, only `SELECT` statements
|
|
|
|
are supported.
|
2019-06-22 13:35:34 -07:00
|
|
|
|
2019-06-29 03:48:09 -07:00
|
|
|
## Features
|
|
|
|
Not all features are available yet, put parsing select statements (even complex ones!) and
|
|
|
|
performing analysis on them works!
|
2019-06-22 13:35:34 -07:00
|
|
|
|
2019-06-29 03:48:09 -07:00
|
|
|
### Just parsing
|
|
|
|
You can parse the abstract syntax tree of sqlite statements with `SqlEngine.parse`.
|
2019-06-22 13:35:34 -07:00
|
|
|
```dart
|
|
|
|
import 'package:sqlparser/sqlparser.dart';
|
|
|
|
|
|
|
|
final engine = SqlEngine();
|
2019-06-29 13:29:16 -07:00
|
|
|
final result = engine.parse('''
|
2019-06-22 13:35:34 -07:00
|
|
|
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
|
|
|
|
''');
|
2019-06-29 13:29:16 -07:00
|
|
|
// result.rootNode contains the select statement in tree form
|
2019-06-26 14:07:30 -07:00
|
|
|
```
|
|
|
|
|
2019-06-29 03:48:09 -07:00
|
|
|
### Analysis
|
2019-06-26 14:07:30 -07:00
|
|
|
Given information about all tables and a sql statement, this library can:
|
|
|
|
|
2019-06-29 03:48:09 -07:00
|
|
|
1. Determine which result columns a query is going to have, including types and nullability
|
|
|
|
2. Make an educated guess about what type the variables in the query should have (it's not really
|
|
|
|
possible to be 100% accurate about this because sqlite is very flexible at types)
|
|
|
|
3. Issue warnings about queries that are syntactically valid but won't run
|
|
|
|
|
|
|
|
To use the analyzer, first register all known tables via `SqlEngine.registerTable`. Then,
|
|
|
|
`SqlEngine.analyze(sql)` gives you a `AnalysisContext` which contains an annotated ast and information
|
|
|
|
about errors. The type of result columns and expressions can be inferred by using
|
|
|
|
`AnalysisContext.typeOf()`. Here's an example.
|
|
|
|
|
|
|
|
```dart
|
|
|
|
final id = TableColumn('id', const ResolvedType(type: BasicType.int));
|
|
|
|
final content = TableColumn('content', const ResolvedType(type: BasicType.text));
|
|
|
|
final demoTable = Table(
|
|
|
|
name: 'demo',
|
|
|
|
resolvedColumns: [id, content],
|
|
|
|
);
|
|
|
|
final engine = SqlEngine()..registerTable(demoTable);
|
|
|
|
|
|
|
|
final context =
|
|
|
|
engine.analyze('SELECT id, d.content, *, 3 + 4 FROM demo AS d');
|
|
|
|
|
|
|
|
final select = context.root as SelectStatement;
|
|
|
|
final resolvedColumns = select.resolvedColumns;
|
|
|
|
|
|
|
|
resolvedColumns.map((c) => c.name)); // id, content, id, content, 3 + 4
|
|
|
|
resolvedColumns.map((c) => context.typeOf(c).type.type) // int, text, int, text, int, int
|
|
|
|
```
|
|
|
|
|
2019-06-29 13:29:16 -07:00
|
|
|
## Limitations
|
|
|
|
- For now, only `SELECT` and `DELETE` expressions are implemented, `UPDATE` and `INSERT` will follow
|
|
|
|
soon.
|
|
|
|
- Windowing is not supported yet
|
|
|
|
- Common table expressions and compound select statements `UNION` / `INTERSECT` are not supported
|
|
|
|
and probably won't be in the near future.
|
|
|
|
|
2019-06-29 03:48:09 -07:00
|
|
|
## Thanks
|
|
|
|
- To [Bob Nystrom](https://github.com/munificent) for his amazing ["Crafting Interpreters"](https://craftinginterpreters.com/)
|
|
|
|
book, which was incredibly helpful when writing the parser.
|
2019-06-29 13:29:16 -07:00
|
|
|
- To the authors of [SQLDelight](https://github.com/square/sqldelight). This library uses their algorithm
|
2019-06-29 03:48:09 -07:00
|
|
|
for type inference.
|