cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r732699 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/query/ test/java/org/apache/cayenne/access/jdbc/ test/java/org/apache/cayenne/query/
Date Thu, 08 Jan 2009 13:16:47 GMT
Author: aadamchik
Date: Thu Jan  8 05:16:47 2009
New Revision: 732699

URL: http://svn.apache.org/viewvc?rev=732699&view=rev
Log:
CAY-1047 Aligning query capabilities

patch from Dima Loiko - support for caching of the EJBQL query results

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQueryMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java?rev=732699&r1=732698&r2=732699&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
Thu Jan  8 05:16:47 2009
@@ -43,7 +43,8 @@
 public class EJBQLTranslationContext {
 
     private EJBQLCompiledExpression compiledExpression;
-    private Map<Object, Object> parameters;
+    protected Map<String, Object> namedParameters;
+    protected Map<Integer, Object> positionalParameters;
     private EJBQLTranslatorFactory translatorFactory;
     private EntityResolver entityResolver;
     private List<Object> resultSetMetadata;
@@ -71,7 +72,8 @@
         this.compiledExpression = compiledExpression;
         this.resultSetMetadata = query.getMetaData(entityResolver).getResultSetMapping();
 
-        this.parameters = query.getParameters();
+        this.namedParameters = query.getNamedParameters();
+        this.positionalParameters = query.getPositionalParameters();
         this.translatorFactory = translatorFactory;
         this.usingAliases = true;
 
@@ -297,11 +299,11 @@
     }
 
     String bindPositionalParameter(int position) {
-        return bindParameter(parameters.get(Integer.valueOf(position)));
+        return bindParameter(positionalParameters.get(position));
     }
 
     String bindNamedParameter(String name) {
-        return bindParameter(parameters.get(name));
+        return bindParameter(namedParameters.get(name));
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java?rev=732699&r1=732698&r2=732699&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
Thu Jan  8 05:16:47 2009
@@ -38,7 +38,8 @@
 
     protected String name;
     protected String ejbqlStatement;
-    protected Map<Object, Object> parameters;
+    protected Map<String, Object> namedParameters;
+    protected Map<Integer, Object> positionalParameters;
 
     protected transient EJBQLCompiledExpression expression;
     EJBQLQueryMetadata metadata = new EJBQLQueryMetadata();
@@ -51,14 +52,36 @@
         metadata.resolve(resolver, this);
         return metadata;
     }
-
+    
     public boolean isFetchingDataRows() {
         return metadata.isFetchingDataRows();
     }
-
+    
+    
     public void setFetchingDataRows(boolean flag) {
         metadata.setFetchingDataRows(flag);
     }
+    
+    public String[] getCacheGroups() {
+        return metadata.getCacheGroups();
+    }
+    
+    public QueryCacheStrategy getCacheStrategy() {
+        return metadata.getCacheStrategy();
+    }
+
+
+    public void setCacheGroups(String... cacheGroups) {
+        this.metadata.setCacheGroups(cacheGroups);
+    }
+    
+    public void setCacheStrategy(QueryCacheStrategy strategy) {
+        metadata.setCacheStrategy(strategy);
+    }
+    
+
+
+
 
     public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery)
{
         DataMap map = getMetaData(resolver).getDataMap();
@@ -108,9 +131,16 @@
      * Returns unmodifiable map of combined named and positional parameters. Positional
      * parameter keys are Integers, while named parameter keys are strings.
      */
-    public Map<Object, Object> getParameters() {
-        return parameters != null
-                ? Collections.unmodifiableMap(parameters)
+    
+    public Map<String, Object> getNamedParameters() {
+        return namedParameters != null
+                ? Collections.unmodifiableMap(namedParameters)
+                : Collections.EMPTY_MAP;
+    }
+    
+    public Map<Integer, Object> getPositionalParameters() {
+        return positionalParameters != null
+                ? Collections.unmodifiableMap(positionalParameters)
                 : Collections.EMPTY_MAP;
     }
 
@@ -127,11 +157,11 @@
         // TODO: andrus, 6/12/2007 - validate against available query parameters - JPA
         // spec requires it.
 
-        if (parameters == null) {
-            parameters = new HashMap<Object, Object>();
+        if (namedParameters == null) {
+            namedParameters = new HashMap<String, Object>();
         }
 
-        parameters.put(name, object);
+        namedParameters.put(name, object);
     }
 
     /**
@@ -148,11 +178,11 @@
         // TODO: andrus, 6/12/2007 - validate against available query parameters - JPA
         // spec requires it.
 
-        if (parameters == null) {
-            parameters = new HashMap<Object, Object>();
+        if (positionalParameters == null) {
+            positionalParameters = new HashMap<Integer, Object>();
         }
 
-        parameters.put(Integer.valueOf(position), object);
+        positionalParameters.put(Integer.valueOf(position), object);
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQueryMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQueryMetadata.java?rev=732699&r1=732698&r2=732699&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQueryMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQueryMetadata.java
Thu Jan  8 05:16:47 2009
@@ -18,6 +18,11 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
 import org.apache.cayenne.ejbql.EJBQLCompiledExpression;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.map.ObjEntity;
@@ -38,7 +43,54 @@
 
         ObjEntity root = expression.getRootDescriptor().getEntity();
 
-        // TODO: andrus, 4/3/2007 - generate cache key based on EJBQL statement
-        return super.resolve(root, resolver, null);
+        if (super.resolve(root, resolver, null)) {
+
+            if (QueryCacheStrategy.NO_CACHE == getCacheStrategy()) {
+
+            }
+            else {
+
+                // create a unique key based on entity, EJBQL, and parameters
+                StringBuilder key = new StringBuilder();
+
+                if (query.getEjbqlStatement() != null) {
+                    key.append('/').append(query.getEjbqlStatement());
+                }
+
+                if (query.getFetchLimit() > 0) {
+                    key.append('/').append(query.getFetchLimit());
+                }
+
+                Map<String, Object> namedParameters = query.getNamedParameters();
+                if (!namedParameters.isEmpty()) {
+
+                    List<String> keys = new ArrayList<String>(namedParameters.keySet());
+                    Collections.sort(keys);
+                    for (String parameterKey : keys) {
+                        key.append('/').append(parameterKey).append('=').append(
+                                namedParameters.get(parameterKey));
+                    }
+                }
+
+                Map<Integer, Object> positionalParameters = query
+                        .getPositionalParameters();
+                if (!positionalParameters.isEmpty()) {
+
+                    List<Integer> keys = new ArrayList<Integer>(positionalParameters
+                            .keySet());
+                    Collections.sort(keys);
+                    for (Integer parameterKey : keys) {
+                        key.append('/').append(parameterKey).append('=').append(
+                                positionalParameters.get(parameterKey));
+                    }
+                }
+
+                this.cacheKey = key.toString();
+            }
+
+            return true;
+        }
+
+        return false;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java?rev=732699&r1=732698&r2=732699&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslatorTest.java
Thu Jan  8 05:16:47 2009
@@ -35,16 +35,17 @@
         return translateSelect(ejbql, Collections.EMPTY_MAP);
     }
 
+   
     private SQLTemplate translateSelect(
             String ejbql,
-            final Map<Object, Object> queryParameters) {
+            final Map<Integer, Object> queryParameters) {
         EJBQLParser parser = EJBQLParserFactory.getParser();
         EJBQLCompiledExpression select = parser.compile(ejbql, getDomain()
                 .getEntityResolver());
         EJBQLQuery query = new EJBQLQuery(ejbql) {
 
             @Override
-            public Map<Object, Object> getParameters() {
+            public Map<Integer, Object>  getPositionalParameters(){
                 return queryParameters;
             }
         };
@@ -55,6 +56,7 @@
         return tr.getQuery();
     }
 
+
     public void testSelectFrom() {
         SQLTemplate query = translateSelect("select a from Artist a");
         String sql = query.getDefaultTemplate();
@@ -232,7 +234,7 @@
     }
 
     public void testSelectPositionalParameters() {
-        Map<Object, Object> params = new HashMap<Object, Object>();
+        Map<Integer, Object> params = new HashMap<Integer, Object>();
         params.put(new Integer(1), "X");
         params.put(new Integer(2), "Y");
         SQLTemplate query = translateSelect(

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java?rev=732699&r1=732698&r2=732699&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
Thu Jan  8 05:16:47 2009
@@ -36,12 +36,55 @@
         query.setParameter(1, "X");
         query.setParameter("name", "Y");
 
-        Map parameters = query.getParameters();
-        assertEquals(2, parameters.size());
-        assertEquals("X", parameters.get(new Integer(1)));
+        Map parameters = query.getNamedParameters();
+        Map parameters1 = query.getPositionalParameters();
+        assertEquals(1, parameters.size());
+        assertEquals(1, parameters1.size());
+        assertEquals("X", parameters1.get(new Integer(1)));
         assertEquals("Y", parameters.get("name"));
     }
 
+    public void testCacheParameters() {
+        String ejbql1 = "select a FROM Artist a WHERE a.artistName = ?1 OR a.artistName =
:name";
+        EJBQLQuery q1 = new EJBQLQuery(ejbql1);
+        q1.setParameter(1, "X");
+        q1.setParameter("name", "Y");
+        q1.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
+
+        String ejbql2 = "select a FROM Artist a WHERE a.artistName = ?1 OR a.artistName =
:name";
+        EJBQLQuery q2 = new EJBQLQuery(ejbql2);
+        q2.setParameter(1, "X");
+        q2.setParameter("name", "Y");
+        q2.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
+
+        EntityResolver resolver = getDomain().getEntityResolver();
+
+        assertEquals(q1.getMetaData(resolver).getCacheKey(), q2
+                .getMetaData(resolver)
+                .getCacheKey());
+    }
+
+    public void testCacheStrategy() {
+        insertValue();
+        DataContext contex = createDataContext();
+        String ejbql = "select a FROM Artist a";
+        EJBQLQuery query = new EJBQLQuery(ejbql);
+        query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
+        List<Artist> artist1 = contex.performQuery(query);
+        blockQueries();
+        List<Artist> artist2;
+        try {
+            EJBQLQuery query1 = new EJBQLQuery(ejbql);
+            query1.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
+            artist2 = contex.performQuery(query1);
+        }
+        finally {
+            unblockQueries();
+        }
+
+        assertEquals(artist1.get(0).getArtistName(), artist2.get(0).getArtistName());
+    }
+
     public void testDataRows() {
         insertValue();
         String ejbql = "select a FROM Artist a";
@@ -57,7 +100,6 @@
                 (DataRow) row,
                 true);
         assertEquals("a0", artist.getArtistName());
-
     }
 
     private void insertValue() {
@@ -88,6 +130,22 @@
         assertEquals("XYZ", query.getName());
     }
 
+    public void testUniqueKeyEntity() {
+        // insertValue();
+        EntityResolver resolver = getDomain().getEntityResolver();
+        String ejbql = "select a FROM Artist a";
+
+        EJBQLQuery q1 = new EJBQLQuery(ejbql);
+        q1.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
+
+        EJBQLQuery q2 = new EJBQLQuery(ejbql);
+        q2.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
+
+        assertEquals(q1.getMetaData(resolver).getCacheKey(), q2
+                .getMetaData(resolver)
+                .getCacheKey());
+    }
+
     public void testGetMetadata() {
 
         EntityResolver resolver = getDomain().getEntityResolver();



Mime
View raw message