cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1561512 - in /cayenne/main/trunk/cayenne-server/src: main/java/org/apache/cayenne/access/trans/ main/java/org/apache/cayenne/exp/parser/ test/java/org/apache/cayenne/exp/ test/java/org/apache/cayenne/exp/parser/
Date Sun, 26 Jan 2014 16:27:24 GMT
Author: aadamchik
Date: Sun Jan 26 16:27:23 2014
New Revision: 1561512

URL: http://svn.apache.org/r1561512
Log:
CAY-1796

patch by Oleg Artyukevich of Jan 21, 2014

Modified:
    cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
    cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTList.java
    cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTScalar.java
    cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
    cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
    cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryTest.java

Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java?rev=1561512&r1=1561511&r2=1561512&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
(original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
Sun Jan 26 16:27:23 2014
@@ -271,6 +271,10 @@ public abstract class QueryAssemblerHelp
         if (val == null) {
             out.append("NULL");
         } else if (val instanceof Persistent) {
+            // TODO: see cay1796
+            // This check is unlikely to happen,
+            // since Expression got ObjectId from Persistent object.
+            // Left for future research.
             ObjectId id = ((Persistent) val).getObjectId();
 
             // check if this id is acceptable to be a parameter
@@ -293,6 +297,25 @@ public abstract class QueryAssemblerHelp
 
             // checks have been passed, use id value
             appendLiteralDirect(snap.get(snap.keySet().iterator().next()), attr, parentExpression);
+        } else if(val instanceof ObjectId){
+            
+            ObjectId id = (ObjectId)val;
+            
+            if (id.isTemporary()) {
+                throw new CayenneRuntimeException("Can't use NEW object as a query parameter.");
+            }
+
+            Map<String, Object> snap = id.getIdSnapshot();
+            if (snap.size() != 1) {
+                StringBuilder msg = new StringBuilder();
+                msg.append("Object must have a single primary key column ").append("to serve
as a query parameter. ")
+                        .append("This object has ").append(snap.size()).append(": ").append(snap);
+
+                throw new CayenneRuntimeException(msg.toString());
+            }
+
+            // checks have been passed, use id value
+            appendLiteralDirect(snap.get(snap.keySet().iterator().next()), attr, parentExpression);
         } else {
             appendLiteralDirect(val, attr, parentExpression);
         }

Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTList.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTList.java?rev=1561512&r1=1561511&r2=1561512&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTList.java
(original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTList.java
Sun Jan 26 16:27:23 2014
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.cayenne.Persistent;
 import org.apache.cayenne.exp.Expression;
 
 /**
@@ -174,7 +175,9 @@ public class ASTList extends SimpleNode 
         if (value == null) {
             this.values = null;
         } else if (value instanceof Object[]) {
-            this.values = (Object[]) value;
+            int size = ((Object[]) value).length;
+            this.values = new Object[size];
+            System.arraycopy((Object[]) value, 0, this.values, 0, size);
         } else if (value instanceof Collection) {
             this.values = ((Collection) value).toArray();
         } else if (value instanceof Iterator) {
@@ -189,8 +192,17 @@ public class ASTList extends SimpleNode 
             throw new IllegalArgumentException("Invalid value class '" + value.getClass().getName()
                     + "', expected null, Object[], Collection, Iterator");
         }
+        convertValues();
     }
-
+    
+    private void convertValues() {
+        for (int i = 0; i < values.length; i++) {
+            if (values[i] instanceof Persistent) {
+                values[i] = ((Persistent)values[i]).getObjectId();
+            }
+        }
+    }
+    
     @Override
     public void jjtClose() {
         super.jjtClose();

Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTScalar.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTScalar.java?rev=1561512&r1=1561511&r2=1561512&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTScalar.java
(original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ASTScalar.java
Sun Jan 26 16:27:23 2014
@@ -82,13 +82,14 @@ public class ASTScalar extends SimpleNod
         // Persistent processing is a hack for a rather special case of a single
         // column PK
         // object.. full implementation pending...
+        //
+        // cay1796 : change check for Persistent object by check for ObjectId
         Object scalar = value;
-        if (scalar instanceof Persistent) {
 
-            Persistent persistent = (Persistent) scalar;
-            ObjectId id = persistent.getObjectId();
-            if (!id.isTemporary() && id.getIdSnapshot().size() == 1) {
-                scalar = id.getIdSnapshot().values().iterator().next();
+        if(scalar instanceof ObjectId) {
+            ObjectId temp = (ObjectId)value;
+            if (!temp.isTemporary() && temp.getIdSnapshot().size() == 1) {
+                scalar = temp.getIdSnapshot().values().iterator().next();
             }
         }
 
@@ -96,7 +97,11 @@ public class ASTScalar extends SimpleNod
     }
 
     public void setValue(Object value) {
-        this.value = value;
+    	if (value instanceof Persistent){
+    		this.value = ((Persistent)value).getObjectId();
+    	} else {
+    		this.value = value; 
+    	}
     }
 
     public Object getValue() {

Modified: cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java?rev=1561512&r1=1561511&r2=1561512&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
(original)
+++ cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTest.java
Sun Jan 26 16:27:23 2014
@@ -252,6 +252,7 @@ public class ExpressionFactoryTest exten
     // CAY-416
     public void testCollectionMatch() {
         Artist artist = context.newObject(Artist.class);
+        artist.setArtistName("artist");
         Painting p1 = context.newObject(Painting.class), p2 = context
                 .newObject(Painting.class), p3 = context.newObject(Painting.class);
         p1.setPaintingTitle("p1");
@@ -259,6 +260,8 @@ public class ExpressionFactoryTest exten
         p3.setPaintingTitle("p3");
         artist.addToPaintingArray(p1);
         artist.addToPaintingArray(p2);
+        
+        context.commitChanges();
 
         assertTrue(ExpressionFactory.matchExp("paintingArray", p1).match(artist));
         assertFalse(ExpressionFactory.matchExp("paintingArray", p3).match(artist));

Modified: cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionTest.java?rev=1561512&r1=1561511&r2=1561512&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
(original)
+++ cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
Sun Jan 26 16:27:23 2014
@@ -237,13 +237,13 @@ public class ExpressionTest extends Serv
         Painting p1 = context.newObject(Painting.class);
         p1.setToArtist(a1);
         p1.setPaintingTitle("painting1");
+        
+        context.commitChanges();
 
         SelectQuery query = new SelectQuery(Painting.class);
         Expression e = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1);
         query.setQualifier(e);
 
-        context.commitChanges();
-
         assertNotSame(context2, context);
 
         List<Artist> objects = context2.performQuery(query);
@@ -263,8 +263,10 @@ public class ExpressionTest extends Serv
         assertTrue(ex2.match(objects.get(0)));
 
         Artist a2 = context.newObject(Artist.class);
-        a1.setArtistName("Equals");
+        a2.setArtistName("Equals");
 
+        context.commitChanges();
+        
         SelectQuery q = new SelectQuery(Painting.class);
         Expression ex = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a2);
         q.setQualifier(ex);

Modified: cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryTest.java?rev=1561512&r1=1561511&r2=1561512&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryTest.java
(original)
+++ cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionEvaluateInMemoryTest.java
Sun Jan 26 16:27:23 2014
@@ -190,7 +190,15 @@ public class ExpressionEvaluateInMemoryT
         Painting p1 = (Painting) context.newObject("Painting");
         Painting p2 = (Painting) context.newObject("Painting");
         Painting p3 = (Painting) context.newObject("Painting");
-
+        
+        a1.setArtistName("a1");
+        a2.setArtistName("a2");
+        p1.setPaintingTitle("p1");
+        p2.setPaintingTitle("p2");
+        p3.setPaintingTitle("p3");
+        
+        context.commitChanges();
+        
         p1.setToArtist(a1);
         p2.setToArtist(a2);
 



Mime
View raw message