LogoLogo
6.4.0
6.4.0
  • Introduction
  • What's New?
  • Installation & Usage
  • Migration Guide
  • Contributing & Filing Issues
  • Contributors
  • Query Builder
    • Return Format
    • Interception Points
    • Retrieving Results
    • Aggregates
    • Selects
    • From
    • Joins
    • Where Clauses
    • Conditionals
    • Ordering, Grouping & Limit
    • Unions
    • Common Table Expressions (i.e. CTEs)
    • Inserts
    • Updates
    • Deletes
    • New Query
    • Clone
  • Schema Builder
    • Overview
    • Create
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Alter
    • Drop
Powered by GitBook
On this page
  • Retrieving All Rows From A Table
  • Retrieving A Single Row / Column From A Table
  • Options parameter: Retrieving results from alternative datasources

Was this helpful?

Edit on Git
Export as PDF
  1. Query Builder

Retrieving Results

Usage## Retrieving Results

Retrieving All Rows From A Table

// qb
var getAllResults = query.from( "users" ).get();
writeDump( getAllResults );

// sql
// select * from users

// more qb examples
var getAllResults = query.from( "users" )
    .get(
        columns = [ "Id", "Name" ],
        options = { datasource = "myAdditionalDatasource" }
    );

The get method returns an Array of Structs by default. Both columns and options are optional.

public any function get( any columns, struct options = {} )

Retrieving A Single Row / Column From A Table

If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single record (a Struct by default). If no row is found an empty Struct will be returned by default.

//qb
var getResults = query.from('users')
    .first();
writeDump(getResults);

//sql
select * from users limit(1)
public any function first( struct options = {} )

If you don't even need an entire row, you may extract a single value from each record using the values method. This method will return the value of the column directly:

query.from( "users" ).values( "email" );
// [ "john@example.com", "jane@example.com", "jim@example.com", ... ]
public any function values( required string column, struct options = {} );

If you only need a single column for the first record returned, use the value function:

query.from( "users" ).value( "email" );
// "john@example.com"
public any function value( required string column, struct options = {} );

Options parameter: Retrieving results from alternative datasources

In application.cfc you can specify your default datasource which will be used by qb. If you want to retrieve data from other datasources you can specify this in all retrieval functions by using the extra options parameter such as:

//qb
var getAllResults = query.from('users')
    .get( options={ datasource= 'MyOtherDatasourceName'} );
writeDump(getAllResults);

If you also want to use a non-default SQL Grammar you have to specify this when creating your querybuilder, e.g

var query =  wirebox.getInstance('QueryBuilder@qb')
    .setGrammar( wirebox.getInstance('MSSQLGrammar@qb') );
PreviousInterception PointsNextAggregates

Last updated 5 years ago

Was this helpful?