User's Manual

Chapter 9. Persistence Tutorial 85
9.5.1.6. Filtering Using Functions
The filtering methods discussed so far work well when the developer only needs to filter directly off
of columns or values. However, they do not work well if the developer wants a case-insensitive filter
or needs to use some other function to manipulate the data in the columns.
To meet this need and to appropriately handle null values, the system provides a method within Fil-
terFactory named compare that allows developers to pass in two expressions and have the system
compare them to each other.
The most common use case for this is a case-insensitive comparison where the developer wants to
know whether a string exists in a column, but does not care about the case. For instance, suppose a de-
veloper wants to retrieve all articles titled "Disaster Strikes," but does not care about the capitalization.
He could use use the following code to achieve this:
DataCollection pub = SessionManager.getSession().retrieve("tutorial.Articles");
Filter filter = pub.addFilter(pub.getFilterFactory().compare("upper(title)",
FilterFactory.EQUALS,
":title"));
// we set the title to all upper case so that we do not make oracle do
// it for us which would be slower
filter.set("title", "DISASTER STRIKES");
If the developer actually wants all articles with either the word "Disaster" or "Strikes," he can do the
following:
DataCollection pub = \
SessionManager.getSession().retrieve("tutorial.Articles");
FilterFactory factory = pub.getFilterFactory();
Filter disasterFilter = \
factory.compare("upper(title)", FilterFactory.CONTAINS,
":disasterTitle"));
filter.set("disasterTitle", "DISASTER");
Filter strikesFilter = \
factory.compare("upper(title)", FilterFactory.CONTAINS,
":strikesTitle"));
filter.set("disasterTitle", "STRIKES");
pub.addFilter(factory.or()
.addFilter(disasterFilter)
.addFilter(strikesFitler));
The important thing to realize is that it this method will handle problem with null values faced by
Oracle and Postgres, whereas using a standard simple filter will not.
9.5.2. Ordering
Use the addOrder(String order) method to order a DataQuery, DataCollection, and a
DataAssociationCursor object. The addOrder method takes a String as its only parameter
and the format of the string is the optional object type following by a dot and then then required
attribute name ([
object type name .] attribute ) you wish to order by.
The string parameter passed to the addOrder(String order) method is used in an ORDER BY
clause, which is appended to the SELECT statement. The order is specified by constructing a string
representing the ORDER BY clause, but instead of specifying column names, you specify attribute
names. The persistence layer automatically converts the attribute names to column names using the
property mappings defined in the PDL. For example, see the following DataQuery defined in PDL
(remove the "\" and make it all one line):
query UsersGroups {