The following methods all have the same return value:
insert
, update
, and delete
actions always return a query object for query
, regardless of your configured returnFormat
.
Name | Type | Required | Default | Description |
---|---|---|---|---|
You can insert a single record by passing a struct:
You can specify any query param options such as the SQL type by passing a struct with the parameters you would pass to cfqueryparam
.
Raw values can be supplied to an insert statement.
Multiple rows can be inserted in a batch by passing an array of structs to insert
.
This is not the same as looping over and array and calling insert
in the loop. Using an array with insert
will batch the inserts in one SQL call. Looping over an array and calling insert
each time will create a SQL request for each item in the array. Bottom line, pass your array to insert
!
Inserts data into a table while ignoring duplicate key conflicts.
target
is only required for SQLServerGrammar
and OracleGrammar
Inserts data into a table using a subquery as the source.
You can also pass in an array of column names to avoid aliasing in your source query.
Alternatively, the source can be defined as a QueryBuilder object:
Updates a table with a struct of column and value pairs.
You can specify any query param options such as the SQL type by passing a struct with the parameters you would pass to cfqueryparam
.
Any constraining of the update query should be done using the appropriate WHERE statement before calling update
.
You can update a column based on another column using a raw expression.
Null values can be inserted by using queryparam syntax:
if you are using full null support the following (easier) syntax is also allowed:
Subselects can be used to update values by passing a closure as the value
You can also pass a builder instance in place of the closure.
qb will correctly format JOIN
clauses in your UPDATE
statements for your database grammar.
OracleGrammar
does not support JOIN
clauses inUPDATE
statements. Consider using subselects in your UPDATE
statement instead.
Adds values to a later update
, similar to addSelect
.
Performs an update statement if the configured query returns true
for exists
. Otherwise, performs an insert statement.
If an update statement is performed qb applies a limit( 1 )
to the update statement.
If the configured query returns 0 records, then an insert statement is performed.
An upsert is a batch operation that either inserts or updates a row depending on if a target match is found. If a row is matched with the target column(s), then the matched row is updated. Otherwise a new row is inserted.
In most database grammars, the target columns are required to be primary key or unique indexes.
The update clause in a upsert can also accept raw values, making it very useful for tracking data like statistics.
A source callback or QueryBuilder instance can be used instead of explicit values. This allows you to do upserts across tables or subqueries.
To do this, provide a source
that is either a function to configure a new QueryBuilder instance or an already configured QueryBuilder instance. Then specify the columns that will be affected as an array of strings to values
.
Deletes all records that the query returns.
The id
argument is a convenience to delete a single record by id.
returning
is only supported in PostgresGrammar,
SqlServerGrammar, and SQLiteGrammar.
Using this method on unsupported grammars will result in an UnsupportedOperation
exception. Be aware that using this method constrains your grammar choices.
Specifies columns to be returned from the insert query.
The returning
function also applies to update
and delete
calls.
You can also use raw
Expressions in a returning
call. This is especially useful for SQL Server returning both the old and new values from an update
call.
Name | Type | Required | Default | Description |
---|---|---|---|---|
Name | Type | Required | Default | Description |
---|---|---|---|---|
values
struct | array<struct>
true
A struct or array of structs to insert in to the table.
options
struct
false
{}
Any additional queryExecute
options.
toSQL
boolean
false
false
If true
, returns the raw SQL string instead of running the query. Useful for debugging.
values
struct | array<struct>
true
A struct or array of structs to insert in to the table.
target
array<string>
false
[]
An array of key column names to match on. (SQL Server and Oracle grammars only.)
options
struct
false
{}
Any additional queryExecute
options.
toSQL
boolean
false
false
If true
, returns the raw SQL string instead of running the query. Useful for debugging.
source
function | QueryBuilder
true
A callback or builder instance to serve as the source of the insert.
columns
array<string>
false
An array of column names that will be inserted. If no columns are passed, the columns will be derived from the source columns and aliases.
options
struct
false
{}
Any additional queryExecute
options.
toSQL
boolean
false
false
If true
, returns the raw SQL string instead of running the query. Useful for debugging.
Name
Type
Required
Default
Description
values
struct
false
{}
A struct of column and value pairs to update. These column and value pairs are appended to any already set with the addUpdate
method.
options
struct
false
{}
Any additional queryExecute
options.
toSQL
boolean
false
false
If true
, returns the raw SQL string instead of running the query. Useful for debugging.
Name
Type
Required
Default
Description
values
struct
true
A struct of column and value pairs to add to the update clause.
Name
Type
Required
Default
Description
values
struct
true
A struct of column and value pairs to either update or insert.
options
boolean
false
{}
Any additional queryExecute
options.
toSql
boolean
false
false
If true
, returns the raw SQL string instead of running the query. Useful for debugging.
Name
Type
Required
Default
Description
values
struct | array<struct> | array<string>
true
A struct or array of structs to insert into or update on the table. If a source
is provided, this should be an array of column names to update instead.
target
string | array<string>
true
A column name or array of column names to match the values to the table. If a match is found, the record will be updated. Otherwise, a new record will be inserted. Most database grammars required these columns to have either a primary key or a unique index.
update
array | struct
false
null
Either an array of columns to update using the current value
matched or a struct containing the column names as keys and the corresponding to update. If blank, it will update all the columns in the passed in value
.
source
function | QueryBuilder
false
null
A callback function or QueryBuilder object to use as the source for the upsert. When using this parameter, values
must be an array of column names to update.
deleteUmatched
boolean
false
false
Boolean flag to delete any unmatched source records as part the upsert. (SQL Server only.)
options
boolean
false
{}
Any additional queryExecute
options.
toSql
boolean
false
false
If true
, returns the raw SQL string instead of running the query. Useful for debugging.
Name
Type
Required
Default
Description
id
any
false
A convenience argument for `where( "id", "=", arguments.id ). The query can be constrained by normal WHERE methods as well.
idColumn
string
false
"id"
The name of the id column for the delete shorthand.
options
boolean
false
{}
Any additional queryExecute
options.
toSql
boolean
false
false
If true
, returns the raw SQL string instead of running the query. Useful for debugging.
Name
Type
Required
Default
Description
columns
string | array
true
A single column, a list or columns, or an array of columns to return from the inserted query.
The query builder also provides a variety of aggregate methods such as count
, max
, min
, and sum
. These methods take the headache out of setting up these common aggregate functions.
When executing any of the aggregate functions, any where
restrictions on your query will still be applied.
Instead of returning a query, these methods return a simple value.
Returns true
if the query returns any rows. Returns false
otherwise.
Returns an integer number of rows returned by the query.
Returns the maximum value for the given column.
Returns the minimum value for the given column.
Returns the sum of all returned rows for the given column.
Returns the sum of all returned rows for the expression.
Retrieves the columns for the configured table.
The get
method is the most common method used for retrieving results. It executes using the configured QueryBuilder
and returns the results.
get
can also take a list or array of columns to use as a shortcut. If any are passed, those columns will be used instead of any columns previously set on the QueryBuilder
.
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.
throws: EntityNotFound
If you don't even need an entire row, you may extract a single value from each record using the values
method. The values
method will return the column of your choosing as a simple array.
An expression can also be passed to values
:
The values
method will return the expression given for each row as a simple array.
This method is similar to values
except it only returns a single, simple value. Where values
calls get
under the hood, this method calls first
.
If no records are returned from the query, one of two things will happen. If the throwWhenNotFound
boolean is set to true
, a RecordCountException
will be thrown. Otherwise the defaultValue
provided to the method will be returned.
An expression can also be passed to value
:
The value
method will return the expression given for the first row found.
Large datasets can be broken up and retrieved in chunks. This allows you to work with a subset of results at once to keep your memory footprint under control.
chunk
can be called on any query like you would call get
. You can stop the retrieving and processing early by returning false
from the callback.
Generates a pagination struct along with the results of the executed query. It does this by calling both count
and forPage
.
The behavior when a maxRows
of 0 or lower is passed is determined by the shouldMaxRowsOverrideToAll
callback function. The default callback returns all rows for values <= 0
. You can customize this behavior by passing a new callback to the shouldMaxRowsOverrideToAll
setting or init
argument.
Generates a simple pagination struct along with the results of the executed query. It does so without getting a count
of the number of records the query would return. This can be desirable for performance reasons if your query count is rather large. It instead determines if there are more records by asking for one more row that your specified maxRows
. If the number of rows returned exceeds your specified maxRows
then the pagination returns hasMore: true
. The results
will always contain your specified maxRows
(or less, if there aren't enough records).
The behavior when a maxRows
of 0 or lower is passed is determined by the shouldMaxRowsOverrideToAll
callback function. The default callback returns all rows for values <= 0
. You can customize this behavior by passing a new callback to the shouldMaxRowsOverrideToAll
setting or init
argument.
You can set your custom pagination collector either in the constructor using the paginationCollector
argument or by calling setPaginationCollector
on a query builder instance.
In qb 8.4.0 the simplePaginate
method was added. This uses a new method on the paginationCollector
.
If you use a custom paginationCollector
, ensure it has been updated with this new generateSimpleWithResults
method before calling simplePaginate
.
Returns the first matching row for the configured query, just like . If no records are found, it throws an EntityNotFound
exception.
The function can make this pattern more ergonomic.
The function can make this pattern more ergonomic.
A pagination collector is the name given to the struct returned from calling the method. It can be a struct or a component. It needs one function defined and will be passed the following parameters.
By default, qb ships with as its pagination collector. The return format of cbpaginator
is the example shown above.
Name
Type
Required
Default
Description
options
struct
false
{}
Any additional queryExecute
options.
Name
Type
Required
Default
Description
column
string
false
"*"
The column on which to count records.
options
struct
false
{}
Any additional queryExecute
options.
Name
Type
Required
Default
Description
column
string
true
The column on which to find the max.
options
struct
false
{}
Any additional queryExecute
options.
Name
Type
Required
Default
Description
column
string
true
The column on which to find the min.
options
struct
false
{}
Any additional queryExecute
options.
Name
Type
Required
Default
Description
column
string
true
The column to sum.
options
struct
false
{}
Any additional queryExecute
options.
Name
Type
Required
Default
Description
column
string
true
The column to sum.
options
struct
false
{}
Any additional queryExecute
options.
Name
Type
Required
Default
Description
asQuery
boolean
false
false
Flag to retrieve the columnList as a query instead of an array.
datasource
string
false
Optional datasource to from which to retrieve the columnList.
Name | Type | Required | Default | Description |
errorMessage | string |
| An optional string error message or callback to produce a string error message. If a callback is used, it is passed the QueryBuilder instance as the only argument. |
options | struct |
|
| Any additional |
Name | Type | Default | Description |
max | numeric | The number of results to return in each chunk. |
callback | Function | The function that will be called with each chunk. |
options | struct |
| Any additional |
Name | Type | Required | Default | Description |
page | numeric |
|
| The page number to retrieve. |
maxRows | numeric |
|
| The number of records per page. If a number less than 0 is passed, 0 is used instead. |
options | struct |
|
| Any additional |
Name | Type | Required | Default | Description |
page | numeric |
|
| The page number to retrieve. |
maxRows | numeric |
|
| The number of records per page. If a number less than 0 is passed, 0 is used instead. |
options | struct |
|
| Any additional |
Name | Type | Description |
totalRecords | numeric | The total records count. |
results | any | The results of the query execution. It will be passed as whatever return format the user has defined. |
page | numeric | The current page number. |
maxRows | numeric | The maximum number of rows retrieved per page. |
Name | Type | Description |
results | any | The results of the query execution. It will be passed as whatever return format the user has defined. |
page | numeric | The current page number. |
maxRows | numeric | The maximum number of rows retrieved per page. |
Name | Type | Required | Default | Description |
columns | string | array | false | A shortcut parameter to retrieve only these columns overriding any columns previously set on the QueryBuilder. |
options | struct | false |
| Any additional |
Name | Type | Required | Default | Description |
options | struct |
|
| Any additional |
Name | Type | Required | Default | Description |
column | any |
|
options | struct |
|
| Any additional |
Name | Type | Required | Default | Description |
column | string |
|
options | struct |
|
| Any additional |
Name | Type | Required | Default | Description |
column | any |
|
defaultValue | string |
| (empty string) | The default value returned if there are no records returned for the query. |
throwWhenNotFound | boolean |
|
| If |
options | struct |
|
| Any additional |
Name | Type | Required | Default | Description |
column | string |
|
defaultValue | string |
| (empty string) | The default value returned if there are no records returned for the query. |
throwWhenNotFound | boolean |
|
| If |
options | struct |
|
| Any additional |
The name of the column to retrieve or an to retrieve.
The sql to use as an .
The name of the column to retrieve or an to retrieve.
The sql to use as an .