qb
qb copied to clipboard
.when() interferes with combinator (8.4)
See these two similar query scopes on a Quick entity:
function scopeActiveSeasons( qb, includeRegistration = true ) {
var adjTime = dateAdd( 'h', application.timeOffset, now() );
return qb
.forClient()
.where( function( sQ1 ) {
sQ1.whereSeasonID( application.currentSeasonID )
.orWhere( "seasonID", "=", application.tournamentSeasonID )
.orWhere( function( sQ3 ) {
sQ3.where( "registrationStart", "<", adjTime )
.andWhere( "registrationEnd", ">", adjTime )
.andWhere( "disabled", false );
} );
} );
}
This one works - it produces:
SELECT (a bunch of fields)
FROM [seasons]
WHERE [seasons].[clientID] = ?
AND ([seasons].[seasonID] = ? OR [seasons].[seasonID] = ?
**OR** ([seasons].[registrationStart] < ? AND [seasons].[registrationEnd] > ? AND [seasons].[disabled] = ?))
If I put that sq3
inside a .when()
, that highlighted OR becomes an AND:
function scopeActiveSeasons( qb, includeRegistration = true ) {
var adjTime = dateAdd( 'h', application.timeOffset, now() );
return qb
.forClient()
.where( function( sQ1 ) {
sQ1.whereSeasonID( application.currentSeasonID )
.orWhere( "seasonID", "=", application.tournamentSeasonID )
.when( ( includeRegistration ), function( sQ2 ) {
sQ2.orWhere( function( sQ3 ) {
sQ3.where( "registrationStart", "<", adjTime )
.andWhere( "registrationEnd", ">", adjTime )
.andWhere( "disabled", false );
} );
} );
} );
}
Even though the syntax called is sq2.orWhere()
, the SQL produced is:
SELECT (a bunch of fields)
FROM [seasons]
WHERE [seasons].[clientID] = ?
AND (([seasons].[seasonID] = ? OR [seasons].[seasonID] = ?)
**AND** (([seasons].[registrationStart] < ? AND [seasons].[registrationEnd] > ? AND [seasons].[disabled] = ?)))