Query Parameters and Bindings
Custom Parameter Types
When passing a parameter to qb, it will infer the sql type to be used. Numeric values use INTEGER, BIGINT, or DECIMAL by default, depending on their value. If it is a date, TIMESTAMP, and so forth. If you need more control, you can pass a struct with the parameters you would pass to cfqueryparam.
query.from( "users" )
.where( "id", "=", { value = 18, cfsqltype = "VARCHAR" } );SELECT *
FROM `users`
WHERE `id` = ?This can be used when inserting or updating records as well.
query.table( "users" )
.insert( {
"id" = { value 1, cfsqltype = "VARCHAR" },
"age" = 18,
"updatedDate" = { value = now(), cfsqltype = "DATE" }
} );INSERT INTO `users`
(`id`, `age`, `updatedDate`)
VALUES
(?, ?, ?)Numeric SQL Types
qb uses separate types for signed 32-bit whole numbers, larger whole numbers, and values with a decimal portion. You can customize them with the integerSQLType, bigIntegerSQLType, and decimalSQLType settings. See SQL Type Inference for the boundaries.
qb automatically calculates a scale for decimal bindings when one is not supplied. For an array binding, it uses the greatest scale among its members.
Numeric Lists
When an array must share one binding type, qb chooses a common numeric SQL type that covers its members. It considers declared SQL types, not just whether the current values happen to fit into a smaller type. Member order does not affect the result, and null members are ignored.
The cast ensures the large value is numeric on engines that parse large integer literals as strings. Numeric-looking strings retain their string type.
TINYINT and SMALLINT
SMALLINT
INTEGER and BIGINT
BIGINT
INTEGER or BIGINT and DECIMAL
DECIMAL
INTEGER and REAL
DOUBLE
INTEGER and explicitly declared FLOAT
FLOAT
REAL and DOUBLE
DOUBLE
BIGINT and DOUBLE
VARCHAR, or an exception in strict mode
DECIMAL and FLOAT
VARCHAR, or an exception in strict mode
Explicit member cfsqltype and sqltype values participate in promotion, including cf_sql_ aliases. Supported numeric types include BIT, TINYINT, SMALLINT, INTEGER, BIGINT, MONEY4, MONEY, DECIMAL, NUMERIC, REAL, FLOAT, and DOUBLE. Java float and double values without an explicit SQL type first use qb's normal value inference; fractional values infer DECIMAL by default.
A larger magnitude range does not guarantee enough precision. qb conservatively treats exact decimal or money types mixed with approximate types, and BIGINT mixed with approximate types, as unsafe. The default is VARCHAR.
An explicit cfsqltype or sqltype on the outer binding takes precedence over inference. The setting does not validate caller-selected conversions or the destination column's precision and scale. Falling back to VARCHAR preserves the binding representation, but the database may still convert it during query execution.
Bindings
Bindings are the values that will be sent as parameters to a prepared SQL statement. This protects you from SQL injection. In CFML, this uses cfqueryparam to parameterize the values.
If you need to inspect the bindings for the current query you can retrieve them in order using the getBindings method.
getBindings
No arguments
This method returns the current bindings in order to be used for the query.
You can also retrieve the bindings associated to their corresponding types.
getRawBindings
No arguments
This method returns the current bindings to be used for the query associated to their corresponding types.
addBindings
Adds a single binding or an array of bindings to a query for a given type.
newBindings
Struct | Array<Struct>
true
A single binding or an array of bindings to add for a given type.
type
String
false
"where"
The type of binding to add.
addBindingsFromBuilder
Adds all of the bindings from another builder instance.
qb
QueryBuilder
true
Another builder instance to copy all of the bindings from.
Last updated
Was this helpful?