dt-sql-parser
English | 简体中文
dt-sql-parser is a SQL Parser project built with ANTLR4, and it's mainly for the BigData domain. The ANTLR4 generated the basic Parser, Visitor, and Listener, so it's easy to complete the syntax validation, tokenizer, traverse the AST, and so on features.
Besides, it' provides some helper methods, like split SQL, and filter the --
and /**/
types of comments in SQL.
Supported SQL:
- MySQL
- Flink SQL
- Spark SQL
- Hive SQL
- PL/SQL
Tips: This project is the default for Javascript language, also you can try to compile it to other languages if you need.
Installation
// use npm
npm i dt-sql-parser --save
// use yarn
yarn add dt-sql-parser
Usage
Syntax Validation
First, we need to import the Parser object from dt-sql-parser
, the different language needs
different Parser, so if you need to handle the Flink SQL, you can import the FlinkSQL Parser.
The below is a GenericSQL Parser example:
import { GenericSQL } from 'dt-sql-parser';
const parser = new GenericSQL();
const correctSql = 'select id,name from user1;';
const errors = parser.validate(correctSql);
console.log(errors);
Output:
Validate failed:
const incorrectSql = 'selec id,name from user1;'
const errors = parser.validate(incorrectSql);
console.log(errors);
Output:
We instanced a Parser object, and use the validate method to check the SQL syntax, if failed
returns an array object includes error message.
Tokenizer
Get all tokens by the Parser:
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)
Visitor
Traverse the tree node by the Visitor:
import { GenericSQL, SqlParserVisitor } from 'dt-sql-parser';
const parser = new GenericSQL()
const sql = `select id,name from user1;`
const tree = parser.parse(sql)
class MyVisitor extends SqlParserVisitor {
visitTableName(ctx) {
let tableName = ctx.getText().toLowerCase()
console.log('TableName', tableName)
}
visitSelectElements(ctx) {
let selectElements = ctx.getText().toLowerCase()
console.log('SelectElements', selectElements)
}
}
const visitor = new MyVisitor()
visitor.visit(tree)
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 the Listener
import { GenericSQL, SqlParserListener } from 'dt-sql-parser';
const parser = new GenericSQL();
const sql = 'select id,name from user1;'
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);
Tips: The node's method name can be found in the Listener file under the corresponding SQL directory
Clean
Clear the 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)
Split SQL
When the SQL text is very big, you can think about to split it by ;
, and handle it by each line.
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)
Other API
- parserTreeToString(input: string)
Parse the input and convert the AST to a List-like
tree string.
Roadmap
- Auto-complete
- Code formatting
License
MIT