UNPKG

@wener/miniquery

Version:

SQL Where like **safe** filter expression for ORM.

189 lines (188 loc) 4.79 kB
import { MiniQueryGrammar, MiniQuerySemantics } from "./grammar/index.js"; var Ops = { "&&": "and", "||": "or", "=": "eq", "==": "eq", "!=": "ne", "<>": "ne", ">": "gt", ">=": "gte", "<": "lt", "<=": "lte", ":": "has" }; export function getMiniQueryASTOp(v) { var s = v.toLowerCase().trim().replaceAll(/\s+/g, " "); return Ops[s] || s; } var actions = { nonEmpty: function nonEmpty(first, _, rest, _pad) { return [ first ].concat(rest.children).map(function (v) { return v.toAST(); }); }, rel: function rel(a, op, b) { var v = op.sourceString; if (op.isIteration()) { v = op.children.map(function (vv) { return vv.sourceString; }).join(" "); } return { type: "rel", op: getMiniQueryASTOp(v), a: a.toAST(), b: b.toAST(), v: "bool" }; }, empty: function empty() { return []; } }; export function toMiniQueryAST(s) { var match; if (typeof s === "string") { match = MiniQueryGrammar.match(s); } else { match = s; } if (match.failed()) { throw new SyntaxError("Invalid MiniQuery: ".concat(match.message)); } return MiniQuerySemantics(match).toAST(); } MiniQuerySemantics.addOperation("toAST()", { Main: function Main(expr, _) { return expr.toAST(); }, LogicExpr_match: function LogicExpr_match(a, op, b) { return { type: "logic", op: getMiniQueryASTOp(op.sourceString), a: a.toAST(), b: b.toAST(), v: "bool" }; }, RelExpr_match: actions.rel, RelExpr_match_eq: actions.rel, RelExpr_has: actions.rel, InExpr_match: actions.rel, PredicateExpr_like: actions.rel, PredicateExpr_is: actions.rel, BetweenExpr_match: function BetweenExpr_match(a, op, b, _, c) { return { type: "between", op: op.sourceString, a: a.toAST(), b: b.toAST(), c: c.toAST(), v: "bool" }; }, CallExpr_match: function CallExpr_match(n, _, v, _end) { return { type: "call", name: n.sourceString, value: v.toAST() }; }, PriExpr_paren: function PriExpr_paren(_, v, _end) { return { type: "paren", value: v.toAST() }; }, PriExpr_not: function PriExpr_not(op, v) { return { type: "unary", op: getMiniQueryASTOp(op.sourceString), value: v.toAST(), v: "bool" }; }, PriExpr_pos: function PriExpr_pos(op, v) { return { type: "unary", op: getMiniQueryASTOp(op.sourceString), value: v.toAST() }; }, PriExpr_neg: function PriExpr_neg(op, v) { return { type: "unary", op: getMiniQueryASTOp(op.sourceString), value: v.toAST() }; }, Array: function Array1(_, list, _end) { return { type: "array", value: list.toAST(), v: Array }; }, int: function (s, _, v) { return { type: "int", value: parseInt("".concat(s.sourceString || "").concat(v.sourceString)), v: "int" }; }, float: function (i, _, f) { return { type: "float", value: parseFloat("".concat((i === null || i === void 0 ? void 0 : i.sourceString) || 0, ".").concat(f.sourceString)), v: Number }; }, string: function (_, v, _end) { return { type: "string", value: v.sourceString, v: "string" }; }, ident: function (a, b) { return { type: "identifier", name: [ a, b ].map(function (v) { return v.sourceString; }).join("") }; }, null: function (_) { return { type: "null", v: "null" }; }, bool: function (v) { return { type: "bool", value: v.sourceString.toLowerCase() === "true", v: "bool" }; }, ref: function ref(a, _b, c) { return { type: "ref", name: [ a.sourceString ].concat(c.children.map(function (v) { var ast = v.toAST(); return ast.name || ast.value; })) }; }, TrailNonEmptyListOf: actions.nonEmpty, EmptyListOf: actions.empty });