snack-query-builder
Version:
Query generator for SQL
134 lines • 4.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConditionalQueryBuilder = void 0;
const aggregation_enum_1 = require("./aggregation.enum");
class ConditionalQueryBuilder {
constructor(qb) {
this.$conditions = [];
this.$queryBuilder = qb;
}
isNull(agg, field) {
this.$conditions.push(`${this.aggregation(agg)}${field} is NULL`);
return this;
}
isNotNull(agg, field) {
this.$conditions.push(`${this.aggregation(agg)}${field} is not NULL`);
return this;
}
notEqualThan(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} <> '${value}'`);
return this;
}
equalThan(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} = '${value}'`);
return this;
}
greaterThan(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} > '${value}'`);
return this;
}
lowerThan(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} < '${value}'`);
return this;
}
greaterOrEqualThan(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} >= '${value}'`);
return this;
}
lowerOrEqualThan(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} <= '${value}'`);
return this;
}
between(agg, field, value, value2) {
this.$conditions.push(`${this.aggregation(agg)}${field} between '${value}' and '${value2}'`);
return this;
}
startsWith(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} like '%${value}'`);
return this;
}
endsWith(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} like '${value}%'`);
return this;
}
contains(agg, field, value) {
this.$conditions.push(`${this.aggregation(agg)}${field} like '%${value}%'`);
return this;
}
in(agg, field, ...values) {
values = values.map(i => `'${i}'`);
this.$conditions.push(`${this.aggregation(agg)}${field} in (${values.join(', ')})`);
return this;
}
openParentheses(agg) {
this.$conditions.push(`${this.aggregation(agg)}(`);
return this;
}
closeParentheses() {
this.$conditions.push(`)`);
return this;
}
isToday(agg, field) {
const $today = new Date(new Date().toDateString());
const $tomorrow = new Date($today);
$tomorrow.setDate($tomorrow.getDate() + 1);
this.between(agg, field, $today.toISOString(), $tomorrow.toISOString());
return this;
}
isEmpty(agg, field) {
this.openParentheses(agg);
this.isNull(aggregation_enum_1.Aggregation.and, field);
this.equalThan(aggregation_enum_1.Aggregation.or, field, '');
this.closeParentheses();
return this;
}
isNotEmpty(agg, field) {
this.openParentheses(agg);
this.isNotNull(aggregation_enum_1.Aggregation.and, field);
this.notEqualThan(aggregation_enum_1.Aggregation.or, field, '');
this.closeParentheses();
return this;
}
toString() {
const spaces = ' ';
if (this.$conditions.length === 0) {
return '';
}
const conditions = this.$conditions.join(`\n${spaces}`);
if (!this.checkParenthesesBalanced(conditions)) {
throw 'Parentheses are not balanced';
}
return `where ${conditions}`;
}
aggregation(agg) {
if (this.$conditions.length === 0) {
return '';
}
if (this.$conditions[this.$conditions.length - 1].endsWith('(')) {
return '';
}
return `${agg} `;
}
checkParenthesesBalanced(input) {
const brackets = '[]{}()';
const stack = [];
const inputBrackets = input
.split('')
.filter(i => brackets.includes(i))
.join('');
for (const bracket of inputBrackets) {
const bracketsIndex = brackets.indexOf(bracket);
if (bracketsIndex % 2 === 0) {
stack.push(bracketsIndex + 1);
}
else {
if (stack.pop() !== bracketsIndex) {
return false;
}
}
}
return stack.length === 0;
}
}
exports.ConditionalQueryBuilder = ConditionalQueryBuilder;
//# sourceMappingURL=conditional-query-builder.js.map