This commit is contained in:
HSunboy
2018-07-02 18:01:01 +08:00
commit a729770b76
13 changed files with 17964 additions and 0 deletions

75
lib/filter.js Normal file
View File

@ -0,0 +1,75 @@
const replaceStrFormIndexArr=require("../utils").replaceStrFormIndexArr;
function filterComments(sql) {
let tmpArr = [];
const comments = [];
for (let i = 0; i < sql.length; i++) {
let char = sql[i];
//读取字符
if (char == "'" || char == "\"" || char == "-" || char == "\n") {
//推入数组
tmpArr.push({
index: i,
char: char
});
}
//校验数组是否有匹配语法
if (tmpArr.length < 2) {
if (tmpArr[0] && tmpArr[0].char == "\n") {
tmpArr = [];
}
continue;
}
let firstChar = tmpArr[0];
let lastChar = tmpArr[tmpArr.length - 1];
if (firstChar.char == "'" || firstChar.char == "\"") {
//引号匹配,则清空
if (lastChar.char == firstChar.char) {
tmpArr = [];
continue;
}
} else if (firstChar.char == "-") {
//假如第一个是横线,则开始校验注释规则
//判断是否为两个注释符号,不是,则清空
if (tmpArr[1].char != "-") {
tmpArr = [];
continue;
}
//为注释作用域,遇到换行符,则结束注释
else if (lastChar.char == "\n") {
comments.push({
begin: firstChar.index,
end: lastChar.index
})
tmpArr = [];
continue;
}
//解析结束
else if (i == sql.length - 1) {
comments.push({
begin: firstChar.index,
end: i
})
continue;
}
} else {
tmpArr = [];
}
}
sql = replaceStrFormIndexArr(sql, '', comments)
return sql;
}
function cleanSql(sql){
return filterComments(sql).trim();
}
exports.filterComments=filterComments;
exports.cleanSql=cleanSql;

22
lib/parser.js Normal file
View File

@ -0,0 +1,22 @@
const Parser = require('../core/astParser');
const Cache = require("../core/cache");
const filter = require("./filter");
const astCache=new Cache();
function parse(sql){
const cleanSql=filter.cleanSql(sql);
console.log(cleanSql)
let ast=astCache.get(cleanSql);
if(ast){
return ast
}else{
ast=Parser.parse(cleanSql).ast;
astCache.set(cleanSql,ast);
return ast;
}
}
exports.parse=parse;