Skip to content

Commit e5d6272

Browse files
committed
Add documentation, update license headers, and update class headers.
1 parent e080542 commit e5d6272

File tree

38 files changed

+481
-62
lines changed

38 files changed

+481
-62
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/AggregateChangeExecutor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
*
2929
* @author Jens Schauder
3030
* @author Myeonghyeon Lee
31+
* @author Chirag Tailor
3132
* @since 2.0
3233
*/
3334
class AggregateChangeExecutor {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateChangeExecutionContext.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 the original author or authors.
2+
* Copyright 2019-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@
5353
* @author Jens Schauder
5454
* @author Umut Erturk
5555
* @author Myeonghyeon Lee
56+
* @author Chirag Tailor
5657
*/
5758
class JdbcAggregateChangeExecutionContext {
5859

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
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+
*/
116
package org.springframework.data.jdbc.core.convert;
217

318
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
419

20+
/**
21+
* Strategy for executing a batch insert.
22+
*
23+
* @author Chirag Tailor
24+
*/
525
interface BatchInsertStrategy {
26+
27+
/**
28+
* @param sql the insert sql. Must not be {@code null}.
29+
* @param sqlParameterSources the sql parameters for each record to be inserted. Must not be {@code null}.
30+
* @return the ids corresponding to each record that was inserted, if ids were generated. If ids were not generated,
31+
* elements will be {@code null}.
32+
*/
633
Object[] execute(String sql, SqlParameterSource[] sqlParameterSources);
734
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BatchJdbcOperations.java

+55-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package org.springframework.data.jdbc.core.convert;
217

318
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
@@ -14,6 +29,7 @@
1429
import org.springframework.jdbc.support.JdbcUtils;
1530
import org.springframework.jdbc.support.KeyHolder;
1631
import org.springframework.lang.Nullable;
32+
import org.springframework.util.Assert;
1733

1834
import java.sql.PreparedStatement;
1935
import java.sql.ResultSet;
@@ -22,21 +38,53 @@
2238
import java.util.List;
2339
import java.util.Map;
2440

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+
*/
2547
public class BatchJdbcOperations {
2648
private final JdbcOperations jdbcOperations;
2749

2850
public BatchJdbcOperations(JdbcOperations jdbcOperations) {
2951
this.jdbcOperations = jdbcOperations;
3052
}
3153

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+
*/
3367
int[] batchUpdate(String sql, SqlParameterSource[] batchArgs, KeyHolder generatedKeyHolder) {
3468
return batchUpdate(sql, batchArgs, generatedKeyHolder, null);
3569
}
3670

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+
*/
3885
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs, KeyHolder generatedKeyHolder,
39-
String[] keyColumnNames) {
86+
@Nullable String[] keyColumnNames) {
87+
4088
if (batchArgs.length == 0) {
4189
return new int[0];
4290
}
@@ -89,10 +137,13 @@ public int getBatchSize() {
89137
return rowsAffectedArray;
90138
}
91139
};
92-
return jdbcOperations.execute(psc, preparedStatementCallback);
140+
int[] result = jdbcOperations.execute(psc, preparedStatementCallback);
141+
Assert.state(result != null, "No result array");
142+
return result;
93143
}
94144

95145
private PreparedStatementCallback<Void> storeGeneratedKeys(KeyHolder generatedKeyHolder) {
146+
96147
return ps -> {
97148
List<Map<String, Object>> generatedKeys = generatedKeyHolder.getKeyList();
98149
generatedKeys.clear();

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/CascadingDataAccessStrategy.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
* @author Tyler Van Gorder
3636
* @author Milan Milanov
3737
* @author Myeonghyeon Lee
38+
* @author Chirag Tailor
3839
* @since 1.1
3940
*/
4041
public class CascadingDataAccessStrategy implements DataAccessStrategy {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DataAccessStrategy.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 the original author or authors.
2+
* Copyright 2019-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,25 +36,37 @@
3636
* @author Tyler Van Gorder
3737
* @author Milan Milanov
3838
* @author Myeonghyeon Lee
39+
* @author Chirag Tailor
3940
*/
4041
public interface DataAccessStrategy extends RelationResolver {
4142

4243
/**
43-
* Inserts a the data of a single entity. Referenced entities don't get handled.
44+
* Inserts the data of a single entity. Referenced entities don't get handled.
4445
*
4546
* @param <T> the type of the instance.
4647
* @param instance the instance to be stored. Must not be {@code null}.
4748
* @param domainType the type of the instance. Must not be {@code null}.
4849
* @param identifier information about data that needs to be considered for the insert but which is not part of the
4950
* entity. Namely references back to a parent entity and key/index columns for entities that are stored in a
5051
* {@link Map} or {@link List}.
51-
* @param includeId
52+
* @param includeId an indicator of whether the insert includes the id.
5253
* @return the id generated by the database if any.
5354
* @since 1.1
5455
*/
5556
@Nullable
5657
<T> Object insert(T instance, Class<T> domainType, Identifier identifier, boolean includeId);
5758

59+
/**
60+
* Inserts the data of a multiple entities.
61+
*
62+
* @param <T> the type of the instance.
63+
* @param insertSubjects the subjects to be inserted, where each subject contains the instance and its identifier.
64+
* Must not be {@code null}.
65+
* @param domainType the type of the instance. Must not be {@code null}.
66+
* @param includeId an indicator of whether the insert includes the id.
67+
* @return the ids corresponding to each record that was inserted, if ids were generated. If ids were not generated,
68+
* elements will be {@code null}.
69+
*/
5870
<T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, boolean includeId);
5971

6072
/**

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@
5454
* @author Myeonghyeon Lee
5555
* @author Yunyoung LEE
5656
* @author Radim Tlusty
57+
* @author Chirag Tailor
5758
* @since 1.1
5859
*/
5960
public class DefaultDataAccessStrategy implements DataAccessStrategy {
@@ -75,8 +76,8 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
7576
* @since 1.1
7677
*/
7778
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
78-
JdbcConverter converter, NamedParameterJdbcOperations operations, SqlParametersFactory sqlParametersFactory,
79-
InsertStrategyFactory insertStrategyFactory) {
79+
JdbcConverter converter, NamedParameterJdbcOperations operations, SqlParametersFactory sqlParametersFactory,
80+
InsertStrategyFactory insertStrategyFactory) {
8081
Assert.notNull(sqlGeneratorSource, "SqlGeneratorSource must not be null");
8182
Assert.notNull(context, "RelationalMappingContext must not be null");
8283
Assert.notNull(converter, "JdbcConverter must not be null");

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DelegatingDataAccessStrategy.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
* @author Tyler Van Gorder
3333
* @author Milan Milanov
3434
* @author Myeonghyeon Lee
35+
* @author Chirag Tailor
3536
* @since 1.1
3637
*/
3738
public class DelegatingDataAccessStrategy implements DataAccessStrategy {
@@ -44,7 +45,7 @@ public class DelegatingDataAccessStrategy implements DataAccessStrategy {
4445
*/
4546
@Override
4647
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier, boolean includeId) {
47-
return delegate.insert(instance, domainType, identifier, false);
48+
return delegate.insert(instance, domainType, identifier, includeId);
4849
}
4950

5051
@Override

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/IdGeneratingBatchInsertStrategy.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package org.springframework.data.jdbc.core.convert;
217

318
import java.util.Arrays;
@@ -12,6 +27,12 @@
1227
import org.springframework.jdbc.support.GeneratedKeyHolder;
1328
import org.springframework.lang.Nullable;
1429

30+
/**
31+
* A {@link BatchInsertStrategy} that expects ids to be generated from the batch insert. When the {@link Dialect} does
32+
* not support id generation for batch operations, this implementation falls back to performing the inserts serially.
33+
*
34+
* @author Chirag Tailor
35+
*/
1536
class IdGeneratingBatchInsertStrategy implements BatchInsertStrategy {
1637

1738
private final InsertStrategy insertStrategy;
@@ -68,6 +89,7 @@ public Object[] execute(String sql, SqlParameterSource[] sqlParameterSources) {
6889
}
6990

7091
private String[] getKeyColumnNames() {
92+
7193
return Optional.ofNullable(idColumn)
7294
.map(idColumn -> new String[]{idColumn.getReference(dialect.getIdentifierProcessing())})
7395
.orElse(new String[0]);

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/IdGeneratingInsertStrategy.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package org.springframework.data.jdbc.core.convert;
217

318
import org.springframework.dao.DataRetrievalFailureException;
@@ -14,6 +29,11 @@
1429
import java.util.Map;
1530
import java.util.Optional;
1631

32+
/**
33+
* An {@link InsertStrategy} that expects an id to be generated from the insert.
34+
*
35+
* @author Chirag Tailor
36+
*/
1737
class IdGeneratingInsertStrategy implements InsertStrategy {
1838

1939
private final Dialect dialect;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
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+
*/
116
package org.springframework.data.jdbc.core.convert;
217

318
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
419
import org.springframework.lang.Nullable;
520

21+
/**
22+
* Strategy for executing an insert.
23+
*
24+
* @author Chirag Tailor
25+
*/
626
interface InsertStrategy {
27+
28+
/**
29+
* @param sql the insert sql. Must not be {@code null}.
30+
* @param sqlParameterSource the sql parameters for the record to be inserted. Must not be {@code null}.
31+
* @return the id corresponding to the record that was inserted, if one was generated. If an id was not generated,
32+
* this will be {@code null}.
33+
*/
734
@Nullable
835
Object execute(String sql, SqlParameterSource sqlParameterSource);
936
}

0 commit comments

Comments
 (0)