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

@ -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;
/**
* 分割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;