3. Querying in Qcodo
* Performing Custom SQL Queries
<< Previous   |   Back to Main   |   * Qcodo Query: Object Oriented Database Querying >>

View Source
will open in a new window
SQL Queries in Qcodo
Although the Qcodo 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 Qcodo Query, or QQ for short. 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 Qcodo 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, Qcodo 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 Payments, managed by Karen Wolfe (with 2 team members)
ACME Website1, managed by Karen Wolfe (with 0 team members)
State College HR Sxzxxystem, managed by Mike Ho (with 4 team members)
xx, managed by Teo Dirac (with 8 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 Sxzxxystem has a budget of $80500
ACME Website1 has a budget of $9560
ACME Payments has a budget of $5125

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

ACME Website1 has a budget of $9560
ACME Payments has a budget of $5125