·¬ÇÑÉçÇø

Brightspot CMS Developer Guide

Binding variables


Dari supports bind variables in query strings using ? for placeholders.


In a basic scenario, placeholders refer to variables with single values or single objects, such as String, Integer, or Date.

String userName = "John Smith";
 User user = Query.from(User.class)
                  .where("name = ?", userName).first();

Referring to the previous snippet, at runtime Dari creates the following where clause:

WHERE name = 'John Smith'

In the next example, the query filters on a Date object, returning all ToolUsers for which the last login date is after January 17, 2019.

 String startDateString = "01/17/2019";
 DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
 Date date = null;

 try {
     date = df.parse(startDateString);
 }
 catch (ParseException e) {
     /* Error handling */
 }

 List<ToolUser> toolUser = Query.from(ToolUser.class)
                                 .where("lastLoginDate  >  ?", date).selectAll();


You can also bind a variable that is a collection, such as a or , to a query. At run time, Dari performs the operation in the .where method against all elements in the collection. In the following snippet, the query searches for all instances of User with a name value that matches any name in the list names.

List<String> names = new ArrayList<String>();
 names.add("John Smith");
 names.add("Jane Doe");
 List<User> users = Query.from(User.class)
                         .where("name = ?", names).selectAll();


If a .where method has more than one placeholder, Dari substitutes the placeholders for the parameters in the order they appear.

List<ToolUser> toolUser = Query.from(ToolUser.class)
                               .where("name = ? OR email = ?", "joe", "joe@brightspot.com")
                               .selectAll();

Referring to the previous snippet, at runtime Dari creates the following where clause:

WHERE name = 'joe' OR email = 'joe@brightspot.com'

You can indicate the value for a placeholder by specifying a position—a useful technique to avoid repeating the same value in a long list of parameters.

List<Author> authors = Query.from(Author.class)
                             .where("firstName = ?0 OR lastName = ?0 OR email contains ?1", "rand", "brightspot.com").selectAll();

Referring to the previous snippet, at runtime Dari creates the following where clause:

WHERE firstName = 'rand'
OR lastName = 'rand'
OR email LIKE '%brightspot.com%'

Previous Topic
Paginated results
Next Topic
Object references
Was this topic helpful?
Thanks for your feedback.
Our robust, flexible Design System provides hundreds of pre-built components you can use to build the presentation layer of your dreams.

•
•
•
Brightspot is packaged with content types that get you up and running in a matter of days, including assets, modules and landing pages.

• Content types
• Modules
• Landing pages
Everything you need to know when creating, managing, and administering content within Brightspot CMS.

• Dashboards
• Publishing
• Workflows
• Admin configurations
A guide for installing, supporting, extending, modifying and administering code on the Brightspot platform.

• Field types
• Content modeling
• Rich-text elements
• Images
A guide to configuring Brightspot's library of integrations, including pre-built options and developer-configured extensions.

• Google Analytics
• Shopify
• Apple News