2019-01-28 20:25:26 +08:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2020-08-28 13:29:47 +08:00
|
|
|
exports.splitSql = exports.replaceStrFormIndexArr = void 0;
|
2018-07-02 18:01:01 +08:00
|
|
|
function replaceStrFormIndexArr(str, replaceStr, indexArr) {
|
2020-08-28 13:29:47 +08:00
|
|
|
let result = '';
|
2018-07-02 18:01:01 +08:00
|
|
|
let index = 0;
|
|
|
|
if (!indexArr || indexArr.length < 1) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < indexArr.length; i++) {
|
2020-08-28 13:29:47 +08:00
|
|
|
const indexItem = indexArr[i];
|
|
|
|
const begin = indexItem.begin;
|
2018-07-02 18:01:01 +08:00
|
|
|
result = result + str.substring(index, begin) + replaceStr;
|
|
|
|
index = indexItem.end + 1;
|
|
|
|
if (i == indexArr.length - 1) {
|
|
|
|
result = result + str.substring(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-01-28 20:25:26 +08:00
|
|
|
exports.replaceStrFormIndexArr = replaceStrFormIndexArr;
|
2019-09-25 15:43:36 +08:00
|
|
|
function splitSql(sql) {
|
|
|
|
let haveEnd = true;
|
|
|
|
if (!sql.endsWith(';')) {
|
|
|
|
sql += ';';
|
|
|
|
haveEnd = false;
|
|
|
|
}
|
2019-09-29 17:38:52 +08:00
|
|
|
function pushSql(parser, sql) {
|
|
|
|
if (!haveEnd && parser.index == sql.length - 1) {
|
|
|
|
parser.sqls.push(parser.index - 1);
|
|
|
|
parser.queue = '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parser.sqls.push(parser.index);
|
|
|
|
parser.queue = '';
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 15:43:36 +08:00
|
|
|
// 处理引号
|
|
|
|
function quoteToken(parser, sql) {
|
2020-08-28 13:29:47 +08:00
|
|
|
const queue = parser.queue;
|
|
|
|
const endsWith = queue[queue.length - 1];
|
2019-09-25 15:43:36 +08:00
|
|
|
if (endsWith == '\'' || endsWith == '"') {
|
2020-08-28 13:29:47 +08:00
|
|
|
const nextToken = sql.indexOf(endsWith, parser.index + 1);
|
2019-09-25 15:43:36 +08:00
|
|
|
if (nextToken != -1) {
|
|
|
|
parser.index = nextToken;
|
|
|
|
parser.queue = '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parser.index = sql.length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 处理单行注释
|
|
|
|
function singleLineCommentToken(parser, sql) {
|
|
|
|
let queue = parser.queue;
|
|
|
|
if (queue.endsWith('--')) {
|
2020-08-28 13:29:47 +08:00
|
|
|
const nextToken = sql.indexOf('\n', parser.index + 1);
|
2019-09-25 15:43:36 +08:00
|
|
|
if (nextToken != -1) {
|
|
|
|
parser.index = nextToken;
|
|
|
|
queue = '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parser.index = sql.length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 处理多行注释
|
|
|
|
function multipleLineCommentToken(parser, sql) {
|
2020-08-28 13:29:47 +08:00
|
|
|
const queue = parser.queue;
|
2019-09-25 15:43:36 +08:00
|
|
|
if (queue.endsWith('/*')) {
|
2020-08-28 13:29:47 +08:00
|
|
|
const nextToken = sql.indexOf('*/', parser.index + 1);
|
2019-09-25 15:43:36 +08:00
|
|
|
if (nextToken != -1) {
|
|
|
|
parser.index = nextToken + 1;
|
|
|
|
parser.queue = '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parser.index = sql.length - 1;
|
|
|
|
parser.queue = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function splitToken(parser, sql) {
|
2020-08-28 13:29:47 +08:00
|
|
|
const queue = parser.queue;
|
2019-09-25 15:43:36 +08:00
|
|
|
if (queue.endsWith(';')) {
|
2019-09-29 17:38:52 +08:00
|
|
|
pushSql(parser, sql);
|
2019-09-25 15:43:36 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 13:29:47 +08:00
|
|
|
const parser = {
|
2019-09-25 15:43:36 +08:00
|
|
|
index: 0,
|
|
|
|
queue: '',
|
2020-08-28 13:29:47 +08:00
|
|
|
sqls: [],
|
2019-09-25 15:43:36 +08:00
|
|
|
};
|
|
|
|
for (parser.index = 0; parser.index < sql.length; parser.index++) {
|
2020-08-28 13:29:47 +08:00
|
|
|
const char = sql[parser.index];
|
2019-09-25 15:43:36 +08:00
|
|
|
parser.queue += char;
|
2020-08-28 13:29:47 +08:00
|
|
|
const tokenFuncs = [
|
|
|
|
quoteToken,
|
|
|
|
singleLineCommentToken,
|
|
|
|
multipleLineCommentToken,
|
|
|
|
splitToken,
|
|
|
|
];
|
2019-09-25 15:43:36 +08:00
|
|
|
for (let i = 0; i < tokenFuncs.length; i++) {
|
|
|
|
tokenFuncs[i](parser, sql);
|
|
|
|
}
|
2019-09-29 17:38:52 +08:00
|
|
|
if (parser.index == sql.length - 1 && parser.queue) {
|
|
|
|
pushSql(parser, sql);
|
|
|
|
}
|
2019-09-25 15:43:36 +08:00
|
|
|
}
|
|
|
|
return parser.sqls;
|
|
|
|
}
|
|
|
|
exports.splitSql = splitSql;
|