@appland/sql-parser

中文文档
1.5.1发布于 2022/03/08MITCommonJS零依赖近 30 天下载 8.4万

JavaScript implementation of SQLite 3 query parser

原始使用文档
文档目录8 节
  1. Install
  2. Install as a global module (since v1.0.0)
  3. Basic Usage
  4. Use parser on Node streams (experimental) (since v1.0.0)
  5. AST
  6. Example
  7. Syntax Errors
  8. Contributing

sqlite-parser

NPM Version Image dependencies Status Image devDependencies Status Image License Type Image

This JavaScript library parses SQLite queries to generate abstract syntax tree (AST) representations of the parsed statements. It also parses some non-SQLite constructs, like PostgreSQL custom operators, although non-SQLite parsing can't be 100% accurate due to slight syntax differences (eg. different operator precedences) so corner cases might be parsed wrong.

Try out the interactive demo to see it in action.

This parser is written against the SQLite 3 spec.

Install

npm install sqlite-parser

Install as a global module (since v1.0.0)

Use the command-line interface of the parser by installing it as a global module. The 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 sqlite-parser --help.

npm i -g sqlite-parser

Basic Usage

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 = require('sqlite-parser');
var query = 'select pants from laundry;';
// sync
var ast = sqliteParser(query);
console.log(ast);

// async
sqliteParser(query, function (err, ast) {
  if (err) {
    console.error(err);
    return;
  }
  console.log(ast);
});

Use parser on Node streams (experimental) (since v1.0.0)

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 = require('sqlite-parser').createParser();
var readStream = require('fs').createReadStream('./large-input-file.sql');

readStream.pipe(parserTransform);
parserTransform.pipe(process.stdout);

parserTransform.on('error', function (err) {
  console.error(err);
  process.exit(1);
});

parserTransform.on('finish', function () {
  process.exit(0);
});

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 = require('fs');
var sqliteParser = require('sqlite-parser');
var parserTransform = sqliteParser.createParser();
var singleNodeTransform = sqliteParser.createStitcher();
var readStream = fs.createReadStream('./large-input-file.sql');
var writeStream = fs.createWriteStream('./large-output-file.json');

readStream.pipe(parserTransform);
parserTransform.pipe(singleNodeTransform);
singleNodeTransform.pipe(writeStream);

parserTransform.on('error', function (err) {
  console.error(err);
  process.exit(1);
});

writeStream.on('finish', function () {
  process.exit(0);
});

AST

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 CHANGELOG.

Example

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.

Input SQL

SELECT
 MAX(honey) AS "Max Honey"
FROM
 BeeHive

Result AST

{
  "type": "statement",
  "variant": "list",
  "statement": [
    {
      "type": "statement",
      "variant": "select",
      "result": [
        {
          "type": "function",
          "name": {
            "type": "identifier",
            "variant": "function",
            "name": "max"
          },
          "args": {
            "type": "expression",
            "variant": "list",
            "expression": [
              {
                "type": "identifier",
                "variant": "column",
                "name": "honey"
              }
            ]
          },
          "alias": "Max Honey"
        }
      ],
      "from": {
        "type": "identifier",
        "variant": "table",
        "name": "beehive"
      }
    }
  ]
}

Syntax Errors

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 (e.g.: Syntax error found near Column Identifier (WHERE Clause)).

Contributing

Contributions are welcome! You can get started by checking out the contributing guidelines.

版本与发布记录

@appland/sql-parser 共发布 6 个版本,首次发布于 2021/12/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.1latest2022/03/084 年前
1.5.02022/02/174 年前
1.4.02022/02/134 年前
1.3.02022/02/094 年前
1.2.02022/02/054 年前
1.1.12021/12/094 年前

关于 @appland/sql-parser 的常见问题

@appland/sql-parser 是什么?

JavaScript implementation of SQLite 3 query parser

如何安装 @appland/sql-parser?

在项目目录运行 npm install @appland/sql-parser,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 sqlite-parser,可以用 npx sqlite-parser 直接运行。

@appland/sql-parser 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/appland__sql-parser 是否存在。

@appland/sql-parser 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@appland/sql-parser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=4。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@appland/sql-parser 可以商用吗?

@appland/sql-parser 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@appland/sql-parser 的下载量有多少?

根据 npm 官方下载统计,@appland/sql-parser 在 2026/10/07 – 2026/11/05 这 30 天内被下载 83,781 次,日均约 2,793 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appland/sql-parser 还在维护吗?

最新版本 1.5.1 发布于 2022/03/08(4 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/12/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。