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

fix: refactor QueryBuilder.createWhereExpression #6402

Merged
merged 1 commit into from Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = [];
Copy link
Member

Choose a reason for hiding this comment

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

type is missing btw


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);
})));
});