|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
1 | 16 | package org.springframework.data.jdbc.core.convert;
|
2 | 17 |
|
3 | 18 | import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
|
14 | 29 | import org.springframework.jdbc.support.JdbcUtils;
|
15 | 30 | import org.springframework.jdbc.support.KeyHolder;
|
16 | 31 | import org.springframework.lang.Nullable;
|
| 32 | +import org.springframework.util.Assert; |
17 | 33 |
|
18 | 34 | import java.sql.PreparedStatement;
|
19 | 35 | import java.sql.ResultSet;
|
|
22 | 38 | import java.util.List;
|
23 | 39 | import java.util.Map;
|
24 | 40 |
|
| 41 | +/** |
| 42 | + * Counterpart to {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations} containing |
| 43 | + * methods for performing batch updates with generated keys. |
| 44 | + * |
| 45 | + * @author Chirag Tailor |
| 46 | + */ |
25 | 47 | public class BatchJdbcOperations {
|
26 | 48 | private final JdbcOperations jdbcOperations;
|
27 | 49 |
|
28 | 50 | public BatchJdbcOperations(JdbcOperations jdbcOperations) {
|
29 | 51 | this.jdbcOperations = jdbcOperations;
|
30 | 52 | }
|
31 | 53 |
|
32 |
| - @Nullable |
| 54 | + /** |
| 55 | + * Execute a batch using the supplied SQL statement with the batch of supplied arguments, |
| 56 | + * returning generated keys. |
| 57 | + * @param sql the SQL statement to execute |
| 58 | + * @param batchArgs the array of {@link SqlParameterSource} containing the batch of |
| 59 | + * arguments for the query |
| 60 | + * @param generatedKeyHolder a {@link KeyHolder} that will hold the generated keys |
| 61 | + * @return an array containing the numbers of rows affected by each update in the batch |
| 62 | + * (may also contain special JDBC-defined negative values for affected rows such as |
| 63 | + * {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED}) |
| 64 | + * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update |
| 65 | + * @see org.springframework.jdbc.support.GeneratedKeyHolder |
| 66 | + */ |
33 | 67 | int[] batchUpdate(String sql, SqlParameterSource[] batchArgs, KeyHolder generatedKeyHolder) {
|
34 | 68 | return batchUpdate(sql, batchArgs, generatedKeyHolder, null);
|
35 | 69 | }
|
36 | 70 |
|
37 |
| - @Nullable |
| 71 | + /** |
| 72 | + * Execute a batch using the supplied SQL statement with the batch of supplied arguments, |
| 73 | + * returning generated keys. |
| 74 | + * @param sql the SQL statement to execute |
| 75 | + * @param batchArgs the array of {@link SqlParameterSource} containing the batch of |
| 76 | + * arguments for the query |
| 77 | + * @param generatedKeyHolder a {@link KeyHolder} that will hold the generated keys |
| 78 | + * @param keyColumnNames names of the columns that will have keys generated for them |
| 79 | + * @return an array containing the numbers of rows affected by each update in the batch |
| 80 | + * (may also contain special JDBC-defined negative values for affected rows such as |
| 81 | + * {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED}) |
| 82 | + * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update |
| 83 | + * @see org.springframework.jdbc.support.GeneratedKeyHolder |
| 84 | + */ |
38 | 85 | public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs, KeyHolder generatedKeyHolder,
|
39 |
| - String[] keyColumnNames) { |
| 86 | + @Nullable String[] keyColumnNames) { |
| 87 | + |
40 | 88 | if (batchArgs.length == 0) {
|
41 | 89 | return new int[0];
|
42 | 90 | }
|
@@ -89,10 +137,13 @@ public int getBatchSize() {
|
89 | 137 | return rowsAffectedArray;
|
90 | 138 | }
|
91 | 139 | };
|
92 |
| - return jdbcOperations.execute(psc, preparedStatementCallback); |
| 140 | + int[] result = jdbcOperations.execute(psc, preparedStatementCallback); |
| 141 | + Assert.state(result != null, "No result array"); |
| 142 | + return result; |
93 | 143 | }
|
94 | 144 |
|
95 | 145 | private PreparedStatementCallback<Void> storeGeneratedKeys(KeyHolder generatedKeyHolder) {
|
| 146 | + |
96 | 147 | return ps -> {
|
97 | 148 | List<Map<String, Object>> generatedKeys = generatedKeyHolder.getKeyList();
|
98 | 149 | generatedKeys.clear();
|
|
0 commit comments