Repository: cayenne
Updated Branches:
  refs/heads/master cfbfb6887 -> f9c7d647c


CAY-1968 SQLSelect cleanup and omissions

* also a unit test for SQLSelect


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/f9c7d647
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/f9c7d647
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/f9c7d647

Branch: refs/heads/master
Commit: f9c7d647c1f2b4add5132c78aa3fdb69ac79349f
Parents: cfbfb68
Author: aadamchik <aadamc...@apache.org>
Authored: Sat Nov 15 11:10:47 2014 +0300
Committer: aadamchik <aadamc...@apache.org>
Committed: Sat Nov 15 11:16:55 2014 +0300

----------------------------------------------------------------------
 .../apache/cayenne/query/BaseQueryMetadata.java |  4 +-
 .../org/apache/cayenne/query/SQLSelect.java     | 31 ++++---
 .../org/apache/cayenne/query/SQLSelectTest.java | 91 ++++++++++++++++++++
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |  1 +
 4 files changed, 115 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/f9c7d647/cayenne-server/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
----------------------------------------------------------------------
diff --git 
a/cayenne-server/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java 
b/cayenne-server/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
index 7f7c6dd..af402b4 100644
--- 
a/cayenne-server/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
+++ 
b/cayenne-server/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
@@ -43,6 +43,8 @@ import org.apache.cayenne.util.XMLSerializable;
  */
 class BaseQueryMetadata implements QueryMetadata, XMLSerializable, 
Serializable {
 
+       private static final long serialVersionUID = 5129792493303459115L;
+       
        int fetchLimit = QueryMetadata.FETCH_LIMIT_DEFAULT;
        int fetchOffset = QueryMetadata.FETCH_OFFSET_DEFAULT;
 
@@ -156,7 +158,7 @@ class BaseQueryMetadata implements QueryMetadata, 
XMLSerializable, Serializable
        void initWithProperties(Map<String, ?> properties) {
                // must init defaults even if properties are empty
                if (properties == null) {
-                       properties = Collections.EMPTY_MAP;
+                       properties = Collections.emptyMap();
                }
 
                Object fetchOffset = 
properties.get(QueryMetadata.FETCH_OFFSET_PROPERTY);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f9c7d647/cayenne-server/src/main/java/org/apache/cayenne/query/SQLSelect.java
----------------------------------------------------------------------
diff --git 
a/cayenne-server/src/main/java/org/apache/cayenne/query/SQLSelect.java 
b/cayenne-server/src/main/java/org/apache/cayenne/query/SQLSelect.java
index 5509cb4..b4504f4 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/query/SQLSelect.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/query/SQLSelect.java
@@ -19,6 +19,7 @@
 package org.apache.cayenne.query;
 
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -246,15 +247,14 @@ public class SQLSelect<T> extends IndirectQuery 
implements Select<T> {
         * running the query. This is a short-hand notation for:
         * 
         * <pre>
-        * query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
-        * query.setCacheGroups(&quot;group1&quot;, &quot;group2&quot;);
+        * query.cacheStrategy(QueryCacheStrategy.LOCAL_CACHE, cacheGroups);
         * </pre>
-        * 
-        * @since 4.0
         */
-       public void useLocalCache(String... cacheGroups) {
+       public SQLSelect<T> localCache(String... cacheGroups) {
                cacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
                cacheGroups(cacheGroups);
+
+               return this;
        }
 
        /**
@@ -262,11 +262,10 @@ public class SQLSelect<T> extends IndirectQuery 
implements Select<T> {
         * running the query. This is a short-hand notation for:
         * 
         * <pre>
-        * query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
-        * query.setCacheGroups(&quot;group1&quot;, &quot;group2&quot;);
+        * query.cacheStrategy(QueryCacheStrategy.SHARED_CACHE, cacheGroups);
         * </pre>
         */
-       public SQLSelect<T> useSharedCache(String... cacheGroups) {
+       public SQLSelect<T> sharedCache(String... cacheGroups) {
                return 
cacheStrategy(QueryCacheStrategy.SHARED_CACHE).cacheGroups(cacheGroups);
        }
 
@@ -274,13 +273,13 @@ public class SQLSelect<T> extends IndirectQuery 
implements Select<T> {
                return cacheStrategy;
        }
 
-       public SQLSelect<T> cacheStrategy(QueryCacheStrategy strategy) {
+       public SQLSelect<T> cacheStrategy(QueryCacheStrategy strategy, 
String... cacheGroups) {
                if (this.cacheStrategy != strategy) {
                        this.cacheStrategy = strategy;
                        this.replacementQuery = null;
                }
 
-               return this;
+               return cacheGroups(cacheGroups);
        }
 
        public String[] getCacheGroups() {
@@ -288,11 +287,21 @@ public class SQLSelect<T> extends IndirectQuery 
implements Select<T> {
        }
 
        public SQLSelect<T> cacheGroups(String... cacheGroups) {
-               this.cacheGroups = cacheGroups;
+               this.cacheGroups = cacheGroups != null && cacheGroups.length > 
0 ? cacheGroups : null;
                this.replacementQuery = null;
                return this;
        }
 
+       public SQLSelect<T> cacheGroups(Collection<String> cacheGroups) {
+
+               if (cacheGroups == null) {
+                       return cacheGroups((String) null);
+               }
+
+               String[] array = new String[cacheGroups.size()];
+               return cacheGroups(cacheGroups.toArray(array));
+       }
+
        /**
         * Returns a column name capitalization policy applied to selecting 
queries.
         * This is used to simplify mapping of the queries like "SELECT * FROM 
...",

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f9c7d647/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectTest.java
----------------------------------------------------------------------
diff --git 
a/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectTest.java 
b/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectTest.java
new file mode 100644
index 0000000..88e1b6a
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectTest.java
@@ -0,0 +1,91 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.query;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.util.Arrays;
+
+import org.apache.cayenne.DataRow;
+import org.junit.Test;
+
+public class SQLSelectTest {
+       
+       @Test
+       public void testCacheGroups_Collection() {
+               SQLSelect<DataRow> q = SQLSelect.dataRowQuery("bla");
+
+               assertNull(q.getCacheStrategy());
+               assertNull(q.getCacheGroups());
+
+               q.cacheGroups(Arrays.asList("a", "b"));
+               assertNull(q.getCacheStrategy());
+               assertArrayEquals(new String[] { "a", "b" }, 
q.getCacheGroups());
+       }
+
+       @Test
+       public void testCacheStrategy() {
+               SQLSelect<DataRow> q = SQLSelect.dataRowQuery("bla");
+
+               assertNull(q.getCacheStrategy());
+               assertNull(q.getCacheGroups());
+
+               q.cacheStrategy(QueryCacheStrategy.LOCAL_CACHE, "a", "b");
+               assertSame(QueryCacheStrategy.LOCAL_CACHE, 
q.getCacheStrategy());
+               assertArrayEquals(new String[] { "a", "b" }, 
q.getCacheGroups());
+
+               q.cacheStrategy(QueryCacheStrategy.SHARED_CACHE);
+               assertSame(QueryCacheStrategy.SHARED_CACHE, 
q.getCacheStrategy());
+               assertNull(q.getCacheGroups());
+       }
+       
+       @Test
+       public void testLocalCache() {
+               SQLSelect<DataRow> q = SQLSelect.dataRowQuery("bla");
+
+               assertNull(q.getCacheStrategy());
+               assertNull(q.getCacheGroups());
+
+               q.localCache("a", "b");
+               assertSame(QueryCacheStrategy.LOCAL_CACHE, 
q.getCacheStrategy());
+               assertArrayEquals(new String[] { "a", "b" }, 
q.getCacheGroups());
+
+               q.localCache();
+               assertSame(QueryCacheStrategy.LOCAL_CACHE, 
q.getCacheStrategy());
+               assertNull(q.getCacheGroups());
+       }
+       
+       @Test
+       public void testSharedCache() {
+               SQLSelect<DataRow> q = SQLSelect.dataRowQuery("bla");
+
+               assertNull(q.getCacheStrategy());
+               assertNull(q.getCacheGroups());
+
+               q.sharedCache("a", "b");
+               assertSame(QueryCacheStrategy.SHARED_CACHE, 
q.getCacheStrategy());
+               assertArrayEquals(new String[] { "a", "b" }, 
q.getCacheGroups());
+
+               q.sharedCache();
+               assertSame(QueryCacheStrategy.SHARED_CACHE, 
q.getCacheStrategy());
+               assertNull(q.getCacheGroups());
+       }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/f9c7d647/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt 
b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index ea1e1fd..86ec1c3 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -71,6 +71,7 @@ CAY-1962 Implement CayenneTable column resize on double-click 
on the header sepa
 CAY-1965 Change version from 3.2 to 4.0
 CAY-1966 SQLTemplate/SQLSelect positional parameter binding
 CAY-1967 Deprecate SQLTemplate parameter batches
+CAY-1968 SQLSelect cleanup and omissions
 
 Bug Fixes:
 

Reply via email to