Skip to content

Commit

Permalink
feat(Criteria): automatically fall back to host alias for complex que…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
RWOverdijk committed May 29, 2017
1 parent 1a283eb commit 3e32ebc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
23 changes: 19 additions & 4 deletions src/Criteria/Criteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export class Criteria {
*/
private hostMapping: Mapping<any>;

/**
* Alias for the host entity.
*
* @type {string}
*/
private hostAlias: string;

/**
* Mappings for entities (joins).
*
Expand All @@ -87,13 +94,15 @@ export class Criteria {
* @constructor
*
* @param {QueryBuilder} statement
* @param {Mapping} hostMapping
* @param {{}} [mappings]
* @param {Mapping} hostMapping
* @param {{}} [mappings]
* @param {string} [hostAlias]
*/
public constructor(statement: QueryBuilder, hostMapping: Mapping<any>, mappings?: {[key: string]: Mapping<any>}) {
public constructor(statement: QueryBuilder, hostMapping: Mapping<any>, mappings?: {[key: string]: Mapping<any>}, hostAlias?: string) {
this.statement = statement;
this.mappings = mappings || {};
this.hostMapping = hostMapping;
this.hostAlias = hostAlias;
}

/**
Expand Down Expand Up @@ -236,6 +245,12 @@ export class Criteria {
return parts.join('.');
}

return this.hostMapping.getFieldName(property, property);
let columnName = this.hostMapping.getFieldName(property, property);

if (this.hostAlias && this.hostMapping.getField(property, true)) {
columnName = this.hostAlias + '.' + columnName;
}

return columnName;
}
}
6 changes: 3 additions & 3 deletions src/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ export class QueryBuilder<T> {
this.alias = alias;
this.mappings = {[alias]: mapping};
this.statement = statement;
this.whereCriteria = new Where(this.statement, mapping, this.mappings);
this.havingCriteria = new Having(this.statement, mapping, this.mappings);
this.onCriteria = new On(this.statement, mapping, this.mappings);
this.whereCriteria = new Where(this.statement, mapping, this.mappings, alias);
this.havingCriteria = new Having(this.statement, mapping, this.mappings, alias);
this.onCriteria = new On(this.statement, mapping, this.mappings, alias);
this.entityManager = entityManager;
this.hydrator = new Hydrator(entityManager);
this.query = new Query(statement, this.hydrator, this.children);
Expand Down

0 comments on commit 3e32ebc

Please sign in to comment.