mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixes #479
This commit is contained in:
parent
3a512c4181
commit
3ae7ebecb2
@ -985,8 +985,10 @@ export class QueryBuilder<Entity> {
|
||||
condition = mainAliasName + "." + metadata.primaryColumns[0].propertyName + " IN (:ids)";
|
||||
}
|
||||
}
|
||||
const [queryWithIdsSql, queryWithIdsParameters] = this.clone({queryRunnerProvider: this.queryRunnerProvider})
|
||||
.andWhere(condition, parameters)
|
||||
const clonnedQb = this.clone({queryRunnerProvider: this.queryRunnerProvider});
|
||||
clonnedQb.expressionMap.extraAppendedAndWhereCondition = condition;
|
||||
const [queryWithIdsSql, queryWithIdsParameters] = clonnedQb
|
||||
.setParameters(parameters)
|
||||
.getSqlWithParameters();
|
||||
rawResults = await queryRunner.query(queryWithIdsSql, queryWithIdsParameters);
|
||||
const rawRelationIdResults = await relationIdLoader.load(rawResults);
|
||||
@ -1443,10 +1445,15 @@ export class QueryBuilder<Entity> {
|
||||
if (this.expressionMap.mainAlias!.hasMetadata) {
|
||||
const mainMetadata = this.expressionMap.mainAlias!.metadata;
|
||||
if (mainMetadata.discriminatorColumn)
|
||||
return ` WHERE ${ conditions.length ? "(" + conditions + ") AND" : "" } ${mainMetadata.discriminatorColumn.databaseName}=:discriminatorColumnValue`;
|
||||
return ` WHERE ${ conditions.length ? "(" + conditions + ") AND" : "" } ${this.escapeColumn(mainMetadata.discriminatorColumn.databaseName)}=:discriminatorColumnValue`;
|
||||
}
|
||||
|
||||
if (!conditions.length) return "";
|
||||
if (!conditions.length)
|
||||
return "";
|
||||
|
||||
if (this.expressionMap.extraAppendedAndWhereCondition)
|
||||
return " WHERE (" + conditions + ") AND " + this.replacePropertyNames(this.expressionMap.extraAppendedAndWhereCondition);
|
||||
|
||||
return " WHERE " + conditions;
|
||||
}
|
||||
|
||||
|
||||
@ -131,6 +131,12 @@ export class QueryExpressionMap {
|
||||
*/
|
||||
enableRelationIdValues: boolean = false;
|
||||
|
||||
/**
|
||||
* Extra where condition appended to the end of original where conditions with AND keyword.
|
||||
* Original condition will be wrapped into brackets.
|
||||
*/
|
||||
extraAppendedAndWhereCondition: string = "";
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
12
test/github-issues/479/entity/Car.ts
Normal file
12
test/github-issues/479/entity/Car.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Entity } from "../../../../src/decorator/entity/Entity";
|
||||
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import { Column } from "../../../../src/decorator/columns/Column";
|
||||
|
||||
@Entity()
|
||||
export class Car {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
}
|
||||
49
test/github-issues/479/issue-479.ts
Normal file
49
test/github-issues/479/issue-479.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import "reflect-metadata";
|
||||
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
|
||||
import {Connection} from "../../../src/connection/Connection";
|
||||
import {expect} from "chai";
|
||||
import {Car} from "./entity/Car";
|
||||
|
||||
describe("github issues > #479 orWhere breaks skip / take", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("where expression of the skip/take should not break original where query", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
const car1 = new Car();
|
||||
car1.name = "Test1";
|
||||
const car2 = new Car();
|
||||
car2.name = "Test2";
|
||||
const car3 = new Car();
|
||||
car3.name = "Test3";
|
||||
const car4 = new Car();
|
||||
car4.name = "BMW";
|
||||
const car5 = new Car();
|
||||
car5.name = "Mercedes";
|
||||
const car6 = new Car();
|
||||
car6.name = "Porshe";
|
||||
|
||||
await connection
|
||||
.getRepository(Car)
|
||||
.save([car1, car2, car3, car4, car5, car6]);
|
||||
|
||||
const cars = await connection
|
||||
.getRepository(Car)
|
||||
.createQueryBuilder("car")
|
||||
.where("car.name LIKE :filter1", { filter1: "Test%" })
|
||||
.orWhere("car.name LIKE :filter2", { filter2: "BM%" })
|
||||
.skip(0)
|
||||
.take(1)
|
||||
.getMany();
|
||||
|
||||
expect(cars.length).to.be.equal(1);
|
||||
})));
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user