UNPKG

ts-db-helper

Version:
163 lines 5.67 kB
import { ColumnClauseValue } from './column-clause-value.model'; import { ClauseComparators } from '../constants/clause-comparators.constant'; import { ClauseOperators } from '../constants/clause-operators.constant'; import { QueryError } from '../../errors/query.error'; import { QueryPart } from './query-part.model'; /** * @public API * @class CompositeClause * * @description * Clause that allow integrators to compare tuple of value like in this raw query: * "SELECT * FROM Animals WHERE (species, color) IN (('dog', 'white'), ('cat', 'ginger'), ('bird', 'blue'))" * * @example * ```typescript * // Create a group of clauses * const composite = new CompositeClause(['species', 'color'], [['dog', 'white'], ['cat', 'ginger'], ['bird', 'blue']]); * composite.comparator = ClauseCompators.IN * * // start select clause * Select(Animal).where(composite).exec().subscribe((QueryResult<Animal>) => { * // do something with the result... * }, (err) => { * // do something with the error... * }); * ``` * * @author Olivier Margarit * @since 0.2 */ var CompositeClause = /** @class */ (function () { /** * @public * @constructor create new instance of composite clause * @param {Array<string>} keys optional list of columns from the composite clause to compare with * @param {Array<any>} values optional list of values to compare with */ function CompositeClause(keys, values) { /** * @public * @property {string} operator is used as join clause condition * * @default ClauseOperators.AND */ this.operator = ClauseOperators.AND; /** * @private * @property {Array<string>} keysValue list of column from the composite clause */ this.keysValue = []; /** * @private * @property {Array<any>} valuesValue list of values matching with the column keys to compare with */ this.valuesValue = []; /** * @public * @property {boolean} not is an additionnal operator to invert condition */ this.not = false; /** * @public * @property {string} comparator is a comparator that define the type of * relation with the value to compare with and the result. * * @default ClauseComparators.EQ */ this.comparator = ClauseComparators.EQ; if (keys) { this.keysValue = keys; } if (values) { this.valuesValue = values; } } Object.defineProperty(CompositeClause.prototype, "keys", { /** * @public * @property {Array<string>} key is table column name corresponding to the field * to compare with */ get: function () { return this.keysValue; }, enumerable: true, configurable: true }); Object.defineProperty(CompositeClause.prototype, "values", { /** * @public * @property {Array<any>} value is the column value to compare with */ get: function () { return this.valuesValue; }, enumerable: true, configurable: true }); /** * @public * @method addKey add column to compare with * * @param {string} value the column reference */ CompositeClause.prototype.addKey = function (value) { this.keysValue.push(value); }; /** * @public * @method addValue add value to compare with * @param {any} value the value to compare with */ CompositeClause.prototype.addValue = function (value) { this.valuesValue.push(value); }; /** * @public * @method build should be removed to be a part of the private API * * @return {QueryPart} query part of the query with the string part and * clauses params. */ CompositeClause.prototype.build = function () { if (!this.keys && !this.values) { throw new QueryError('one clause has no key for column name.', '', ''); } var queryPart = new QueryPart(); if (this.not) { queryPart.appendContent('NOT'); } queryPart.appendContent('(`' + this.keysValue.join('`, `') + '`)'); queryPart.appendContent(this.comparator); queryPart.appendContent('('); var isFirst = true; for (var _i = 0, _a = this.valuesValue; _i < _a.length; _i++) { var value = _a[_i]; var valueQueryPart = new QueryPart(); if (!isFirst) { valueQueryPart.appendContent(','); } else { isFirst = false; } if (Array.isArray(value)) { valueQueryPart.appendContent('(' + Array(value.length).fill('(?)').join(', ') + ')'); valueQueryPart.params = value; } else if (value instanceof ColumnClauseValue) { valueQueryPart.appendContent(value.fqn()); } else { valueQueryPart.appendContent('(?)'); valueQueryPart.params.push(value === undefined ? null : value); } queryPart.append(valueQueryPart); } queryPart.appendContent(')'); return queryPart; }; return CompositeClause; }()); export { CompositeClause }; //# sourceMappingURL=composite-clause.model.js.map