Skip to content

Commit

Permalink
implement or, and, filterWhereNot for DataSync; add where alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyj0 committed Feb 8, 2022
1 parent 051a9fd commit f90ebb2
Showing 1 changed file with 118 additions and 3 deletions.
121 changes: 118 additions & 3 deletions lib/IHP/DataSync/ihp-querybuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,94 @@ function ihpBackendUrl(path) {
return host + path;
}

function where() {
return filterWhere.apply(this, Array.from(arguments))
}

function whereNot() {
return filterWhereNot.apply(this, Array.from(arguments))
}

function filterWhere() {
return ConditionBuilder.prototype.filterWhere.apply(new ConditionBuilder(), Array.from(arguments))
}

function filterWhereNot() {
return ConditionBuilder.prototype.filterWhereNot.apply(new ConditionBuilder(), Array.from(arguments))
}

class ConditionBuilder {
constructor() {
this.conditions = null;
}

#addCondition(operator, conditionExpression) {
if (this.conditions === null) {
this.conditions = conditionExpression;
} else {
this.conditions = {
tag: 'InfixOperatorExpression',
left: this.conditions,
op: operator,
right: conditionExpression,
};
}
}

where() {
return this.filterWhere.apply(this, Array.from(arguments))
}

whereNot() {
return this.filterWhereNot.apply(this, Array.from(arguments))
}

filterWhere(field, value) {
const left = { tag: 'ColumnExpression', field };
const op = 'OpEqual';
const right = { tag: 'LiteralExpression', value: jsValueToDynamicValue(value) }
const conditionExpression = { tag: 'InfixOperatorExpression', left, op, right }
this.#addCondition('OpAnd', conditionExpression)

return this
}

filterWhereNot(field, value) {
const left = { tag: 'ColumnExpression', field }
const op = 'OpNotEqual'
const right = { tag: 'LiteralExpression', value: jsValueToDynamicValue(value) }
const conditionExpression = { tag: 'InfixOperatorExpression', left, op, right }
this.#addCondition('OpAnd', conditionExpression)

return this
}

or(conditionBuilder) {
if (this.conditions === null) {
throw new Error('You are attempting to add a condition using `or`, but there are no previous conditions to apply the `or` to. You probably want `where` instead.')
}
const conditionExpression = conditionBuilder.build();
this.#addCondition('OpOr', conditionExpression)
return this
}

and(conditionBuilder) {
if (this.conditions === null) {
throw new Error('You are attempting to add a condition using `and`, but there are no previous conditions to apply the `and` to. You probably want `where` instead.')
}
const conditionExpression = conditionBuilder.build();
this.#addCondition('OpAnd', conditionExpression)
return this
}

build() {
if (this.conditions === null) {
return { tag: 'LiteralExpression', value: jsValueToDynamicValue(true) }
}
return this.conditions;
}
}

class QueryBuilder {
constructor(table) {
// Maps to 'DynamicSQLQuery'
Expand All @@ -34,14 +122,23 @@ class QueryBuilder {
if (this.query.whereCondition === null) {
this.query.whereCondition = conditionExpression;
} else {
this.query.whereCondition = { tag: 'InfixOperatorExpression', this.query.whereCondition, operator, conditionExpression };
this.query.whereCondition = {
tag: 'InfixOperatorExpression',
left: this.query.whereCondition,
op: operator,
right: conditionExpression,
};
}
}

where() {
return this.filterWhere.apply(this, Array.from(arguments))
}

whereNot() {
return this.filterWhereNot.apply(this, Array.from(arguments))
}

filterWhere(field, value) {
const left = { tag: 'ColumnExpression', field };
const op = 'OpEqual';
Expand All @@ -56,12 +153,30 @@ class QueryBuilder {
const left = { tag: 'ColumnExpression', field }
const op = 'OpNotEqual'
const right = { tag: 'LiteralExpression', value: jsValueToDynamicValue(value) }
const conditionExpression = { tag: 'InfixOperatorExpression', value: jsValueToDynamicValue(value) }
const conditionExpression = { tag: 'InfixOperatorExpression', left, op, right }
this.#addWhereCondition('OpAnd', conditionExpression)

return this;
}

or(conditionBuilder) {
if (this.query.whereCondition === null) {
throw new Error('You are attempting to add a condition using `or`, but there are no previous conditions to apply the `or` to. You probably want `where` instead.')
}
const conditionExpression = conditionBuilder.build();
this.#addWhereCondition('OpOr', conditionExpression)
return this
}

and(conditionBuilder) {
if (this.query.whereCondition === null) {
throw new Error('You are attempting to add a condition using `and`, but there are no previous conditions to apply the `and` to. You probably want `where` instead.')
}
const conditionExpression = conditionBuilder.build();
this.#addWhereCondition('OpAnd', conditionExpression)
return this
}

orderBy(column) {
return this.orderByAsc(column);
}
Expand Down Expand Up @@ -135,4 +250,4 @@ function jsValueToDynamicValue(value) {
throw new Error('Could no transform JS value to DynamicValue. Supported types: string, number, boolean, null, undefined');
}

export { QueryBuilder, query, ihpBackendUrl, fetchAuthenticated };
export { QueryBuilder, query, ihpBackendUrl, fetchAuthenticated, filterWhere, where, filterWhereNot, whereNot };

0 comments on commit f90ebb2

Please sign in to comment.