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:
returning
is only supported in PostgresGrammar
and SqlServerGrammar
. 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.
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.
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
columns
string | array
true
A single column, a list or columns, or an array of columns to return from the inserted query.
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.