diff --git a/README.md b/README.md index 052e16d..b9506b6 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,6 @@ 本项目用于处理sql,目前含有功能 1. 解析sql生成语法树(不支持CREATE等语句,具体可以查看`core/astParser`文件),支持单条sql语句 -2. 去除sql中的的注释(目前支持--类型注释) +2. 去除sql中的的注释(目前支持`--`,`/**/`类型注释) 语法解析模块代码来自[nquery](http://github.com/alibaba/nquery/) \ No newline at end of file diff --git a/index.js b/index.js index c2efdd3..db61d06 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const parser = require("./lib/parser"); const filter = require("./lib/filter"); -console.log(filter) + module.exports={ parser, filter diff --git a/lib/filter.js b/lib/filter.js index bd49440..73bac6d 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -1,5 +1,9 @@ -const replaceStrFormIndexArr=require("../utils").replaceStrFormIndexArr; +const replaceStrFormIndexArr = require("../utils").replaceStrFormIndexArr; +/** + * 过滤--注释 + * @param {String} sql + */ function filterComments(sql) { let tmpArr = []; const comments = []; @@ -8,7 +12,7 @@ function filterComments(sql) { let char = sql[i]; //读取字符 - if (char == "'" || char == "\"" || char == "-" || char == "\n") { + if (char == "'" || char == "\"" || char == "-" || char == "\n" || char == "/" || char == "*") { //推入数组 tmpArr.push({ index: i, @@ -17,7 +21,7 @@ function filterComments(sql) { } //校验数组是否有匹配语法 if (tmpArr.length < 2) { - if (tmpArr[0] && tmpArr[0].char == "\n") { + if (tmpArr[0] && (tmpArr[0].char == "\n" || tmpArr[0].char == "*")) { tmpArr = []; } continue; @@ -32,6 +36,19 @@ function filterComments(sql) { tmpArr = []; continue; } + } else if (firstChar.char == "/") { + if (tmpArr[1].char != "*") { + tmpArr = []; + } else if (lastChar.char == "/") { + if (tmpArr.length > 3 && tmpArr[tmpArr.length - 2].char == "*") { + comments.push({ + begin: firstChar.index, + end: lastChar.index + }) + tmpArr = []; + } + } + continue; } else if (firstChar.char == "-") { //假如第一个是横线,则开始校验注释规则 @@ -67,9 +84,50 @@ function filterComments(sql) { return sql; } -function cleanSql(sql){ +/** + * 清除注释和前后空格 + * @param {String} sql + */ +function cleanSql(sql) { return filterComments(sql).trim(); } -exports.filterComments=filterComments; -exports.cleanSql=cleanSql; \ No newline at end of file +/** + * 分割sql + * @param {String} sqlText + */ +function splitSql(sqlText) { + if (!sqlText) { + return sqlText; + } + sqlText = sqlText.trim(); + if (!endsWith(sqlText, ';')) { + sqlText += ';'; + } + + let results = []; + let index = 0; + let tmpChar = null; + for (let i = 0; i < sqlText.length; i++) { + let char = sqlText[i]; + + if (char == "'" || char == '"') { + if (tmpChar == char) { + tmpChar = null; + } else { + tmpChar = char; + } + } else if (char == ';') { + if (tmpChar == null) { + results.push(sqlText.substring(index, i)); + index = i + 1; + } + } + } + + return results; +} + +exports.filterComments = filterComments; +exports.cleanSql = cleanSql; +exports.splitSql = splitSql; \ No newline at end of file diff --git a/test/example.js b/test/example.js index f6284f2..292c4a0 100644 --- a/test/example.js +++ b/test/example.js @@ -5,11 +5,11 @@ const example={ JOIN muyun_test_down2 ON muyun_test_down1.id = muyun_test_down2.id `, - test1:`create table sql_task_comment_test(id int comment 'id') comment 'sql test'; + test1:`/*asf*/create table sql_task_comment_test(id int comment 'id') comment 'sql test'; --sdfsss`, - test2:` INSERT INTO TABLE muyun_test_down4 - SELECT muyun_test_down1.id, muyun_test_down1.name, muyun_test_down2.age - FROM muyun_test_down1 + test2:` /**/INSERT INTO TABLE muyun_test_down4 + SELECT /*lkaslfklasf*/ muyun_test_down1.id, muyun_test_down1.name, muyun_test_down2.age + FROM /**/ muyun_test_down1 JOIN muyun_test_down2 ON muyun_test_down1.id = muyun_test_down2.id;`, test3:`--alter table sx_622_1 add partition(pa=${"${bdp.system.bizdate}"}); diff --git a/test/index.js b/test/index.js index cbaaa34..450ecc9 100644 --- a/test/index.js +++ b/test/index.js @@ -6,7 +6,8 @@ for(let [key,value] of testMap){ console.log(`******${key}********`) console.log(value) console.log(`******result********`) - console.log(dtSqlParser.parser.parse(value)); + // console.log(dtSqlParser.parser.parse(value)); + console.log(dtSqlParser.filter.filterComments(value)) console.log(`********************`) }