@@ -301,16 +301,18 @@ export abstract class WhereBaseBuilder<
301301
302302 public between (
303303 expression : TExpression ,
304- // startExpression: TExpression,
305- // endExpression: TExpression
306- value1 : ValueTypeToParse ,
307- value2 : ValueTypeToParse ,
304+ startExpression : TExpression ,
305+ endExpression : TExpression
306+ // value1: ValueTypeToParse,
307+ // value2: ValueTypeToParse,
308308 ) : TWhere {
309309 this . buildWhereColumn (
310310 [ Condition . Between ] ,
311311 this . getColumnParams ( expression ) ,
312- // [this.getColumnParams(startExpression), this.getColumnParams(endExpression)]);
313- [ value1 , value2 ] ) ;
312+ this . getColumnParams ( startExpression ) ,
313+ this . getColumnParams ( endExpression )
314+ ) ;
315+ // [startExpression, endExpression]);
314316 return this . _getInstance ( ) ;
315317 }
316318
@@ -393,10 +395,30 @@ export abstract class WhereBaseBuilder<
393395 this . buildWhereColumn ( condition , column1 , column2 ) ;
394396 }
395397
398+ protected buildWhereColumn (
399+ condition : Condition [ ] ,
400+ ...valuesOrColumns : ( ColumnParams | string | ValueTypeToParse [ ] ) [ ]
401+ // left: ColumnParams | string | ValueTypeToParse[],
402+ ) {
403+ const columnsParams = valuesOrColumns . map ( x => this . processParam ( x ) ) ;
404+ // const columnRight = this.processParam(right);
405+ // const columnLeft = this.processParam(left);
406+ this . buildWhereParams (
407+ condition ,
408+ columnsParams . map ( x => Utils . addAlias ( x . column , this . _alias ) ) ,
409+ columnsParams . map ( ( value ) => value . params )
410+ // alternative Array.flat()
411+ // https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/flat#Alternativa
412+ . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] ) ,
413+ // Utils.addAlias(columnRight.column, this._alias),
414+ // Utils.addAlias(columnLeft.column, this._alias),
415+ // columnRight.params.concat(columnLeft.params)
416+ ) ;
417+ }
396418 // protected buildWhereColumn(
397419 // condition: Condition[],
398- // ...values: ( ColumnParams | string | ValueTypeToParse[]) [],
399- // // left: ColumnParams | string | ValueTypeToParse[],
420+ // right: ColumnParams | string | ValueTypeToParse[],
421+ // left: ColumnParams | string | ValueTypeToParse[],
400422 // ) {
401423 // const columnRight = this.processParam(right);
402424 // const columnLeft = this.processParam(left);
@@ -407,41 +429,34 @@ export abstract class WhereBaseBuilder<
407429 // columnRight.params.concat(columnLeft.params)
408430 // );
409431 // }
410- protected buildWhereColumn (
411- condition : Condition [ ] ,
412- right : ColumnParams | string | ValueTypeToParse [ ] ,
413- left : ColumnParams | string | ValueTypeToParse [ ] ,
414- ) {
415- const columnRight = this . processParam ( right ) ;
416- const columnLeft = this . processParam ( left ) ;
417- this . buildWhereParams (
418- condition ,
419- Utils . addAlias ( columnRight . column , this . _alias ) ,
420- Utils . addAlias ( columnLeft . column , this . _alias ) ,
421- columnRight . params . concat ( columnLeft . params )
422- ) ;
423- }
424432
425433 protected buildWhereParams (
426434 condition : Condition [ ] ,
427- column1 : string ,
428- column2 : string ,
429- params : ValueTypeToParse [ ]
435+ columns : string [ ] ,
436+ // column1: string,
437+ // column2: string,
438+ params : ValueTypeToParse [ ] ,
430439 ) {
431- this . buildWhere ( condition ,
432- column1 ,
433- column2
440+ this . buildWhere (
441+ condition ,
442+ columns
434443 ) ;
444+ // this.buildWhere(condition,
445+ // column1,
446+ // column2
447+ // );
435448 this . addParam ( params ) ;
436449 }
437450
438451 protected buildWhere (
439452 conditions : Condition [ ] ,
440- column1 : string ,
441- column2 : string | string [ ] ,
453+ columns : string [ ] ,
454+ // column1: string,
455+ // column2: string | string[],
442456 ) {
443457 this . checkWhere ( ) ;
444- this . _where += this . createWhere ( conditions , column1 , column2 ) ;
458+ this . _where += this . createWhere ( conditions , columns ) ;
459+ // this._where += this.createWhere(conditions, column1, column2);
445460 }
446461
447462 protected addParam (
@@ -492,7 +507,8 @@ export abstract class WhereBaseBuilder<
492507 this . addParam ( metadata . right ) ;
493508 metadata . right = "?" ;
494509 }
495- this . buildWhere ( metadata . condition , Utils . addAlias ( metadata . left , this . _alias ) , Utils . addAlias ( metadata . right , this . _alias ) ) ;
510+ this . buildWhere ( metadata . condition , [ Utils . addAlias ( metadata . left , this . _alias ) , Utils . addAlias ( metadata . right , this . _alias ) ] ) ;
511+ // this.buildWhere(metadata.condition, Utils.addAlias(metadata.left, this._alias), Utils.addAlias(metadata.right, this._alias));
496512 }
497513
498514 private addValueParam (
@@ -505,20 +521,29 @@ export abstract class WhereBaseBuilder<
505521
506522 private createWhere (
507523 conditions : Condition [ ] ,
508- column1 : string ,
509- column2 : string | string [ ] ,
524+ columns : string [ ] ,
525+ // column1: string,
526+ // column2: string | string[],
510527 ) {
511528 // TODO: verificar se colunas não são condition, para remover a condition
512529 let conditionsArray = this . _pendingConditions . concat ( conditions ) ;
513530 this . _pendingConditions = [ ] ;
514- const isConditionIsNullInColumn2 = column2 === Condition . IsNull ;
515- if ( isConditionIsNullInColumn2 ) {
516- conditionsArray = this . conditionIsNull ( conditionsArray ) ;
531+ if ( columns . length == 2 ) {
532+ const isConditionIsNullInColumn2 = columns [ 1 ] === Condition . IsNull ;
533+ if ( isConditionIsNullInColumn2 ) {
534+ conditionsArray = this . conditionIsNull ( conditionsArray ) ;
535+ columns . pop ( ) ;
536+ }
517537 }
518538 return this . buildConditions (
519539 conditionsArray ,
520- column1 ,
521- isConditionIsNullInColumn2 ? void 0 : column2 ) ;
540+ columns
541+ ) ;
542+ // return this.buildConditions(
543+ // conditionsArray,
544+ // column1,
545+ // isConditionIsNullInColumn2 ? void 0 : column2
546+ // );
522547 }
523548
524549 private conditionIsNull ( currentConditions : Condition [ ] ) : Condition [ ] {
@@ -538,44 +563,69 @@ export abstract class WhereBaseBuilder<
538563
539564 private buildConditions (
540565 conditions : Condition [ ] ,
541- column1 : string ,
542- column2 : string | string [ ] ,
566+ columns : string [ ] ,
567+ // column1: string,
568+ // column2: string | string[],
543569 ) : string {
544570 // new scope
545571 if ( ! conditions || ( conditions . length === 1 && conditions [ 0 ] === void 0 ) ) {
546- return `(${ column1 } )` ;
572+ return `(${ columns [ 0 ] } )` ;
573+ // return `(${column1})`;
547574 }
548575 switch ( conditions . toString ( ) ) {
549576 case [ Condition . Between ] . toString ( ) :
550577 case [ Condition . Not , Condition . Between ] . toString ( ) :
551- // ${column} BETWEEN ? AND ?
552- if ( ! Utils . isArray ( column2 ) || column2 . length === 2 ) {
553- console . log ( column2 ) ;
554- return `${ column1 } ${ this . builderConditions ( conditions ) } ? ${ WhereBaseBuilder . AND } ?` ;
555- // return `${column1} ${this.builderConditions(conditions)} ${column2[0]} ${WhereBaseBuilder.AND} ${column2[1]}`;
578+ // ${column} BETWEEN ${columnOrParam} AND ${columnOrParam}
579+ if ( columns . length === 3 ) {
580+ return `${ columns [ 0 ] } ${ this . builderConditions ( conditions ) } ${ columns [ 1 ] } ${ WhereBaseBuilder . AND } ${ columns [ 2 ] } ` ;
556581 }
557- throw new DatabaseBuilderError ( `Length (${ column2 . length } ) (values: ${ column2 } ) parameter to '${ conditions } ' condition incorrect!` ) ;
582+ throw new DatabaseBuilderError ( `Length (${ columns . length } ) (values: ${ columns } ) parameter to '${ conditions } ' condition incorrect!` ) ;
583+ // // ${column} BETWEEN ? AND ?
584+ // if (!Utils.isArray(column2) || column2.length === 2) {
585+ // return `${column1} ${this.builderConditions(conditions)} ? ${WhereBaseBuilder.AND} ?`;
586+ // }
587+ // throw new DatabaseBuilderError(`Length (${column2.length}) (values: ${column2}) parameter to '${conditions}' condition incorrect!`);
558588 case [ Condition . In ] . toString ( ) :
559589 case [ Condition . Not , Condition . In ] . toString ( ) :
560590 // ${column} IN (?, ?, ...)
561- return `${ column1 } ${ this . builderConditions ( conditions ) } (${ column2 } )` . trim ( ) ;
591+ return `${ columns [ 0 ] } ${ this . builderConditions ( conditions ) } (${ columns [ 1 ] } )` . trim ( ) ;
562592 case [ Condition . Not , Condition . IsNull ] . toString ( ) :
563- return `${ column1 } IS NOT NULL` . trim ( ) ;
593+ return `${ columns [ 0 ] } IS NOT NULL` . trim ( ) ;
564594 case [ Condition . Not , Condition . Equal ] . toString ( ) :
565- return `${ column1 } <> ${ column2 } ` . trim ( ) ;
595+ return `${ columns [ 0 ] } <> ${ columns [ 1 ] } ` . trim ( ) ;
566596 case [ Condition . Not , Condition . Great ] . toString ( ) :
567- return this . buildConditions ( [ Condition . LessAndEqual ] , column1 , column2 ) ;
597+ return this . buildConditions ( [ Condition . LessAndEqual ] , columns ) ;
568598 case [ Condition . Not , Condition . GreatAndEqual ] . toString ( ) :
569- return this . buildConditions ( [ Condition . Less ] , column1 , column2 ) ;
599+ return this . buildConditions ( [ Condition . Less ] , columns ) ;
570600 case [ Condition . Not , Condition . Less ] . toString ( ) :
571- return this . buildConditions ( [ Condition . GreatAndEqual ] , column1 , column2 ) ;
601+ return this . buildConditions ( [ Condition . GreatAndEqual ] , columns ) ;
572602 case [ Condition . Not , Condition . LessAndEqual ] . toString ( ) :
573- return this . buildConditions ( [ Condition . Great ] , column1 , column2 ) ;
603+ return this . buildConditions ( [ Condition . Great ] , columns ) ;
574604 default :
575- if ( column2 ) {
576- return `${ column1 } ${ this . builderConditions ( conditions ) } ${ column2 } ` . trim ( ) ;
605+ if ( columns [ 1 ] ) {
606+ return `${ columns [ 0 ] } ${ this . builderConditions ( conditions ) } ${ columns [ 1 ] } ` . trim ( ) ;
577607 }
578- return `${ column1 } ${ this . builderConditions ( conditions ) } ` . trim ( ) ;
608+ return `${ columns [ 0 ] } ${ this . builderConditions ( conditions ) } ` . trim ( ) ;
609+ // case [Condition.Not, Condition.In].toString():
610+ // // ${column} IN (?, ?, ...)
611+ // return `${column1} ${this.builderConditions(conditions)} (${column2})`.trim();
612+ // case [Condition.Not, Condition.IsNull].toString():
613+ // return `${column1} IS NOT NULL`.trim();
614+ // case [Condition.Not, Condition.Equal].toString():
615+ // return `${column1} <> ${column2}`.trim();
616+ // case [Condition.Not, Condition.Great].toString():
617+ // return this.buildConditions([Condition.LessAndEqual], column1, column2);
618+ // case [Condition.Not, Condition.GreatAndEqual].toString():
619+ // return this.buildConditions([Condition.Less], column1, column2);
620+ // case [Condition.Not, Condition.Less].toString():
621+ // return this.buildConditions([Condition.GreatAndEqual], column1, column2);
622+ // case [Condition.Not, Condition.LessAndEqual].toString():
623+ // return this.buildConditions([Condition.Great], column1, column2);
624+ // default:
625+ // if (column2) {
626+ // return `${column1} ${this.builderConditions(conditions)} ${column2}`.trim();
627+ // }
628+ // return `${column1} ${this.builderConditions(conditions)}`.trim();
579629 }
580630 }
581631
0 commit comments