cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject cayenne git commit: CAY-1995 | Improve Select queries API
Date Wed, 01 Apr 2015 10:07:55 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 9777372dc -> 8a5165edf


CAY-1995 | Improve Select queries API


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

Branch: refs/heads/master
Commit: 8a5165edfad0540ae214f0faf8c5baa9ed840f36
Parents: 9777372
Author: Savva Kolbachev <s.kolbachev@gmail.com>
Authored: Wed Apr 1 13:07:00 2015 +0300
Committer: Savva Kolbachev <s.kolbachev@gmail.com>
Committed: Wed Apr 1 13:07:00 2015 +0300

----------------------------------------------------------------------
 .../java/org/apache/cayenne/ObjectContext.java  | 13 +--
 .../java/org/apache/cayenne/ResultIterator.java |  3 +-
 .../org/apache/cayenne/query/ObjectSelect.java  | 44 +++++-----
 .../org/apache/cayenne/query/SQLSelect.java     | 42 ++++-----
 .../java/org/apache/cayenne/query/Select.java   | 46 ++++++++++
 .../org/apache/cayenne/query/SelectById.java    | 47 ++++++----
 .../org/apache/cayenne/query/SelectQuery.java   | 37 ++++++--
 .../cayenne/remote/IncrementalSelectQuery.java  | 31 ++++++-
 .../cayenne/query/ObjectSelect_RunIT.java       | 54 ++++++++++--
 .../org/apache/cayenne/query/SQLSelectIT.java   | 90 ++++++++++++++------
 .../org/apache/cayenne/query/SelectQueryIT.java | 85 +++++++++++++++---
 11 files changed, 372 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/main/java/org/apache/cayenne/ObjectContext.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/ObjectContext.java b/cayenne-server/src/main/java/org/apache/cayenne/ObjectContext.java
index a925bdd..5efb19a 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/ObjectContext.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/ObjectContext.java
@@ -19,15 +19,15 @@
 
 package org.apache.cayenne;
 
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.List;
-
 import org.apache.cayenne.graph.GraphManager;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.query.Query;
 import org.apache.cayenne.query.Select;
 
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+
 /**
  * A Cayenne object facade to a persistent store. Instances of ObjectContext are
  * used in the application code to access Cayenne persistence features.
@@ -206,8 +206,9 @@ public interface ObjectContext extends DataChannel, Serializable {
      * Creates a ResultIterator based on the provided query. It is usually
      * backed by an open result set and is useful for processing of large data
      * sets, preserving a constant memory footprint. The caller must wrap
-     * iteration in try/finally and close the ResultIterator explicitly. Or use
-     * {@link #iterate(Select, ResultIteratorCallback)} as an alternative.
+     * iteration in try/finally (or try-with-resources for Java 1.7 and higher) and
+     * close the ResultIterator explicitly.
+     * Or use {@link #iterate(Select, ResultIteratorCallback)} as an alternative.
      * 
      * @since 4.0
      */

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/main/java/org/apache/cayenne/ResultIterator.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/ResultIterator.java b/cayenne-server/src/main/java/org/apache/cayenne/ResultIterator.java
index 264b13e..060004f 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/ResultIterator.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/ResultIterator.java
@@ -19,6 +19,7 @@
 
 package org.apache.cayenne;
 
+import java.io.Closeable;
 import java.util.List;
 
 /**
@@ -29,7 +30,7 @@ import java.util.List;
  * {@link ObjectContext#iterate(org.apache.cayenne.query.Select, ResultIteratorCallback)}
  * method that handles resource management.
  */
-public interface ResultIterator<T> extends Iterable<T> {
+public interface ResultIterator<T> extends Iterable<T>, Closeable {
 
     /**
      * Returns all yet unread rows from ResultSet without closing it.

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/main/java/org/apache/cayenne/query/ObjectSelect.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/query/ObjectSelect.java b/cayenne-server/src/main/java/org/apache/cayenne/query/ObjectSelect.java
index 022bcf6..1c9f53b 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/query/ObjectSelect.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/query/ObjectSelect.java
@@ -18,21 +18,23 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.DataRow;
 import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.map.ObjEntity;
 
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
 /**
  * A selecting query providing chainable API. Can be viewed as an alternative to
  * {@link SelectQuery}.
@@ -597,7 +599,7 @@ public class ObjectSelect<T> extends IndirectQuery implements Select<T>
{
 		return cacheStrategy(QueryCacheStrategy.SHARED_CACHE, cacheGroups);
 	}
 
-	public String[] getCacheGroups() {
+    public String[] getCacheGroups() {
 		return cacheGroups;
 	}
 
@@ -652,26 +654,26 @@ public class ObjectSelect<T> extends IndirectQuery implements
Select<T> {
 		return prefetches;
 	}
 
-	/**
-	 * Selects objects using provided context. Essentially the inversion of
-	 * "ObjectContext.select(query)".
-	 */
-	public List<T> select(ObjectContext context) {
+    @Override
+    public List<T> select(ObjectContext context) {
 		return context.select(this);
 	}
 
-	/**
-	 * Selects a single object using provided context. The query is expected to
-	 * match zero or one object. It returns null if no objects were matched. If
-	 * query matched more than one object, {@link CayenneRuntimeException} is
-	 * thrown.
-	 * <p>
-	 * Essentially the inversion of "ObjectContext.selectOne(Select)".
-	 */
-	public T selectOne(ObjectContext context) {
+    @Override
+    public T selectOne(ObjectContext context) {
 		return context.selectOne(this);
 	}
 
+    @Override
+    public <T> void iterate(ObjectContext context, ResultIteratorCallback<T>
callback) {
+        context.iterate((Select<T>) this, callback);
+    }
+
+    @Override
+    public ResultIterator<T> iterator(ObjectContext context) {
+        return context.iterator(this);
+    }
+
 	/**
 	 * Selects a single object using provided context. The query itself can
 	 * match any number of objects, but will return only the first one. It

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/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 62b4547..2a51081 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
@@ -18,20 +18,22 @@
  ****************************************************************/
 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;
-import java.util.Map;
-
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.DataRow;
 import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.map.SQLResult;
 
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * A selecting query based on raw SQL and featuring fluent API.
  * 
@@ -112,27 +114,27 @@ public class SQLSelect<T> extends IndirectQuery implements Select<T>
{
 		this.pageSize = QueryMetadata.PAGE_SIZE_DEFAULT;
 	}
 
-	/**
-	 * Selects objects using provided context. Essentially the inversion of
-	 * "ObjectContext.select(query)".
-	 */
+    @Override
 	public List<T> select(ObjectContext context) {
 		return context.select(this);
 	}
 
-	/**
-	 * Selects a single object using provided context. The query is expected to
-	 * match zero or one object. It returns null if no objects were matched. If
-	 * query matched more than one object, {@link CayenneRuntimeException} is
-	 * thrown.
-	 * <p>
-	 * Essentially the inversion of "ObjectContext.selectOne(Select)".
-	 */
+    @Override
 	public T selectOne(ObjectContext context) {
 		return context.selectOne(this);
 	}
 
-	/**
+    @Override
+    public <T> void iterate(ObjectContext context, ResultIteratorCallback<T>
callback) {
+        context.iterate((Select<T>) this, callback);
+    }
+
+    @Override
+    public ResultIterator<T> iterator(ObjectContext context) {
+        return context.iterator(this);
+    }
+
+    /**
 	 * Selects a single object using provided context. The query itself can
 	 * match any number of objects, but will return only the first one. It
 	 * returns null if no objects were matched.

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/main/java/org/apache/cayenne/query/Select.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/query/Select.java b/cayenne-server/src/main/java/org/apache/cayenne/query/Select.java
index 06a6271..4cfe9f3 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/query/Select.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/query/Select.java
@@ -19,10 +19,56 @@
 
 package org.apache.cayenne.query;
 
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
+
+import java.util.List;
+
 /**
  * A common interface for grouping together different kinds of queries
  * that return results. 
  */
 public interface Select<T> extends Query {
 
+    /**
+     * Selects objects using provided context.
+     * <p>
+     * Essentially the inversion of "ObjectContext.select(Select)".
+     * @since 4.0
+     */
+    <T> List<T> select(ObjectContext context);
+
+    /**
+     * Selects a single object using provided context. The query is expected to
+     * match zero or one object. It returns null if no objects were matched. If
+     * query matched more than one object, {@link org.apache.cayenne.CayenneRuntimeException}
is
+     * thrown.
+     * <p>
+     * Essentially the inversion of "ObjectContext.selectOne(Select)".
+     */
+    <T> T selectOne(ObjectContext context);
+
+    /**
+     * Creates a ResultIterator based on the provided context and passes it to a
+     * callback for processing. The caller does not need to worry about closing
+     * the iterator. This method takes care of it.
+     * <p>
+     * Essentially the inversion of "ObjectContext.iterate(Select, ResultIteratorCallback)".
+     * @since 4.0
+     */
+    <T> void iterate(ObjectContext context, ResultIteratorCallback<T> callback);
+
+    /**
+     * Creates a ResultIterator based on the provided context. It is usually
+     * backed by an open result set and is useful for processing of large data
+     * sets, preserving a constant memory footprint. The caller must wrap
+     * iteration in try/finally (or try-with-resources for Java 1.7 and higher) and
+     * close the ResultIterator explicitly.
+     * Or use {@link #iterate(ObjectContext, ResultIteratorCallback)} as an alternative.
+     * <p>
+     * Essentially the inversion of "ObjectContext.iterator(Select)".
+     * @since 4.0
+     */
+    <T> ResultIterator<T> iterator(ObjectContext context);
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/main/java/org/apache/cayenne/query/SelectById.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/query/SelectById.java b/cayenne-server/src/main/java/org/apache/cayenne/query/SelectById.java
index 92060ad..4c3187f 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/query/SelectById.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/query/SelectById.java
@@ -18,20 +18,23 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
-import static java.util.Collections.singletonMap;
-import static org.apache.cayenne.exp.ExpressionFactory.matchAllDbExp;
-
-import java.util.Collection;
-import java.util.Map;
-
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.DataRow;
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.ObjectId;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.map.ObjEntity;
 
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import static java.util.Collections.singletonMap;
+import static org.apache.cayenne.exp.ExpressionFactory.matchAllDbExp;
+
 /**
  * A query to select single objects by id.
  * 
@@ -125,20 +128,32 @@ public class SelectById<T> extends IndirectQuery implements Select<T>
{
 		}
 	}
 
-	/**
-	 * Selects a single object using provided context. The query is expected to
-	 * match zero or one object. It returns null if no objects were matched. If
-	 * query matched more than one object, {@link CayenneRuntimeException} is
-	 * thrown. Since we are selecting by ID, multiple matched objects likely
-	 * indicate a database referential integrity problem.
-	 * <p>
-	 * Essentially the inversion of "ObjectContext.selectOne(Select)".
-	 */
+
+    @Override
+    public List<T> select(ObjectContext context) {
+        return context.select(this);
+    }
+
+    /**
+     * Since we are selecting by ID, multiple matched objects likely
+     * indicate a database referential integrity problem.
+     */
+    @Override
 	public T selectOne(ObjectContext context) {
 		return context.selectOne(this);
 	}
 
-	/**
+    @Override
+    public <T> void iterate(ObjectContext context, ResultIteratorCallback<T>
callback) {
+        context.iterate((Select<T>) this, callback);
+    }
+
+    @Override
+    public ResultIterator<T> iterator(ObjectContext context) {
+        return context.iterator(this);
+    }
+
+    /**
 	 * Instructs Cayenne to look for query results in the "local" cache when
 	 * running the query. This is a short-hand notation for:
 	 * 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQuery.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQuery.java b/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQuery.java
index 8618bed..fd446b8 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQuery.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQuery.java
@@ -19,13 +19,10 @@
 
 package org.apache.cayenne.query;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-
 import org.apache.cayenne.DataRow;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.map.DbEntity;
@@ -36,6 +33,12 @@ import org.apache.cayenne.map.Procedure;
 import org.apache.cayenne.util.XMLEncoder;
 import org.apache.cayenne.util.XMLSerializable;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
 /**
  * A query that selects persistent objects of a certain type or "raw data" (aka
  * DataRows). Supports expression qualifier, multiple orderings and a number of
@@ -304,7 +307,27 @@ public class SelectQuery<T> extends AbstractQuery implements ParameterizedQuery,
 		this.setQualifier(qualifier);
 	}
 
-	/**
+    @Override
+    public <T> List<T> select(ObjectContext context) {
+        return context.performQuery(this);
+    }
+
+    @Override
+    public T selectOne(ObjectContext context) {
+        return context.selectOne(this);
+    }
+
+    @Override
+    public <T> void iterate(ObjectContext context, ResultIteratorCallback<T>
callback) {
+        context.iterate((Select<T>) this, callback);
+    }
+
+    @Override
+    public ResultIterator<T> iterator(ObjectContext context) {
+        return context.iterator(this);
+    }
+
+    /**
 	 * @since 1.2
 	 */
 	@Override

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/main/java/org/apache/cayenne/remote/IncrementalSelectQuery.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/remote/IncrementalSelectQuery.java
b/cayenne-server/src/main/java/org/apache/cayenne/remote/IncrementalSelectQuery.java
index bf935df..451885c 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/remote/IncrementalSelectQuery.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/remote/IncrementalSelectQuery.java
@@ -18,10 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.remote;
 
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.access.IncrementalFaultList;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.map.DataMap;
@@ -42,6 +41,10 @@ import org.apache.cayenne.query.SortOrder;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.util.XMLEncoder;
 
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
 /**
  * A SelectQuery decorator that overrides the metadata to ensure that query result is
  * cached on the server, so that subranges could be retrieved at a later time. Note that
a
@@ -340,4 +343,24 @@ class IncrementalSelectQuery<T> extends SelectQuery<T> {
     public String toString() {
         return query.toString();
     }
+
+    @Override
+    public <T> List<T> select(ObjectContext context) {
+        return query.select(context);
+    }
+
+    @Override
+    public T selectOne(ObjectContext context) {
+        return query.selectOne(context);
+    }
+
+    @Override
+    public <T> void iterate(ObjectContext context, ResultIteratorCallback<T>
callback) {
+        query.iterate(context, callback);
+    }
+
+    @Override
+    public ResultIterator<T> iterator(ObjectContext context) {
+        return query.iterator(context);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/test/java/org/apache/cayenne/query/ObjectSelect_RunIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/query/ObjectSelect_RunIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/query/ObjectSelect_RunIT.java
index 17e128b..8c018aa 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/query/ObjectSelect_RunIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/query/ObjectSelect_RunIT.java
@@ -18,16 +18,10 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-
-import java.util.List;
-
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.DataRow;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.test.jdbc.DBHelper;
@@ -38,6 +32,14 @@ import org.apache.cayenne.unit.di.server.ServerCase;
 import org.apache.cayenne.unit.di.server.UseServerRuntime;
 import org.junit.Test;
 
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
 @UseServerRuntime(CayenneProjects.TESTMAP_PROJECT)
 public class ObjectSelect_RunIT extends ServerCase {
 
@@ -72,6 +74,42 @@ public class ObjectSelect_RunIT extends ServerCase {
 		assertEquals("artist14", a.getArtistName());
 	}
 
+    @Test
+    public void test_Iterate() throws Exception {
+        createArtistsDataSet();
+
+        final int[] count = new int[1];
+        ObjectSelect.query(Artist.class).iterate(context, new ResultIteratorCallback<Artist>()
{
+
+            @Override
+            public void next(Artist object) {
+                assertNotNull(object.getArtistName());
+                count[0]++;
+            }
+        });
+
+        assertEquals(20, count[0]);
+    }
+
+    @Test
+    public void test_Iterator() throws Exception {
+        createArtistsDataSet();
+
+        ResultIterator<Artist> it = ObjectSelect.query(Artist.class).iterator(context);
+
+        try {
+            int count = 0;
+
+            for (Artist a : it) {
+                count++;
+            }
+
+            assertEquals(20, count);
+        } finally {
+            it.close();
+        }
+    }
+
 	@Test
 	public void test_SelectDataRows() throws Exception {
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectIT.java b/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectIT.java
index 155eff4..8a5f209 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/query/SQLSelectIT.java
@@ -18,16 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.query;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.sql.Types;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 import org.apache.cayenne.DataRow;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.test.jdbc.DBHelper;
@@ -39,6 +32,16 @@ import org.apache.cayenne.unit.di.server.UseServerRuntime;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.sql.Types;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
 @UseServerRuntime(CayenneProjects.TESTMAP_PROJECT)
 public class SQLSelectIT extends ServerCase {
 
@@ -58,7 +61,7 @@ public class SQLSelectIT extends ServerCase {
                 .setColumnTypes(Types.INTEGER, Types.VARCHAR, Types.DECIMAL);
 	}
 
-    protected void createArtistsDataSet() throws Exception {
+    protected void createPaintingsDataSet() throws Exception {
         for (int i = 1; i <= 20; i++) {
             tPainting.insert(i, "painting" + i, 10000. * i);
         }
@@ -67,7 +70,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_DataRows_DataMapNameRoot() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<DataRow> q1 = SQLSelect.dataRowQuery("testmap", "SELECT * FROM PAINTING");
 		assertTrue(q1.isFetchingDataRows());
@@ -80,7 +83,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_DataRows_DefaultRoot() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<DataRow> q1 = SQLSelect.dataRowQuery("SELECT * FROM PAINTING");
 		assertTrue(q1.isFetchingDataRows());
@@ -93,7 +96,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_DataRows_ClassRoot() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<Painting> q1 = SQLSelect.query(Painting.class, "SELECT * FROM PAINTING").columnNameCaps(CapsStrategy.UPPER);
 		assertFalse(q1.isFetchingDataRows());
@@ -105,7 +108,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_DataRows_ClassRoot_Parameters() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<Painting> q1 = SQLSelect.query(Painting.class, "SELECT * FROM PAINTING
WHERE PAINTING_TITLE = #bind($a)");
 		q1.params("a", "painting3").columnNameCaps(CapsStrategy.UPPER);
@@ -118,7 +121,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_DataRows_ClassRoot_Bind() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<Painting> q1 = SQLSelect.query(Painting.class,
 				"SELECT * FROM PAINTING WHERE PAINTING_TITLE = #bind($a) OR PAINTING_TITLE = #bind($b)").columnNameCaps(CapsStrategy.UPPER);
@@ -145,7 +148,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_DataRows_FetchLimit() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<DataRow> q1 = SQLSelect.dataRowQuery("SELECT * FROM PAINTING");
 		q1.limit(5);
@@ -156,7 +159,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_DataRows_FetchOffset() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<DataRow> q1 = SQLSelect.dataRowQuery("SELECT * FROM PAINTING");
 		q1.offset(4);
@@ -167,7 +170,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_Append() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		SQLSelect<Painting> q1 = SQLSelect.query(Painting.class, "SELECT * FROM PAINTING")
 				.append(" WHERE PAINTING_TITLE = #bind($a)").params("a", "painting3").columnNameCaps(CapsStrategy.UPPER);
@@ -179,7 +182,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_Select() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		List<Painting> result = SQLSelect.query(Painting.class, "SELECT * FROM PAINTING WHERE
PAINTING_TITLE = #bind($a)")
 				.params("a", "painting3").columnNameCaps(CapsStrategy.UPPER).select(context);
@@ -190,7 +193,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_SelectOne() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
         Painting a = SQLSelect.query(Painting.class, "SELECT * FROM PAINTING WHERE PAINTING_TITLE
= #bind($a)")
 				.params("a", "painting3").columnNameCaps(CapsStrategy.UPPER).selectOne(context);
@@ -198,10 +201,47 @@ public class SQLSelectIT extends ServerCase {
 		assertEquals("painting3", a.getPaintingTitle());
 	}
 
+    @Test
+    public void test_Iterate() throws Exception {
+        createPaintingsDataSet();
+
+        final int[] count = new int[1];
+        SQLSelect.query(Painting.class, "SELECT * FROM PAINTING").columnNameCaps(CapsStrategy.UPPER)
+                .iterate(context, new ResultIteratorCallback<Painting>() {
+                    @Override
+                    public void next(Painting object) {
+                        assertNotNull(object.getPaintingTitle());
+                        count[0]++;
+                    }
+                });
+
+        assertEquals(20, count[0]);
+    }
+
+    @Test
+    public void test_Iterator() throws Exception {
+        createPaintingsDataSet();
+
+        ResultIterator<Painting> it = SQLSelect.query(Painting.class, "SELECT * FROM
PAINTING")
+                .columnNameCaps(CapsStrategy.UPPER).iterator(context);
+
+        try {
+            int count = 0;
+
+            for (Painting p : it) {
+                count++;
+            }
+
+            assertEquals(20, count);
+        } finally {
+            it.close();
+        }
+    }
+
 	@Test
 	public void test_SelectLong() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		long id = SQLSelect.scalarQuery(Integer.class, "SELECT PAINTING_ID FROM PAINTING WHERE
PAINTING_TITLE = #bind($a)")
 				.params("a", "painting3").selectOne(context);
@@ -212,7 +252,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_SelectLongArray() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		List<Integer> ids = SQLSelect.scalarQuery(Integer.class, "SELECT PAINTING_ID FROM
PAINTING ORDER BY PAINTING_ID").select(
 				context);
@@ -224,7 +264,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_SelectCount() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		int c = SQLSelect.scalarQuery(Integer.class, "SELECT #result('COUNT(*)' 'int') FROM PAINTING").selectOne(context);
 
@@ -234,7 +274,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_ParamsArray_Single() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
         Integer id = SQLSelect.scalarQuery(Integer.class, "SELECT PAINTING_ID FROM PAINTING
WHERE PAINTING_TITLE = #bind($a)")
 				.paramsArray("painting3").selectOne(context);
@@ -245,7 +285,7 @@ public class SQLSelectIT extends ServerCase {
 	@Test
 	public void test_ParamsArray_Multiple() throws Exception {
 
-		createArtistsDataSet();
+		createPaintingsDataSet();
 
 		List<Integer> ids = SQLSelect
 				.scalarQuery(Integer.class,

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8a5165ed/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java b/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
index 9becc33..cf532f4 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
@@ -19,21 +19,11 @@
 
 package org.apache.cayenne.query;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import java.sql.Types;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
 import org.apache.cayenne.Cayenne;
 import org.apache.cayenne.DataRow;
 import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.ResultIteratorCallback;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
@@ -55,6 +45,18 @@ import org.apache.cayenne.unit.di.server.UseServerRuntime;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.sql.Types;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
 @UseServerRuntime(CayenneProjects.TESTMAP_PROJECT)
 public class SelectQueryIT extends ServerCase {
 
@@ -488,6 +490,65 @@ public class SelectQueryIT extends ServerCase {
 		assertEquals(1, objects.size());
 	}
 
+    @Test
+    public void testSelect() throws Exception {
+        createArtistsDataSet();
+
+        SelectQuery query = new SelectQuery(Artist.class);
+        List<?> objects = query.select(context);
+        assertEquals(20, objects.size());
+    }
+
+    @Test
+    public void testSelectOne() throws Exception {
+        createArtistsDataSet();
+
+        SelectQuery query = new SelectQuery(Artist.class);
+        Expression qual = ExpressionFactory.matchExp("artistName", "artist1");
+        query.setQualifier(qual);
+
+        Artist artist = (Artist) query.selectOne(context);
+        assertEquals("artist1", artist.getArtistName());
+    }
+
+    @Test
+    public void testIterate() throws Exception {
+        createArtistsDataSet();
+
+        SelectQuery<Artist> q1 = new SelectQuery<Artist>(Artist.class);
+        final int[] count = new int[1];
+        q1.iterate(context, new ResultIteratorCallback<Artist>() {
+
+            @Override
+            public void next(Artist object) {
+                assertNotNull(object.getArtistName());
+                count[0]++;
+            }
+        });
+
+        assertEquals(20, count[0]);
+    }
+
+    @Test
+    public void testIterator() throws Exception {
+        createArtistsDataSet();
+
+        SelectQuery<Artist> q1 = new SelectQuery<Artist>(Artist.class);
+        ResultIterator<Artist> it = q1.iterator(context);
+
+        try {
+            int count = 0;
+
+            for (Artist a : it) {
+                count++;
+            }
+
+            assertEquals(20, count);
+        } finally {
+            it.close();
+        }
+    }
+
 	/**
 	 * Tests that all queries specified in prefetch are executed in a more
 	 * complex prefetch scenario.


Mime
View raw message