UNPKG

@sequeljs/ast

Version:

A SQL AST manager for JavaScript

204 lines 7.41 kB
import And from '../nodes/And'; import Between from '../nodes/Between'; import DoesNotMatch from '../nodes/DoesNotMatch'; import Equality from '../nodes/Equality'; import GreaterThan from '../nodes/GreaterThan'; import GreaterThanOrEqual from '../nodes/GreaterThanOrEqual'; import Grouping from '../nodes/Grouping'; import In from '../nodes/In'; import IsDistinctFrom from '../nodes/IsDistinctFrom'; import IsNotDistinctFrom from '../nodes/IsNotDistinctFrom'; import LessThan from '../nodes/LessThan'; import LessThanOrEqual from '../nodes/LessThanOrEqual'; import Matches from '../nodes/Matches'; import NotEqual from '../nodes/NotEqual'; import NotIn from '../nodes/NotIn'; import NotRegexp from '../nodes/NotRegexp'; import Or from '../nodes/Or'; import Regexp from '../nodes/Regexp'; import buildQuoted from '../nodes/buildQuoted'; export default class Predications { groupingAll(method, others, ...extras) { const nodes = others.map((expr) => method(expr, ...extras)); return new Grouping(new And(nodes)); } groupingAny(method, others, ...extras) { const nodes = others.map((expr) => method(expr, ...extras)); return new Grouping(nodes.reduce((memo, node) => new Or(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(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(this, this.quotedNode(other), caseSensitive); } eq(other) { return new Equality(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(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(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(this, other.ast); } if (Array.isArray(other) && Symbol.iterator in other) { return new In(this, this.quotedArray(other)); } return new In(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(this, this.quotedNode(other)); } isDistinctFrom(other) { return new IsDistinctFrom(this, this.quotedNode(other)); } lt(other) { return new LessThan(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(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(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(this, this.quotedNode(other), caseSensitive); } notEq(other) { return new NotEqual(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(this, other.ast); } if (Array.isArray(other) && Symbol.iterator in other) { return new NotIn(this, this.quotedArray(other)); } return new NotIn(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 buildQuoted(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(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); } } //# sourceMappingURL=Predications.js.map