sqlparser
v0.43.1Parses sqlite statements and performs static analysis on them
Архив пакета: https://pubdev.letsnova.ru/api/archives/sqlparser/0.43.1.tar.gz
dart pub add sqlparserREADME
sqlparser
SQL parser and static analyzer written in Dart. At the moment, this library targets the SQLite dialect only.
Features
This library supports every SQLite statement and expression, which includes parsing and
detailed static analysis.
We can resolve what type a column in a SELECT statement has, infer types for variables,
find semantic errors and more.
This library supports most SQLite features:
- DQL: Full support, including joins,
group by, nested and compound selects,WITHclauses and window function. - DDL: Supports all
CREATEandDROPstatements supported by SQLite, including advanced features like foreign keys or virtual tables (when a matching module likefts5is enabled).ALTER TABLEstatements can also be parsed. - Transactions: Supports parsing
BEGIN,SAVEPOINT,RELEASE,COMMITandROLLBACKstatements. - Management: All statements (
VACUUM,ATTACH,REINDEX,ANALYZE,PRAGMA).
Using the parser
To obtain an abstract syntax tree from an SQL statement, use SqlEngine.parse.
import 'package:sqlparser/sqlparser.dart';
final engine = SqlEngine();
final result = engine.parse('''
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.rootNode contains the select statement in tree form
Analysis
Given information about all tables and an SQL statement, this library can:
- Determine which result columns a query is going to have, including types and nullability
- 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, but this library gets it mostly right)
- Issue basic 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,
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
AnalysisContext.typeOf(). Here's an example:
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
But why?
Drift, a persistence library for Dart apps, uses this package to generate type-safe methods from SQL.
Thanks
- To Bob Nystrom for his amazing "Crafting Interpreters" book, which was incredibly helpful when writing the parser.
История изменений
0.43.1
- Make
NamedWindowDeclarationan AST node. - Support parsing
DROP,VACUUM,REINDEX,ATTACH,DETACH,SAVEPOINTRELEASE,ROLLBACK,ANALYZE,PRAGMAandALTER TABLEstatements. - Add
SchemaBufferclass to applyALTER TABLEandDROPstatements toCREATEstatements.
0.43.0
- Allow parsing SQL from
FileSpans, which is convenient for SQL embedded as text in outer structures.
0.42.1
- Support new features introduced in SQLite version 3.51.0.
0.42.0
- Fix unresolved references in CTEs resolving against the scope of the main query.
- Static analysis support for the PowerSync SQLite extension.
0.41.2
- Improve error message on unknown columns when it looks like the identifier should have been a string literal.
- Infer the output of
lag,leadandnth_valueto be nullable.
0.41.1
- Support new features introduced in SQLite version 3.50.0.
0.41.0
- Replace
ColonNamedVariablewithNamedVariablefor all named variables. - Analysis support for SQLite 3.48.
- Fix nullability analysis around fts5 tables (enabled when raising the version to 3.48 to preserve backwards-compatibility).
0.40.0
- Add support for the
dbstatmodule. - Prioritize null propagation in type resolver, leading to more accurate analysis on which columns are nullable.
0.39.2
- Fix false-positive lint for a parameter count mismatch on
bm25(). - Fix type interference around
fts5function calls.
0.39.1
- Improve recovery in parser when encountering syntax errors.
0.39.0
- When using the
parsemethods onSqlEngine, tokenizer errors are now included in the result instead of throwing an exception.
0.38.1
- Fix upcoming
unreachable_switch_defaultlint.
0.38.0
- Resolve
json_extractreturn types from context if possible. - Warn when
INSTEAD OFtriggers are not used on views. - Represent writes to views in
TableWrite.
0.37.1
- Refine nullability analysis for subquery expressions: Queries with aggregate invocations are no longer considered nullable (the aggregate invocation had to be a top-level expression before).
0.37.0
- Add support for sqlite 3.46.0.
- Make columns coming from subquery expressions nullable.
0.36.0
- Add support for the geopoly module.
0.35.1
- Fix
INexpressions accepting aliases, causing some queries to get parsed incorrectly.
0.35.0
- Fix parsing binary literals.
- Expand support for
INexpressions, they now support tuples on the left-hand side and the shorthand syntax for table references and table-valued functions. - Drift extensions: Allow custom class names for
CREATE VIEWstatements. - Drift extensions: Support the
INT64hint forCREATE TABLEstatements.
0.34.1
- Allow selecting from virtual tables using the table-valued function syntax.
0.34.0
- Fix explicit
NULLcolumn constraints being dropped when converting nodes to SQL. - Add analysis errors for illegal unqualified references to
oldandnewin triggers. - Analysis support for sqlite 3.45 and jsonb functions.
0.33.0
- Support the column-name-list syntax for updates, thanks to @tibotix.
0.32.1
- Treat the result of
sum()as nullable when inferring types. - Support features added in sqlite 3.44:
ORDER BYclauses as part of aggregate functions.- Support
concat,concat_wsandstring_agg.
0.32.0
- Turn
ResolvedType.hintsinto a list, supporting multiple type hints.
0.31.3
- Fix star columns expanding to more columns than they should.
0.31.2
- Add
CaseInsensitiveMap.ofto wrap existing maps.
0.31.1
- Add the
sqlite3_schematable to the builtin tables supported by everySqlEngineinstance. - Support the
timediffandoctet_lengthfunctions from sqlite 3.43.0.
0.31.0
- Add
SqlEngine.parseMultipleto parse multiple statements into one AST.
0.30.3
- Fix
WITHclauses not being resolved for compound select statements.
0.30.2
- Fix false-positive "unknown table" errors when the same table is used in a join with and then without an alias.
0.30.1
- Report syntax error for
WITHclauses in triggers.
0.30.0
- Add
previousandnextfields for tokens
0.29.0
- Parser support for constructor names in
WITHdrift syntax. - Support resolving
IIFfunctions. - Fix a crash when a CTE is used on an insert, update or delete statement.
- Fix wrong column names being reported for references in subqueries and CTEs.
0.28.1
- Fix false-positive warnings about
ASaliases in subqueries used in triggers.
0.28.0
- Support the
unhexfunction added in sqlite 3.41.0 - Support custom keyword sets when formatting SQL.
0.27.0
- Add
mappedBytoExpressionResultColumnwhen parsing in drift mode.
0.26.1
- Fix missing space when formatting aggregate functions.
0.26.0
- Remove token parameter from constructor in
Literalsubclasses andNumberedVariable.
0.25.0
- Better analysis support for
ANYcolumns inSTRICTtables. - Assign resolved schema columns to synctactical
ResultColumnin queries.
0.24.0
- Make
Fts5Tableconstructor public.
0.23.3
- Analysis support for the spellfix extension.
0.23.2
- Support resolving the
fts5vocabmodule whenfts5is enabled - thanks to @FaFre. - Support the
rtreeextension - also thanks to @FaFre! - Improve references resolving across subqueries.
0.23.1
- Gracefully handle tokenizer errors related to
@or$variables.
0.23.0
- Apply type hints for date times on textual datetime functions as well.
0.22.0
- Refactor how tables and columns are resolved internally.
- Lint for
DISTINCTmisuse in aggregate function calls. - Support the
RIGHTandFULLjoin operators added in sqlite 3.39.0. - Support
IS DISTINCT FROMandIS NOT DISTINCT FROMsyntax added in sqlite 3.39.0. - Fix type inference for
SUM()calls around int "subtypes" (like booleans).
0.21.0
- Analysis support for new features in sqlite version 3.38.
- Replace internal
moorreferences withdriftreferences.
0.20.1
- Fix SQL generation for upsert statements with a conflict target.
0.20.0
- Support
LISTcolumns for a drift-specific feature.
0.19.2
- Improve handling of drift-specific column types in
STRICTtables.
0.19.1
- Make the result of
group_concatnullable.
0.19.0
- Support generated columns.
- Support features introduced in sqlite version 3.37, most notably
STRICTtables.
0.18.1
- Fix the AST comparator missing errors for different amount of children.
0.18.0
- Fix unecessary errors around
fts5tables - Merge all moor-specific nodes into a single
visitMoorSpecificvisitor method - Parse
BEGINandCOMMITstatements - Improve type inference around
RETURNINGclauses.
0.17.2
- Fix nullability analysis of
COALESCEandIFNULL
0.17.1
- Fix nullability analysis of references and star columns
0.17.0
- Refactor how tables and columns are resolved in statements
- The new
ResultSetAvailableInStatementclass describes a result set that has been added to a statement, for instance through a from clause - A
TableOrSubquerywith an alias now introduces aTableAliasinstead of the original table
0.16.0
- New analysis checks for
RETURNING: Disallowtable.*syntax and aggregate expressions - Support
RAISEexpressions in triggers - Fix resolving columns when
RETURNINGis used in anUPDATE FROMstatement - Fix aliases to rowid being reported as nullable
0.15.0
- Breaking: Change
InsertStatement.upsertto a list of upsert clauses- Support multiple upsert clauses
- Do not require a conflict target in the last clause
- Support
RETURNINGclauses in updates, deletes and inserts - Support
FROMclauses inUPDATEstatements - Support
MATERIALIZED/NOT MATERIALIZEDhints in common table expressions - Add
BuiltInMathExtensionwhich corresponds to the-DSQLITE_ENABLE_MATH_FUNCTIONScompile-time option for sqlite. - Add
EngineOptions.versionargument to specify the desired sqlite version. Using newer features will be reported as analysis warnings. - Fix
rankcolumns of fts5 tables being misreported as integers
0.14.0
- Fix views using common table expressions
0.13.0-nullsafety.0
- Parse ordering in table key constraints
- Deprecate
KeyClause.indexedColumnsin favor ofKeyClause.columns
- Deprecate
0.12.0-nullsafety.0
- Migrate to null-safety
- Remove legacy type inference
- Parser support for new moor features
0.11.0
- New
package:sqlparser/utils/node_to_text.dartlibrary that turns an AST node back into a textual representation. - Fix precedence of
CASEexpressions
0.10.1
- Scan identifiers with
[bracket syntax] NumericTokennow contains individual lexemes making up the number- Improve error messages in some scenarios
- Fix type inference for binary expressions where the operands have incompatible types
- Improve type inference around
NULL
0.10.0
- Breaking: Made
RecursiveVisitor.visit,visitListandvisitExceptan extension onAstVisitor. - Support the transformer pattern to modify ast nodes
- Breaking:
FrameBoundary,DeleteTarget,UpdateTarget,DefaultValuesandInsertTargetare no longer constant - Breaking: Removed
visitQueryable. UsedefaultQueryableinstead. - Support parsing and analyzing
CREATE VIEWstatements (seeSchemaFromCreateTable.readView). Thanks to @mqus for their contribution! SqlEngine.parsewill no longer throw when there's a parsing error (useParseResult.errorsinstead).- Parse
DEFERRABLEclauses on foreign key constraints - Parse
NULLS FIRSTandNULLS LASTonORDER BYterms
0.9.0
- New
package:sqlparser/utils/find_referenced_tables.dartlibrary. Use it to easily find all referenced tables in a query. - Support row values including warnings about misuse
0.8.1
- Support collate expressions in the new type inference ([#533](htt ps://github.com/simolus3/moor/issues/533))
- Added
visitCollateExpressionto the visitor classes
0.8.0
- Remove
SqlEngine.withOptionsconstructor - the default constructor now takes options - Changed
SelectStatement.fromfromList<Queryable>toQueryable?. Selecting from multiple tables with a comma will now be parsed as aJoinClause. - Changed
SelectStatementAsSource.statementfromSelectStatementtoBaseSelectStatementand allow compound select statements to appear in aFROMclause - Support the
VALUESclause as select statement - The new type inference engine is now enabled by default and the
enableExperimentalTypeInferenceoption has been removed. To continue using the old engine, theuseLegacyTypeInferenceflag can be used.
0.7.0
- New feature: Table valued functions.
- Breaking: Removed the
enableJson1parameter onEngineOptions. Add aJson1Extensioninstance toenabledExtensionsinstead. - Parse
rowidas a valid reference when needed (SELECT rowid FROM tblis now parsed correctly) - Parse
CURRENT_TIME,CURRENT_DATEandCURRENT_TIMESTAMP - Parse
UPSERTclauses for insert statements
0.6.0
- Breaking: Added an argument type and argument to the visitor classes
- Experimental new type inference algorithm
(
SqlEngine.withOptions(EngineOptions(enableExperimentalTypeInference: true))) - Support
CASTexpressions and theISNULL/NOTNULLpostfixes - Support parsing
CREATE TRIGGERstatements - Support parsing
CREATE INDEXstatements
0.5.0
- Optionally support the
json1module - Optionally support the
fts5module
0.4.0
- Support common table expressions
- Handle special
rowid,oid,__rowid__references - Support references to
sqlite_masterandsqlite_sequencetables
0.3.0
- parse compound select statements
- scan comment tokens
- experimental auto-complete engine (only supports a tiny subset based on the grammar only)
- some features that are specific to moor
0.3.0+1: Accept \r characters as whitespace
0.2.0
- Parse
CREATE TABLEstatements - Extract schema information from parsed create table statements with
SchemaFromCreateTable.
0.1.2
- parse
COLLATEexpressions - fix wrong order in parsed
LIMITclauses
0.1.1
Attempt to recognize when a bound variable should be an array (eg. in WHERE x IN ?). Also fixes a number of parsing
bugs:
- Parses tuples, proper type resolution for
INexpressions - Don't resolve references to tables that don't appear in the surrounding statement.
- Parse joins without any additional operator, e.g.
table1 JOIN table2instead oftable1 CROSS JOIN table2. - Parser now complains when parsing a query doesn't fully consume the input
0.1.0
Initial version, can parse most statements but not DELETE, common table expressions and other advanced features.
