mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
refactoring query builder
This commit is contained in:
parent
2e98bf89cc
commit
09bd77a235
@ -1225,12 +1225,6 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected rawResultsToEntities(results: any[], rawRelationIdResults: RelationIdLoadResult[], rawRelationCountResults: RelationCountLoadResult[]) {
|
||||
return new RawSqlResultsToEntityTransformer(this.connection.driver, this.expressionMap.joinAttributes, rawRelationIdResults, rawRelationCountResults)
|
||||
.transform(results, this.expressionMap.mainAlias!);
|
||||
}
|
||||
|
||||
protected async executeCountQuery(options: { release: boolean }): Promise<number> {
|
||||
|
||||
const mainAlias = this.expressionMap.mainAlias!.name; // todo: will this work with "fromTableName"?
|
||||
@ -1272,15 +1266,16 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> {
|
||||
* Executes sql generated by query builder and returns object with raw results and entities created from them.
|
||||
*/
|
||||
protected async executeEntitiesAndRawResults(options: { release: boolean }): Promise<{ entities: Entity[], raw: any[] }> {
|
||||
const broadcaster = new Broadcaster(this.connection);
|
||||
const relationIdLoader = new RelationIdLoader(this.connection, this.queryRunner, this.expressionMap.relationIdAttributes);
|
||||
const relationCountLoader = new RelationCountLoader(this.connection, this.queryRunner, this.expressionMap.relationCountAttributes);
|
||||
const relationIdMetadataTransformer = new RelationIdMetadataToAttributeTransformer(this.expressionMap);
|
||||
relationIdMetadataTransformer.transform();
|
||||
const relationCountMetadataTransformer = new RelationCountMetadataToAttributeTransformer(this.expressionMap);
|
||||
relationCountMetadataTransformer.transform();
|
||||
try { // we wrap everything into try/catch because in any case scenario we must release created connection
|
||||
|
||||
const broadcaster = new Broadcaster(this.connection);
|
||||
const relationIdLoader = new RelationIdLoader(this.connection, this.queryRunner, this.expressionMap.relationIdAttributes);
|
||||
const relationCountLoader = new RelationCountLoader(this.connection, this.queryRunner, this.expressionMap.relationCountAttributes);
|
||||
const relationIdMetadataTransformer = new RelationIdMetadataToAttributeTransformer(this.expressionMap);
|
||||
relationIdMetadataTransformer.transform();
|
||||
const relationCountMetadataTransformer = new RelationCountMetadataToAttributeTransformer(this.expressionMap);
|
||||
relationCountMetadataTransformer.transform();
|
||||
|
||||
try {
|
||||
if (!this.expressionMap.mainAlias)
|
||||
throw new Error(`Alias is not set. Looks like nothing is selected. Use select*, delete, update method to set an alias.`);
|
||||
|
||||
@ -1293,6 +1288,7 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> {
|
||||
throw new NoVersionOrUpdateDateColumnError(metadata.name);
|
||||
}
|
||||
|
||||
let rawResults: any[] = [], entities: any[] = [];
|
||||
const mainAliasName = this.expressionMap.mainAlias.name;
|
||||
if (this.expressionMap.skip || this.expressionMap.take) {
|
||||
// we are skipping order by here because its not working in subqueries anyway
|
||||
@ -1337,7 +1333,6 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> {
|
||||
idsQuery += " OFFSET " + this.expressionMap.skip;
|
||||
}
|
||||
|
||||
let entities: any[] = [];
|
||||
let rawResults: any[] = await this.queryRunner.query(idsQuery, parameters);
|
||||
if (rawResults.length > 0) {
|
||||
let condition = "";
|
||||
@ -1366,37 +1361,31 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> {
|
||||
.setParameters(parameters)
|
||||
.getSqlAndParameters();
|
||||
rawResults = await this.queryRunner.query(queryWithIdsSql, queryWithIdsParameters);
|
||||
const rawRelationIdResults = await relationIdLoader.load(rawResults);
|
||||
const rawRelationCountResults = await relationCountLoader.load(rawResults);
|
||||
entities = this.rawResultsToEntities(rawResults, rawRelationIdResults, rawRelationCountResults);
|
||||
if (this.expressionMap.mainAlias.hasMetadata) {
|
||||
await broadcaster.broadcastLoadEventsForAll(this.expressionMap.mainAlias.target, rawResults);
|
||||
}
|
||||
|
||||
}
|
||||
return {
|
||||
raw: rawResults,
|
||||
entities: entities,
|
||||
};
|
||||
|
||||
} else {
|
||||
|
||||
const [sql, parameters] = this.getSqlAndParameters();
|
||||
rawResults = await this.queryRunner.query(sql, parameters);
|
||||
}
|
||||
|
||||
const rawResults = await this.queryRunner.query(sql, parameters);
|
||||
if (rawResults.length > 0) {
|
||||
|
||||
// transform raw results into entities
|
||||
const rawRelationIdResults = await relationIdLoader.load(rawResults);
|
||||
const rawRelationCountResults = await relationCountLoader.load(rawResults);
|
||||
const entities = this.rawResultsToEntities(rawResults, rawRelationIdResults, rawRelationCountResults);
|
||||
const transformer = new RawSqlResultsToEntityTransformer(this.connection.driver, this.expressionMap.joinAttributes, rawRelationIdResults, rawRelationCountResults);
|
||||
entities = transformer.transform(rawResults, this.expressionMap.mainAlias!);
|
||||
|
||||
// broadcast all "after load" events
|
||||
if (this.expressionMap.mainAlias.hasMetadata)
|
||||
await broadcaster.broadcastLoadEventsForAll(this.expressionMap.mainAlias.target, rawResults);
|
||||
|
||||
return {
|
||||
raw: rawResults,
|
||||
entities: entities,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
raw: rawResults,
|
||||
entities: entities,
|
||||
};
|
||||
|
||||
} finally {
|
||||
if (options.release && this.ownQueryRunner) // means we created our own query runner
|
||||
await this.queryRunner.release();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user