11import { ProjectionCompiled } from "../projection-compiled" ;
22import { UnionType } from "../../core/union-type" ;
33import { ProjectionBuilder } from "../projection-builder" ;
4- import { ExpressionOrColumn , ParamType , Utils } from "../../core/utils" ;
4+ import { ExpressionOrColumn , ParamType , Utils , TypeOrderBy } from "../../core/utils" ;
55import { WhereBuilder } from "../where-builder" ;
66import { OrderBy } from "../../core/enums/order-by" ;
77import { WhereCompiled } from "../where-compiled" ;
@@ -22,6 +22,11 @@ import { DatabaseBuilderError } from "../../core";
2222import { QueryBuilder } from "./query-builder" ;
2323import { ModelUtils } from "../../core/model-utils" ;
2424import { ParamFilter } from "../../core/param-filter" ;
25+ import { PlanRef } from "../../core/plan-ref" ;
26+ import { ProjectionModel } from "../projection-model" ;
27+ import { ProjectionCompile } from "../projection-compile" ;
28+ import { Replaceable } from "../../core/replaceable" ;
29+ import { QueryHelper } from "../../core/query-helper" ;
2530
2631export abstract class QueryBuilderBase < T ,
2732 TQuery extends QueryBuilderBase < T , TQuery > >
@@ -44,10 +49,11 @@ export abstract class QueryBuilderBase<T,
4449 params : [ ]
4550 } as WhereCompiled ;
4651
47- protected _projectionCompiled : ProjectionCompiled = {
48- projection : "" ,
49- params : [ ]
50- } as ProjectionCompiled ;
52+ protected _projections : ProjectionModel [ ] = [ ] ;
53+ // protected _projectionCompiled: ProjectionCompiled = {
54+ // projection: "",
55+ // params: []
56+ // } as ProjectionCompiled;
5157 // TODO: remove "_joinParams" e utilizar SqlAndParams como é realizado nos projections
5258 protected _joinParams : ParamType [ ] = [ ] ;
5359 protected _ignoreQueryFilter : boolean = false ;
@@ -193,7 +199,8 @@ export abstract class QueryBuilderBase<T,
193199 ) : TQuery {
194200 const instanceProjection : ProjectionBuilder < T > = this . createProjectionBuilder ( ) ;
195201 projectionCallback ( instanceProjection ) ;
196- this . compileProjection ( instanceProjection . compile ( ) ) ;
202+ this . buildProjections ( instanceProjection . result ( ) ) ;
203+ // this.compileProjection(instanceProjection.compile());
197204 return this . _getInstance ( ) ;
198205 }
199206
@@ -204,21 +211,33 @@ export abstract class QueryBuilderBase<T,
204211 }
205212
206213 public orderBy < TReturn > (
207- expression : ExpressionOrColumn < TReturn , T > ,
214+ expression : TypeOrderBy < TReturn , T > ,
208215 order : OrderBy = OrderBy . ASC
209216 ) : TQuery {
210- this . compileOrderBy ( `${ Utils . addAlias ( Utils . getColumn ( expression ) , this . _alias ) } ${ order } ` ) ;
217+ let columnName ;
218+ if ( Utils . isQueryCompiled ( expression ) ) {
219+ return this . orderBy ( `(${ QueryHelper . compileWithoutParams ( expression as QueryCompiled ) } )` , order ) ;
220+ } else if ( Utils . isQueryCompiledArray ( expression ) ) {
221+ return this . orderBy ( ( expression as QueryCompiled [ ] ) [ 0 ] , order ) ;
222+ } else if ( Utils . isPlanRef ( expression ) ) {
223+ columnName = ( expression as PlanRef ) . result ( ) ;
224+ } else if ( Utils . isNumber ( expression ) ) {
225+ columnName = `${ ( expression as number ) } ` ;
226+ } else {
227+ columnName = Utils . addAlias ( Utils . getColumn ( expression as ExpressionOrColumn < TReturn , T > ) , this . _alias ) ;
228+ }
229+ this . compileOrderBy ( `${ columnName } ${ order } ` ) ;
211230 return this . _getInstance ( ) ;
212231 }
213232
214233 public asc < TReturn > (
215- expression : ExpressionOrColumn < TReturn , T >
234+ expression : TypeOrderBy < TReturn , T >
216235 ) : TQuery {
217236 return this . orderBy ( expression , OrderBy . ASC ) ;
218237 }
219238
220239 public desc < TReturn > (
221- expression : ExpressionOrColumn < TReturn , T >
240+ expression : TypeOrderBy < TReturn , T >
222241 ) : TQuery {
223242 return this . orderBy ( expression , OrderBy . DESC ) ;
224243 }
@@ -246,6 +265,20 @@ export abstract class QueryBuilderBase<T,
246265 return this . _getInstance ( ) ;
247266 }
248267
268+ /**
269+ * Find projection by alias and result index (base 1...N+1)
270+ * @param projectionAlias alias to find the projection
271+ * @returns index (base 1...N+1)
272+ */
273+ public getIndexProjection < TReturn > ( projectionAlias : ExpressionOrColumn < TReturn , T > ) : number {
274+ const projectionColumnAlias = Utils . getColumn ( projectionAlias ) ;
275+ const index = this . _projections . findIndex ( x => x . projection . endsWith ( ` AS ${ projectionColumnAlias } ` ) ) ;
276+ if ( index > - 1 ) {
277+ return index + 1 ;
278+ }
279+ throw new DatabaseBuilderError ( `Not found projection alias ("${ projectionColumnAlias } " in projections (current value: "${ ProjectionCompile . compile ( this . _projections ) . projection } "))` ) ;
280+ }
281+
249282 public ignoreQueryFilters ( ) : TQuery {
250283 if ( this . _joinsQuery . length ) {
251284 throw new DatabaseBuilderError ( `Can't apply 'ignoreQueryFilters' after joining 'join()'` ) ;
@@ -336,7 +369,8 @@ export abstract class QueryBuilderBase<T,
336369 this . _joinsQuery . push ( joinQuery ) ;
337370 this . _joinParams = this . _joinParams . concat ( joinQuery . _getParams ( ) ) ;
338371 this . compileWhere ( this . whereCompiled , joinQuery . _getWhere ( ) ) ;
339- this . compileProjection ( joinQuery . _getSelect ( ) ) ;
372+ this . buildProjections ( joinQuery . _getProjections ( ) ) ;
373+ // this.compileProjection(joinQuery._getSelect());
340374 this . compileGroupBy ( joinQuery . _getGroupBy ( ) ) ;
341375 this . compileHaving ( joinQuery . _getHaving ( ) ) ;
342376 this . compileOrderBy ( joinQuery . _getOrderBy ( ) ) ;
@@ -347,10 +381,14 @@ export abstract class QueryBuilderBase<T,
347381 }
348382
349383 protected getColumnsCompiled ( ) : ProjectionCompiled {
350- if ( ! this . _projectionCompiled . projection . length ) {
384+ if ( this . _projections . length === 0 ) {
351385 this . setDefaultColumns ( ) ;
352386 }
353- return this . _projectionCompiled ;
387+ return ProjectionCompile . compile ( this . _projections ) ;
388+ // if (!this._projectionCompiled.projection.length) {
389+ // this.setDefaultColumns();
390+ // }
391+ // return this._projectionCompiled;
354392 }
355393
356394 protected buildBase ( ) : QueryCompiled {
@@ -404,15 +442,20 @@ export abstract class QueryBuilderBase<T,
404442 return queryBase ;
405443 }
406444
407- private compileProjection ( compiled : ProjectionCompiled ) {
408- if ( compiled . projection . length ) {
409- this . _projectionCompiled . projection +=
410- `${ ( this . _projectionCompiled . projection . length ? ", " : "" ) } ${ compiled . projection } ` ;
411- compiled . params . forEach ( ( value : any ) => {
412- this . _projectionCompiled . params . push ( value ) ;
413- } ) ;
445+ private buildProjections ( projections : ProjectionModel [ ] ) {
446+ if ( projections . length > 0 ) {
447+ this . _projections = [ ...this . _projections , ...projections ] ;
414448 }
415449 }
450+ // private compileProjection(compiled: ProjectionCompiled) {
451+ // if (compiled.projection.length) {
452+ // this._projectionCompiled.projection +=
453+ // `${(this._projectionCompiled.projection.length ? ", " : "")}${compiled.projection}`;
454+ // compiled.params.forEach((value: any) => {
455+ // this._projectionCompiled.params.push(value);
456+ // });
457+ // }
458+ // }
416459
417460 private compileTableJoins < TJoin , TQueryJoin extends JoinQueryBuilderContract < TJoin , TQueryJoin > > (
418461 tablesBase : QueryCompiled ,
0 commit comments