diff -r 0effb4e865dd -r eb7e656c22e8 src/net/lemnik/eodsql/impl/BatchUpdateMethodImplementation.java --- a/src/net/lemnik/eodsql/impl/BatchUpdateMethodImplementation.java Sat Sep 18 23:37:19 2010 +0200 +++ b/src/net/lemnik/eodsql/impl/BatchUpdateMethodImplementation.java Sun Sep 19 21:49:44 2010 +0200 @@ -32,9 +32,17 @@ private final ParameterViewFactory[] viewFactories; + private final int firstIndexOfFiniteCollection; + BatchUpdateMethodImplementation(final Method method) throws ParseException { super(method); viewFactories = createParameterViewFactories(method); + firstIndexOfFiniteCollection = findFirstIndexOfFiniteCollection(viewFactories); + if (firstIndexOfFiniteCollection == -1) { + throw new RuntimeException("Method '" + method.getDeclaringClass().getSimpleName() + + "." + method.getName() + + "' supposed to do batch update, but has no batch parameter."); + } } private Iterator[] createParameterViews(final Object[] parameters) { @@ -83,7 +91,7 @@ ctx, parameters); public boolean hasNext() { - return views[0].hasNext(); + return views[firstIndexOfFiniteCollection].hasNext(); } public Context next() { @@ -129,6 +137,17 @@ return factories; } + private static int findFirstIndexOfFiniteCollection(ParameterViewFactory[] viewFactories) { + for (int i = 0; i < viewFactories.length; ++i) + { + if (viewFactories[i].isFiniteCollection()) + { + return i; + } + } + return -1; + } + @Override protected Class[] getParameterTypes(final Method method) { final Type[] genericTypes = method.getGenericParameterTypes(); @@ -242,6 +261,11 @@ */ Collection createView(Object parameter); + /** + * Returns true if this view is a collection with a finite number of members. + */ + boolean isFiniteCollection(); + } static abstract class IteratingCollection @@ -288,6 +312,11 @@ }; } + public boolean isFiniteCollection() + { + return true; + } + } private static class CollectionViewFactory implements ParameterViewFactory { @@ -296,6 +325,11 @@ return ((Collection)parameter); } + public boolean isFiniteCollection() + { + return true; + } + } private static class SimpleParameterIteratorFactory @@ -320,5 +354,10 @@ }; } + public boolean isFiniteCollection() + { + return false; + } + } } diff -r 0effb4e865dd -r eb7e656c22e8 test/net/lemnik/eodsql/BatchUpdateTest.java --- a/test/net/lemnik/eodsql/BatchUpdateTest.java Sat Sep 18 23:37:19 2010 +0200 +++ b/test/net/lemnik/eodsql/BatchUpdateTest.java Sun Sep 19 21:49:44 2010 +0200 @@ -72,6 +72,17 @@ query.insertBatchCollectionUniformData(validation, data); } + private void insertDataBatchCollectionStartWithUniformData(int index, + final Collection validation) { + + for(int i = 0; i < 1000; i++) { + final SimpleObject object = newSimpleObject(i); + validation.add(object); + } + + query.insertBatchCollectionUniformData(index, validation); + } + public void testBatchUpdateArray() throws Exception { final Set validation = new HashSet(); @@ -149,4 +160,29 @@ objects.close(); } + public void testBatchUpdateCollectionStartWithUniformData() throws Exception { + final Set validation = new HashSet(); + + insertDataBatchCollectionStartWithUniformData(99, validation); + + final DataSet objects = query.getConnected(); + final ListIterator li = objects.listIterator(); + + assertFalse(li.hasPrevious()); + assertTrue(li.hasNext()); + assertEquals(0, li.nextIndex()); + assertEquals(-1, li.previousIndex()); + + final SimpleObject so = li.next(); + + assertNotNull(so); + + assertTrue(li.hasPrevious()); + assertTrue(li.hasNext()); + assertEquals(0, li.previousIndex()); + assertEquals(1, li.nextIndex()); + + objects.close(); + } + } diff -r 0effb4e865dd -r eb7e656c22e8 test/net/lemnik/eodsql/DataSetQuery.java --- a/test/net/lemnik/eodsql/DataSetQuery.java Sat Sep 18 23:37:19 2010 +0200 +++ b/test/net/lemnik/eodsql/DataSetQuery.java Sun Sep 19 21:49:44 2010 +0200 @@ -33,6 +33,10 @@ "VALUES(?{1.id}, ?{2}, ?{1.order})", batchUpdate = true) public void insertBatchCollectionUniformData(Collection object, String data); + @Update(sql = "INSERT INTO objects (id, index) " + + "VALUES(?{2.id}, ?{1})", batchUpdate = true) + public void insertBatchCollectionUniformData(int index, Collection object); + @Select(sql = "SELECT * FROM objects ORDER BY index", disconnected = true) public DataSet getDisconnected();