After learning how you can create, update, and delete data, here is a new entry in the ORDA series. In this post, you will see how you can query your datastore in an object-oriented mode. With ORDA, queries are lighter, more readable, and you can navigate through the whole data model using object-oriented concepts !
On top of that, ORDA makes it possible to very easily query several related tables, manage recursive relations on the same table, and use several logical operators in a single query. Plus, you don’t have to worry about setting relations between tables … manually or automatically !
GET STARTED WITH ORDA QUERYING
As when you would run a query on a table, querying with ORDA is done on a dataclass (which corresponds to a table).
There are several ways to query a dataclass. The following provides a quick overview. For more in-depth information about the query() method, check out the documentation.
1- Provide the query as a single String
Here is a query to get all employees whose first name starts with ‘S’:
$employees:=ds.Employee.query("firstName='S@'") // Query on Employee table
2- Provide the query and values separately using placeholders
Now, let’s say you want to get all employees whose first name starts with ‘D’ or ‘E’. Please note that values to apply are offset in the query and they are referred to with :1, :2, … :n, as shown below:
$employees:=ds.Employee.query("firstName=:1 or firstName=:2";"D@";"E@") // Query on Employee table
Variation: You can also use placeholders through a collection to provide the values. As the example below demonstrates:
// You can use a collection to provide values to apply in the query
$employees:=ds.Employee.query("firstName=:1 or firstName=:2";$params) // Query on Employee table
some Perks of the new way of querying
EASILY USE A RECURSIVE LINK ON A SINGLE TABLE
Consider this recursive link on the “Employee” table:
Using the classic way
This code requests all employees whose first name starts with the letter ‘A’, with a manager having a last name that begins with the letter ‘S’:
SELECTION TO ARRAY([Employee]ID;_managersIDs)
QUERY WITH ARRAY([Employee]managerID;_managersIDs)
This query is much lighter…
$entitySelection := ds.Employee.query("firstName=:1 and manager.lastName=:2";"A@";"S@")
Don’t wait … start using ORDA now for shorter, cleaner, and more readable code!