diff --git a/core/src/main/java/org/sql2o/Query.java b/core/src/main/java/org/sql2o/Query.java index 98f4b978..d5aa0bb7 100644 --- a/core/src/main/java/org/sql2o/Query.java +++ b/core/src/main/java/org/sql2o/Query.java @@ -52,6 +52,7 @@ public class Query implements AutoCloseable { private final Map> paramNameToIdxMap; private final Map parameters; private String parsedQuery; + private Integer fetchSize; private int maxBatchRecords = 0; private int currentBatchRecords = 0; @@ -127,6 +128,15 @@ public Query setName(String name) { return this; } + public Integer getFetchSize() { + return fetchSize; + } + + public Query setFetchSize(int fetchSize) { + this.fetchSize = fetchSize; + return this; + } + public ResultSetHandlerFactoryBuilder getResultSetHandlerFactoryBuilder() { if (resultSetHandlerFactoryBuilder == null) { resultSetHandlerFactoryBuilder = new DefaultResultSetHandlerFactoryBuilder(); @@ -435,6 +445,9 @@ private PreparedStatement buildPreparedStatement(boolean allowArrayParameters) { } else { preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery); } + if (preparedStatement != null && fetchSize != null) { + preparedStatement.setFetchSize(fetchSize); + } } catch(SQLException ex) { throw new Sql2oException(String.format("Error preparing statement - %s", ex.getMessage()), ex); } diff --git a/core/src/test/java/org/sql2o/ConnectionTest.java b/core/src/test/java/org/sql2o/ConnectionTest.java index a3b439a1..dac5853d 100644 --- a/core/src/test/java/org/sql2o/ConnectionTest.java +++ b/core/src/test/java/org/sql2o/ConnectionTest.java @@ -76,4 +76,29 @@ public boolean returnGeneratedKeysByDefault() { // check statement was closed verify(ps,times(1)).close(); } + + public void test_createQueryWithFetchSize() throws Throwable { + DataSource dataSource = mock(DataSource.class); + Connection jdbcConnection = mock(Connection.class); + when(jdbcConnection.isClosed()).thenReturn(false); + when(dataSource.getConnection()).thenReturn(jdbcConnection); + PreparedStatement ps = mock(PreparedStatement.class); + when(jdbcConnection.prepareStatement(anyString())).thenReturn(ps); + + Sql2o sql2o = new Sql2o(dataSource,new NoQuirks(){ + @Override + public boolean returnGeneratedKeysByDefault() { + return false; + } + }); + org.sql2o.Connection cn = new org.sql2o.Connection(sql2o,false); + cn.createQueryWithParams("select * from Users").setFetchSize(10).buildPreparedStatement(); + + verify(dataSource,times(1)).getConnection(); + verify(jdbcConnection).isClosed(); + verify(jdbcConnection,times(1)).prepareStatement("select * from Users"); + verify(ps, times(1)).setFetchSize(10); + // check statement still alive + verify(ps,never()).close(); + } } diff --git a/core/src/test/java/org/sql2o/reflect/AbstractFieldGetterFactoryTest.java b/core/src/test/java/org/sql2o/reflect/AbstractFieldGetterFactoryTest.java index 4fb328ca..e3b66f1c 100644 --- a/core/src/test/java/org/sql2o/reflect/AbstractFieldGetterFactoryTest.java +++ b/core/src/test/java/org/sql2o/reflect/AbstractFieldGetterFactoryTest.java @@ -59,11 +59,14 @@ public void testAllTypes() throws IllegalAccessException { Field[] fields = pojo.getClass().getDeclaredFields(); for (Field field : fields) { - Getter getter = fgf.newGetter(field); - assertSame(field.getType(),getter.getType()); + if (!field.isSynthetic()) { + field.setAccessible(true); + Getter getter = fgf.newGetter(field); + assertSame(field.getType(),getter.getType()); - Object val1 = field.get(pojo); - assertEquals(val1, getter.getProperty(pojo)); + Object val1 = field.get(pojo); + assertEquals(val1, getter.getProperty(pojo)); + } } } diff --git a/core/src/test/java/org/sql2o/reflect/AbstractFieldSetterFactoryTest.java b/core/src/test/java/org/sql2o/reflect/AbstractFieldSetterFactoryTest.java index c3b065bf..079a5a35 100644 --- a/core/src/test/java/org/sql2o/reflect/AbstractFieldSetterFactoryTest.java +++ b/core/src/test/java/org/sql2o/reflect/AbstractFieldSetterFactoryTest.java @@ -63,14 +63,17 @@ public void testAllTypes() throws IllegalAccessException { Field[] fields = pojo1.getClass().getDeclaredFields(); for (Field field : fields) { - Setter setter = fsf.newSetter(field); - assertSame(field.getType(),setter.getType()); - Object val1 = field.get(pojo1); - Object val2 = field.get(pojo2); - assertFalse(val1.equals(val2)); - setter.setProperty(pojo2,val1); - Object val3 = field.get(pojo2); - assertEquals(val1,val3); + if (!field.isSynthetic()) { + field.setAccessible(true); + Setter setter = fsf.newSetter(field); + assertSame(field.getType(),setter.getType()); + Object val1 = field.get(pojo1); + Object val2 = field.get(pojo2); + assertFalse(val1.equals(val2)); + setter.setProperty(pojo2,val1); + Object val3 = field.get(pojo2); + assertEquals(val1,val3); + } } assertEquals(pojo1,pojo2); @@ -78,20 +81,23 @@ public void testAllTypes() throws IllegalAccessException { // let's reset all values to NULL // primitive fields will not be affected for (Field field : fields) { - Setter setter = fsf.newSetter(field); - Object val1 = field.get(pojo1); - assertNotNull(val1); - - setter.setProperty(pojo1,null); - - Object val2 = field.get(pojo1); - if(!setter.getType().isPrimitive()){ - assertNull(val2); - continue; + if (!field.isSynthetic()) { + field.setAccessible(true); + Setter setter = fsf.newSetter(field); + Object val1 = field.get(pojo1); + assertNotNull(val1); + + setter.setProperty(pojo1,null); + + Object val2 = field.get(pojo1); + if(!setter.getType().isPrimitive()){ + assertNull(val2); + continue; + } + assertNotNull(val2); + // not affected + assertEquals(val1,val2); } - assertNotNull(val2); - // not affected - assertEquals(val1,val2); } pojo2._obj = null; assertEquals(pojo2,pojo1);