Custom SQL Queries in QCubed

Although the QCubed Query can generate the SQL query code for most of your application, you will undoubtedly need to write your own custom queries, to either perform more refined Load methods, execute searches, generate reports, etc.

The framework offers multiple ways for performing your own custom SQL queries, from completely free form queries using just the database adapter, itself, to completely structured object-oriented queries using QCubed Query. In general, there are three main ways to perform queries, with pros and cons for each: The examples below provide a quick sample of each of these three query types. And then the following examples will illustrate QCubed Query in much greater detail.

As a final note, all the examples here are coded below on the page, itself. However, it is always a good practice to have query code like this written within the classes, themselves. Especially for any Load-related methods, QCubed tries to be consistent in following the Singleton design pattern with static "loadBy" and "loadArrayBy" methods, so the SELECT queries for any table can reside in that table's ORM class, itself. For more on this, be sure to view the code generated commented out sample code in your custom ORM subclasses in /includes/data_classes. See Customized Load Methods in Section 2 for more information.

Ad Hoc Query: Selecting the Projects, their managers and team member count

ACME Payment System, managed by Karen Wolfe (with 7 team members)
ACME Website Redesign, managed by Karen Wolfe (with 5 team members)
Blueman Industrial Site Architecture, managed by John Doe (with 5 team members)
State College HR System, managed by Mike Ho (with 6 team members)

Ad Hoc NonQuery: Updating Project #3's budget to 2500

Updated. (Use View Source above to see the code for this)

Custom Load Query: Select all Projects with Budgets over $5000, ordered by Descending Budget

State College HR System has a budget of $80500.00
ACME Website Redesign has a budget of $9560.25
ACME Payment System has a budget of $5124.67

QCubed Query: Select all Projects which have a Budget over $5000 and under $10000, ordered by Descending Budget

ACME Website Redesign has a budget of $9560.25
ACME Payment System has a budget of $5124.67