Blog

Eric Peterson

December 19, 2017

Spread the word


Share your thoughts

In the CFML scene, there seems to be two names when it comes to data persistance plain ole' `cfquery` and ORM — not much inbetween. Our module highlight today is out to change that.

Meet qb.

qb

qb is a query builder. It allows you to specify SQL commands with a fluent syntax. The builder is then compiled to different database grammars such as MySQL, Oracle, and SQL Server. The result is fluent, readable code that can compile to any database grammar you need. Let's take a quick high-level overview of the syntax:

Selects, Joins, and Wheres

Query methods can be specified in any order — qb will take care of putting them in the right order for you. You end with a call to get to execute the query.

query.from( "posts" )
    .select( "post_id", "author_id", "title", "body" )
    .join( "authors", "authors.id", "=", "posts.author_id" )
    .whereLike( "authors.name", "Ja%" )
    .orderBy( "posts.published_at" )
    .get();

/*
  SELECT `post_id`,
         `author_id`,
         `title`,
         `body`
    FROM `posts`
    JOIN `authors`
      ON `authors`.`id` = `posts`.`author_id`
   WHERE `authors`.`name` LIKE 'Ja%'
ORDER BY `posts`.`published_at`
*/

Inserts, Updates, and Deletes

Inserts, updates, and deletes use the same methods to restrict the data set. The only difference is the method called at the end of the builder chain — insert, update, or delete instead of get.

query.from( "posts" )
    .whereId( 1 )
    .update( {
        "title" = "New Title",
        "updated_at" = now()
    } );

/*
UPDATE `posts`
   SET `title` = 'New Title',
       `updated_at` = '2017-12-19 08:00:   00'
 WHERE `id` = 1
*/

Aggregates

qb provides helpers for common aggregate functions on data sets. As usual, use any other qb methods to shape the data set before getting the aggregate.

query.from( "users" ).count();

/*
SELECT COUNT(*)
  FROM `users`
*/

query.from( "orders" )
    .whereBetween("created_date", dateAdd( "d", -7, now() ), now() )
    .sum( "total" );

/*
SELECT SUM(`total`)
  FROM `orders`
 WHERE `created_date` BETWEEN '2017-12-12 08:00:   00' AND '2017-12-19 08:00:   00'
*/

Interception Points

qb supports two interception points — preQBExecute and postQBExecute. Use it to log or modify the sql as you need.

Wrap Up

qb enables a slew of new patterns. Check out an example of one in this gist. Be sure to check out all the examples in the official docs to harness the raw power qb offers.

Add Your Comment

Recent Entries

Mastering Events and Listeners in CBWIRE

Mastering Events and Listeners in CBWIRE

In CBWIRE, events and listeners are the backbone of building responsive, modular applications without relying heavily on JavaScript. This guide walks you through setting up and using CBWIRE events to create seamless interactions between components, from dispatching events in CFML and frontend templates to listening with Alpine.js and JavaScript. Learn how to make your applications feel dynamic and engaging by effortlessly connecting components. Whether you’re triggering events to update a dashboard or targeting specific parts of your app with dispatchTo, these techniques will empower you to create a modern, interactive CFML experience with ease.

Grant Copley
Grant Copley
November 11, 2024
10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

ColdFusion remains a powerful and versatile platform for building dynamic web applications. However, keeping your ColdFusion environment optimized, secure, and scalable requires specialized expertise. Whether managing a long-standing ColdFusion application or planning new development projects, hiring a dedicated ColdFusion consulting and support team can be a game-changer for CTOs, CIOs, and developers. Here's why:

1. Expert Guidance on ColdFusion Web Development

...

Cristobal Escobar
Cristobal Escobar
November 08, 2024
ColdBox Free Tip 5 - Building Named Routes with a Struct

ColdBox Free Tip 5 - Building Named Routes with a Struct

**Did you know ColdBox provides flexible ways to build routes using structs?** In this tip, we’ll cover how to use the `event.buildLink()` and `event.route()` methods for named routes, a feature that’s especially handy when working with dynamic URLs.

Maria Jose Herrera
Maria Jose Herrera
November 07, 2024