Skip to content

Commit 2c66ce5

Browse files
committed
Polishing.
Formatting. See #1159 Original pull request # 1191
1 parent 014bb71 commit 2c66ce5

File tree

38 files changed

+467
-365
lines changed

38 files changed

+467
-365
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
import org.springframework.dao.OptimisticLockingFailureException;
3232
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
3333
import org.springframework.data.jdbc.core.convert.Identifier;
34+
import org.springframework.data.jdbc.core.convert.InsertSubject;
3435
import org.springframework.data.jdbc.core.convert.JdbcConverter;
3536
import org.springframework.data.jdbc.core.convert.JdbcIdentifierBuilder;
36-
import org.springframework.data.jdbc.core.convert.InsertSubject;
3737
import org.springframework.data.mapping.PersistentProperty;
3838
import org.springframework.data.mapping.PersistentPropertyAccessor;
3939
import org.springframework.data.mapping.PersistentPropertyPath;
@@ -94,7 +94,8 @@ <T> void executeInsertRoot(DbAction.InsertRoot<T> insert) {
9494

9595
setNewVersion(initialVersion);
9696
} else {
97-
id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), Identifier.empty(), insert.getIdValueSource());
97+
id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), Identifier.empty(),
98+
insert.getIdValueSource());
9899
}
99100

100101
add(new DbActionExecutionResult(insert, id));
@@ -103,7 +104,8 @@ <T> void executeInsertRoot(DbAction.InsertRoot<T> insert) {
103104
<T> void executeInsert(DbAction.Insert<T> insert) {
104105

105106
Identifier parentKeys = getParentKeys(insert, converter);
106-
Object id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), parentKeys, insert.getIdValueSource());
107+
Object id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), parentKeys,
108+
insert.getIdValueSource());
107109
add(new DbActionExecutionResult(insert, id));
108110
}
109111

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

+37-30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18+
import java.sql.PreparedStatement;
19+
import java.sql.ResultSet;
20+
import java.sql.SQLException;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Map;
24+
1825
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
1926
import org.springframework.jdbc.core.ColumnMapRowMapper;
2027
import org.springframework.jdbc.core.JdbcOperations;
@@ -31,37 +38,30 @@
3138
import org.springframework.lang.Nullable;
3239
import org.springframework.util.Assert;
3340

34-
import java.sql.PreparedStatement;
35-
import java.sql.ResultSet;
36-
import java.sql.SQLException;
37-
import java.util.ArrayList;
38-
import java.util.List;
39-
import java.util.Map;
40-
4141
/**
42-
* Counterpart to {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations} containing
43-
* methods for performing batch updates with generated keys.
42+
* Counterpart to {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations} containing methods for
43+
* performing batch updates with generated keys.
4444
*
4545
* @author Chirag Tailor
4646
* @since 2.4
4747
*/
4848
public class BatchJdbcOperations {
49+
4950
private final JdbcOperations jdbcOperations;
5051

5152
public BatchJdbcOperations(JdbcOperations jdbcOperations) {
5253
this.jdbcOperations = jdbcOperations;
5354
}
5455

5556
/**
56-
* Execute a batch using the supplied SQL statement with the batch of supplied arguments,
57-
* returning generated keys.
57+
* Execute a batch using the supplied SQL statement with the batch of supplied arguments, returning generated keys.
58+
*
5859
* @param sql the SQL statement to execute
59-
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of
60-
* arguments for the query
60+
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of arguments for the query
6161
* @param generatedKeyHolder a {@link KeyHolder} that will hold the generated keys
62-
* @return an array containing the numbers of rows affected by each update in the batch
63-
* (may also contain special JDBC-defined negative values for affected rows such as
64-
* {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED})
62+
* @return an array containing the numbers of rows affected by each update in the batch (may also contain special
63+
* JDBC-defined negative values for affected rows such as
64+
* {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED})
6565
* @throws org.springframework.dao.DataAccessException if there is any problem issuing the update
6666
* @see org.springframework.jdbc.support.GeneratedKeyHolder
6767
* @since 2.4
@@ -71,16 +71,15 @@ int[] batchUpdate(String sql, SqlParameterSource[] batchArgs, KeyHolder generate
7171
}
7272

7373
/**
74-
* Execute a batch using the supplied SQL statement with the batch of supplied arguments,
75-
* returning generated keys.
74+
* Execute a batch using the supplied SQL statement with the batch of supplied arguments, returning generated keys.
75+
*
7676
* @param sql the SQL statement to execute
77-
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of
78-
* arguments for the query
77+
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of arguments for the query
7978
* @param generatedKeyHolder a {@link KeyHolder} that will hold the generated keys
8079
* @param keyColumnNames names of the columns that will have keys generated for them
81-
* @return an array containing the numbers of rows affected by each update in the batch
82-
* (may also contain special JDBC-defined negative values for affected rows such as
83-
* {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED})
80+
* @return an array containing the numbers of rows affected by each update in the batch (may also contain special
81+
* JDBC-defined negative values for affected rows such as
82+
* {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED})
8483
* @throws org.springframework.dao.DataAccessException if there is any problem issuing the update
8584
* @see org.springframework.jdbc.support.GeneratedKeyHolder
8685
* @since 2.4
@@ -91,7 +90,7 @@ int[] batchUpdate(String sql, SqlParameterSource[] batchArgs, KeyHolder generate
9190
if (batchArgs.length == 0) {
9291
return new int[0];
9392
}
94-
93+
9594
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
9695
SqlParameterSource paramSource = batchArgs[0];
9796
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
@@ -105,6 +104,7 @@ int[] batchUpdate(String sql, SqlParameterSource[] batchArgs, KeyHolder generate
105104
Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
106105
PreparedStatementCreator psc = pscf.newPreparedStatementCreator(params);
107106
BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
107+
108108
@Override
109109
public void setValues(PreparedStatement ps, int i) throws SQLException {
110110
Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
@@ -117,19 +117,24 @@ public int getBatchSize() {
117117
}
118118
};
119119
PreparedStatementCallback<int[]> preparedStatementCallback = ps -> {
120+
120121
int batchSize = bpss.getBatchSize();
121122
generatedKeyHolder.getKeyList().clear();
122123
if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
124+
123125
for (int i = 0; i < batchSize; i++) {
126+
124127
bpss.setValues(ps, i);
125128
ps.addBatch();
126129
}
127130
int[] results = ps.executeBatch();
128131
storeGeneratedKeys(generatedKeyHolder, ps, batchSize);
129132
return results;
130133
} else {
134+
131135
List<Integer> rowsAffected = new ArrayList<>();
132136
for (int i = 0; i < batchSize; i++) {
137+
133138
bpss.setValues(ps, i);
134139
rowsAffected.add(ps.executeUpdate());
135140
storeGeneratedKeys(generatedKeyHolder, ps, 1);
@@ -146,18 +151,20 @@ public int getBatchSize() {
146151
return result;
147152
}
148153

149-
private void storeGeneratedKeys(KeyHolder generatedKeyHolder, PreparedStatement ps, int rowsExpected) throws SQLException {
154+
private void storeGeneratedKeys(KeyHolder generatedKeyHolder, PreparedStatement ps, int rowsExpected)
155+
throws SQLException {
150156

151157
List<Map<String, Object>> generatedKeys = generatedKeyHolder.getKeyList();
152158
ResultSet keys = ps.getGeneratedKeys();
153159
if (keys != null) {
160+
154161
try {
155-
RowMapperResultSetExtractor<Map<String, Object>> rse =
156-
new RowMapperResultSetExtractor<>(new ColumnMapRowMapper(), rowsExpected);
157-
//noinspection ConstantConditions
162+
163+
RowMapperResultSetExtractor<Map<String, Object>> rse = new RowMapperResultSetExtractor<>(
164+
new ColumnMapRowMapper(), rowsExpected);
165+
// noinspection ConstantConditions
158166
generatedKeys.addAll(rse.extractData(keys));
159-
}
160-
finally {
167+
} finally {
161168
JdbcUtils.closeResultSet(keys);
162169
}
163170
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface DataAccessStrategy extends RelationResolver {
7676
<T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource);
7777

7878
/**
79-
* Inserts the data of a multiple entities.
79+
* Inserts the data of multiple entities.
8080
*
8181
* @param <T> the type of the instance.
8282
* @param insertSubjects the subjects to be inserted, where each subject contains the instance and its identifier.

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
7979
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, RelationalMappingContext context,
8080
JdbcConverter converter, NamedParameterJdbcOperations operations, SqlParametersFactory sqlParametersFactory,
8181
InsertStrategyFactory insertStrategyFactory) {
82+
8283
Assert.notNull(sqlGeneratorSource, "SqlGeneratorSource must not be null");
8384
Assert.notNull(context, "RelationalMappingContext must not be null");
8485
Assert.notNull(converter, "JdbcConverter must not be null");
@@ -108,24 +109,28 @@ public <T> Object insert(T instance, Class<T> domainType, Identifier identifier)
108109
@Override
109110
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource) {
110111

111-
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forInsert(instance, domainType, identifier, idValueSource);
112+
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forInsert(instance, domainType, identifier,
113+
idValueSource);
112114

113115
String insertSql = sql(domainType).getInsert(parameterSource.getIdentifiers());
114116

115-
return insertStrategyFactory.insertStrategy(idValueSource, getIdColumn(domainType)).execute(insertSql, parameterSource);
117+
return insertStrategyFactory.insertStrategy(idValueSource, getIdColumn(domainType)).execute(insertSql,
118+
parameterSource);
116119
}
117120

118121
@Override
119122
public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) {
120123

121124
Assert.notEmpty(insertSubjects, "Batch insert must contain at least one InsertSubject");
122125
SqlIdentifierParameterSource[] sqlParameterSources = insertSubjects.stream()
123-
.map(insertSubject -> sqlParametersFactory.forInsert(insertSubject.getInstance(), domainType, insertSubject.getIdentifier(), idValueSource))
126+
.map(insertSubject -> sqlParametersFactory.forInsert(insertSubject.getInstance(), domainType,
127+
insertSubject.getIdentifier(), idValueSource))
124128
.toArray(SqlIdentifierParameterSource[]::new);
125129

126130
String insertSql = sql(domainType).getInsert(sqlParameterSources[0].getIdentifiers());
127131

128-
return insertStrategyFactory.batchInsertStrategy(idValueSource, getIdColumn(domainType)).execute(insertSql, sqlParameterSources);
132+
return insertStrategyFactory.batchInsertStrategy(idValueSource, getIdColumn(domainType)).execute(insertSql,
133+
sqlParameterSources);
129134
}
130135

131136
/*
@@ -210,7 +215,8 @@ public void delete(Object rootId, PersistentPropertyPath<RelationalPersistentPro
210215

211216
String delete = sql(rootEntity.getType()).createDeleteByPath(propertyPath);
212217

213-
SqlIdentifierParameterSource parameters = sqlParametersFactory.forQueryById(rootId, rootEntity.getType(), ROOT_ID_PARAMETER);
218+
SqlIdentifierParameterSource parameters = sqlParametersFactory.forQueryById(rootId, rootEntity.getType(),
219+
ROOT_ID_PARAMETER);
214220
operations.update(delete, parameters);
215221
}
216222

@@ -229,6 +235,7 @@ public <T> void deleteAll(Class<T> domainType) {
229235
*/
230236
@Override
231237
public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> propertyPath) {
238+
232239
operations.getJdbcOperations()
233240
.update(sql(propertyPath.getBaseProperty().getOwner().getType()).createDeleteAllSql(propertyPath));
234241
}
@@ -410,8 +417,8 @@ private SqlGenerator sql(Class<?> domainType) {
410417

411418
@Nullable
412419
private <T> SqlIdentifier getIdColumn(Class<T> domainType) {
420+
413421
return Optional.ofNullable(context.getRequiredPersistentEntity(domainType).getIdProperty())
414-
.map(RelationalPersistentProperty::getColumnName)
415-
.orElse(null);
422+
.map(RelationalPersistentProperty::getColumnName).orElse(null);
416423
}
417424
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18+
import java.util.List;
19+
1820
import org.springframework.data.domain.Pageable;
1921
import org.springframework.data.domain.Sort;
2022
import org.springframework.data.mapping.PersistentPropertyPath;
@@ -23,8 +25,6 @@
2325
import org.springframework.data.relational.core.sql.LockMode;
2426
import org.springframework.util.Assert;
2527

26-
import java.util.List;
27-
2828
/**
2929
* Delegates all method calls to an instance set after construction. This is useful for {@link DataAccessStrategy}s with
3030
* cyclic dependencies.

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class IdGeneratingBatchInsertStrategy implements BatchInsertStrategy {
4141
private final BatchJdbcOperations batchJdbcOperations;
4242
private final SqlIdentifier idColumn;
4343

44-
public IdGeneratingBatchInsertStrategy(InsertStrategy insertStrategy,
45-
Dialect dialect, BatchJdbcOperations batchJdbcOperations,
46-
@Nullable SqlIdentifier idColumn) {
44+
IdGeneratingBatchInsertStrategy(InsertStrategy insertStrategy, Dialect dialect,
45+
BatchJdbcOperations batchJdbcOperations, @Nullable SqlIdentifier idColumn) {
46+
4747
this.insertStrategy = insertStrategy;
4848
this.dialect = dialect;
4949
this.batchJdbcOperations = batchJdbcOperations;
@@ -54,9 +54,9 @@ public IdGeneratingBatchInsertStrategy(InsertStrategy insertStrategy,
5454
public Object[] execute(String sql, SqlParameterSource[] sqlParameterSources) {
5555

5656
if (!dialect.getIdGeneration().supportedForBatchOperations()) {
57+
5758
return Arrays.stream(sqlParameterSources)
58-
.map(sqlParameterSource -> insertStrategy.execute(sql, sqlParameterSource))
59-
.toArray();
59+
.map(sqlParameterSource -> insertStrategy.execute(sql, sqlParameterSource)).toArray();
6060
}
6161

6262
GeneratedKeyHolder holder = new GeneratedKeyHolder();
@@ -75,6 +75,7 @@ public Object[] execute(String sql, SqlParameterSource[] sqlParameterSources) {
7575
Object[] ids = new Object[sqlParameterSources.length];
7676
List<Map<String, Object>> keyList = holder.getKeyList();
7777
for (int i = 0; i < keyList.size(); i++) {
78+
7879
Map<String, Object> keys = keyList.get(i);
7980
if (keys.size() > 1) {
8081
if (idColumn != null) {
@@ -92,7 +93,7 @@ public Object[] execute(String sql, SqlParameterSource[] sqlParameterSources) {
9293
private String[] getKeyColumnNames() {
9394

9495
return Optional.ofNullable(idColumn)
95-
.map(idColumn -> new String[]{idColumn.getReference(dialect.getIdentifierProcessing())})
96+
.map(idColumn -> new String[] { idColumn.getReference(dialect.getIdentifierProcessing()) })
9697
.orElse(new String[0]);
9798
}
9899
}

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18+
import java.util.Map;
19+
import java.util.Optional;
20+
1821
import org.springframework.dao.DataRetrievalFailureException;
1922
import org.springframework.dao.InvalidDataAccessApiUsageException;
2023
import org.springframework.data.relational.core.dialect.Dialect;
@@ -26,9 +29,6 @@
2629
import org.springframework.jdbc.support.KeyHolder;
2730
import org.springframework.lang.Nullable;
2831

29-
import java.util.Map;
30-
import java.util.Optional;
31-
3232
/**
3333
* An {@link InsertStrategy} that expects an id to be generated from the insert.
3434
*
@@ -41,8 +41,8 @@ class IdGeneratingInsertStrategy implements InsertStrategy {
4141
private final NamedParameterJdbcOperations jdbcOperations;
4242
private final SqlIdentifier idColumn;
4343

44-
public IdGeneratingInsertStrategy(Dialect dialect, NamedParameterJdbcOperations jdbcOperations,
45-
@Nullable SqlIdentifier idColumn) {
44+
IdGeneratingInsertStrategy(Dialect dialect, NamedParameterJdbcOperations jdbcOperations,
45+
@Nullable SqlIdentifier idColumn) {
4646
this.dialect = dialect;
4747
this.jdbcOperations = jdbcOperations;
4848
this.idColumn = idColumn;
@@ -68,7 +68,7 @@ public Object execute(String sql, SqlParameterSource sqlParameterSource) {
6868
}
6969

7070
try {
71-
// MySQL just returns one value with a special name
71+
// MySQL just returns one value with a special name
7272
return holder.getKey();
7373
} catch (DataRetrievalFailureException | InvalidDataAccessApiUsageException e) {
7474
// Postgres returns a value for each column
@@ -85,7 +85,7 @@ public Object execute(String sql, SqlParameterSource sqlParameterSource) {
8585

8686
private String[] getKeyColumnNames() {
8787
return Optional.ofNullable(idColumn)
88-
.map(idColumn -> new String[]{idColumn.getReference(dialect.getIdentifierProcessing())})
88+
.map(idColumn -> new String[] { idColumn.getReference(dialect.getIdentifierProcessing()) })
8989
.orElse(new String[0]);
9090
}
9191
}

0 commit comments

Comments
 (0)