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.
Qualifiers
Simple String Qualifier
Simple Integer Qualifier
Wildcard Qualifiers
Compound 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.
The following are examples of qualifier strings:
agent.firstName caseInsensitiveLike %@
bedrooms >= %d
bathrooms <= %d AND bedrooms = %d
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.
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"})); |
The code below constructs a qualifier to find the listings with at least 3 bedrooms.
EOQualifier.qualifierWithQualifierFormat("bedrooms >= %d", new NSArray(new Object[] {3})); |
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*"})); |
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.
Last updated: 2007-07-11