lava-oushudb-dt-sql-parser/lib/filter.js

58 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-07-02 19:36:09 +08:00
const replaceStrFormIndexArr = require("../utils").replaceStrFormIndexArr;
2018-07-05 11:16:30 +08:00
const commentFilter = require('../core/comment');
2018-07-02 18:01:01 +08:00
2018-07-02 19:36:09 +08:00
/**
* 过滤--注释
* @param {String} sql
*/
2018-07-02 18:01:01 +08:00
function filterComments(sql) {
2018-07-05 11:16:30 +08:00
return commentFilter.parse(sql)
2018-07-02 18:01:01 +08:00
}
2018-07-02 19:36:09 +08:00
/**
* 清除注释和前后空格
* @param {String} sql
*/
function cleanSql(sql) {
2018-07-05 11:16:30 +08:00
return filterComments(sql);
2018-07-02 18:01:01 +08:00
}
2018-07-02 19:36:09 +08:00
/**
* 分割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;