BATCH-514:Additional small refactorings on Jdbc readers.
This commit is contained in:
@@ -435,6 +435,9 @@ public class JdbcCursorItemReader extends ExecutionContextUserSupport implements
|
||||
this.saveState = saveState;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private class BufferredResultSetReader implements ItemReader{
|
||||
|
||||
private ResultSet rs;
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.ColumnMapRowMapper;
|
||||
import org.springframework.jdbc.core.SqlTypeValue;
|
||||
import org.springframework.jdbc.core.StatementCreatorUtils;
|
||||
@@ -23,14 +24,14 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
* @see KeyMappingPreparedStatementSetter
|
||||
* @see ItemPreparedStatementSetter
|
||||
* @see ColumnMapRowMapper
|
||||
*/
|
||||
public class ColumnMapKeyMappingPreparedStatementSetter implements KeyMappingPreparedStatementSetter {
|
||||
public class ColumnMapItemPreparedStatementSetter implements ItemPreparedStatementSetter {
|
||||
|
||||
public void setValues(PreparedStatement ps, Object key) throws SQLException {
|
||||
Assert.isInstanceOf(Map.class, key, "Input to map PreparedStatement parameters must be of type Map.");
|
||||
Set keySet = ((Map)key).entrySet();
|
||||
public void setValues(Object item, PreparedStatement ps) throws SQLException {
|
||||
Assert.isInstanceOf(Map.class, item, "Input to map PreparedStatement parameters must be of type Map.");
|
||||
Set keySet = ((Map)item).entrySet();
|
||||
int counter = 1;
|
||||
for(Iterator it = keySet.iterator(); it.hasNext();){
|
||||
Entry entry = (Entry)it.next();
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ExecutionContextUserSupport;
|
||||
import org.springframework.batch.item.database.DrivingQueryItemReader;
|
||||
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
|
||||
import org.springframework.batch.item.database.KeyCollector;
|
||||
import org.springframework.jdbc.core.ColumnMapRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -51,7 +52,7 @@ public class MultipleColumnJdbcKeyCollector extends ExecutionContextUserSupport
|
||||
|
||||
private RowMapper keyMapper = new ColumnMapRowMapper();
|
||||
|
||||
private KeyMappingPreparedStatementSetter keyMappingSetter = new ColumnMapKeyMappingPreparedStatementSetter();
|
||||
private ItemPreparedStatementSetter preparedStatementSetter = new ColumnMapItemPreparedStatementSetter();
|
||||
|
||||
private String sql;
|
||||
|
||||
@@ -89,7 +90,7 @@ public class MultipleColumnJdbcKeyCollector extends ExecutionContextUserSupport
|
||||
|
||||
if (executionContext.size() > 0) {
|
||||
Object key = executionContext.get(getKey(CURRENT_KEY));
|
||||
return jdbcTemplate.query(restartSql, new PreparedStatementSetterKeyWrapper(key, keyMappingSetter), keyMapper);
|
||||
return jdbcTemplate.query(restartSql, new PreparedStatementSetterKeyWrapper(key, preparedStatementSetter), keyMapper);
|
||||
}
|
||||
else {
|
||||
return jdbcTemplate.query(sql, keyMapper);
|
||||
@@ -149,23 +150,23 @@ public class MultipleColumnJdbcKeyCollector extends ExecutionContextUserSupport
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
public void setKeyMappingSetter(
|
||||
KeyMappingPreparedStatementSetter keyMappingSetter) {
|
||||
this.keyMappingSetter = keyMappingSetter;
|
||||
public void setPreparedStatementSetter(
|
||||
ItemPreparedStatementSetter preparedStatementSetter) {
|
||||
this.preparedStatementSetter = preparedStatementSetter;
|
||||
}
|
||||
|
||||
private class PreparedStatementSetterKeyWrapper implements PreparedStatementSetter{
|
||||
|
||||
private Object key;
|
||||
private KeyMappingPreparedStatementSetter pss;
|
||||
private ItemPreparedStatementSetter pss;
|
||||
|
||||
public PreparedStatementSetterKeyWrapper(Object key, KeyMappingPreparedStatementSetter pss) {
|
||||
public PreparedStatementSetterKeyWrapper(Object key, ItemPreparedStatementSetter pss) {
|
||||
this.key = key;
|
||||
this.pss = pss;
|
||||
}
|
||||
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
pss.setValues(ps, key);
|
||||
pss.setValues(key, ps);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.springframework.core.CollectionFactory;
|
||||
*/
|
||||
public class ColumnMapExecutionContextRowMapperTests extends TestCase {
|
||||
|
||||
private ColumnMapKeyMappingPreparedStatementSetter mapper;
|
||||
private ColumnMapItemPreparedStatementSetter mapper;
|
||||
|
||||
private Map key;
|
||||
|
||||
@@ -28,7 +28,7 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
|
||||
super.setUp();
|
||||
|
||||
ps = (PreparedStatement)psControl.getMock();
|
||||
mapper = new ColumnMapKeyMappingPreparedStatementSetter();
|
||||
mapper = new ColumnMapItemPreparedStatementSetter();
|
||||
|
||||
key = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(2);
|
||||
key.put("1", new Integer(1));
|
||||
@@ -38,7 +38,7 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
|
||||
public void testSetValuesWithInvalidType() throws Exception {
|
||||
|
||||
try{
|
||||
mapper.setValues(ps, new Object());
|
||||
mapper.setValues(new Object(), ps);
|
||||
fail();
|
||||
}catch(IllegalArgumentException ex){
|
||||
//expected
|
||||
@@ -58,7 +58,7 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
|
||||
public void testCreateExecutionContextFromEmptyKeys() throws Exception {
|
||||
|
||||
psControl.replay();
|
||||
mapper.setValues(ps, new HashMap());
|
||||
mapper.setValues(new HashMap(), ps);
|
||||
psControl.verify();
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
|
||||
ps.setObject(1, new Integer(1));
|
||||
ps.setObject(2, new Integer(2));
|
||||
psControl.replay();
|
||||
mapper.setValues(ps, key);
|
||||
mapper.setValues(key, ps);
|
||||
psControl.verify();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user