@opra/common
Version:
Opra common package
127 lines (126 loc) • 3.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.$or = $or;
exports.$and = $and;
exports.$date = $date;
exports.$time = $time;
exports.$number = $number;
exports.$array = $array;
exports.$field = $field;
exports.$eq = $eq;
exports.$ne = $ne;
exports.$gt = $gt;
exports.$gte = $gte;
exports.$lt = $lt;
exports.$lte = $lte;
exports.$in = $in;
exports.$notIn = $notIn;
exports.$like = $like;
exports.$notLike = $notLike;
exports.$ilike = $ilike;
exports.$notILike = $notILike;
exports.$paren = $paren;
exports.$arithmetic = $arithmetic;
const index_js_1 = require("./ast/index.js");
function $or(...items) {
return new index_js_1.LogicalExpression({ op: 'or', items });
}
function $and(...items) {
return new index_js_1.LogicalExpression({ op: 'and', items });
}
function $date(v) {
return new index_js_1.DateLiteral(v);
}
function $time(v) {
return new index_js_1.TimeLiteral(v);
}
function $number(v) {
return new index_js_1.NumberLiteral(v);
}
function $array(...items) {
return new index_js_1.ArrayExpression(items.map(wrapEntryValue));
}
function $field(v) {
return new index_js_1.QualifiedIdentifier(v);
}
function $eq(left, right) {
return comparisonExpression('=', left, right);
}
function $ne(left, right) {
return comparisonExpression('!=', left, right);
}
function $gt(left, right) {
return comparisonExpression('>', left, right);
}
function $gte(left, right) {
return comparisonExpression('>=', left, right);
}
function $lt(left, right) {
return comparisonExpression('<', left, right);
}
function $lte(left, right) {
return comparisonExpression('<=', left, right);
}
function $in(left, right) {
return comparisonExpression('in', left, right);
}
function $notIn(left, right) {
return comparisonExpression('!in', left, right);
}
function $like(left, right) {
return comparisonExpression('like', left, right);
}
function $notLike(left, right) {
return comparisonExpression('!like', left, right);
}
function $ilike(left, right) {
return comparisonExpression('ilike', left, right);
}
function $notILike(left, right) {
return comparisonExpression('!ilike', left, right);
}
function $paren(expression) {
return new index_js_1.ParenthesizedExpression(expression);
}
function $arithmetic(n) {
const exp = new index_js_1.ArithmeticExpression();
exp.add = (expression) => {
exp.append('+', _wrapEntryValue(expression));
return exp;
};
exp.sub = (expression) => {
exp.append('-', _wrapEntryValue(expression));
return exp;
};
exp.mul = (expression) => {
exp.append('*', _wrapEntryValue(expression));
return exp;
};
exp.div = (expression) => {
exp.append('/', _wrapEntryValue(expression));
return exp;
};
exp.append('+', wrapEntryValue(n));
return exp;
}
function comparisonExpression(op, left, right) {
const lex = typeof left === 'string'
? new index_js_1.QualifiedIdentifier(left)
: wrapEntryValue(left);
const rex = wrapEntryValue(right);
return new index_js_1.ComparisonExpression({ op, left: lex, right: rex });
}
const wrapEntryValue = (v) => Array.isArray(v) ? $array(...v.map(_wrapEntryValue)) : _wrapEntryValue(v);
const _wrapEntryValue = (v) => {
if (v instanceof index_js_1.Expression)
return v;
if (typeof v === 'boolean')
return new index_js_1.BooleanLiteral(v);
if (typeof v === 'number' || typeof v === 'bigint')
return new index_js_1.NumberLiteral(v);
if (v == null)
return new index_js_1.NullLiteral();
if (v instanceof Date)
return new index_js_1.DateLiteral(v);
return new index_js_1.StringLiteral('' + v);
};