Permalink
Browse files

feat(Criteria): automatically fall back to host alias for complex que…

…ries
  • Loading branch information...
RWOverdijk committed May 29, 2017
1 parent 1a283eb commit 3e32ebc467b47e6b8922701be4e6ff4d6250ca40
Showing with 22 additions and 7 deletions.
  1. +19 −4 src/Criteria/Criteria.ts
  2. +3 −3 src/QueryBuilder.ts
@@ -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).
*
@@ -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;
}
/**
@@ -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;
}
}
@@ -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);

0 comments on commit 3e32ebc

Please sign in to comment.