用pegjs重写sql分割方法

This commit is contained in:
HSunboy
2018-07-06 16:22:57 +08:00
parent 01834251ee
commit a948c4562a
4 changed files with 105 additions and 40 deletions

View File

@ -6,7 +6,7 @@ const commentFilter = require('../core/comment');
* @param {String} sql
*/
function filterComments(sql) {
return commentFilter.parse(sql)
return commentFilter.parse(sql).text;
}
/**
@ -22,36 +22,7 @@ function cleanSql(sql) {
* @param {String} sqlText
*/
function splitSql(sqlText) {
sqlText=cleanSql(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;
return commentFilter.parse(sqlText).lines
}
exports.filterComments = filterComments;