Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing prepared statement fetch size parameter #311

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
13 changes: 13 additions & 0 deletions core/src/main/java/org/sql2o/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class Query implements AutoCloseable {
private final Map<String, List<Integer>> paramNameToIdxMap;
private final Map<String, ParameterSetter> parameters;
private String parsedQuery;
private Integer fetchSize;
private int maxBatchRecords = 0;
private int currentBatchRecords = 0;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/org/sql2o/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,41 @@ 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);

// 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);
Expand Down