2018-07-03 11:33:53 +08:00
|
|
|
|
# dt-sql-parser
|
2018-07-02 18:01:01 +08:00
|
|
|
|
|
2020-05-12 09:39:49 +08:00
|
|
|
|
[![NPM version][npm-image]][npm-url]
|
|
|
|
|
|
|
|
|
|
[npm-image]: https://img.shields.io/npm/v/dt-sql-parser.svg?style=flat-square
|
|
|
|
|
[npm-url]: https://www.npmjs.com/package/dt-sql-parser
|
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
此项目是基于 [ANTLR4](https://github.com/antlr/antlr4) 开发的 SQL 语言解析器。主要用于大数据开发中,对各类 SQL 的解析。目前支持的 SQL:
|
|
|
|
|
|
|
|
|
|
- MySQL
|
|
|
|
|
- Flink SQL
|
|
|
|
|
- Spark SQL
|
|
|
|
|
- Hive SQL
|
|
|
|
|
- PL/SQL
|
|
|
|
|
|
|
|
|
|
每种 SQL 都提供了对应基础类、Visitor 类和 Listener 类,包含了生成 token、生成 AST、语法校验、visitor 和 listener 模式遍历 AST 指定节点等功能。
|
|
|
|
|
|
|
|
|
|
此外,为了方便解析,还提供了几个辅助方法可以在解析前对 SQL 进行格式处理。主要作用是清除 SQL 语句中的 '--' 和 '/**/' 两种类型的注释,以及拆分大段 SQL。
|
2018-07-02 18:01:01 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
提示:项目中的 grammar 文件也可以通过 [ANTLR4](https://github.com/antlr/antlr4) 编译成其他语言
|
2018-07-02 18:01:01 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
[English](./README.md) | 简体中文
|
2018-07-05 11:29:39 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
## 安装
|
2018-07-05 11:29:39 +08:00
|
|
|
|
|
|
|
|
|
```
|
2020-12-15 15:04:46 +08:00
|
|
|
|
// use npm
|
|
|
|
|
npm i dt-sql-parser --save
|
|
|
|
|
|
|
|
|
|
// use yarn
|
|
|
|
|
yarn add dt-sql-parser
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 示例
|
2018-07-05 11:29:39 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
### Clean
|
2020-08-28 13:29:47 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
清除注释和前后空格
|
2018-08-16 19:30:22 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
```javascript
|
|
|
|
|
import { cleanSql } from 'dt-sql-parser';
|
|
|
|
|
|
|
|
|
|
const sql = `-- comment comment
|
|
|
|
|
select id,name from user1; `
|
|
|
|
|
const cleanedSql = cleanSql(sql)
|
|
|
|
|
console.log(cleanedSql)
|
2018-08-16 19:30:22 +08:00
|
|
|
|
|
|
|
|
|
/*
|
2020-12-15 15:04:46 +08:00
|
|
|
|
select id,name from user1;
|
2018-08-16 19:30:22 +08:00
|
|
|
|
*/
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
### Split
|
2018-09-03 17:56:34 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
分割 sql
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
```javascript
|
|
|
|
|
import { splitSql } from 'dt-sql-parser';
|
2020-08-28 13:29:47 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
const sql = `select id,name from user1;
|
|
|
|
|
select id,name from user2;`
|
|
|
|
|
const sqlList = splitSql(sql)
|
|
|
|
|
console.log(sqlList)
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
/*
|
|
|
|
|
["select id,name from user1;", "\nselect id,name from user2;"]
|
|
|
|
|
*/
|
|
|
|
|
```
|
2020-08-28 13:29:47 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
### Tokens
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
对 sql 语句进行词法分析,生成 token
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
```javascript
|
|
|
|
|
import { GenericSQL } from 'dt-sql-parser';
|
2020-08-28 13:29:47 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
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"
|
|
|
|
|
},
|
|
|
|
|
...
|
|
|
|
|
]
|
|
|
|
|
*/
|
|
|
|
|
```
|
2018-10-08 18:02:26 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
### Syntax validation
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
validate 方法对 sql 语句的语法正确性进行校验,返回一个由 error 组成的数组
|
2020-08-28 13:29:47 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
```javascript
|
|
|
|
|
import { GenericSQL } from 'dt-sql-parser';
|
2018-10-08 18:02:26 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
const validate = (sql) => {
|
|
|
|
|
const parser = new GenericSQL()
|
|
|
|
|
const errors = parser.validate(sql)
|
|
|
|
|
console.log(errors)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
语法正确的 sql:
|
|
|
|
|
```javascript
|
|
|
|
|
const correctSql = 'select id,name from user1;'
|
|
|
|
|
validate(correctSql)
|
|
|
|
|
/*
|
|
|
|
|
[]
|
|
|
|
|
*/
|
|
|
|
|
```
|
|
|
|
|
包含错误语法的 sql:
|
|
|
|
|
```javascript
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
使用 visitor 模式访问 AST 中的指定节点
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
```javascript
|
|
|
|
|
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 {
|
|
|
|
|
// 重写 visitTableName 方法
|
|
|
|
|
visitTableName(ctx) {
|
|
|
|
|
let tableName = ctx.getText().toLowerCase()
|
|
|
|
|
console.log('TableName', tableName)
|
|
|
|
|
}
|
|
|
|
|
// 重写 visitSelectElements 方法
|
|
|
|
|
visitSelectElements(ctx) {
|
|
|
|
|
let selectElements = ctx.getText().toLowerCase()
|
|
|
|
|
console.log('SelectElements', selectElements)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const visitor = new MyVisitor()
|
|
|
|
|
visitor.visit(tree)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
SelectElements id,name
|
|
|
|
|
TableName user1
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
提示:使用 Visitor 模式时,节点的方法名称可以在对应 SQL 目录下的 Visitor 文件中查找
|
|
|
|
|
|
|
|
|
|
### Listener
|
|
|
|
|
|
|
|
|
|
listener 模式,利用 [ANTLR4](https://github.com/antlr/antlr4) 提供的 ParseTreeWalker 对象遍历 AST,进入各个节点时调用对应的方法。
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
```
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
提示:使用 Listener 模式时,节点的方法名称可以在对应 SQL 目录下的 Listener 文件中查找
|
2020-08-28 13:29:47 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
### 其他
|
2019-09-25 15:57:25 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
- parserTreeToString 将 SQL 解析成 AST,再转成 string 形式
|
2018-10-08 17:58:36 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
## 路线图
|
2018-11-30 13:40:48 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
- Auto-complete
|
|
|
|
|
- Impala SQL
|
2018-11-30 13:40:48 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
## 许可证
|
2018-11-30 13:40:48 +08:00
|
|
|
|
|
2020-12-15 15:04:46 +08:00
|
|
|
|
[MIT](./LICENSE)
|