Skip to content

Commit

Permalink
feat: add EqualsAny as an operand type
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
otaviojacobi committed Jan 21, 2025
1 parent 31da05d commit 2fd6cfa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "",
"license": "BSD",
"dependencies": {
"@balena/abstract-sql-compiler": "^10.0.1",
"@balena/odata-parser": "^3.1.0",
"@balena/abstract-sql-compiler": "10.2.0-build-compile-any-node-as-operand-77c25f0bfe74e1d1b8c5c46dddb5c14658c7144c-1",
"@balena/odata-parser": "4.0.0-build-compile-in-as-eq-any-116c561f994ba6fe2d0f50d7ea599ac8ac04ddad-1",
"@types/lodash": "^4.17.10",
"@types/memoizee": "^0.4.11",
"@types/string-hash": "^1.1.3",
Expand Down
23 changes: 22 additions & 1 deletion src/odata-to-abstract-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import type {
IsDistinctFromNode,
UnknownTypeNodes,
FromTypeNode,
EqualsAnyNode,
} from '@balena/abstract-sql-compiler';
import type {
ODataBinds,
Expand Down Expand Up @@ -136,6 +137,7 @@ export type ResourceFunction = (

const comparison = {
eq: 'IsNotDistinctFrom',
eqany: 'EqualsAny',
ne: 'IsDistinctFrom',
gt: 'GreaterThan',
ge: 'GreaterThanOrEqual',
Expand Down Expand Up @@ -1142,6 +1144,7 @@ export class OData2AbstractSQL {
const [type, ...rest] = match;
switch (type) {
case 'eq':
case 'eqany':
case 'ne':
case 'gt':
case 'ge':
Expand All @@ -1155,7 +1158,8 @@ export class OData2AbstractSQL {
| GreaterThanNode
| GreaterThanOrEqualNode
| LessThanNode
| LessThanOrEqualNode;
| LessThanOrEqualNode
| EqualsAnyNode;
}
case 'and':
case 'or':
Expand Down Expand Up @@ -1256,6 +1260,7 @@ export class OData2AbstractSQL {
this.DateMatch,
this.DurationMatch,
this.Math,
this.EqualsAny,
]) {
const result = matcher.call<
OData2AbstractSQL,
Expand All @@ -1275,6 +1280,22 @@ export class OData2AbstractSQL {
}
throw new SyntaxError('Could not match operand');
}
EqualsAny(match: any): EqualsAnyNode | undefined {
if (match[0] !== 'eqany') {
return;
}

if (match[1][0] !== 'Bind') {
throw new SyntaxError(
'Equals any must have a bind as the second argument',
);
}
return [
'EqualsAny',
this.Operand(match[0]),
this.Operand(match[1]) as BindNode,
];
}
Math(
match: any,
): AddNode | SubtractNode | MultiplyNode | DivideNode | undefined {
Expand Down
5 changes: 2 additions & 3 deletions test/filterby.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const sqlOps = {
mul: 'Multiply',
div: 'Divide',
in: 'In',
eqany: 'EqualsAny',
};

const methodMaps = {
Expand All @@ -73,9 +74,7 @@ const createExpression = function (lhs, op, rhs) {
if (op === 'in') {
return {
odata: operandToOData(lhs) + ' ' + op + ' ' + operandToOData(rhs),
abstractsql: [sqlOps[op], operandToAbstractSQL(lhs)].concat(
operandToAbstractSQL(rhs),
),
abstractsql: [sqlOps['eqany'], operandToAbstractSQL(lhs), ['Bind', 0]],
};
}
if (rhs == null) {
Expand Down

0 comments on commit 2fd6cfa

Please sign in to comment.