Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added in predicate. #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/queryCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ export abstract class QueryCompiler extends QueryVisitor<CompilationContext> {
return context;
}

protected visitInPredicate: CompileMethod<ast.InPredicate> = (context, node) => {
context.queryString =
"("
+ this.visitNode(context, node.predicate).queryString
+ (node.negate ? " NOT IN " : " IN ")
+ "("
+ this.visitNode(context, node.target).queryString
Copy link
Contributor

@charlesprakash charlesprakash Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this cover the IN predicate with an array as well? Like SELECT * FROM a WHERE a.category IN ('Cat1', 'Cat2', 'Cat3')? Please add a test case if so.

+ ")"
+ ")";

return context;
}

protected visitColumnName: CompileMethod<ast.ColumnName> = (context, node) => {
context.queryString = node.table ? node.table.toString() + "." + node.name.toString() : node.name.toString();
return context;
Expand Down
21 changes: 21 additions & 0 deletions test/queryCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});