Skip to content

Parse SQL (select) statements into abstract syntax tree (AST) and convert ASTs back to SQL.

License

Notifications You must be signed in to change notification settings

peg-sql-parser/sql-parser

This branch is 14 commits behind florajs/sql-parser:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jan 4, 2022
b416d5f · Jan 4, 2022
Jan 4, 2022
Jan 4, 2022
Jan 4, 2022
Dec 29, 2021
Dec 29, 2021
Dec 29, 2021
Dec 29, 2021
Sep 21, 2020
Feb 22, 2019
Jan 4, 2022
Jan 3, 2017
Jan 4, 2022
Nov 22, 2021

Repository files navigation

@florajs/sql-parser

NPM version NPM downloads

Parse simple SQL statements into an abstract syntax tree (AST) and convert it back to SQL.

Usage

Create AST for SQL statement

const { Parser } = require('@florajs/sql-parser');
const parser = new Parser();
const ast = parser.parse('SELECT * FROM t');

console.log(ast);

Convert AST back to SQL

const { Parser } = require('@florajs/sql-parser');
const ast = (new Parser()).parse('SELECT * FROM t');
const toSQL = require('@florajs/sql-parser').util.astToSQL;

console.log(toSQL(ast));

The generated SQL is ANSI SQL compliant. To run those queries on MySQL, make sure you set correct SQL mode

SET SESSION sql_mode = 'ANSI';

before running any query.

Acknowledgement

This project is based on the SQL parser extracted from Alibaba's nquery module.

License

GPL-2.0

About

Parse SQL (select) statements into abstract syntax tree (AST) and convert ASTs back to SQL.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 73.7%
  • PEG.js 26.3%