Skip to content

Commit 9c029c4

Browse files
committed
Add WHERE IN support for constants
1 parent a607bec commit 9c029c4

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/modules/relexp.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,30 @@ function convertExpr(
254254
return expr.value;
255255
}
256256

257+
case 'InExpressionListPredicate':
258+
if (expr.right.type !== 'ExpressionList') {
259+
// Currently IN expressions are only supported with lists of values
260+
throw new Error('Query not supported');
261+
}
262+
263+
let orIn: Array<any> = [];
264+
for (const inSetElem of expr.right.value) {
265+
const inExpr = {
266+
type: 'ComparisonBooleanPrimary',
267+
left: expr.left,
268+
operator: '=',
269+
right: inSetElem,
270+
};
271+
addToExpr(orIn, inExpr, types, tables);
272+
}
273+
const inOrExpr = {or: {clauses: orIn}};
274+
275+
if (expr.hasNot === 'NOT') {
276+
return {not: {clause: inOrExpr}};
277+
} else {
278+
return inOrExpr;
279+
}
280+
257281
default:
258282
console.log(expr);
259283
// Produce an error if the expression is unsupported

src/modules/relexp.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,3 +766,53 @@ it('should remove quotes from string literals', () => {
766766
},
767767
});
768768
});
769+
770+
/** @test {relexp} */
771+
it('should convert conditions with IN', () => {
772+
const sql = parser.parse('SELECT * FROM foo WHERE id IN (1, 2)');
773+
const action = exprFromSql(sql.value, {foo: ['id']});
774+
expect(reducer({}, action)).toMatchObject({
775+
expr: {
776+
selection: {
777+
arguments: {
778+
select: {
779+
or: {
780+
clauses: [
781+
{cmp: {lhs: 'id', op: '$eq', rhs: '1'}},
782+
{cmp: {lhs: 'id', op: '$eq', rhs: '2'}},
783+
],
784+
},
785+
},
786+
},
787+
children: [{relation: 'foo'}],
788+
},
789+
},
790+
});
791+
});
792+
793+
/** @test {relexp} */
794+
it('should convert conditions with IN', () => {
795+
const sql = parser.parse('SELECT * FROM foo WHERE id NOT IN (1, 2)');
796+
const action = exprFromSql(sql.value, {foo: ['id']});
797+
expect(reducer({}, action)).toMatchObject({
798+
expr: {
799+
selection: {
800+
arguments: {
801+
select: {
802+
not: {
803+
clause: {
804+
or: {
805+
clauses: [
806+
{cmp: {lhs: 'id', op: '$eq', rhs: '1'}},
807+
{cmp: {lhs: 'id', op: '$eq', rhs: '2'}},
808+
],
809+
},
810+
},
811+
},
812+
},
813+
},
814+
children: [{relation: 'foo'}],
815+
},
816+
},
817+
});
818+
});

0 commit comments

Comments
 (0)