SQL Parser
Parse simple SQL statements into an abstract syntax tree (AST) with the vis...
README
Nodejs SQL Parser
Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList, columnList and convert it back to SQL.
:star: Features
- support multiple sql statement seperate by semicolon
- support select, delete, update and insert type
- support drop, truncate and rename command
- output the table and column list that the sql visited with the corresponding authority
- support various databases engine
:tada: Install
From npmjs
- ```bash
- npm install node-sql-parser --save
- or
- yarn add node-sql-parser
- ```
- ```bash
- npm install @taozhi8833998/node-sql-parser --registry=https://npm.pkg.github.com/
- ```
From Browser
Import the JS file in your page:
- ```javascript
- // support all database parser, but file size is about 750K
- <script src="https://unpkg.com/node-sql-parser/umd/index.umd.js"></script>
- // or you can import specified database parser only, it's about 150K
- <script src="https://unpkg.com/node-sql-parser/umd/mysql.umd.js"></script>
- <script src="https://unpkg.com/node-sql-parser/umd/postgresql.umd.js"></script>
- ```
- NodeSQLParser object is on window
- ```html
- <!DOCTYPE html>
- <html lang="en" >
- <head>
- <title>node-sql-parser</title>
- <meta charset="utf-8" />
- </head>
- <body>
- <p><em>Check console to see the output</em></p>
- <script src="https://unpkg.com/node-sql-parser/umd/mysql.umd.js"></script>
- <script>
- window.onload = function () {
- // Example parser
- const parser = new NodeSQLParser.Parser()
- const ast = parser.astify("select id, name from students where age < 18")
- console.log(ast)
- const sql = parser.sqlify(ast)
- console.log(sql)
- }
- </script>
- </body>
- </html>
- ```
:rocket: Usage
Supported Database SQL Syntax
- Athena
- BigQuery
- DB2
- Hive
- MariaDB
- MySQL
- PostgresQL
- Redshift
- Sqlite
- TransactSQL
- FlinkSQL
- Snowflake(alpha)
- Noql
- New issue could be made for other new database.
Create AST for SQL statement
- ```javascript
- // import Parser for all databases
- const { Parser } = require('node-sql-parser');
- const parser = new Parser();
- const ast = parser.astify('SELECT * FROM t'); // mysql sql grammer parsed by default
- console.log(ast);
- ```
- ast for SELECT * FROM t
- ```json
- {
- "with": null,
- "type": "select",
- "options": null,
- "distinct": null,
- "columns": "*",
- "from": [
- {
- "db": null,
- "table": "t",
- "as": null
- }
- ],
- "where": null,
- "groupby": null,
- "having": null,
- "orderby": null,
- "limit": null
- }
- ```
Get node location in the AST
- ```javascript
- const { Parser } = require('node-sql-parser');
- const parser = new Parser();
- const ast = parser.astify('SELECT * FROM t', { parseOptions: { includeLocations: true } });
- console.log(ast);
- ```
- ast for SELECT * FROM t with the loc property indicating locations and ranges
- ```json
- {
- "with": null,
- "type": "select",
- "options": null,
- "distinct": null,
- "columns": [
- {
- "expr": {
- "type": "column_ref",
- "table": null,
- "column": "*"
- },
- "as": null,
- "loc": {
- "start": {
- "offset": 7,
- "line": 1,
- "column": 8
- },
- "end": {
- "offset": 8,
- "line": 1,
- "column": 9
- }
- }
- }
- ],
- "into": {
- "position": null
- },
- "from": [
- {
- "db": null,
- "table": "t",
- "as": null,
- "loc": {
- "start": {
- "offset": 14,
- "line": 1,
- "column": 15
- },
- "end": {
- "offset": 15,
- "line": 1,
- "column": 16
- }
- }
- }
- ],
- "where": null,
- "groupby": null,
- "having": null,
- "orderby": null,
- "limit": null,
- "locking_read": null,
- "window": null,
- "loc": {
- "start": {
- "offset": 0,
- "line": 1,
- "column": 1
- },
- "end": {
- "offset": 15,
- "line": 1,
- "column": 16
- }
- }
- }
- ```