|
1 | 1 | package com.redis.riot;
|
2 | 2 |
|
3 |
| -import com.redis.riot.core.RiotException; |
4 |
| -import org.springframework.batch.item.database.JdbcCursorItemReader; |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import javax.sql.DataSource; |
| 6 | + |
5 | 7 | import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
|
6 | 8 | import org.springframework.jdbc.core.ColumnMapRowMapper;
|
| 9 | +import org.springframework.util.Assert; |
7 | 10 |
|
8 |
| -import javax.sql.DataSource; |
9 |
| -import java.util.Map; |
| 11 | +import com.redis.riot.core.RiotException; |
10 | 12 |
|
11 | 13 | 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 |
| - } |
| 14 | + |
| 15 | + public static JdbcCursorItemReaderBuilder<Map<String, Object>> create(String sql, DataSourceArgs dataSourceArgs, |
| 16 | + DatabaseReaderArgs readerArgs) { |
| 17 | + Assert.hasLength(sql, "No SQL statement specified"); |
| 18 | + DataSource dataSource; |
| 19 | + try { |
| 20 | + dataSource = dataSourceArgs.dataSource(); |
| 21 | + } catch (Exception e) { |
| 22 | + throw new RiotException(e); |
| 23 | + } |
| 24 | + |
| 25 | + JdbcCursorItemReaderBuilder<Map<String, Object>> reader = new JdbcCursorItemReaderBuilder<>(); |
| 26 | + reader.dataSource(dataSource); |
| 27 | + reader.sql(sql); |
| 28 | + reader.saveState(false); |
| 29 | + reader.rowMapper(new ColumnMapRowMapper()); |
| 30 | + reader.fetchSize(readerArgs.getFetchSize()); |
| 31 | + reader.maxRows(readerArgs.getMaxRows()); |
| 32 | + reader.queryTimeout(Math.toIntExact(readerArgs.getQueryTimeout().getValue().toMillis())); |
| 33 | + reader.useSharedExtendedConnection(readerArgs.isUseSharedExtendedConnection()); |
| 34 | + reader.verifyCursorPosition(readerArgs.isVerifyCursorPosition()); |
| 35 | + if (readerArgs.getMaxItemCount() > 0) { |
| 36 | + reader.maxItemCount(readerArgs.getMaxItemCount()); |
| 37 | + } |
| 38 | + reader.name(sql); |
| 39 | + return reader; |
| 40 | + } |
| 41 | + |
38 | 42 | }
|
0 commit comments