From 01834251ee34cca2c8b02a838f3e4a3f76595543 Mon Sep 17 00:00:00 2001 From: HSunboy Date: Thu, 5 Jul 2018 11:29:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BE=8B=E5=AD=90?= =?UTF-8?q?=F0=9F=8C=B0=20=EF=BC=8Csql=E5=88=86=E5=89=B2=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 +++++++++++++++++++++++++++++ lib/filter.js | 1 + 2 files changed, 30 insertions(+) diff --git a/README.md b/README.md index cc2f2aa..862cbea 100644 --- a/README.md +++ b/README.md @@ -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/) \ No newline at end of file diff --git a/lib/filter.js b/lib/filter.js index 2c6aa07..fb93a92 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -22,6 +22,7 @@ function cleanSql(sql) { * @param {String} sqlText */ function splitSql(sqlText) { + sqlText=cleanSql(sqlText); if (!sqlText) { return sqlText; }