Skip to content

Commit

Permalink
fix: Query builder makes query with joins, without limit for inherite…
Browse files Browse the repository at this point in the history
…d entities (#6402)

Closes: #6399

Co-authored-by: Gregory Komagurov <Komagurov.GV@gazprom-neft.ru>
  • Loading branch information
grkov90 and Gregory Komagurov committed Jul 17, 2020
1 parent afc62fd commit 874e573
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/query-builder/QueryBuilder.ts
Expand Up @@ -598,7 +598,10 @@ export abstract class QueryBuilder<Entity> {
* Creates "WHERE" expression.
*/
protected createWhereExpression() {
let conditions = this.createWhereExpressionString();
const conditionsArray = [];

const whereExpression = this.createWhereExpressionString();
whereExpression.trim() && conditionsArray.push(this.createWhereExpressionString());

if (this.expressionMap.mainAlias!.hasMetadata) {
const metadata = this.expressionMap.mainAlias!.metadata;
Expand All @@ -609,7 +612,7 @@ export abstract class QueryBuilder<Entity> {
: metadata.deleteDateColumn.propertyName;

const condition = `${this.replacePropertyNames(column)} IS NULL`;
conditions = `${ conditions.length ? "(" + conditions + ") AND" : "" } ${condition}`;
conditionsArray.push(condition);
}

if (metadata.discriminatorColumn && metadata.parentEntityMetadata) {
Expand All @@ -618,17 +621,22 @@ export abstract class QueryBuilder<Entity> {
: metadata.discriminatorColumn.databaseName;

const condition = `${this.replacePropertyNames(column)} IN (:...discriminatorColumnValues)`;
return ` WHERE ${ conditions.length ? "(" + conditions + ") AND" : "" } ${condition}`;
conditionsArray.push(condition);
}
}

if (!conditions.length) // TODO copy in to discriminator condition
return this.expressionMap.extraAppendedAndWhereCondition ? " WHERE " + this.replacePropertyNames(this.expressionMap.extraAppendedAndWhereCondition) : "";

if (this.expressionMap.extraAppendedAndWhereCondition)
return " WHERE (" + conditions + ") AND " + this.replacePropertyNames(this.expressionMap.extraAppendedAndWhereCondition);
if (this.expressionMap.extraAppendedAndWhereCondition) {
const condition = this.replacePropertyNames(this.expressionMap.extraAppendedAndWhereCondition);
conditionsArray.push(condition);
}

return " WHERE " + conditions;
if (!conditionsArray.length) {
return " ";
} else if (conditionsArray.length === 1) {
return ` WHERE ${conditionsArray[0]}`;
} else {
return ` WHERE ( ${conditionsArray.join(" ) AND ( ")} )`;
}
}

/**
Expand Down
52 changes: 52 additions & 0 deletions test/github-issues/6399/entities.ts
@@ -0,0 +1,52 @@
import {Entity, OneToMany, ManyToOne} from "../../../src";
import {Column} from "../../../src";
import {PrimaryGeneratedColumn} from "../../../src";
import { TableInheritance } from "../../../src";
import {ChildEntity} from "../../../src";
import {JoinColumn} from "../../../src";

@Entity()
export class Comment {
@PrimaryGeneratedColumn()
id: number;

@Column()
text: string;

@Column()
postId: number;

@ManyToOne(
() => Post,
(entity) => entity.comments,
)
@JoinColumn({
name: "postId",
})
post?: Post;

}

@Entity()
@TableInheritance({column: {type: "string", name: "postType"}})
export class Post {
@PrimaryGeneratedColumn()
id: number;

@Column()
title: string;

@Column()
postType: string = "BasePost";

@OneToMany(() => Comment, (entity) => entity.post)
comments?: Comment[];
}

@ChildEntity("TargetPost")
export class TargetPost extends Post {
@Column()
postType: string = "TargetPost";
}


49 changes: 49 additions & 0 deletions test/github-issues/6399/issue-6399.ts
@@ -0,0 +1,49 @@
import {Connection} from "../../../src";
import {expect} from "chai";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Post, TargetPost, Comment} from "./entities";

describe("github issues > #6399 Process extraAppendedAndWhereCondition for inherited entity", () => {
let connections: Connection[];

before(async () => {
return connections = await createTestingConnections({
entities: [Post, TargetPost, Comment],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["mysql"]
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("Query with join and limit for inhered entity", () => Promise.all(connections.map(async (connection) => {
const targetPostRepo = connection.getRepository(TargetPost);

const posts: TargetPost[] = [
{
id: 1,
title: "Post 1",
postType: "TargetPost",
},
{ id: 2,
title: "Post 2",
postType: "TargetPost",
},
{
id: 3,
title: "Post 3",
postType: "TargetPost",
},
];

await targetPostRepo.save(posts);

const result = await targetPostRepo.createQueryBuilder("targetPosts")
.leftJoinAndSelect("targetPosts.comments", "comments")
.take(2)
.getMany();

expect(result.length).eq(2);
})));
});

0 comments on commit 874e573

Please sign in to comment.