Skip to content

Commit

Permalink
Fixed whereIn
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Nov 10, 2022
1 parent 29fe460 commit 7a84c25
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/IHP/DataSync/ihp-querybuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class ConditionBuildable {
}

whereIn(field, values) {
return {
const expression = {
tag: 'InfixOperatorExpression',
left: {
tag: 'ColumnExpression',
Expand All @@ -223,9 +223,11 @@ class ConditionBuildable {
op: 'OpIn',
right: {
tag: 'ListExpression',
value: values.map(jsValueToDynamicValue),
values: values.map(jsValueToDynamicValue),
},
}
};
this._addCondition('OpAnd', expression);
return this;
}
}

Expand Down Expand Up @@ -396,6 +398,9 @@ function jsValueToDynamicValue(value) {
}

export function recordMatchesQuery(query, record) {
function evaluateDynamicValue(value) {
return (value.tag === 'Null' ? null : value.contents);
}
function evaluate(expression) {
switch (expression.tag) {
case 'ColumnExpression': return (expression.field in record) ? record[expression.field] : null;
Expand All @@ -411,10 +416,16 @@ export function recordMatchesQuery(query, record) {
case 'OpOr': return evaluate(expression.left) || evaluate(expression.right);
case 'OpIs': return evaluate(expression.left) == evaluate(expression.right);
case 'OpIsNot': return evaluate(expression.left) != evaluate(expression.right);
case 'OpIn': {
const left = evaluate(expression.left);
const right = evaluate(expression.right);
return Array.isArray(right) && right.includes(left);
}
default: throw new Error('Unsupported operator ' + expression.op);
}
}
case 'LiteralExpression': return (expression.value.tag === 'Null' ? null : expression.value.contents);
case 'LiteralExpression': return evaluateDynamicValue(expression);
case 'ListExpression': return expression.values.map(value => evaluateDynamicValue(value));
default: throw new Error('Unsupported expression in evaluate: ' + expression.tag);
}
}
Expand Down

0 comments on commit 7a84c25

Please sign in to comment.