LogoLogo
11.1.0
11.1.0
  • Introduction
  • What's New?
  • Installation & Usage
  • Migration Guide
  • Contributing & Filing Issues
  • Query Builder
    • Getting a New Query
    • Building Queries
      • Selects
      • From
      • Joins
      • Wheres
      • Order By
      • Group By and Having
      • Limit, Offset, and Pagination
      • Locks
      • Unions
      • Common Table Expressions (i.e. CTEs)
      • Raw Expressions
      • When / Conditionals
      • Query Parameters and Bindings
    • Executing Queries
      • Retrieving Results
      • Aggregates
      • Inserts, Updates, and Deletes
    • Options and Utilities
      • Query Options and Utilities
      • Clone and Reset
      • Return Format
      • Column Formatter
      • Interception Points
    • Debugging
      • sqlCommenter
  • Schema Builder
    • Overview
    • Create
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Alter
    • Drop
    • Debugging
  • External Links
    • API Docs
    • Source Code
    • Issue Tracker
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. Query Builder
  2. Building Queries

When / Conditionals

If you store the builder object in a variable, you can use if and else statements like you would expect.

QueryBuilder
var q = query.from( "posts" );
if ( someFlag ) {
    q.orderBy( "published_date", "desc" );
}

This works, but breaks chainability. To keep chainability you can use the when helper method.

when

Name

Type

Required

Default

Description

condition

boolean

true

The condition to switch on.

onTrue

Function

true

The callback to execute if the condition is true. It is passed the builder object as the only parameter.

onFalse

Function

false

function( q ) { return q; }

The callback to execute if the conditions is false. It is passed the builder object as the only parameter.

withoutScoping

boolean

false

false

Flag to turn off the automatic scoping of where clauses during the callback.

The when helper is used to allow conditional statements when defining queries without using if statements and having to store temporary variables.

QueryBuilder
query.from( "posts" )
    .when( someFlag, function( q ) {
        q.orderBy( "published_date", "desc" );
    } )
    .get();

You can pass a third argument to be called in the else case.

QueryBuilder
query.from( "posts" )
    .when(
        someFlag,
        function( q ) {
            q.orderBy( "published_date", "desc" );
        },
        function( q ) {
            q.orderBy( "modified_date", "desc" );
        }
    );

when callbacks are automatically scoped and grouped. That means that if a where clause is added inside the callback with an OR combinator the clauses will automatically be grouped (have parenthesis put around them.) You can disable this feature by passing withoutScoping = true to the when callback.

qb.from( "users" )
    .where( "active", 1 )
    .when( len( url.q ), function( q ) {
        q.where( "username", "LIKE", q & "%" )
            .orWhere( "email", "LIKE", q & "%" );   
    } );
SELECT *
FROM "users"
WHERE "active" = ?
    AND (
        "username" = ?
        OR "email" = ?
    )
PreviousRaw ExpressionsNextQuery Parameters and Bindings

Was this helpful?