UNPKG

soda-angular

Version:
92 lines (91 loc) 3.95 kB
import { Comparitor } from "../../soql-query/clauses/where/comparitor"; import { Between } from '../../soql-query/clauses/where/functions/between'; import { In } from '../../soql-query/clauses/where/functions/in'; import { Like } from '../../soql-query/clauses/where/functions/like'; import { NotBetween } from '../../soql-query/clauses/where/functions/not-between'; import { NotIn } from '../../soql-query/clauses/where/functions/not-in'; import { NotLike } from '../../soql-query/clauses/where/functions/not-like'; import { StartsWith } from '../../soql-query/clauses/where/functions/starts-with'; import { Operator } from '../../soql-query/clauses/where/operator'; import { WhereFilter } from '../../soql-query/clauses/where/where-filter'; import { WhereOperator } from '../../soql-query/clauses/where/where-operator'; import { WhereValue } from "../../soql-query/clauses/where/where-value"; export class BasicWhereFilter { constructor(query, column, ...prependOperators) { this.query = query; this.column = column; if (!query) { throw new Error("Query must be provided"); } if (!column) { throw new Error("Column must be provided"); } this.prependOperators = prependOperators; } equals(value) { const filter = new WhereFilter(this.column, Comparitor.Equals, new WhereValue(value)); return this.addFilter(filter); } greaterThan(value) { const filter = new WhereFilter(this.column, Comparitor.GreaterThan, new WhereValue(value)); return this.addFilter(filter); } lessThan(value) { const filter = new WhereFilter(this.column, Comparitor.LessThan, new WhereValue(value)); return this.addFilter(filter); } isNull() { const filter = new WhereFilter(this.column, Comparitor.IsNull); return this.addFilter(filter); } isNotNull() { const filter = new WhereFilter(this.column, Comparitor.IsNotNull); return this.addFilter(filter); } between(from, to) { const filter = new Between(this.column, new WhereValue(from), new WhereValue(to)); return this.addFilter(filter); } notBetween(from, to) { const filter = new NotBetween(this.column, new WhereValue(from), new WhereValue(to)); return this.addFilter(filter); } in(...values) { if (!values || values.length === 0) { throw new Error("Values must be provided"); } const filter = new In(this.column, values.map(v => new WhereValue(v))); return this.addFilter(filter); } notIn(...values) { if (!values || values.length === 0) { throw new Error("Values must be provided"); } const filter = new NotIn(this.column, values.map(v => new WhereValue(v))); return this.addFilter(filter); } like(value) { const filter = new Like(this.column, new WhereValue(value)); return this.addFilter(filter); } notLike(value) { const filter = new NotLike(this.column, new WhereValue(value)); return this.addFilter(filter); } startsWith(value) { const filter = new StartsWith(this.column, new WhereValue(value)); return this.addFilter(filter); } not() { if (this.checkIfAlreadyNotted()) { throw new Error('Double negatives are bad and you should feel bad for trying this'); } return new BasicWhereFilter(this.query, this.column, ...this.prependOperators, new WhereOperator(Operator.Not)); } checkIfAlreadyNotted() { return this.prependOperators.length > 0 && this.prependOperators[this.prependOperators.length - 1].Operator === Operator.Not; } addFilter(filter) { return this.query.addFilter(...this.prependOperators, filter); } }