You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use this skill when building database queries with qb (Query Builder) in ColdBox/BoxLang. Covers QueryBuilder injection, select/from/where/join/group/order/limit clauses, aggregates, inserts, updates, deletes, raw expressions, sub-queries, chunking, and grammar configuration for MySQL, Postgres, MSSQL, and SQLite.
applyTo
**/*.{bx,cfc,cfm,bxm}
QB Skill
When to Use This Skill
Load this skill when:
Writing SQL queries using a fluent, chainable builder (no raw SQL)
Performing JOINs, sub-queries, or complex WHERE conditions
Inserting, updating, or deleting records safely with parameter binding
Paginating large result sets
Using database-agnostic code that works across MySQL, Postgres, MSSQL, and SQLite
Installation
box install qb
Configuration
config/modules/qb.cfc
functionconfigure(){return{// Default grammar matching your database enginedefaultGrammar : "MySQLGrammar@qb",// MySQLGrammar | PostgresGrammar | MSSQLGrammar | SQLiteGrammar// Datasource to usedefaultDatasource : "myDatasource",// Return queries as arrays of structs (recommended)returnFormat : "array"}}
Core API
Injection
propertyname="qb"inject="QueryBuilder@qb";
SELECT Queries
// All recordsvarusers=qb.from("users").get()// Specific columnsvarusers=qb.select("id,name,email").from("users").get()// With aliasvarusers=qb.select(["id","email",{"full_name": "name"}]).from("users").get()// Distinctvarroles=qb.distinct().select("role").from("users").get()// First recordvaruser=qb.from("users").where("id",rc.id).first()// Get the value of a single columnvaremail=qb.from("users").where("id",1).value("email")
WHERE Clauses
qb.from("users").where("isActive",true).where("role","=","admin").whereIn("id",[1,2,3]).whereNotIn("status",["banned","deleted"]).whereNull("deletedAt").whereNotNull("emailVerifiedAt").whereBetween("age",18,65).where("email","like","%@example.com%").get()// OR conditionsqb.from("users").where("role","admin").orWhere("role","superadmin").get()// Grouped WHEREqb.from("posts").where(function(q){q.where("status","published").orWhere("featured",true)}).where("userId",rc.userId).get()
// INNER JOINqb.from("posts").join("users","users.id","=","posts.userId").select("posts.id,posts.title,users.name AS authorName").get()// LEFT JOINqb.from("users").leftJoin("profiles","profiles.userId","=","users.id").whereNull("profiles.id")// users without profiles.get()// Complex joinqb.from("orders").join("order_items",function(join){join.on("order_items.orderId","=","orders.id").where("order_items.qty",">",0)}).get()
GROUP BY / HAVING
qb.from("orders").select("userId").selectRaw("COUNT(*) AS orderCount, SUM(total) AS totalSpent").groupBy("userId").having("orderCount",">",5).orderByDesc("totalSpent").get()
// WHERE IN subqueryqb.from("posts").whereIn("userId",function(q){q.select("id").from("users").where("role","admin")}).get()
Raw Expressions
qb.from("products").selectRaw("id, name, price * 1.1 AS priceWithTax").whereRaw("MATCH(name, description) AGAINST (? IN BOOLEAN MODE)",[rc.term]).get()