@@ -460,22 +460,49 @@ class Sequelize {
460
460
461
461
query ( sql , options ) {
462
462
options = Object . assign ( { } , this . options . query , options ) ;
463
- const retryOptions = Object . assign ( { } , this . options . retry , options . retry || { } ) ;
464
463
465
- let bindParameters ;
464
+ if ( options . instance && ! options . model ) {
465
+ options . model = options . instance . constructor ;
466
+ }
466
467
467
- return Promise . resolve ( retry ( retryParameters => Promise . try ( ( ) => {
468
- const isFirstTry = retryParameters . current === 1 ;
468
+ if ( ! options . instance && ! options . model ) {
469
+ options . raw = true ;
470
+ }
469
471
470
- if ( options . instance && ! options . model ) {
471
- options . model = options . instance . constructor ;
472
- }
472
+ // map raw fields to model attributes
473
+ if ( options . mapToModel ) {
474
+ options . fieldMap = _ . get ( options , 'model.fieldAttributeMap' , { } ) ;
475
+ }
473
476
474
- // map raw fields to model attributes
475
- if ( options . mapToModel ) {
476
- options . fieldMap = _ . get ( options , 'model.fieldAttributeMap' , { } ) ;
477
+ options = _ . defaults ( options , {
478
+ logging : this . options . hasOwnProperty ( 'logging' ) ? this . options . logging : console . log ,
479
+ searchPath : this . options . hasOwnProperty ( 'searchPath' ) ? this . options . searchPath : 'DEFAULT'
480
+ } ) ;
481
+
482
+ if ( ! options . type ) {
483
+ if ( options . model || options . nest || options . plain ) {
484
+ options . type = QueryTypes . SELECT ;
485
+ } else {
486
+ options . type = QueryTypes . RAW ;
477
487
}
488
+ }
478
489
490
+ //if dialect doesn't support search_path or dialect option
491
+ //to prepend searchPath is not true delete the searchPath option
492
+ if (
493
+ ! this . dialect . supports . searchPath ||
494
+ ! this . options . dialectOptions ||
495
+ ! this . options . dialectOptions . prependSearchPath ||
496
+ options . supportsSearchPath === false
497
+ ) {
498
+ delete options . searchPath ;
499
+ } else if ( ! options . searchPath ) {
500
+ //if user wants to always prepend searchPath (dialectOptions.preprendSearchPath = true)
501
+ //then set to DEFAULT if none is provided
502
+ options . searchPath = 'DEFAULT' ;
503
+ }
504
+
505
+ return Promise . try ( ( ) => {
479
506
if ( typeof sql === 'object' ) {
480
507
if ( sql . values !== undefined ) {
481
508
if ( options . replacements !== undefined ) {
@@ -498,10 +525,6 @@ class Sequelize {
498
525
499
526
sql = sql . trim ( ) ;
500
527
501
- if ( ! options . instance && ! options . model ) {
502
- options . raw = true ;
503
- }
504
-
505
528
if ( options . replacements && options . bind ) {
506
529
throw new Error ( 'Both `replacements` and `bind` cannot be set at the same time' ) ;
507
530
}
@@ -514,71 +537,49 @@ class Sequelize {
514
537
}
515
538
}
516
539
540
+ let bindParameters ;
541
+
517
542
if ( options . bind ) {
518
- const bindSql = this . dialect . Query . formatBindParameters ( sql , options . bind , this . options . dialect ) ;
519
- sql = bindSql [ 0 ] ;
520
- bindParameters = bindSql [ 1 ] ;
543
+ [ sql , bindParameters ] = this . dialect . Query . formatBindParameters ( sql , options . bind , this . options . dialect ) ;
521
544
}
522
545
523
- options = _ . defaults ( options , {
524
- logging : this . options . hasOwnProperty ( 'logging' ) ? this . options . logging : console . log ,
525
- searchPath : this . options . hasOwnProperty ( 'searchPath' ) ? this . options . searchPath : 'DEFAULT'
526
- } ) ;
546
+ const retryOptions = Object . assign ( { } , this . options . retry , options . retry || { } ) ;
527
547
528
- if ( options . transaction === undefined && Sequelize . _cls ) {
529
- options . transaction = Sequelize . _cls . get ( 'transaction' ) ;
530
- }
548
+ return Promise . resolve ( retry ( retryParameters => Promise . try ( ( ) => {
549
+ const isFirstTry = retryParameters . current === 1 ;
531
550
532
- if ( ! options . type ) {
533
- if ( options . model || options . nest || options . plain ) {
534
- options . type = QueryTypes . SELECT ;
535
- } else {
536
- options . type = QueryTypes . RAW ;
551
+ if ( isFirstTry && this . test . _trackRunningQueries ) {
552
+ this . test . _runningQueries ++ ;
537
553
}
538
- }
539
-
540
- if ( options . transaction && options . transaction . finished ) {
541
- const error = new Error ( `${ options . transaction . finished } has been called on this transaction(${ options . transaction . id } ), you can no longer use it. (The rejected query is attached as the \'sql\' property of this error)` ) ;
542
- error . sql = sql ;
543
- return Promise . reject ( error ) ;
544
- }
545
-
546
- if ( isFirstTry && this . test . _trackRunningQueries ) {
547
- this . test . _runningQueries ++ ;
548
- }
549
-
550
- //if dialect doesn't support search_path or dialect option
551
- //to prepend searchPath is not true delete the searchPath option
552
- if (
553
- ! this . dialect . supports . searchPath ||
554
- ! this . options . dialectOptions ||
555
- ! this . options . dialectOptions . prependSearchPath ||
556
- options . supportsSearchPath === false
557
- ) {
558
- delete options . searchPath ;
559
- } else if ( ! options . searchPath ) {
560
- //if user wants to always prepend searchPath (dialectOptions.preprendSearchPath = true)
561
- //then set to DEFAULT if none is provided
562
- options . searchPath = 'DEFAULT' ;
563
- }
564
554
565
- return options . transaction
566
- ? options . transaction . connection
567
- : this . connectionManager . getConnection ( options ) ;
568
- } ) . then ( connection => {
569
- const query = new this . dialect . Query ( connection , this , options ) ;
555
+ if ( options . transaction === undefined && Sequelize . _cls ) {
556
+ options . transaction = Sequelize . _cls . get ( 'transaction' ) ;
557
+ }
570
558
571
- return query . run ( sql , bindParameters )
572
- . finally ( ( ) => {
573
- if ( this . test . _trackRunningQueries ) {
574
- this . test . _runningQueries -- ;
575
- }
559
+ if ( options . transaction && options . transaction . finished ) {
560
+ const error = new Error ( ` ${ options . transaction . finished } has been called on this transaction( ${ options . transaction . id } ), you can no longer use it. (The rejected query is attached as the \'sql\' property of this error)` ) ;
561
+ error . sql = sql ;
562
+ throw error ;
563
+ }
576
564
577
- if ( ! options . transaction ) {
578
- return this . connectionManager . releaseConnection ( connection ) ;
579
- }
580
- } ) ;
581
- } ) , retryOptions ) ) ;
565
+ return options . transaction
566
+ ? options . transaction . connection
567
+ : this . connectionManager . getConnection ( options ) ;
568
+ } ) . then ( connection => {
569
+ const query = new this . dialect . Query ( connection , this , options ) ;
570
+
571
+ return query . run ( sql , bindParameters )
572
+ . finally ( ( ) => {
573
+ if ( ! options . transaction ) {
574
+ return this . connectionManager . releaseConnection ( connection ) ;
575
+ }
576
+ } ) ;
577
+ } ) , retryOptions ) ) . finally ( ( ) => {
578
+ if ( this . test . _trackRunningQueries ) {
579
+ this . test . _runningQueries -- ;
580
+ }
581
+ } ) ;
582
+ } ) ;
582
583
}
583
584
584
585
/**
0 commit comments