Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RowMapperResultSetExtractor |
|
| 1.6666666666666667;1.667 |
1 | package liquibase.executor.jvm; | |
2 | ||
3 | import java.sql.ResultSet; | |
4 | import java.sql.SQLException; | |
5 | import java.util.ArrayList; | |
6 | import java.util.List; | |
7 | ||
8 | /** | |
9 | * Adapter implementation of the ResultSetExtractor interface that delegates to a RowMapper which is supposed to create | |
10 | * an object for each row. Each object is added to the results List of this ResultSetExtractor. | |
11 | * <p/> | |
12 | * <p> | |
13 | * Useful for the typical case of one object per row in the database table. The number of entries in the results list | |
14 | * will match the number of rows. | |
15 | * <p/> | |
16 | * <p> | |
17 | * Note that a RowMapper object is typically stateless and thus reusable; just the RowMapperResultSetExtractor adapter | |
18 | * is stateful. | |
19 | * <p/> | |
20 | * <p> | |
21 | * A usage example with JdbcTemplate: | |
22 | * <p/> | |
23 | * | |
24 | * <pre class="code"> | |
25 | * JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object | |
26 | * RowMapper rowMapper = new UserRowMapper(); // reusable object | |
27 | * <p/> | |
28 | * List allUsers = (List) jdbcTemplate.query( | |
29 | * "select * from user", | |
30 | * new RowMapperResultSetExtractor(rowMapper, 10)); | |
31 | * <p/> | |
32 | * User user = (User) jdbcTemplate.queryForObject( | |
33 | * "select * from user where id=?", new Object[] {id}, | |
34 | * new RowMapperResultSetExtractor(rowMapper, 1)); | |
35 | * </pre> | |
36 | * <p/> | |
37 | * | |
38 | * @author Spring Framework | |
39 | * @see RowMapper | |
40 | * @see liquibase.executor.Executor | |
41 | */ | |
42 | @SuppressWarnings({ "unchecked" }) | |
43 | class RowMapperResultSetExtractor implements ResultSetExtractor { | |
44 | ||
45 | private final RowMapper rowMapper; | |
46 | ||
47 | private final int rowsExpected; | |
48 | ||
49 | /** | |
50 | * Create a new RowMapperResultSetExtractor. | |
51 | * | |
52 | * @param rowMapper | |
53 | * the RowMapper which creates an object for each row | |
54 | */ | |
55 | public RowMapperResultSetExtractor(RowMapper rowMapper) { | |
56 | 0 | this(rowMapper, 0); |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * Create a new RowMapperResultSetExtractor. | |
61 | * | |
62 | * @param rowMapper | |
63 | * the RowMapper which creates an object for each row | |
64 | * @param rowsExpected | |
65 | * the number of expected rows (just used for optimized collection handling) | |
66 | */ | |
67 | 0 | public RowMapperResultSetExtractor(RowMapper rowMapper, int rowsExpected) { |
68 | 0 | this.rowMapper = rowMapper; |
69 | 0 | this.rowsExpected = rowsExpected; |
70 | 0 | } |
71 | ||
72 | public Object extractData(ResultSet rs) throws SQLException { | |
73 | 0 | List results = (this.rowsExpected > 0 ? new ArrayList(this.rowsExpected) : new ArrayList()); |
74 | 0 | int rowNum = 0; |
75 | 0 | while (rs.next()) { |
76 | 0 | results.add(this.rowMapper.mapRow(rs, rowNum++)); |
77 | } | |
78 | 0 | return results; |
79 | } | |
80 | ||
81 | } |