add new comment
This commit is contained in:
parent
537ac36a25
commit
b2c7777fb6
@ -3,6 +3,6 @@
|
||||
本项目用于处理sql,目前含有功能
|
||||
|
||||
1. 解析sql生成语法树(不支持CREATE等语句,具体可以查看`core/astParser`文件),支持单条sql语句
|
||||
2. 去除sql中的的注释(目前支持--类型注释)
|
||||
2. 去除sql中的的注释(目前支持`--`,`/**/`类型注释)
|
||||
|
||||
语法解析模块代码来自[nquery](http://github.com/alibaba/nquery/)
|
2
index.js
2
index.js
@ -1,6 +1,6 @@
|
||||
const parser = require("./lib/parser");
|
||||
const filter = require("./lib/filter");
|
||||
console.log(filter)
|
||||
|
||||
module.exports={
|
||||
parser,
|
||||
filter
|
||||
|
@ -1,5 +1,9 @@
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除注释和前后空格
|
||||
* @param {String} sql
|
||||
*/
|
||||
function cleanSql(sql) {
|
||||
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.cleanSql = cleanSql;
|
||||
exports.splitSql = splitSql;
|
@ -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}"});
|
||||
|
@ -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(`********************`)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user