@ultipa-graph/ultipa-driver
Version:
NodeJS SDK for ultipa-server 5.2
205 lines • 5.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EasyUqlParse = exports.EasyUqlItem = void 0;
const uql_maker_1 = require("./uql-maker");
let { CommandList } = uql_maker_1.UQLMAKER;
const WriteCommands = [
"alter",
"create",
"drop",
"grant",
"revoke",
"LTE",
"UFE",
"truncate",
"compact",
"insert",
"upsert",
"update",
"delete",
"clear",
"stop",
"pause",
"resume",
"top",
"kill",
CommandList.unmount,
CommandList.mount,
];
const GLOBALCommands = [
CommandList.showUser,
CommandList.getUser,
CommandList.createUser,
CommandList.alterUser,
CommandList.dropUser,
CommandList.grantUser,
CommandList.revokeUser,
CommandList.showPolicy,
CommandList.getPolicy,
CommandList.createPolicy,
CommandList.alterPolicy,
CommandList.dropPolicy,
CommandList.showPrivilege,
CommandList.stats,
CommandList.showGraph,
CommandList.getGraph,
CommandList.createGraph,
CommandList.dropGraph,
CommandList.updateGraph,
CommandList.top,
CommandList.kill,
// CommandList.unmount,
// CommandList.mount,
];
const ExtraCommands = [
CommandList.top,
CommandList.kill,
CommandList.showTask,
CommandList.stopTask,
CommandList.clearTask,
CommandList.stats,
CommandList.showGraph,
CommandList.listAlgo,
CommandList.getGraph,
CommandList.createPolicy,
CommandList.alterPolicy,
CommandList.showPolicy,
CommandList.getPolicy,
CommandList.grantUser,
CommandList.revokeUser,
CommandList.showPrivilege,
CommandList.getUser,
CommandList.getSelfInfo,
CommandList.createUser,
CommandList.alterUser,
CommandList.dropUser,
CommandList.showIndex,
];
class EasyUqlItem {
name;
params;
constructor(name, params) {
this.name = name;
this.params = params;
}
getListParams() {
let ps = [];
let str = this.params.trim();
if (str === "") {
return ps;
}
if (!str.startsWith("}")) {
let ss = str.split(",");
ss.forEach((p) => {
ps.push(p.replace(/^[ |"|']+/g, "").replace(/[ |"|']+$/g, ""));
});
}
else {
ps.push(str);
}
return ps;
}
safelyGetFirstParams() {
let ps = this.getListParams();
if (ps.length > 0) {
return ps[0];
}
return "";
}
isEmptyParams() {
return this.params.trim() === "";
}
}
exports.EasyUqlItem = EasyUqlItem;
class EasyUqlParse {
uql;
commands;
limit;
constructor(uql) {
this.uql = uql;
this.commands = [];
this.parse(this.uql);
}
parse(uqlStr) {
if (!uqlStr) {
return;
}
let commandReg = /([a-z_A-Z]*)\(([^\(|^\)]*)\)/g;
let matchAll = uqlStr.matchAll(commandReg);
for (const m of matchAll) {
// console.log(m)
let name = m[1];
let value = m[2] || "";
value = value.trim();
this.commands.push(new EasyUqlItem(name, value));
}
let limits = uqlStr.split("limit ");
this.limit = limits?.[1]?.trim();
}
commandsEqual(commands) {
var parseCommands = [];
commands.forEach((_, i) => {
let name = "";
let c = this.getCommand(i);
if (c) {
name = c.name;
}
parseCommands.push(name);
});
return commands.join("|") == parseCommands.join("|");
}
getCommand(index) {
if (this.commands.length > index) {
return this.commands[index];
}
return null;
}
getLimit() {
let limit = this.limit || this.getCommandFirstOneWithName("limit")?.getListParams()?.[0];
return limit;
}
getCommandFirstOneWithName(name) {
for (let index = 0; index < this.commands.length; index++) {
const element = this.commands[index];
if (element.name == name) {
return element;
}
}
return null;
}
firstCommandName() {
return this.getCommand(0)?.name;
}
secondCommandName() {
return this.getCommand(1)?.name;
}
hasWith() {
return this.uql?.toLowerCase().split("with").length > 1;
}
hasWrite() {
for (const c of this.commands) {
if (WriteCommands.includes(c.name)) {
return true;
}
}
return false;
}
hasAlgo() {
return this.firstCommandName() == "algo";
}
hasExecTask() {
return this.uql?.toLowerCase().includes("exec task");
}
isGlobal() {
let c1 = this.firstCommandName();
let c2 = `${this.firstCommandName()}().${this.secondCommandName()}`;
return GLOBALCommands.includes(c1) || GLOBALCommands.includes(c2);
}
isExtra() {
let c1 = this.firstCommandName();
let c2 = `${this.firstCommandName()}().${this.secondCommandName()}`;
return ExtraCommands.includes(c1) || ExtraCommands.includes(c2);
}
}
exports.EasyUqlParse = EasyUqlParse;
//# sourceMappingURL=uql.parse.js.map