添加例子🌰 ,sql分割自动清除注释

This commit is contained in:
HSunboy 2018-07-05 11:29:39 +08:00
parent 152f186a2e
commit 01834251ee
2 changed files with 30 additions and 0 deletions

View File

@ -5,4 +5,33 @@
1. 解析sql生成语法树(不支持CREATE等语句具体可以查看`core/astParser`文件)支持单条sql语句
2. 去除sql中的的注释(目前支持`--`,`/**/`类型注释)
## 用法
### 过滤注释 / sql分割
``` javascript
const dtFilter=require("dt-sql-parser").filter;
const sql=`
/*sttttttttart*/create table /*hhhhhhhh
hhhhhh
aaaaaa*/ sql_task_comment_test(id int comment 'id') comment 'sql test';
--eeeeeeeend
`
console.log(dtFilter.filterComments(sql))//过滤注释
console.log(dtFilter.splitSql(sql));//分割sql
```
### 生成ast树
``` javascript
const dtFilter=require("dt-sql-parser").parser;
const sql=`
/*sttttttttart*/select userId as id,name /*hhhhhhhh
hhhhhh
aaaaaa*/ from user where isDeleted=0
--eeeeeeeend
`
console.log(dtFilter.parse(sql))//注意目前只支持单条sql语句所以需要自己调用sql分割一条一条处理
```
语法解析模块代码来自[nquery](http://github.com/alibaba/nquery/)

View File

@ -22,6 +22,7 @@ function cleanSql(sql) {
* @param {String} sqlText
*/
function splitSql(sqlText) {
sqlText=cleanSql(sqlText);
if (!sqlText) {
return sqlText;
}