1
1
import { ProjectionCompiled } from "../projection-compiled" ;
2
2
import { UnionType } from "../../core/union-type" ;
3
3
import { ProjectionBuilder } from "../projection-builder" ;
4
- import { ExpressionOrColumn , ParamType , Utils } from "../../core/utils" ;
4
+ import { ExpressionOrColumn , ParamType , Utils , TypeOrderBy } from "../../core/utils" ;
5
5
import { WhereBuilder } from "../where-builder" ;
6
6
import { OrderBy } from "../../core/enums/order-by" ;
7
7
import { WhereCompiled } from "../where-compiled" ;
@@ -22,6 +22,11 @@ import { DatabaseBuilderError } from "../../core";
22
22
import { QueryBuilder } from "./query-builder" ;
23
23
import { ModelUtils } from "../../core/model-utils" ;
24
24
import { 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" ;
25
30
26
31
export abstract class QueryBuilderBase < T ,
27
32
TQuery extends QueryBuilderBase < T , TQuery > >
@@ -44,10 +49,11 @@ export abstract class QueryBuilderBase<T,
44
49
params : [ ]
45
50
} as WhereCompiled ;
46
51
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;
51
57
// TODO: remove "_joinParams" e utilizar SqlAndParams como é realizado nos projections
52
58
protected _joinParams : ParamType [ ] = [ ] ;
53
59
protected _ignoreQueryFilter : boolean = false ;
@@ -193,7 +199,8 @@ export abstract class QueryBuilderBase<T,
193
199
) : TQuery {
194
200
const instanceProjection : ProjectionBuilder < T > = this . createProjectionBuilder ( ) ;
195
201
projectionCallback ( instanceProjection ) ;
196
- this . compileProjection ( instanceProjection . compile ( ) ) ;
202
+ this . buildProjections ( instanceProjection . result ( ) ) ;
203
+ // this.compileProjection(instanceProjection.compile());
197
204
return this . _getInstance ( ) ;
198
205
}
199
206
@@ -204,21 +211,33 @@ export abstract class QueryBuilderBase<T,
204
211
}
205
212
206
213
public orderBy < TReturn > (
207
- expression : ExpressionOrColumn < TReturn , T > ,
214
+ expression : TypeOrderBy < TReturn , T > ,
208
215
order : OrderBy = OrderBy . ASC
209
216
) : 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 } ` ) ;
211
230
return this . _getInstance ( ) ;
212
231
}
213
232
214
233
public asc < TReturn > (
215
- expression : ExpressionOrColumn < TReturn , T >
234
+ expression : TypeOrderBy < TReturn , T >
216
235
) : TQuery {
217
236
return this . orderBy ( expression , OrderBy . ASC ) ;
218
237
}
219
238
220
239
public desc < TReturn > (
221
- expression : ExpressionOrColumn < TReturn , T >
240
+ expression : TypeOrderBy < TReturn , T >
222
241
) : TQuery {
223
242
return this . orderBy ( expression , OrderBy . DESC ) ;
224
243
}
@@ -246,6 +265,20 @@ export abstract class QueryBuilderBase<T,
246
265
return this . _getInstance ( ) ;
247
266
}
248
267
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
+
249
282
public ignoreQueryFilters ( ) : TQuery {
250
283
if ( this . _joinsQuery . length ) {
251
284
throw new DatabaseBuilderError ( `Can't apply 'ignoreQueryFilters' after joining 'join()'` ) ;
@@ -336,7 +369,8 @@ export abstract class QueryBuilderBase<T,
336
369
this . _joinsQuery . push ( joinQuery ) ;
337
370
this . _joinParams = this . _joinParams . concat ( joinQuery . _getParams ( ) ) ;
338
371
this . compileWhere ( this . whereCompiled , joinQuery . _getWhere ( ) ) ;
339
- this . compileProjection ( joinQuery . _getSelect ( ) ) ;
372
+ this . buildProjections ( joinQuery . _getProjections ( ) ) ;
373
+ // this.compileProjection(joinQuery._getSelect());
340
374
this . compileGroupBy ( joinQuery . _getGroupBy ( ) ) ;
341
375
this . compileHaving ( joinQuery . _getHaving ( ) ) ;
342
376
this . compileOrderBy ( joinQuery . _getOrderBy ( ) ) ;
@@ -347,10 +381,14 @@ export abstract class QueryBuilderBase<T,
347
381
}
348
382
349
383
protected getColumnsCompiled ( ) : ProjectionCompiled {
350
- if ( ! this . _projectionCompiled . projection . length ) {
384
+ if ( this . _projections . length === 0 ) {
351
385
this . setDefaultColumns ( ) ;
352
386
}
353
- return this . _projectionCompiled ;
387
+ return ProjectionCompile . compile ( this . _projections ) ;
388
+ // if (!this._projectionCompiled.projection.length) {
389
+ // this.setDefaultColumns();
390
+ // }
391
+ // return this._projectionCompiled;
354
392
}
355
393
356
394
protected buildBase ( ) : QueryCompiled {
@@ -404,15 +442,20 @@ export abstract class QueryBuilderBase<T,
404
442
return queryBase ;
405
443
}
406
444
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 ] ;
414
448
}
415
449
}
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
+ // }
416
459
417
460
private compileTableJoins < TJoin , TQueryJoin extends JoinQueryBuilderContract < TJoin , TQueryJoin > > (
418
461
tablesBase : QueryCompiled ,
0 commit comments