mirror of https://github.com/AMT-Cheif/drift.git
Update docs of sqlparser library
This commit is contained in:
parent
da07be2da4
commit
b2f79e97e6
|
@ -1,2 +1,3 @@
|
||||||
## 0.1.0
|
## 0.1.0
|
||||||
Initial version
|
Initial version, can parse most statements but not `DELETE`, common table expressions and other
|
||||||
|
advanced features.
|
|
@ -1,12 +1,11 @@
|
||||||
# sqlparser
|
# sqlparser
|
||||||
|
|
||||||
An sql parser and static analyzer, written in pure Dart. At the moment, only `SELECT` and `DELETE`
|
An sql parser and static analyzer, written in pure Dart. At the moment, this library only targets
|
||||||
statements are supported. Further, this library only targets the sqlite dialect at the time being.
|
the sqlite dialect and some advanced features aren't supported yet.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
Not all features are available yet, put parsing select statements (even complex ones!) and
|
This library can parse most statements and perform type analysis for parameters and returned
|
||||||
performing analysis on them works!
|
columns. It supports joins, `group by`, nested sql statements, updates and deletes and more.
|
||||||
|
|
||||||
### Just parsing
|
### Just parsing
|
||||||
You can parse the abstract syntax tree of sqlite statements with `SqlEngine.parse`.
|
You can parse the abstract syntax tree of sqlite statements with `SqlEngine.parse`.
|
||||||
```dart
|
```dart
|
||||||
|
@ -29,13 +28,15 @@ Given information about all tables and a sql statement, this library can:
|
||||||
|
|
||||||
1. Determine which result columns a query is going to have, including types and nullability
|
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
|
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)
|
possible to be 100% accurate about this because sqlite is very flexible at types, but this library
|
||||||
3. Issue warnings about queries that are syntactically valid but won't run
|
gets it mostly right)
|
||||||
|
3. Issue warnings about queries that are syntactically valid but won't run (references unknown
|
||||||
|
tables / columns, uses undefined functions, etc.)
|
||||||
|
|
||||||
To use the analyzer, first register all known tables via `SqlEngine.registerTable`. Then,
|
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
|
`SqlEngine.analyze(sql)` gives you an `AnalysisContext` which contains an annotated ast and information
|
||||||
about errors. The type of result columns and expressions can be inferred by using
|
about errors. The type of result columns and expressions can be inferred by using
|
||||||
`AnalysisContext.typeOf()`. Here's an example.
|
`AnalysisContext.typeOf()`. Here's an example:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
final id = TableColumn('id', const ResolvedType(type: BasicType.int));
|
final id = TableColumn('id', const ResolvedType(type: BasicType.int));
|
||||||
|
@ -57,15 +58,20 @@ resolvedColumns.map((c) => context.typeOf(c).type.type) // int, text, int, text,
|
||||||
```
|
```
|
||||||
|
|
||||||
## But why?
|
## But why?
|
||||||
[Moor](https://pub.dev/packages/moor_flutter), a persistence library for Flutter apps, uses this
|
[Moor](https://pub.dev/packages/moor_flutter), a persistence library for Dart apps, uses this
|
||||||
package to generate type-safe methods from sql.
|
package to generate type-safe methods from sql.
|
||||||
|
|
||||||
## Limitations
|
## Limitations
|
||||||
- For now, only `SELECT` and `DELETE` expressions are implemented, `UPDATE` and `INSERT` will follow
|
Most on this list is just not supported yet because I didn't found a use case for
|
||||||
soon.
|
them yet. If you need them, just leave an issue and I'll try to implement them soon.
|
||||||
|
|
||||||
|
- For now, only `INSERT` statements are not supported, but they will be soon
|
||||||
- Windowing is not supported yet
|
- Windowing is not supported yet
|
||||||
- Common table expressions and compound select statements `UNION` / `INTERSECT` are not supported
|
- Compound select statements (`UNION` / `INTERSECT`) are not supported yet
|
||||||
and probably won't be in the near future.
|
- Common table expressions are not supported
|
||||||
|
- Some advanced expressions, like `COLLATE` or `CAST`s aren't supported yet.
|
||||||
|
|
||||||
|
If you run into parsing errors with what you think is valid sql, please create an issue.
|
||||||
|
|
||||||
## Thanks
|
## Thanks
|
||||||
- To [Bob Nystrom](https://github.com/munificent) for his amazing ["Crafting Interpreters"](https://craftinginterpreters.com/)
|
- To [Bob Nystrom](https://github.com/munificent) for his amazing ["Crafting Interpreters"](https://craftinginterpreters.com/)
|
||||||
|
|
Loading…
Reference in New Issue