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

Add parameter supplier support to spring batch readers #727

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.session.ExecutorType;
Expand All @@ -41,6 +43,7 @@ public class MyBatisCursorItemReader<T> extends AbstractItemCountingItemStreamIt
private SqlSession sqlSession;

private Map<String, Object> parameterValues;
private Supplier<Map<String, Object>> parameterValuesSupplier;

private Cursor<T> cursor;
private Iterator<T> cursorIterator;
Expand All @@ -65,6 +68,8 @@ protected void doOpen() throws Exception {
parameters.putAll(parameterValues);
}

Optional.ofNullable(parameterValuesSupplier).map(Supplier::get).ifPresent(parameters::putAll);

kazuki43zoo marked this conversation as resolved.
Show resolved Hide resolved
sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
cursor = sqlSession.selectCursor(queryId, parameters);
cursorIterator = cursor.iterator();
Expand Down Expand Up @@ -121,4 +126,16 @@ public void setQueryId(String queryId) {
public void setParameterValues(Map<String, Object> parameterValues) {
this.parameterValues = parameterValues;
}

/**
* The parameter supplier used to get parameter values for the query execution.
*
* @param parameterValuesSupplier
* the supplier used to get values keyed by the parameter named used in the query string.
*
* @since 2.1.0
*/
kazuki43zoo marked this conversation as resolved.
Show resolved Hide resolved
public void setParameterValuesSupplier(Supplier<Map<String, Object>> parameterValuesSupplier) {
this.parameterValuesSupplier = parameterValuesSupplier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
Expand All @@ -47,6 +49,8 @@ public class MyBatisPagingItemReader<T> extends AbstractPagingItemReader<T> {

private Map<String, Object> parameterValues;

private Supplier<Map<String, Object>> parameterValuesSupplier;

public MyBatisPagingItemReader() {
setName(getShortName(MyBatisPagingItemReader.class));
}
Expand Down Expand Up @@ -81,6 +85,18 @@ public void setParameterValues(Map<String, Object> parameterValues) {
this.parameterValues = parameterValues;
}

/**
* The parameter supplier used to get parameter values for the query execution.
*
* @param parameterValuesSupplier
* the supplier used to get values keyed by the parameter named used in the query string.
*
* @since 2.1.0
*/
public void setParameterValuesSupplier(Supplier<Map<String, Object>> parameterValuesSupplier) {
this.parameterValuesSupplier = parameterValuesSupplier;
}

/**
* Check mandatory properties.
*
Expand All @@ -102,6 +118,7 @@ protected void doReadPage() {
if (parameterValues != null) {
parameters.putAll(parameterValues);
}
Optional.ofNullable(parameterValuesSupplier).map(Supplier::get).ifPresent(parameters::putAll);
parameters.put("_page", getPage());
parameters.put("_pagesize", getPageSize());
parameters.put("_skiprows", getPage() * getPageSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
Expand All @@ -35,6 +36,7 @@ public class MyBatisCursorItemReaderBuilder<T> {
private SqlSessionFactory sqlSessionFactory;
private String queryId;
private Map<String, Object> parameterValues;
private Supplier<Map<String, Object>> parameterValuesSupplier;
private Boolean saveState;
private Integer maxItemCount;

Expand Down Expand Up @@ -83,6 +85,24 @@ public MyBatisCursorItemReaderBuilder<T> parameterValues(Map<String, Object> par
return this;
}

/**
* Set the parameter supplier to be used to get parameters for the query execution.
*
* @param parameterValuesSupplier
* the parameter supplier to be used to get parameters for the query execution
*
* @return this instance for method chaining
*
* @see MyBatisCursorItemReader#setParameterValuesSupplier(Supplier)
*
* @since 2.1.0
*/
public MyBatisCursorItemReaderBuilder<T> parameterValuesSupplier(
Supplier<Map<String, Object>> parameterValuesSupplier) {
this.parameterValuesSupplier = parameterValuesSupplier;
return this;
}

/**
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should be persisted within
* the {@link org.springframework.batch.item.ExecutionContext} for restart purposes.
Expand Down Expand Up @@ -124,6 +144,7 @@ public MyBatisCursorItemReader<T> build() {
reader.setSqlSessionFactory(this.sqlSessionFactory);
reader.setQueryId(this.queryId);
reader.setParameterValues(this.parameterValues);
reader.setParameterValuesSupplier(this.parameterValuesSupplier);
Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
return reader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
Expand All @@ -35,6 +36,7 @@ public class MyBatisPagingItemReaderBuilder<T> {
private SqlSessionFactory sqlSessionFactory;
private String queryId;
private Map<String, Object> parameterValues;
private Supplier<Map<String, Object>> parameterValuesSupplier;
private Integer pageSize;
private Boolean saveState;
private Integer maxItemCount;
Expand Down Expand Up @@ -84,6 +86,24 @@ public MyBatisPagingItemReaderBuilder<T> parameterValues(Map<String, Object> par
return this;
}

/**
* Set the parameter supplier to be used to get parameters for the query execution.
*
* @param parameterValuesSupplier
* the parameter supplier to be used to get parameters for the query execution
*
* @return this instance for method chaining
*
* @see MyBatisPagingItemReader#setParameterValuesSupplier(Supplier)
*
* @since 2.1.0
*/
public MyBatisPagingItemReaderBuilder<T> parameterValuesSupplier(
Supplier<Map<String, Object>> parameterValuesSupplier) {
this.parameterValuesSupplier = parameterValuesSupplier;
return this;
}

/**
* The number of records to request per page/query. Defaults to 10. Must be greater than zero.
*
Expand Down Expand Up @@ -140,6 +160,7 @@ public MyBatisPagingItemReader<T> build() {
reader.setSqlSessionFactory(this.sqlSessionFactory);
reader.setQueryId(this.queryId);
reader.setParameterValues(this.parameterValues);
reader.setParameterValuesSupplier(this.parameterValuesSupplier);
Optional.ofNullable(this.pageSize).ifPresent(reader::setPageSize);
Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.session.ExecutorType;
Expand Down Expand Up @@ -56,7 +58,10 @@ void setUp() {

Mockito.when(this.sqlSessionFactory.openSession(ExecutorType.SIMPLE)).thenReturn(this.sqlSession);
Mockito.when(this.cursor.iterator()).thenReturn(getFoos().iterator());
Mockito.when(this.sqlSession.selectCursor("selectFoo", Collections.singletonMap("id", 1))).thenReturn(this.cursor);
Map<String, Object> parameters = new HashMap<>();
parameters.put("id", 1);
parameters.put("name", "Doe");
Mockito.when(this.sqlSession.selectCursor("selectFoo", parameters)).thenReturn(this.cursor);
}

@Test
Expand All @@ -67,6 +72,7 @@ void testConfiguration() throws Exception {
.sqlSessionFactory(this.sqlSessionFactory)
.queryId("selectFoo")
.parameterValues(Collections.singletonMap("id", 1))
.parameterValuesSupplier(() -> Collections.singletonMap("name", "Doe"))
.build();
// @formatter:on
itemReader.afterPropertiesSet();
Expand All @@ -93,6 +99,7 @@ void testConfigurationSaveStateIsFalse() throws Exception {
.sqlSessionFactory(this.sqlSessionFactory)
.queryId("selectFoo")
.parameterValues(Collections.singletonMap("id", 1))
.parameterValuesSupplier(() -> Collections.singletonMap("name", "Doe"))
.saveState(false)
.build();
// @formatter:on
Expand All @@ -118,6 +125,7 @@ void testConfigurationMaxItemCount() throws Exception {
.sqlSessionFactory(this.sqlSessionFactory)
.queryId("selectFoo")
.parameterValues(Collections.singletonMap("id", 1))
.parameterValuesSupplier(() -> Collections.singletonMap("name", "Doe"))
.maxItemCount(2)
.build();
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void setUp() {
Mockito.when(this.sqlSessionFactory.openSession(ExecutorType.BATCH)).thenReturn(this.sqlSession);
Map<String, Object> parameters = new HashMap<>();
parameters.put("id", 1);
parameters.put("name", "Doe");
parameters.put("_page", 0);
parameters.put("_pagesize", 10);
parameters.put("_skiprows", 0);
Expand All @@ -80,6 +81,7 @@ void testConfiguration() throws Exception {
.sqlSessionFactory(this.sqlSessionFactory)
.queryId("selectFoo")
.parameterValues(Collections.singletonMap("id", 1))
.parameterValuesSupplier(() -> Collections.singletonMap("name", "Doe"))
.build();
// @formatter:on
itemReader.afterPropertiesSet();
Expand All @@ -105,6 +107,7 @@ void testConfigurationSaveStateIsFalse() throws Exception {
.sqlSessionFactory(this.sqlSessionFactory)
.queryId("selectFoo")
.parameterValues(Collections.singletonMap("id", 1))
.parameterValuesSupplier(() -> Collections.singletonMap("name", "Doe"))
.saveState(false)
.build();
// @formatter:on
Expand All @@ -128,6 +131,7 @@ void testConfigurationMaxItemCount() throws Exception {
.sqlSessionFactory(this.sqlSessionFactory)
.queryId("selectFoo")
.parameterValues(Collections.singletonMap("id", 1))
.parameterValuesSupplier(() -> Collections.singletonMap("name", "Doe"))
.maxItemCount(2)
.build();
// @formatter:on
Expand All @@ -152,6 +156,7 @@ void testConfigurationPageSize() throws Exception {
.sqlSessionFactory(this.sqlSessionFactory)
.queryId("selectFoo")
.parameterValues(Collections.singletonMap("id", 1))
.parameterValuesSupplier(() -> Collections.singletonMap("name", "Doe"))
.pageSize(2)
.build();
// @formatter:on
Expand All @@ -160,6 +165,7 @@ void testConfigurationPageSize() throws Exception {
Map<String, Object> parameters = new HashMap<>();
parameters.put("id", 1);
parameters.put("_page", 0);
parameters.put("name", "Doe");
parameters.put("_pagesize", 2);
parameters.put("_skiprows", 0);
Mockito.when(this.sqlSession.selectList("selectFoo", parameters)).thenReturn(getFoos());
Expand Down