refactoring query builder

This commit is contained in:
Umed Khudoiberdiev 2017-06-23 18:08:19 +05:00
parent 67e50063c2
commit 2e98bf89cc
10 changed files with 561 additions and 489 deletions

View File

@ -54,6 +54,7 @@ of `QueryRunner`, e.g. `queryRunner.connection` and `queryRunner.manager`
* now `update` method in `QueryBuilder` accepts `Partial<Entity>` and property names used in update map are
column property names and they are automatically mapped to column names
* `SpecificRepository` has been removed. Instead new `RelationQueryBuilder` was introduced.
* `getEntitiesAndRawResults` of `QueryBuilder` has been renamed to `getRawAndEntities`
### DEPRECATIONS

View File

@ -952,8 +952,8 @@ let photos = await photoRepository
.where("photo.isPublished=true")
.andWhere("(photo.name=:photoName OR photo.name=:bearName)")
.orderBy("photo.id", "DESC")
.setFirstResult(5)
.setMaxResults(10)
.skip(5)
.take(10)
.setParameters({ photoName: "My", bearName: "Mishka" })
.getMany();
```

View File

@ -1041,8 +1041,8 @@ let photos = await photoRepository
.where("photo.isPublished=true")
.andWhere("(photo.name=:photoName OR photo.name=:bearName)")
.orderBy("photo.id", "DESC")
.setFirstResult(5)
.setMaxResults(10)
.skip(5)
.take(10)
.setParameters({ photoName: "My", bearName: "Mishka" })
.getMany();
```

View File

@ -521,7 +521,7 @@ export class EntityManager {
}
return id;
});
qb.andWhereInIds(ids);
qb.whereInIds(ids);
return qb.getMany();
}
@ -573,7 +573,7 @@ export class EntityManager {
if (!metadata.hasMultiplePrimaryKeys && !(id instanceof Object)) {
id = metadata.createEntityIdMap([id]);
}
qb.andWhereInIds([id]);
qb.whereInIds([id]);
return qb.getOne();
}

View File

@ -270,8 +270,8 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
entities = await this.connection
.getRepository<ObjectLiteral>(subjectGroup.target)
.createQueryBuilder("operateSubject", this.queryRunner)
.andWhereInIds(allIds)
.enableAutoRelationIdsLoad()
.whereInIds(allIds)
.loadAllRelationIds()
.getMany();
}
@ -391,7 +391,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
const qb = this.connection
.getRepository<ObjectLiteral>(valueMetadata.target)
.createQueryBuilder(qbAlias, this.queryRunner) // todo: this wont work for mongodb. implement this in some method and call it here instead?
.enableAutoRelationIdsLoad();
.loadAllRelationIds();
const condition = relation.joinColumns.map(joinColumn => {
return `${qbAlias}.${joinColumn.referencedColumn!.propertyPath} = :${joinColumn.databaseName}`;
@ -481,7 +481,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
.createQueryBuilder(qbAlias, this.queryRunner) // todo: this wont work for mongodb. implement this in some method and call it here instead?
.where(qbAlias + "." + relation.inverseSidePropertyPath + "=:id") // TODO relation.inverseRelation.joinColumns
.setParameter("id", relationIdInDatabaseEntity) // (example) subject.entity is a details here, and the value is details.id
.enableAutoRelationIdsLoad()
.loadAllRelationIds()
.getOne();
// add only if database entity exist - because in the case of inverse side of the one-to-one relation
@ -581,7 +581,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
.createQueryBuilder(qbAlias, this.queryRunner) // todo: this wont work for mongodb. implement this in some method and call it here instead?
.innerJoin(relation.junctionEntityMetadata!.tableName, joinAlias, conditions)
.setParameters(parameters)
.enableAutoRelationIdsLoad()
.loadAllRelationIds()
.getMany();
} else if (relation.isManyToManyNotOwner) {
@ -612,7 +612,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
.createQueryBuilder(qbAlias, this.queryRunner) // todo: this wont work for mongodb. implement this in some method and call it here instead?
.innerJoin(relation.junctionEntityMetadata!.tableName, joinAlias, conditions)
.setParameters(parameters)
.enableAutoRelationIdsLoad()
.loadAllRelationIds()
.getMany();
} else { // this case can only be a oneToMany relation
@ -629,7 +629,7 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
.createQueryBuilder(qbAlias, this.queryRunner) // todo: this wont work for mongodb. implement this in some method and call it here instead?
.where(qbAlias + "." + relation.inverseSidePropertyPath + "=:id")
.setParameter("id", relationIdInDatabaseEntity)
.enableAutoRelationIdsLoad()
.loadAllRelationIds()
.getMany();
}
@ -668,8 +668,8 @@ export class SubjectBuilder<Entity extends ObjectLiteral> {
const databaseEntity = await this.connection
.getRepository<ObjectLiteral>(valueMetadata.target)
.createQueryBuilder(qbAlias, this.queryRunner) // todo: this wont work for mongodb. implement this in some method and call it here instead?
.andWhereInIds([id])
.enableAutoRelationIdsLoad()
.whereInIds([id])
.loadAllRelationIds()
.getOne();
if (databaseEntity) {

View File

@ -63,6 +63,33 @@ export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> {
return this;
}
/**
* Adds new AND WHERE with conditions for the given ids.
*/
whereInIds(ids: any[]): this {
const [whereExpression, parameters] = this.createWhereIdsExpression(ids);
this.andWhere(whereExpression, parameters);
return this;
}
/**
* Adds new AND WHERE with conditions for the given ids.
*/
andWhereInIds(ids: any[]): this {
const [whereExpression, parameters] = this.createWhereIdsExpression(ids);
this.andWhere(whereExpression, parameters);
return this;
}
/**
* Adds new OR WHERE with conditions for the given ids.
*/
orWhereInIds(ids: any[]): this {
const [whereExpression, parameters] = this.createWhereIdsExpression(ids);
this.orWhere(whereExpression, parameters);
return this;
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------

View File

@ -1,15 +1,7 @@
import {EntityMetadata} from "../metadata/EntityMetadata";
import {ObjectLiteral} from "../common/ObjectLiteral";
import {QueryRunner} from "../query-runner/QueryRunner";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {Connection} from "../connection/Connection";
import {PostgresDriver} from "../driver/postgres/PostgresDriver";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
import {LockNotSupportedOnGivenDriverError} from "./error/LockNotSupportedOnGivenDriverError";
import {ColumnMetadata} from "../metadata/ColumnMetadata";
import {QueryExpressionMap} from "./QueryExpressionMap";
import {SelectQuery} from "./SelectQuery";
import {OracleDriver} from "../driver/oracle/OracleDriver";
import {SelectQueryBuilder} from "./SelectQueryBuilder";
import {UpdateQueryBuilder} from "./UpdateQueryBuilder";
import {DeleteQueryBuilder} from "./DeleteQueryBuilder";

File diff suppressed because it is too large Load Diff

View File

@ -62,6 +62,33 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> {
return this;
}
/**
* Adds new AND WHERE with conditions for the given ids.
*/
whereInIds(ids: any[]): this {
const [whereExpression, parameters] = this.createWhereIdsExpression(ids);
this.andWhere(whereExpression, parameters);
return this;
}
/**
* Adds new AND WHERE with conditions for the given ids.
*/
andWhereInIds(ids: any[]): this {
const [whereExpression, parameters] = this.createWhereIdsExpression(ids);
this.andWhere(whereExpression, parameters);
return this;
}
/**
* Adds new OR WHERE with conditions for the given ids.
*/
orWhereInIds(ids: any[]): this {
const [whereExpression, parameters] = this.createWhereIdsExpression(ids);
this.orWhere(whereExpression, parameters);
return this;
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
@ -72,6 +99,7 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> {
protected createUpdateExpression() {
const valuesSet = this.getValueSets();
// prepare columns and values to be updated
const updateColumnAndValues: string[] = [];
Object.keys(valuesSet).forEach(columnProperty => {
const column = this.expressionMap.mainAlias!.metadata.findColumnWithPropertyName(columnProperty);

View File

@ -70,9 +70,9 @@ export class TreeRepository<Entity> extends Repository<Entity> {
// todo: throw exception if there is no column of this relation?
return this
.createDescendantsQueryBuilder("treeEntity", "treeClosure", entity)
.getEntitiesAndRawResults()
.getRawAndEntities()
.then(entitiesAndScalars => {
const relationMaps = this.createRelationMaps("treeEntity", entitiesAndScalars.rawResults);
const relationMaps = this.createRelationMaps("treeEntity", entitiesAndScalars.raw);
this.buildChildrenEntityTree(entity, entitiesAndScalars.entities, relationMaps);
return entity;
});
@ -118,9 +118,9 @@ export class TreeRepository<Entity> extends Repository<Entity> {
// todo: throw exception if there is no column of this relation?
return this
.createAncestorsQueryBuilder("treeEntity", "treeClosure", entity)
.getEntitiesAndRawResults()
.getRawAndEntities()
.then(entitiesAndScalars => {
const relationMaps = this.createRelationMaps("treeEntity", entitiesAndScalars.rawResults);
const relationMaps = this.createRelationMaps("treeEntity", entitiesAndScalars.raw);
this.buildParentEntityTree(entity, entitiesAndScalars.entities, relationMaps);
return entity;
});