cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnth...@apache.org
Subject svn commit: r1425408 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/Cayenne.java test/java/org/apache/cayenne/CayenneTest.java
Date Sun, 23 Dec 2012 05:25:33 GMT
Author: johnthuss
Date: Sun Dec 23 05:25:33 2012
New Revision: 1425408

URL: http://svn.apache.org/viewvc?rev=1425408&view=rev
Log:
Add Cayenne.objectForSelect following CAY-1294 (Generify Query)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/Cayenne.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/Cayenne.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/Cayenne.java?rev=1425408&r1=1425407&r2=1425408&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/Cayenne.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/Cayenne.java
Sun Dec 23 05:25:33 2012
@@ -29,6 +29,7 @@ import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.query.ObjectIdQuery;
 import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.Select;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.reflect.PropertyDescriptor;
 import org.apache.cayenne.reflect.PropertyUtils;
@@ -492,6 +493,16 @@ public final class Cayenne {
         return objects.get(0);
     }
 
+    /**
+     * Returns an object or a DataRow that is a result of a given query. If query returns
+     * more than one object, an exception is thrown. If query returns no objects, null is
+     * returned.
+     */
+    @SuppressWarnings("unchecked")
+	public static <T> T objectForSelect(ObjectContext context, Select<T> query)
{
+    	return (T) objectForQuery(context, query);
+    }
+    
     static ObjectId buildId(ObjectContext context, String objEntityName, Object pk) {
         if (pk == null) {
             throw new IllegalArgumentException("Null PK");

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneTest.java?rev=1425408&r1=1425407&r2=1425408&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneTest.java
Sun Dec 23 05:25:33 2012
@@ -19,6 +19,7 @@
 
 package org.apache.cayenne;
 
+import java.nio.channels.Selector;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -28,6 +29,7 @@ import java.util.Map;
 import org.apache.cayenne.dba.frontbase.FrontBaseAdapter;
 import org.apache.cayenne.dba.openbase.OpenBaseAdapter;
 import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.SQLResult;
 import org.apache.cayenne.query.CapsStrategy;
@@ -175,6 +177,18 @@ public class CayenneTest extends ServerC
         assertEquals("artist2", ((Artist) object).getArtistName());
     }
 
+    public void testObjectForSelect() throws Exception {
+        createOneArtist();
+
+        SelectQuery<Artist> query = SelectQuery.query(Artist.class, ExpressionFactory.matchDbExp("ARTIST_NAME",
"artist2"));
+
+        Artist object = Cayenne.objectForSelect(context, query);
+
+        assertNotNull(object);
+        assertTrue(object instanceof Artist);
+        assertEquals("artist2", ((Artist) object).getArtistName());
+    }
+    
     public void testObjectForQueryNoObject() throws Exception {
 
         ObjectId id = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, new Integer(



Mime
View raw message