lite-ui-sql-parser
Version:
simple node sql parser
62 lines (55 loc) • 2.04 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "./create", "./alter", "./select", "./delete", "./update", "./insert", "./command"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("./create"), require("./alter"), require("./select"), require("./delete"), require("./update"), require("./insert"), require("./command"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.create, global.alter, global.select, global._delete, global.update, global.insert, global.command);
global.union = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _create, _alter, _select, _delete, _update, _insert, _command) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.unionToSQL = unionToSQL;
_exports.multipleToSQL = multipleToSQL;
const typeToSQLFn = {
alter: _alter.alterToSQL,
create: _create.createToSQL,
select: _select.selectToSQL,
delete: _delete.deleteToSQL,
update: _update.updateToSQL,
insert: _insert.insertToSQL,
drop: _command.dropToSQL,
truncate: _command.truncateToSQL,
use: _command.useToSQL,
rename: _command.renameToSQL,
call: _command.callToSQL,
set: _command.setVarToSQL,
lock: _command.lockUnlockToSQL,
unlock: _command.lockUnlockToSQL
};
function unionToSQL(stmt) {
const fun = typeToSQLFn[stmt.type];
const res = [fun(stmt)];
while (stmt._next) {
const unionKeyword = (stmt.union || 'union').toUpperCase();
res.push(unionKeyword, fun(stmt._next));
stmt = stmt._next;
}
return res.join(' ');
}
function multipleToSQL(stmt) {
const res = [];
for (let i = 0, len = stmt.length; i < len; ++i) {
let astInfo = stmt[i] && stmt[i].ast;
if (!astInfo) astInfo = stmt[i];
res.push(unionToSQL(astInfo));
}
return res.join(' ; ');
}
});