Skip to content

Commit

Permalink
fix: support empty IN clause across all dialects (#6887)
Browse files Browse the repository at this point in the history
we were supporting an empty `IN` clause for MySQL and Oracle
and this updates the handling to be for all other dialects as well
by making the "empty" clause be `0=1`

Closes #4865
fixes #2195
  • Loading branch information
imnotjames committed Oct 15, 2020
1 parent 0f0e0b6 commit 9635080
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {ColumnMetadata} from "../metadata/ColumnMetadata";
import {SqljsDriver} from "../driver/sqljs/SqljsDriver";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {OracleDriver} from "../driver/oracle/OracleDriver";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
import {EntitySchema} from "../";
import {FindOperator} from "../find-options/FindOperator";
import {In} from "../find-options/operator/In";
Expand Down Expand Up @@ -939,8 +938,8 @@ export abstract class QueryBuilder<Entity> {
case "between":
return `${aliasPath} BETWEEN ${parameters[0]} AND ${parameters[1]}`;
case "in":
if ((this.connection.driver instanceof OracleDriver || this.connection.driver instanceof MysqlDriver) && parameters.length === 0) {
return `${aliasPath} IN (null)`;
if (parameters.length === 0) {
return "0=1";
}
return `${aliasPath} IN (${parameters.join(", ")})`;
case "any":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ describe("repository > find options > operators", () => {
});
loadedPosts.should.be.eql([{ id: 2, likes: 3, title: "About #2" }]);

const noPosts = await connection.getRepository(Post).find({
title: In([])
});
noPosts.length.should.be.eql(0);
})));

it("not(in)", () => Promise.all(connections.map(async connection => {
Expand All @@ -453,6 +457,10 @@ describe("repository > find options > operators", () => {
});
loadedPosts.should.be.eql([{ id: 2, likes: 3, title: "About #2" }]);

const noPosts = await connection.getRepository(Post).find({
title: Not(In([]))
});
noPosts.length.should.be.eql(2);
})));

it("any", () => Promise.all(connections.map(async connection => {
Expand Down

0 comments on commit 9635080

Please sign in to comment.