Re: Array variables in building a qualifier in EOModeler
Re: Array variables in building a qualifier in EOModeler
- Subject: Re: Array variables in building a qualifier in EOModeler
- From: Zak Burke <email@hidden>
- Date: Fri, 27 Apr 2007 07:00:35 -0400
Saker Ghani wrote on 4/26/07 7:13 PM:
Is there a simple way to build a qualifier in EOModeler such that it is
possible to use a variable binding that is an array? So, for example,
going beyond the traditional "city = $city", is it possible to bind in
the fashion, "city = @cities", and have EOModeler generate the AND/OR
SQL statements to include all the items in the "cities" array? Or is
the only way to do that within code, programatically?
I think you have to do it in code, but it's not hard. Here are two
qualifier-helper methods I use:
/**
* Return a key-value tuple for the where clause.
*
* @param field: The attribute of an entity we are comparing value
param with
* @param qualifier: selector for =, >=, <=, like, etc.
* @param value: value to match field against
* @param conversion: value type: integer %d, string %s, EO %@
* @param join: if value is a list, how the items should be joined
(AND, OR)
*
* @return a qualifier for the given values
*
* @throws I3pkbFetchException if any required parameters (field,
qualifier) are null.
*/
public EOQualifier whereGet(String field, NSSelector s, Object
value, String conversion, int join)
throws I3pkbFetchException
{
if (field == null || s == null)
throw new I3pkbFetchException("invalid where-clause fields;
you must specify a field and a qualifier");
if (this.where == null)
this.where = new NSMutableArray();
String format = field + " " +
EOQualifier.stringForOperatorSelector(s) + " " + conversion;
// value contains an array
if (value instanceof NSArray)
return listQualifier(format, conversion, value, join);
// value is empty
else if (value == null)
return EOQualifier.qualifierWithQualifierFormat(format,
new NSArray(NSKeyValueCoding.NullValue));
// normal
else
return EOQualifier.qualifierWithQualifierFormat(format,
new NSArray(value));
}
/**
* Join values on a list into a single qualifier.
*
* @param format where clause, e.g. "fieldname = %s"
* @param conversion: value type: integer %d, string %s, EO %@
* @param value: list of values which format's field must match
* @param join: AND, OR
*
* @return qualifier for items on the list
*/
protected EOQualifier listQualifier(String format, String
conversion, NSArray list, int join)
{
NSMutableArray args = new NSMutableArray();
for (Enumeration e = list.objectEnumerator(); e.hasMoreElements();)
{
// ints and floats
if (conversion.equals("%d") || conversion.equals("%f"))
args.addObject(EOQualifier.qualifierWithQualifierFormat(format, new
NSArray((Number) e.nextElement())));
// EOs
else if (conversion.equals("%@"))
args.addObject(EOQualifier.qualifierWithQualifierFormat(format, new
NSArray((EOEnterpriseObject) e.nextElement())));
// strings
else
args.addObject(EOQualifier.qualifierWithQualifierFormat(format, new
NSArray((String) e.nextElement())));
}
if (join == OR)
return new EOOrQualifier(args);
else
return new EOAndQualifier(args);
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden