BATCH-377: A PreparedStatement can now be set on JdbcCursorItemReader. Also created a JobParametersPreparedStatementSetter, that when registered as a StepListener will set the values on a prepared statement with values from a PreparedStatement.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package org.springframework.batch.core.resource;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
|
||||
/**
|
||||
* Simple domain object for testing purposes.
|
||||
*/
|
||||
public class Foo {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private int value;
|
||||
|
||||
public Foo(){}
|
||||
|
||||
public Foo(int id, String name, int value) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Foo[id=" +id +",name=" + name + ",value=" + value + "]";
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return EqualsBuilder.reflectionEquals(this, obj);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.springframework.batch.core.resource;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
|
||||
public class FooRowMapper implements RowMapper {
|
||||
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
|
||||
return foo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.springframework.batch.core.resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.SimpleJob;
|
||||
import org.springframework.batch.core.step.tasklet.TaskletStep;
|
||||
import org.springframework.batch.item.database.JdbcCursorItemReader;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
public class JdbcCursorItemReaderPreparedStatementIntegrationTests extends
|
||||
AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
JdbcCursorItemReader itemReader;
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
super.onSetUpInTransaction();
|
||||
|
||||
itemReader = new JdbcCursorItemReader();
|
||||
itemReader.setDataSource(super.getJdbcTemplate().getDataSource());
|
||||
itemReader.setSql("select ID, NAME, VALUE from T_FOOS where ID > ? and ID < ?");
|
||||
itemReader.setIgnoreWarnings(true);
|
||||
itemReader.setVerifyCursorPosition(true);
|
||||
|
||||
itemReader.setMapper(new FooRowMapper());
|
||||
itemReader.setFetchSize(10);
|
||||
itemReader.setMaxRows(100);
|
||||
itemReader.setQueryTimeout(1000);
|
||||
itemReader.setSaveState(true);
|
||||
JobParametersPreparedStatementSetter pss = new JobParametersPreparedStatementSetter();
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", new Long(1)).addLong("end.id", new Long(4)).toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(new Long(1), jobParameters, new SimpleJob());
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, new Long(2));
|
||||
StepExecution stepExecution = new StepExecution(new TaskletStep(), jobExecution, new Long(3) );
|
||||
pss.beforeStep(stepExecution);
|
||||
|
||||
List parameterNames = new ArrayList();
|
||||
parameterNames.add("begin.id");
|
||||
parameterNames.add("end.id");
|
||||
pss.setParameterKeys(parameterNames);
|
||||
|
||||
itemReader.setPreparedStatementSetter(pss);
|
||||
}
|
||||
|
||||
public void testRead() throws Exception{
|
||||
|
||||
Foo foo = (Foo)itemReader.read();
|
||||
assertEquals(2, foo.getId());
|
||||
foo = (Foo)itemReader.read();
|
||||
assertEquals(3, foo.getId());
|
||||
assertNull(itemReader.read());
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "org/springframework/batch/core/resource/data-source-context.xml" };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2006-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.core.resource;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.SimpleJob;
|
||||
import org.springframework.batch.core.step.tasklet.TaskletStep;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JobParametersPreparedStatementSetterTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
JobParametersPreparedStatementSetter pss;
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(getClass(), "data-source-context.xml") };
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
super.onSetUpInTransaction();
|
||||
|
||||
pss = new JobParametersPreparedStatementSetter();
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", new Long(1)).addLong("end.id", new Long(4)).toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(new Long(1), jobParameters, new SimpleJob());
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, new Long(2));
|
||||
StepExecution stepExecution = new StepExecution(new TaskletStep(), jobExecution, new Long(3) );
|
||||
pss.beforeStep(stepExecution);
|
||||
jdbcTemplate = getJdbcTemplate();
|
||||
}
|
||||
|
||||
public void testSetValues(){
|
||||
|
||||
List parameterNames = new ArrayList();
|
||||
parameterNames.add("begin.id");
|
||||
parameterNames.add("end.id");
|
||||
pss.setParameterKeys(parameterNames);
|
||||
final List results = new ArrayList();
|
||||
jdbcTemplate.query("SELECT NAME from T_FOOS where ID > ? and ID < ?", pss, new RowCallbackHandler(){
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
results.add(rs.getString(1));
|
||||
}});
|
||||
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("bar2", results.get(0));
|
||||
assertEquals("bar3", results.get(1));
|
||||
}
|
||||
|
||||
public void testAfterPropertiesSet() throws Exception{
|
||||
try{
|
||||
pss.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch(IllegalArgumentException ex){
|
||||
//expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testNonExistentProperties(){
|
||||
|
||||
List parameterNames = new ArrayList();
|
||||
parameterNames.add("badParameter");
|
||||
parameterNames.add("end.id");
|
||||
pss.setParameterKeys(parameterNames);
|
||||
|
||||
try{
|
||||
jdbcTemplate.query("SELECT NAME from T_FOOS where ID > ? and ID < ?", pss, new RowCallbackHandler(){
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
fail();
|
||||
}});
|
||||
|
||||
fail();
|
||||
}catch(IllegalStateException ex){
|
||||
//expected
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user