Do you mean overloading "matchExp" and "matchAnyExp" with one-parameter
methods?
2009/6/26 Andrus Adamchik <andru..bjectstyle.org>
> Andrey,
>
> thanks for applying this. Do you mind to perform this renaming that I
> suggested earlier to better match the existing factory API:
>
> * s/matchObject/matchExp/
> * s/matchObjects/matchAllExp|matchAnyExp/
>
> If not, no worries, I may do it myself.
>
> Andrus
>
>
> On Jun 26, 2009, at 2:25 PM, andre..pache.org wrote:
>
> Author: andrey
>> Date: Fri Jun 26 11:25:15 2009
>> New Revision: 788657
>>
>> URL: http://svn.apache.org/viewvc?rev=788657&view=rev
>> Log:
>> CAY-1241 Add method to ExpressionFactory to match against the primary key
>> of an object or list of objects (based on patch by Laurentiu Cocanu)
>>
>> Modified:
>>
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
>>
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
>>
>> Modified:
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
>> URL:
>> http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java?rev=788657&r1=788656&r2=788657&view=diff
>>
>> ==============================================================================
>> ---
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
>> (original)
>> +++
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
>> Fri Jun 26 11:25:15 2009
>>.. -25,6 +25,8 @@
>> import java.util.List;
>> import java.util.Map;
>>
>> +import org.apache.cayenne.ObjectId;
>> +import org.apache.cayenne.Persistent;
>> import org.apache.cayenne.exp.parser.ASTAdd;
>> import org.apache.cayenne.exp.parser.ASTAnd;
>> import org.apache.cayenne.exp.parser.ASTBetween;
>>.. -675,4 +677,55 @@
>> }
>> return exp;
>> }
>> +
>> + /**
>> + * Creates an expression that matches the primary key of object in
>> + * <code>ObjectId</code>'s <code>IdSnapshot</code> for the argument
>> + * <code>object</code>.
>> + */
>> + public static Expression matchObjectExp(Persistent object) {
>> + ObjectId obid = object.getObjectId();
>> + Map<String, ?> map = obid.getIdSnapshot();
>> +
>> + List<Expression> pairs = new ArrayList<Expression>(map.size());
>> +
>> + for (Map.Entry<String, ?> entry : map.entrySet()) {
>> + Expression exp = expressionOfType(Expression.EQUAL_TO);
>> + exp.setOperand(0, new ASTDbPath(entry.getKey()));
>> + exp.setOperand(1, wrapPathOperand(entry.getValue()));
>> + pairs.add(exp);
>> + }
>> +
>> + return joinExp(Expression.AND, pairs);
>> + }
>> +
>> + /**
>> + * Creates an expression that matches any of the objects contained in
>> the list
>> + * <code>objects</code>
>> + */
>> + public static Expression matchObjectsExp(List<? extends Persistent>
>> objects) {
>> + if (objects == null || objects.size() == 0) {
>> + return expFalse();
>> + }
>> +
>> + return matchObjectsExp(objects.toArray(new
>> Persistent[objects.size()]));
>> + }
>> +
>> + /**
>> + * Creates an expression that matches any of the objects contained in
>> the
>> + * <code>objects</code> array
>> + */
>> + public static Expression matchObjectsExp(Persistent... objects) {
>> + if (objects == null || objects.length == 0) {
>> + return expFalse();
>> + }
>> +
>> + List<Expression> pairs = new
>> ArrayList<Expression>(objects.length);
>> +
>> + for (Persistent object : objects) {
>> + pairs.add(matchObjectExp(object));
>> + }
>> +
>> + return joinExp(Expression.OR, pairs);
>> + }
>> }
>>
>> Modified:
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
>> URL:
>> http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java?rev=788657&r1=788656&r2=788657&view=diff
>>
>> ==============================================================================
>> ---
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
>> (original)
>> +++
>> cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
>> Fri Jun 26 11:25:15 2009
>>.. -20,11 +20,15 @@
>> package org.apache.cayenne.exp;
>>
>> import java.util.ArrayList;
>> +import java.util.Arrays;
>> import java.util.List;
>>
>> import org.apache.art.Artist;
>> import org.apache.art.Painting;
>> +import org.apache.cayenne.DataObjectUtils;
>> import org.apache.cayenne.ObjectContext;
>> +import org.apache.cayenne.Persistent;
>> +import org.apache.cayenne.query.SelectQuery;
>> import org.apache.cayenne.unit.CayenneCase;
>>
>> public class ExpressionFactoryTest extends CayenneCase {
>>.. -244,4 +248,35 @@
>> assertTrue(ExpressionFactory.inExp("paintingTitle",
>> "p1").match(p1));
>> assertFalse(ExpressionFactory.notInExp("paintingTitle",
>> "p3").match(p3));
>> }
>> +
>> + public void testMatchObject() {
>> + ObjectContext dc = createDataContext();
>> +
>> + Artist a1 = dc.newObject(Artist.class);
>> + a1.setArtistName("a1");
>> + Artist a2 = dc.newObject(Artist.class);
>> + a2.setArtistName("a2");
>> + Artist a3 = dc.newObject(Artist.class);
>> + a3.setArtistName("a3");
>> + dc.commitChanges();
>> +
>> + SelectQuery query = new SelectQuery(Artist.class);
>> +
>> + query.setQualifier(ExpressionFactory.matchObjectExp(a2));
>> + Object res = DataObjectUtils.objectForQuery(dc,
>> query);//exception if >1 result
>> + assertSame(res, a2);
>> + assertTrue(query.getQualifier().match(res));
>> +
>> + query.setQualifier(ExpressionFactory.matchObjectsExp(a1, a3));
>> + query.addOrdering("artistName", true);
>> + List<Persistent> list = dc.performQuery(query);
>> + assertEquals(list.size(), 2);
>> + assertSame(list.get(0), a1);
>> + assertSame(list.get(1), a3);
>> + assertTrue(query.getQualifier().match(a1));
>> + assertTrue(query.getQualifier().match(a3));
>> +
>> + assertEquals(query.getQualifier(),
>> + ExpressionFactory.matchObjectsExp(Arrays.asList(a1,
>> a3)));
>> + }
>> }
>>
>>
>>
>>
>
This archive was generated by hypermail 2.0.0 : Fri Jun 26 2009 - 07:39:12 EDT