Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/florajs/sql-parser
Parse SQL (select) statements into abstract syntax tree (AST) and convert ASTs back to SQL.
https://github.com/florajs/sql-parser
ast parser sql
Last synced: about 1 month ago
JSON representation
Parse SQL (select) statements into abstract syntax tree (AST) and convert ASTs back to SQL.
- Host: GitHub
- URL: https://github.com/florajs/sql-parser
- Owner: florajs
- License: gpl-2.0
- Created: 2015-07-10T14:36:25.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2024-05-08T08:36:54.000Z (8 months ago)
- Last Synced: 2024-11-10T04:15:02.395Z (about 1 month ago)
- Topics: ast, parser, sql
- Language: JavaScript
- Size: 285 KB
- Stars: 279
- Watchers: 18
- Forks: 52
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @florajs/sql-parser
![](https://github.com/florajs/sql-parser/workflows/ci/badge.svg)
[![NPM version](https://img.shields.io/npm/v/@florajs/sql-parser.svg?style=flat)](https://www.npmjs.com/package/@florajs/sql-parser)
[![NPM downloads](https://img.shields.io/npm/dm/@florajs/sql-parser.svg?style=flat)](https://www.npmjs.com/package/@florajs/sql-parser)Parse simple SQL statements into an abstract syntax tree (AST) and convert it back to SQL.
## Usage
### Create AST for SQL statement
```javascript
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
```javascript
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
```sql
SET SESSION sql_mode = 'ANSI';
```before running any query.
## Acknowledgement
This project is based on the SQL parser extracted from Alibaba's [nquery](https://github.com/alibaba/nquery) module.
## License
[GPL-2.0](LICENSE)