cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r414755 - in /incubator/cayenne/main/trunk/cayenne/cayenne-java/src: cayenne/java/org/objectstyle/cayenne/query/ tests/java/org/objectstyle/cayenne/map/ tests/java/org/objectstyle/cayenne/query/
Date Fri, 16 Jun 2006 06:47:28 GMT
Author: aadamchik
Date: Thu Jun 15 23:47:27 2006
New Revision: 414755

URL: http://svn.apache.org/viewvc?rev=414755&view=rev
Log:
SQLTemplate.queryWithParameters() looses cache key (I can't log in to Jira due to Apache network
problems; I will enter the bug once this is resolved)

Modified:
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/query/SQLTemplate.java
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/map/MapLoaderLoadTst.java
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/query/SQLTemplateTst.java

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/query/SQLTemplate.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/query/SQLTemplate.java?rev=414755&r1=414754&r2=414755&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/query/SQLTemplate.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/query/SQLTemplate.java
Thu Jun 15 23:47:27 2006
@@ -70,6 +70,7 @@
 import org.objectstyle.cayenne.map.ObjEntity;
 import org.objectstyle.cayenne.map.Procedure;
 import org.objectstyle.cayenne.map.QueryBuilder;
+import org.objectstyle.cayenne.util.Util;
 import org.objectstyle.cayenne.util.XMLEncoder;
 import org.objectstyle.cayenne.util.XMLSerializable;
 
@@ -389,9 +390,24 @@
         query.selectInfo.copyFromInfo(this.selectInfo);
         query.setParameters(parameters);
 
-        // TODO: implement algorithm for building the name based on the original name and
-        // the hashcode of the map of parameters. This way query clone can take advantage
-        // of caching.
+        // The following algorithm is for building the new query name based
+        // on the original query name and a hashcode of the map of parameters.
+        // This way the query clone can take advantage of caching. Fixes
+        // problem reported in CAY-360.
+
+        if (!Util.isEmptyString(name)) {
+            StringBuffer buffer = new StringBuffer(name);
+
+            if (parameters != null) {
+                for (int i = 0; i < parameters.length; i++) {
+                    if (!parameters[i].isEmpty()) {
+                        buffer.append(parameters[i].hashCode());
+                    }
+                }
+            }
+
+            query.setName(buffer.toString());
+        }
 
         return query;
     }

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/map/MapLoaderLoadTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/map/MapLoaderLoadTst.java?rev=414755&r1=414754&r2=414755&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/map/MapLoaderLoadTst.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/map/MapLoaderLoadTst.java
Thu Jun 15 23:47:27 2006
@@ -62,6 +62,7 @@
 import org.objectstyle.art.Artist;
 import org.objectstyle.art.Gallery;
 import org.objectstyle.cayenne.query.Ordering;
+import org.objectstyle.cayenne.query.SQLTemplate;
 import org.objectstyle.cayenne.query.SelectQuery;
 import org.xml.sax.InputSource;
 
@@ -146,5 +147,9 @@
         assertEquals(1, queryWithPrefetch.getPrefetchTree().nonPhantomNodes().size());
         assertNotNull(queryWithPrefetch.getPrefetchTree().getNode(
                 Gallery.PAINTING_ARRAY_PROPERTY));
+        
+        SQLTemplate nonSelectingQuery = (SQLTemplate) map.getQuery("NonSelectingQuery");
+        assertNotNull(nonSelectingQuery);
+        assertEquals("NonSelectingQuery", nonSelectingQuery.getName());
     }
 }

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/query/SQLTemplateTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/query/SQLTemplateTst.java?rev=414755&r1=414754&r2=414755&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/query/SQLTemplateTst.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/query/SQLTemplateTst.java
Thu Jun 15 23:47:27 2006
@@ -70,6 +70,32 @@
  * @author Andrei Adamchik
  */
 public class SQLTemplateTst extends TestCase {
+    
+    public void testQueryWithParameters() {
+        SQLTemplate q1 = new SQLTemplate("E1", "SELECT");
+        q1.setName("QName");
+        EntityResolver resolver = new EntityResolver();
+
+        Query q2 = q1.queryWithParameters(Collections.EMPTY_MAP);
+        assertNotNull(q2);
+        assertNotSame(q1, q2);
+
+        assertEquals(q1.getName(), q1.getMetaData(resolver).getCacheKey());
+        assertEquals(q1.getName(), q2.getMetaData(resolver).getCacheKey());
+
+        Query q3 = q1.queryWithParameters(Collections.singletonMap("a", "b"));
+        assertNotNull(q3);
+        assertNotSame(q1, q3);
+        assertNotNull(q3.getMetaData(resolver).getCacheKey());
+        assertFalse(q1.getName().equals(q3.getMetaData(resolver).getCacheKey()));
+
+        Query q4 = q1.queryWithParameters(Collections.singletonMap("a", "b"));
+        assertNotNull(q4);
+        assertNotSame(q3, q4);
+        assertEquals(q3.getMetaData(resolver).getCacheKey(), q4
+                .getMetaData(resolver)
+                .getCacheKey());
+    }
 
     public void testSerializability() throws Exception {
         SQLTemplate o = new SQLTemplate("Test", "DO SQL");



Mime
View raw message