From 40f26675d93e36e6e19546c0c6706b3a1c2d2a00 Mon Sep 17 00:00:00 2001 From: "DESKTOP-2IEPJ3T\\RanadheerReddyP" Date: Thu, 7 Jun 2018 19:07:28 +0530 Subject: [PATCH 1/2] Added in predicate. --- lib/queryCompiler.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/queryCompiler.ts b/lib/queryCompiler.ts index becfa00..4393960 100644 --- a/lib/queryCompiler.ts +++ b/lib/queryCompiler.ts @@ -162,6 +162,19 @@ export abstract class QueryCompiler extends QueryVisitor { return context; } + protected visitInPredicate: CompileMethod = (context, node) => { + context.queryString = + "(" + + this.visitNode(context, node.predicate).queryString + + (node.negate ? " NOT IN " : " IN ") + + "(" + + this.visitNode(context, node.target).queryString + + ")" + + ")"; + + return context; + } + protected visitColumnName: CompileMethod = (context, node) => { context.queryString = node.table ? node.table.toString() + "." + node.name.toString() : node.name.toString(); return context; From eca1d7bcf4381b36fc55a6e1f74b2d7da3d478a6 Mon Sep 17 00:00:00 2001 From: "DESKTOP-2IEPJ3T\\RanadheerReddyP" Date: Thu, 7 Jun 2018 19:44:50 +0530 Subject: [PATCH 2/2] In predicate sub query test. --- test/queryCompiler.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/queryCompiler.test.ts b/test/queryCompiler.test.ts index 53f5504..6e911aa 100644 --- a/test/queryCompiler.test.ts +++ b/test/queryCompiler.test.ts @@ -79,4 +79,25 @@ describe("queryCompiler unit tests", () => { expect(actual).toEqual(expected); }); + + it("compiles with multiple expressions with qb.in", () => { + let subQuery = qb.query() + .from("accounts", "a") + .where(qb.equals(qb.column("a.id"), qb.literal(123))) + .select(qb.column("DISTINCT a.city")).toSelect(); + + let query = qb.query() + .from("accounts", "a") + .where(qb.or( + qb.equals(qb.column("a.id"), qb.literal(123)), + qb.in(qb.column("a.city"), subQuery), + qb.equals(qb.column("a.country"), qb.literal("USA")))) + .select(qb.column("a.name")).build(); + + let qc = new sql.MySQLQueryCompiler(query); + let actual = qc.compile(); + let expected = "SELECT a.name FROM accounts AS a WHERE (((a.id = 123) OR (a.city IN (SELECT DISTINCT a.city FROM accounts AS a WHERE (a.id = 123)))) OR (a.country = 'USA'))"; + + expect(actual).toEqual(expected); + }); });