add new comment

This commit is contained in:
HSunboy 2018-07-02 19:36:09 +08:00
parent 537ac36a25
commit b2c7777fb6
5 changed files with 72 additions and 13 deletions

View File

@ -3,6 +3,6 @@
本项目用于处理sql目前含有功能 本项目用于处理sql目前含有功能
1. 解析sql生成语法树(不支持CREATE等语句具体可以查看`core/astParser`文件)支持单条sql语句 1. 解析sql生成语法树(不支持CREATE等语句具体可以查看`core/astParser`文件)支持单条sql语句
2. 去除sql中的的注释(目前支持--类型注释) 2. 去除sql中的的注释(目前支持`--`,`/**/`类型注释)
语法解析模块代码来自[nquery](http://github.com/alibaba/nquery/) 语法解析模块代码来自[nquery](http://github.com/alibaba/nquery/)

View File

@ -1,6 +1,6 @@
const parser = require("./lib/parser"); const parser = require("./lib/parser");
const filter = require("./lib/filter"); const filter = require("./lib/filter");
console.log(filter)
module.exports={ module.exports={
parser, parser,
filter filter

View File

@ -1,5 +1,9 @@
const replaceStrFormIndexArr = require("../utils").replaceStrFormIndexArr; const replaceStrFormIndexArr = require("../utils").replaceStrFormIndexArr;
/**
* 过滤--注释
* @param {String} sql
*/
function filterComments(sql) { function filterComments(sql) {
let tmpArr = []; let tmpArr = [];
const comments = []; const comments = [];
@ -8,7 +12,7 @@ function filterComments(sql) {
let char = sql[i]; let char = sql[i];
//读取字符 //读取字符
if (char == "'" || char == "\"" || char == "-" || char == "\n") { if (char == "'" || char == "\"" || char == "-" || char == "\n" || char == "/" || char == "*") {
//推入数组 //推入数组
tmpArr.push({ tmpArr.push({
index: i, index: i,
@ -17,7 +21,7 @@ function filterComments(sql) {
} }
//校验数组是否有匹配语法 //校验数组是否有匹配语法
if (tmpArr.length < 2) { if (tmpArr.length < 2) {
if (tmpArr[0] && tmpArr[0].char == "\n") { if (tmpArr[0] && (tmpArr[0].char == "\n" || tmpArr[0].char == "*")) {
tmpArr = []; tmpArr = [];
} }
continue; continue;
@ -32,6 +36,19 @@ function filterComments(sql) {
tmpArr = []; tmpArr = [];
continue; 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 == "-") { } else if (firstChar.char == "-") {
//假如第一个是横线,则开始校验注释规则 //假如第一个是横线,则开始校验注释规则
@ -67,9 +84,50 @@ function filterComments(sql) {
return sql; return sql;
} }
/**
* 清除注释和前后空格
* @param {String} sql
*/
function cleanSql(sql) { function cleanSql(sql) {
return filterComments(sql).trim(); return filterComments(sql).trim();
} }
/**
* 分割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.filterComments = filterComments;
exports.cleanSql = cleanSql; exports.cleanSql = cleanSql;
exports.splitSql = splitSql;

View File

@ -5,11 +5,11 @@ const example={
JOIN muyun_test_down2 JOIN muyun_test_down2
ON muyun_test_down1.id = muyun_test_down2.id 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`, --sdfsss`,
test2:` INSERT INTO TABLE muyun_test_down4 test2:` /**/INSERT INTO TABLE muyun_test_down4
SELECT muyun_test_down1.id, muyun_test_down1.name, muyun_test_down2.age SELECT /*lkaslfklasf*/ muyun_test_down1.id, muyun_test_down1.name, muyun_test_down2.age
FROM muyun_test_down1 FROM /**/ muyun_test_down1
JOIN muyun_test_down2 JOIN muyun_test_down2
ON muyun_test_down1.id = muyun_test_down2.id;`, ON muyun_test_down1.id = muyun_test_down2.id;`,
test3:`--alter table sx_622_1 add partition(pa=${"${bdp.system.bizdate}"}); test3:`--alter table sx_622_1 add partition(pa=${"${bdp.system.bizdate}"});

View File

@ -6,7 +6,8 @@ for(let [key,value] of testMap){
console.log(`******${key}********`) console.log(`******${key}********`)
console.log(value) console.log(value)
console.log(`******result********`) console.log(`******result********`)
console.log(dtSqlParser.parser.parse(value)); // console.log(dtSqlParser.parser.parse(value));
console.log(dtSqlParser.filter.filterComments(value))
console.log(`********************`) console.log(`********************`)
} }