@sequeljs/ast
Version:
A SQL AST manager for JavaScript
207 lines • 7.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const And_1 = require("../nodes/And");
const Between_1 = require("../nodes/Between");
const DoesNotMatch_1 = require("../nodes/DoesNotMatch");
const Equality_1 = require("../nodes/Equality");
const GreaterThan_1 = require("../nodes/GreaterThan");
const GreaterThanOrEqual_1 = require("../nodes/GreaterThanOrEqual");
const Grouping_1 = require("../nodes/Grouping");
const In_1 = require("../nodes/In");
const IsDistinctFrom_1 = require("../nodes/IsDistinctFrom");
const IsNotDistinctFrom_1 = require("../nodes/IsNotDistinctFrom");
const LessThan_1 = require("../nodes/LessThan");
const LessThanOrEqual_1 = require("../nodes/LessThanOrEqual");
const Matches_1 = require("../nodes/Matches");
const NotEqual_1 = require("../nodes/NotEqual");
const NotIn_1 = require("../nodes/NotIn");
const NotRegexp_1 = require("../nodes/NotRegexp");
const Or_1 = require("../nodes/Or");
const Regexp_1 = require("../nodes/Regexp");
const buildQuoted_1 = require("../nodes/buildQuoted");
class Predications {
groupingAll(method, others, ...extras) {
const nodes = others.map((expr) => method(expr, ...extras));
return new Grouping_1.default(new And_1.default(nodes));
}
groupingAny(method, others, ...extras) {
const nodes = others.map((expr) => method(expr, ...extras));
return new Grouping_1.default(nodes.reduce((memo, node) => new Or_1.default(memo, node)));
}
isInfinity(value) {
var _a;
return value === Infinity || value === -Infinity || ((_a = value.isInfinite) === null || _a === void 0 ? void 0 : _a.call(value));
}
isOpenEnded(value) {
return value === null || this.isInfinity(value) || this.isUnboundable(value);
}
isUnboundable(value) {
var _a;
return (_a = value === null || value === void 0 ? void 0 : value.isUnboundable) === null || _a === void 0 ? void 0 : _a.call(value);
}
doesNotMatch(other, escape = null, caseSensitive = false) {
return new DoesNotMatch_1.default(this, this.quotedNode(other), escape, caseSensitive);
}
doesNotMatchAll(others, escape = null, caseSensitive = false) {
return this.groupingAll(this.doesNotMatch.bind(this), others, escape, caseSensitive);
}
doesNotMatchAny(others, escape = null, caseSensitive = false) {
return this.groupingAny(this.doesNotMatch.bind(this), others, escape, caseSensitive);
}
doesNotMatchRegexp(other, caseSensitive = true) {
return new NotRegexp_1.default(this, this.quotedNode(other), caseSensitive);
}
eq(other) {
return new Equality_1.default(this, this.quotedNode(other));
}
eqAll(others) {
return this.groupingAll(this.eq.bind(this), this.quotedArray(others));
}
eqAny(others) {
return this.groupingAny(this.eq.bind(this), others);
}
gt(other) {
return new GreaterThan_1.default(this, this.quotedNode(other));
}
gtAll(others) {
return this.groupingAll(this.gt.bind(this), others);
}
gtAny(others) {
return this.groupingAny(this.gt.bind(this), others);
}
gteq(other) {
return new GreaterThanOrEqual_1.default(this, this.quotedNode(other));
}
gteqAll(others) {
return this.groupingAll(this.gteq.bind(this), others);
}
gteqAny(others) {
return this.groupingAny(this.gteq.bind(this), others);
}
inVal(other) {
if (typeof other === 'object' && !Array.isArray(other) && 'ast' in other) {
return new In_1.default(this, other.ast);
}
if (Array.isArray(other) && Symbol.iterator in other) {
return new In_1.default(this, this.quotedArray(other));
}
return new In_1.default(this, this.quotedNode(other));
}
inAll(others) {
return this.groupingAll(this.inVal.bind(this), others);
}
inAny(others) {
return this.groupingAny(this.inVal.bind(this), others);
}
isNotDistinctFrom(other) {
return new IsNotDistinctFrom_1.default(this, this.quotedNode(other));
}
isDistinctFrom(other) {
return new IsDistinctFrom_1.default(this, this.quotedNode(other));
}
lt(other) {
return new LessThan_1.default(this, this.quotedNode(other));
}
ltAll(others) {
return this.groupingAll(this.lt.bind(this), others);
}
ltAny(others) {
return this.groupingAny(this.lt.bind(this), others);
}
lteq(other) {
return new LessThanOrEqual_1.default(this, this.quotedNode(other));
}
lteqAll(others) {
return this.groupingAll(this.lteq.bind(this), others);
}
lteqAny(others) {
return this.groupingAny(this.lteq.bind(this), others);
}
matches(other, escape = null, caseSensitive = false) {
return new Matches_1.default(this, this.quotedNode(other), escape, caseSensitive);
}
matchesAll(others, escape = null, caseSensitive = false) {
return this.groupingAll(this.matches.bind(this), others, escape, caseSensitive);
}
matchesAny(others, escape = null, caseSensitive = false) {
return this.groupingAny(this.matches.bind(this), others, escape, caseSensitive);
}
matchesRegexp(other, caseSensitive = true) {
return new Regexp_1.default(this, this.quotedNode(other), caseSensitive);
}
notEq(other) {
return new NotEqual_1.default(this, this.quotedNode(other));
}
notEqAll(others) {
return this.groupingAll(this.notEq.bind(this), others);
}
notEqAny(others) {
return this.groupingAny(this.notEq.bind(this), others);
}
notInVal(other) {
if (typeof other === 'object' && !Array.isArray(other) && 'ast' in other) {
return new NotIn_1.default(this, other.ast);
}
if (Array.isArray(other) && Symbol.iterator in other) {
return new NotIn_1.default(this, this.quotedArray(other));
}
return new NotIn_1.default(this, this.quotedNode(other));
}
notInAll(others) {
return this.groupingAll(this.notInVal.bind(this), others);
}
notInAny(others) {
return this.groupingAny(this.notInVal.bind(this), others);
}
quotedNode(other) {
return (0, buildQuoted_1.default)(other, this);
}
quotedArray(others) {
return others.map((v) => this.quotedNode(v));
}
between(begin, end, inclusive = true) {
if (this.isUnboundable(begin) || this.isUnboundable(end)) {
return this.inVal([]);
}
if (this.isOpenEnded(begin)) {
if (this.isOpenEnded(end)) {
return this.notInVal([]);
}
if (!inclusive) {
return this.lt(end);
}
return this.lteq(end);
}
if (this.isOpenEnded(end)) {
return this.gteq(begin);
}
if (!inclusive) {
return this.gteq(begin).and(this.lt(end));
}
const left = this.quotedNode(begin);
const right = this.quotedNode(end);
return new Between_1.default(this, left.and(right));
}
notBetween(begin, end, inclusive = true) {
if (this.isUnboundable(begin) || this.isUnboundable(end)) {
return this.notInVal([]);
}
if (this.isOpenEnded(begin)) {
if (this.isOpenEnded(end)) {
return this.inVal([]);
}
if (!inclusive) {
return this.gteq(end);
}
return this.gt(end);
}
if (this.isOpenEnded(end)) {
return this.lt(begin);
}
const left = this.lt(begin);
const right = !inclusive ? this.gteq(end) : this.gt(end);
return left.or(right);
}
}
exports.default = Predications;
//# sourceMappingURL=Predications.js.map