Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Constructing Fetch Specifications

You commonly create and configure fetch specifications using EOModeler’s fetch specification builder. This is described in the chapter “Working With Fetch Specifications” in EOModeler User Guide. However, you also commonly create and configure fetch specifications programmatically, as this section describes.

As discussed in “Objects Involved in Fetching,” a fetch specification includes an entity name, a qualifier (optional), and a sort ordering (optional). The trickiest part of building a fetch specification programmatically is building qualifiers. The code samples here assume that you’re using the Real Estate model and database.

In this section:

Qualifiers
Simple String Qualifier
Simple Integer Qualifier
Wildcard Qualifiers
Compound Qualifiers


Qualifiers

There are many ways to programmatically create a qualifier. One of the most common ways is to provide a format string to a qualifier. A format string is a logical expression that specifies parameters for performing a comparison. In a format string, you specify a data attribute to compare and a value with which to compare it. Enterprise Objects supports comparisons of equality, greater than, less than, greater than or equal to, less than or equal to, not equal, like, and case–insensitive like.

A format string also includes a conversion character, which specifies the data type of the value in the comparison. “Table 6-3” lists the available conversion characters.

Table 5-3  Format string conversion characters

Conversion character

Expected value or result

%s

A String object, or the result of the method toString.

%d

An Integer object or something that can be converted to an Integer.

%f

A Double object or something that can be converted to a Double.

%@

An arbitrary object (like an EOEnterpriseObject). No conversions are performed.

%K

Similar to %@ except the argument is coerced into a String with the method toString and is treated as a key that can determine whether the resulting qualifier is an EOKeyValueQualifier or an EOKeyComparisonQualifier.

%%

Results in a literal % character.

The following are examples of qualifier strings:

The following sections provide code examples that teach you how to build format strings. Many types of format strings are possible; only a few are presented here. See the API reference for com.webobjects.eocontrol.EOQualifier for more information. The following sections provide concrete code examples that show you how to build different kinds of qualifiers, after a section introducing qualifiers.

Simple String Qualifier

The code below constructs a qualifier to find the listings associated with a particular agent, based on the agent’s last name.

EOQualifier.qualifierWithQualifierFormat("agent.lastName = %s", new           NSArray(new Object[] {"Basset", "Travers"}));

Simple Integer Qualifier

The code below constructs a qualifier to find the listings with at least 3 bedrooms.

EOQualifier.qualifierWithQualifierFormat("bedrooms >= %d", new NSArray(new Object[] {3}));

Wildcard Qualifiers

The code below constructs a qualifier to find the Listings associated with Agents whose first names begins with the letter b.

EOQualifier.qualifierWithQualifierFormat("agent.lastName caseInsensitiveLike %@", new           NSArray(new Object[] {"B*"}));

Compound Qualifiers

Providing a fetch specification with a single qualifier often doesn’t provide the precision you need for search criteria. Fortunately, you can easily form boolean combinations of qualifiers. These types of qualifiers, qualifiers composed of other qualifiers, are called compound qualifiers. Enterprise Objects supports AND, OR, and NOT boolean combinations of qualifiers.

This code builds a compound qualifier that combines the qualifiers constructed in “Simple String Qualifier” and in “Wildcard Qualifiers”:

EOQualifier compoundQualifier = new EOAndQualifier(new NSArray(new Object[]
     {EOQualifier.qualifierWithQualifierFormat("agent.lastName = %s", new NSArray(new
       Object[] {"Basset", "Travers"})),
      EOQualifier.qualifierWithQualifierFormat("agent.lastName caseInsensitiveLike %s",
        new NSArray(new Object[] {"B*"}))}));

This creates a qualifier that searches for the criteria specified in both qualifiers.



< Previous PageNext Page > Hide TOC


Last updated: 2007-07-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice