Placeholders for attribute paths in ORDA queries

The ORDA features keep coming with 4D v17 R5! In a previous blog post, we showed you how to create generic queries with named placeholders for values. This post will focus on how to use placeholders for attribute paths (field names in tables). 

HDI: Example of placeholders for attribute paths in ORDA queries

Two types of placeholders are available:

Indexed placeholders

Let’s look at an example using indexed placeholders for attribute paths. We want to find clients whose names begin with “Bravo” and have “New client” in the comment field:

C_OBJECT($clients)
$clients:=ds.Clients.query(":1 = 'Bravo@' and :2 = 'New client'";"name";"comment")

The placeholders are inserted as :paramIndex (e.g., :1, :2, … with an increment of 1) in the query string. Their corresponding values are provided by the sequence of value parameter(s). 

named placeholders

You can also use named placeholders. They use the same concept as named placeholders for values. Parameters are inserted as :paramName and their values are provided in the query string:

C_OBJECT($clients;$settings)
$settings:=New object
$settings.attributes:=New object("pathOfName";"name";"pathOfComment";"comment")
$clients:=ds.Clients.query(":pathOfName = 'Bravo@' and :pathOfComment = 'New client'";$settings)

Why use placeholders?

Let’s look at the licencesInfo object field. It indicates the number of licenses in a licenceName:number style.

{"licencesInfo":
{"4DDevPro":10, //This client has 10 4DDevPro licences
"4DServerV17_2":18,
"4DWebappserverV17_2":23}
}

To find the number of “4DDevPro” licences, you can write this query:

C_OBJECT($clients)
$clients:=ds.Clients.query("licencesInfo.4DDevPro >=0")

But now look at this licencesInfo object field. You can see that using placeholders for attribute paths can be especially useful when querying an object whose property names are not compliant with dot notation:

{"licencesInfo":
{"4D Dev. Pro":10, //This client has 10 4D Dev. Pro licences
"4D Server V17.2":18,
"4D Web app/server V17.2":23}
}

In this particular case, providing the parameters as a collection of strings solves the problem because it’ll be interpreted by the query as an attribute path. 

C_OBJECT($clients)
$clients:=ds.Clients.query(":1 >=0";New collection("licensesInfo";"4D Dev. Pro"))

 

That’s not the only advantage of using placeholders. There’s an increased security aspect as well, since using placeholders prevents malicious code insertion. There’s also no more need to worry about formatting or character issues. The list goes on …

The examples above are querying a dataClass, but if you check the documentation, you’ll see it is applicable on collections too. You’ll also discover more examples showing why using placeholders is a good call!

 

Avatar
- Product Owner - Marie-Sophie Landrieu-Yvert joined the 4D Product team as a Product Owner in 2017. In this role, she is responsible for writing user stories and translating them into functional specifications. She also ensures that the delivered feature implementation meets the customer's needs. Marie-Sophie graduated from the engineering school ESIGELEC and began her career as an engineer at IBM in 1995. She took part in various projects (maintenance and development projects) and worked as a COBOL developer. She then moved on to work as a UML designer and Java developer. More recently, her main responsibilities included analyzing and writing functional requirements, and coordinating business and development teams.