diff --git a/README.md b/README.md index 862cbea..6780a28 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ 1. 解析sql生成语法树(不支持CREATE等语句,具体可以查看`core/astParser`文件),支持单条sql语句 2. 去除sql中的的注释(目前支持`--`,`/**/`类型注释) +3. sql分割,根据`;`将sql分割为数组 ## 用法 diff --git a/core/comment.js b/core/comment.js index daf3fc2..68d844d 100644 --- a/core/comment.js +++ b/core/comment.js @@ -113,7 +113,7 @@ module.exports = (function(){ result0 = parse_union_stmt(); if (result0 !== null) { result0 = (function(offset, union_stmt) { - return union_stmt; + return {lines,text:union_stmt}; })(pos0, result0); } if (result0 === null) { @@ -229,6 +229,24 @@ module.exports = (function(){ if (result2 === null) { pos = pos4; } + if (result2 === null) { + pos4 = pos; + if (input.charCodeAt(pos) === 59) { + result2 = ";"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\";\""); + } + } + if (result2 !== null) { + result2 = (function(offset) {isSplit=true;return ";"})(pos4); + } + if (result2 === null) { + pos = pos4; + } + } } if (result2 !== null) { result1 = [result1, result2]; @@ -241,7 +259,16 @@ module.exports = (function(){ pos = pos3; } if (result1 !== null) { - result1 = (function(offset, words, stmt) {return words.join("")+stmt})(pos2, result1[0], result1[1]); + result1 = (function(offset, words, stmt) { + const text=words.join("")+stmt; + let index=Math.max(lines.length-1,0); + lines[index]=(lines[index]||'')+text; + if(isSplit){ + isSplit=false; + lines.push(''); + } + return text; + })(pos2, result1[0], result1[1]); } if (result1 === null) { pos = pos2; @@ -348,6 +375,24 @@ module.exports = (function(){ if (result2 === null) { pos = pos4; } + if (result2 === null) { + pos4 = pos; + if (input.charCodeAt(pos) === 59) { + result2 = ";"; + pos++; + } else { + result2 = null; + if (reportFailures === 0) { + matchFailed("\";\""); + } + } + if (result2 !== null) { + result2 = (function(offset) {isSplit=true;return ";"})(pos4); + } + if (result2 === null) { + pos = pos4; + } + } } if (result2 !== null) { result1 = [result1, result2]; @@ -360,7 +405,16 @@ module.exports = (function(){ pos = pos3; } if (result1 !== null) { - result1 = (function(offset, words, stmt) {return words.join("")+stmt})(pos2, result1[0], result1[1]); + result1 = (function(offset, words, stmt) { + const text=words.join("")+stmt; + let index=Math.max(lines.length-1,0); + lines[index]=(lines[index]||'')+text; + if(isSplit){ + isSplit=false; + lines.push(''); + } + return text; + })(pos2, result1[0], result1[1]); } if (result1 === null) { pos = pos2; @@ -400,7 +454,12 @@ module.exports = (function(){ pos = pos1; } if (result0 !== null) { - result0 = (function(offset, stmt, other) {return stmt.join("")+other.join("")})(pos0, result0[0], result0[1]); + result0 = (function(offset, stmt, other) { + const text=stmt.join("")+other.join("") + let index=Math.max(lines.length-1,0); + lines[index]=lines[index]+other.join(""); + return text; + })(pos0, result0[0], result0[1]); } if (result0 === null) { pos = pos0; @@ -771,6 +830,17 @@ module.exports = (function(){ matchFailed("\"'\""); } } + if (result0 === null) { + if (input.charCodeAt(pos) === 59) { + result0 = ";"; + pos++; + } else { + result0 = null; + if (reportFailures === 0) { + matchFailed("\";\""); + } + } + } } } } @@ -911,6 +981,10 @@ module.exports = (function(){ } + let lines=[]; + let isSplit=false; + + var result = parseFunctions[startRule](); /* diff --git a/lib/filter.js b/lib/filter.js index fb93a92..6b8b7e5 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -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; diff --git a/peg/comment.pegjs b/peg/comment.pegjs index 6c486e1..c87fb06 100644 --- a/peg/comment.pegjs +++ b/peg/comment.pegjs @@ -1,9 +1,14 @@ +{ + let lines=[]; + let isSplit=false; +} + start = union_stmt:union_stmt { - return union_stmt; + return {lines,text:union_stmt}; } @@ -11,10 +16,24 @@ union_stmt =stmt: ( words:(!kw_start word:. {return word})* - stmt:(comment:comment {return ''}/quote:quote {return quote}) - {return words.join("")+stmt} + stmt:(comment:comment {return ''}/quote:quote {return quote}/";" {isSplit=true;return ";"}) + { + const text=words.join("")+stmt; + let index=Math.max(lines.length-1,0); + lines[index]=(lines[index]||'')+text; + if(isSplit){ + isSplit=false; + lines.push(''); + } + return text; + } )* other:.* -{return stmt.join("")+other.join("")} +{ + const text=stmt.join("")+other.join("") + let index=Math.max(lines.length-1,0); + lines[index]=lines[index]+other.join(""); + return text; +} comment =comment:(multiLine/singleLine) @@ -57,7 +76,7 @@ quote -kw_start=KW_SINGLE_LINE_START/KW_MULTI_LINE_START/"\""/"'" +kw_start=KW_SINGLE_LINE_START/KW_MULTI_LINE_START/"\""/"'"/";" KW_SINGLE_LINE_START = "--";