Picking database columns for QQuery
Most of the time, QQuery selects all the columns from the table and thus populates all the properties of the resulting objects. Normally, this is the right thing to do - the most expensive part of a typical query is hitting the database and performing the query;
However, when some tables have a large number of columns, or some columns that contain large objects (BLOB, TEXT, etc.), this may become expensive, both in terms of the traffic generated between application and database, and in terms of the memory footprint of the application.
Also, more and more databases are preventing you from creating SQL queries that might produce ambiguous results when using aggregate clauses. For example, if you create a query that groups employees by last name, and counts how many employees have each last name, but then also tries to select a first name, if there are mulitple employees with the same last name, the database will be confused and won't know which first name to show. Most databases will error in this situation. However, it would be perfectly fine to select a last name, because each group has the same last name. You need a way to specify particular database fields to select.
QQ::select solves this problem by allowing you to pick particular columns to fetch from the database.
QQ::Select can be passed as a clause to any query method. As shown in the second example below, it can also be passed as an argument to QQ::expand() to pick specific columns to fetch for the object to be expanded.
Note, that when QQ::Select is used, by default the primary keys are automatically added to the select list. This is illustrated by the first example below, where QQN::person()->Id is not part of the QQ::Select list, but $objPerson->Id is populated and used afterwards. This behaviour can be changed by using using the SetSkipPrimaryKey() method of QQSelect, as shown in the second example. This is typically useful for simple queries with the distict clause, where the presence of the primary keys would prevent distinct from having the desired effect
One QQ::select() can be used to select multiple columns, as shown in the fourth example below:
QQ::select(QQN::person()->Address->Street, QQN::person()->Address->City)
The same example also shows the use of QQ::select() in QQ::expandAsArray().
You may also notice, that many times the QQ::Select clause is passed as the last argument to the query method. Even though this is not ideal (since in SQL the select clause is the first in a statement), it was necessary for backward compatibility reasons with older versions of QCubed.
1. Get the first names of all the people
- 1 John
- 2 Kendall
- 3 Ben
- 4 Mike
- 5 Alex
- 6 Wendy
- 7 Karen
- 8 Samantha
- 9 Linda
- 10 Jennifer
- 11 Brett
- 12 Jacob
- 49 5
- 50 5
- 51 5
- 52 57417
- 53 Joseph
- 54 Joseph
- 55 Joseph
- 56 sdbv
- 57 sdbv
- 58 John
- 59 Johnww
- 60 ttttttt
- 61 ttttttt
- 62 ttttttt
- 63 ttttttt
- 64 ttttttt
- 65 ttttttt
2. Get all the distinct first names of all the people
- John
- Kendall
- Ben
- Mike
- Alex
- Wendy
- Karen
- Samantha
- Linda
- Jennifer
- Brett
- Jacob
- 5
- 57417
- Joseph
- sdbv
- Johnww
- ttttttt
3. Get the last names of all the people, and the amount spent on the project they manage (if any), for Projects that have 'ACME' or 'HR' in it. Sort the result by Last Name, then First Name
Notice how some people may be listed twice, if they manage more than one project.
- Ho's project spent $73200.00
- Wolfe's project spent $10250.00
- Wolfe's project spent $5175.00
4. Projects and Addresses for each Person
- Karen
Addresses: 1 Pine St, San Jose; 421 Central Expw, Mountain View;
Projects where this person is a project manager: started on Mar 01 2004 (milestones: Milestone A; Milestone B; Milestone C; )
started on Aug 15 2005 (milestones: Milestone G; Milestone H; Milestone I; Milestone J; ) - Mike
Addresses: none
Projects where this person is a project manager: started on Feb 15 2006 (milestones: Milestone D; Milestone E; ) - John
Addresses: 1 Love Drive, Phoenix;
Projects where this person is a project manager: started on Mar 01 2006 (milestones: Milestone F; ) - Kendall
Addresses: 2 Doves and a Pine Cone Dr., Dallas;
Projects where this person is a project manager: none - Ben
Addresses: 3 Gold Fish Pl., New York; 323 W QCubed, New York;
Projects where this person is a project manager: none - Alex
Addresses: 22 Elm St, Palo Alto;
Projects where this person is a project manager: none - Wendy
Addresses: none
Projects where this person is a project manager: none - Samantha
Addresses: none
Projects where this person is a project manager: none - Linda
Addresses: none
Projects where this person is a project manager: none - Jennifer
Addresses: none
Projects where this person is a project manager: none - Brett
Addresses: none
Projects where this person is a project manager: none - Jacob
Addresses: none
Projects where this person is a project manager: none - 5
Addresses: none
Projects where this person is a project manager: none - 5
Addresses: none
Projects where this person is a project manager: none - 5
Addresses: none
Projects where this person is a project manager: none - 57417
Addresses: none
Projects where this person is a project manager: none - Joseph
Addresses: none
Projects where this person is a project manager: none - Joseph
Addresses: none
Projects where this person is a project manager: none - Joseph
Addresses: none
Projects where this person is a project manager: none - sdbv
Addresses: none
Projects where this person is a project manager: none - sdbv
Addresses: none
Projects where this person is a project manager: none - John
Addresses: none
Projects where this person is a project manager: none - Johnww
Addresses: none
Projects where this person is a project manager: none - ttttttt
Addresses: none
Projects where this person is a project manager: none - ttttttt
Addresses: none
Projects where this person is a project manager: none - ttttttt
Addresses: none
Projects where this person is a project manager: none - ttttttt
Addresses: none
Projects where this person is a project manager: none - ttttttt
Addresses: none
Projects where this person is a project manager: none - ttttttt
Addresses: none
Projects where this person is a project manager: none