Skip to content

Commit ca3d389

Browse files
committed
refactor to use common reader
1 parent b1f10bf commit ca3d389

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.redis.riot;
2+
3+
import com.redis.riot.core.RiotException;
4+
import org.springframework.batch.item.database.JdbcCursorItemReader;
5+
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
6+
import org.springframework.jdbc.core.ColumnMapRowMapper;
7+
8+
import javax.sql.DataSource;
9+
import java.util.Map;
10+
11+
public class JdbcCursorItemReaderFactory {
12+
public static JdbcCursorItemReader<Map<String, Object>> createReader(String sql,
13+
DataSourceArgs dataSourceArgs,
14+
DatabaseReaderArgs readerArgs) {
15+
DataSource dataSource;
16+
try {
17+
dataSource = dataSourceArgs.dataSource();
18+
} catch (Exception e) {
19+
throw new RiotException(e);
20+
}
21+
22+
JdbcCursorItemReaderBuilder<Map<String, Object>> reader = new JdbcCursorItemReaderBuilder<>();
23+
reader.dataSource(dataSource);
24+
reader.sql(sql);
25+
reader.saveState(false);
26+
reader.rowMapper(new ColumnMapRowMapper());
27+
reader.fetchSize(readerArgs.getFetchSize());
28+
reader.maxRows(readerArgs.getMaxRows());
29+
reader.queryTimeout(Math.toIntExact(readerArgs.getQueryTimeout().getValue().toMillis()));
30+
reader.useSharedExtendedConnection(readerArgs.isUseSharedExtendedConnection());
31+
reader.verifyCursorPosition(readerArgs.isVerifyCursorPosition());
32+
if (readerArgs.getMaxItemCount() > 0) {
33+
reader.maxItemCount(readerArgs.getMaxItemCount());
34+
}
35+
reader.name(sql);
36+
return reader.build();
37+
}
38+
}

0 commit comments

Comments
 (0)