Go to file
2020-12-17 10:12:05 +08:00
.github Update issue templates 2020-04-26 17:36:06 +08:00
build fix: restore antlr4 config 2020-12-14 11:09:57 +08:00
docs docs: update tasks status 2020-09-17 20:53:56 +08:00
src Merge branch 'dev' into feat_flinkSql 2020-12-14 14:03:28 +08:00
test Merge branch 'dev' into feat_flinkSql 2020-12-14 14:03:28 +08:00
.eslintrc.js build: optimize cli and add eslint 2020-08-28 13:29:47 +08:00
.gitignore chore: ingore 2020-09-17 20:54:16 +08:00
.npmignore feat: add generic and plsql basic parser file 2020-09-11 17:39:10 +08:00
CONTRIBUTING.md feat: add generic and plsql basic parser file 2020-09-11 17:39:10 +08:00
jest.config.js feat: add generic and plsql basic parser file 2020-09-11 17:39:10 +08:00
NeREADME.md feat: add generic and plsql basic parser file 2020-09-11 17:39:10 +08:00
package.json build: update parser lib 2020-09-11 18:47:53 +08:00
README-zh_CN.md update example code format 2020-12-17 10:12:05 +08:00
README.md update example code format 2020-12-17 10:12:05 +08:00
tsconfig.json build: update parser lib 2020-09-11 18:47:53 +08:00
yarn.lock build: optimize cli and add eslint 2020-08-28 13:29:47 +08:00

dt-sql-parser

NPM version

dt-sql-parser is a SQL parser built on ANTLR4 .It's mainly used for analyzing all kinds of SQL in the development of big data. Supported SQL:

  • MySQL
  • Flink SQL
  • Spark SQL
  • Hive SQL
  • PL/SQL

It provides the basic class, Visitor class, and Listener class. These class including the ability to generate tokens, generate parse tree, syntax validation, and Visitor & Listener patterns to traverse the AST.

In addition, several helper methods are provided to format the SQL before parsing. The main effect is to clear the '--' and '/**/' types of comments in SQL statements, and to split large chunks of SQL

tips: The Grammar file can also be compiled into other languages with ANTLR4 .

English | 简体中文

Installation

// use npm
npm i dt-sql-parser --save

// use yarn
yarn add dt-sql-parser

Usage

Clean

clear comments and Spaces before and after

import { cleanSql } from 'dt-sql-parser';

const sql = `-- comment comment
select id,name from user1; `
const cleanedSql = cleanSql(sql)
console.log(cleanedSql)

/*
select id,name from user1;
*/

Split

split sql

import { splitSql } from 'dt-sql-parser';

const sql = `select id,name from user1;
select id,name from user2;`
const sqlList = splitSql(sql)
console.log(sqlList)

/*
["select id,name from user1;", "\nselect id,name from user2;"]
*/

Tokens

lexical analysis, generate token

import { GenericSQL } from 'dt-sql-parser';

const parser = new GenericSQL()
const sql = 'select id,name,sex from user1;'
const tokens = parser.getAllTokens(sql)
console.log(tokens)
/*
[
    {
        channel: 0
        column: 0
        line: 1
        source: [SqlLexer, InputStream]
        start: 0
        stop: 5
        tokenIndex: -1
        type: 137
        _text: null
        text: "SELECT"
    },
    ...
]
*/

Syntax validation

verifies the syntax correctness of the SQL statement and returns an array of errors

import { GenericSQL } from 'dt-sql-parser';

const validate = (sql) => {
    const parser = new GenericSQL()
    const errors = parser.validate(sql)
    console.log(errors)
}

correct sql:

const correctSql = 'select id,name from user1;'
validate(correctSql)
/*
[]
*/

incorrect sql:

const incorrectSql = 'selec id,name from user1;'
validate(incorrectSql)
/*
[
    {
        endCol: 5,
        endLine: 1,
        startCol: 0,
        startLine: 1,
        message: "mismatched input 'SELEC' expecting {<EOF>, 'ALTER', 'ANALYZE', 'CALL', 'CHANGE', 'CHECK', 'CREATE', 'DELETE', 'DESC', 'DESCRIBE', 'DROP', 'EXPLAIN', 'GET', 'GRANT', 'INSERT', 'KILL', 'LOAD', 'LOCK', 'OPTIMIZE', 'PURGE', 'RELEASE', 'RENAME', 'REPLACE', 'RESIGNAL', 'REVOKE', 'SELECT', 'SET', 'SHOW', 'SIGNAL', 'UNLOCK', 'UPDATE', 'USE', 'BEGIN', 'BINLOG', 'CACHE', 'CHECKSUM', 'COMMIT', 'DEALLOCATE', 'DO', 'FLUSH', 'HANDLER', 'HELP', 'INSTALL', 'PREPARE', 'REPAIR', 'RESET', 'ROLLBACK', 'SAVEPOINT', 'START', 'STOP', 'TRUNCATE', 'UNINSTALL', 'XA', 'EXECUTE', 'SHUTDOWN', '--', '(', ';'}"
    }
]
*/

Visitor

access the specified node in the AST by Visitor pattern

import { GenericSQL, SqlParserVisitor } from 'dt-sql-parser';

const parser = new GenericSQL()
const sql = `select id,name from user1;`
// parseTree
const tree = parser.parse(sql)
class MyVisitor extends SqlParserVisitor {
    // overwrite visitTableName
    visitTableName(ctx) {
        let tableName = ctx.getText().toLowerCase()
        console.log('TableName', tableName)
    }
    // overwrite visitSelectElements
    visitSelectElements(ctx) {
        let selectElements = ctx.getText().toLowerCase()
        console.log('SelectElements', selectElements)
    }
}
const visitor = new MyVisitor()
visitor.visit(tree)

/*
SelectElements id,name
TableName user1
*/

tips: The node's method name can be found in the Visitor file under the corresponding SQL directory

Listener

access the specified node in the AST by Listener pattern

import { GenericSQL, SqlParserListener } from 'dt-sql-parser';

const parser = new GenericSQL();
const sql = 'select id,name from user1;'
// parseTree
const tree = parser.parse(sql)
class MyListener extends SqlParserListener {
    enterTableName(ctx) {
        let tableName = ctx.getText().toLowerCase()
        console.log('TableName', tableName)
    }
    enterSelectElements(ctx) {
        let selectElements = ctx.getText().toLowerCase()
        log('SelectElements', selectElements)
    }
}
const listenTableName = new MyListener();
parser.listen(listenTableName, tree);

/*
SelectElements id,name
TableName user1
*/

tips: The node's method name can be found in the Listener file under the corresponding SQL directory

Other

  • parserTreeToString (parse the SQL into AST and turn it into a String)

Roadmap

  • Auto-complete
  • Impala SQL

License

MIT