Try out the interactive demo to see it in action.
This parser is written against the SQLite 3 spec.
npm install sqlite-parser
Use the command-line interface of the parser by installing it as a global module.
sqlite-parser command is then available to use to parse input SQL files and
write the results to stdout or a JSON file. Additional usage
instructions and options available through
npm i -g sqlite-parser
The library exposes a function that accepts two arguments: a string containing SQL to parse and a callback function. If an AST cannot be generated from the input string then a descriptive error is generated.
If invoked without a callback function the parser will runs synchronously and return the resulting AST or throw an error if one occurs.
var sqliteParser = ;var query = 'select pants from laundry;';// syncvar ast = ;console;// async;
This library also includes experimental support as a stream transform that can accept a readable stream of SQL statements and produce a JSON string, representing the AST of each statement, as it is read and transformed. Using this method, the parser can handle files containing hundreds or thousands of queries at once without running into memory limitations. The AST for each statement is pushed down the stream as soon as it is read and parsed instead of reading the entire file into memory before parsing begins.
var parserTransform = ;var readStream = ;readStream;parserTransform;parserTransform;parserTransform;
To pipe the output into a file that contains a single valid JSON structure, the output of the parser steam transform needs to be wrapped in statement list node where every statement is separated by a comma.
var fs = ;var sqliteParser = ;var parserTransform = sqliteParser;var singleNodeTransform = sqliteParser;var readStream = fs;var writeStream = fs;readStream;parserTransform;singleNodeTransform;parserTransform;writeStream;
The AST is stable as of release
1.0.0. However, if changes need to be made to
improve consistency between node types, they will be explicitly listed in the
You can provide one or more SQL statements at a time. The resulting AST object has, at the highest level, a statement list node that contains an array of statements.
SELECTMAX(honey) AS "Max Honey"FROMBeeHive
This parser will try to create descriptive error messages when it cannot parse
some input SQL. In addition to an approximate location for the syntax error,
the parser will attempt to describe the area of concern
Syntax error found near Column Identifier (WHERE Clause)).
Contributions are welcome! You can get started by checking out the contributing guidelines.