Skip to content

Commit f70ef4b

Browse files
committed
use 'var' to clean up code in org.hibernate.engine.jdbc package
1 parent c2ae4cd commit f70ef4b

12 files changed

+173
-263
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/BatchBuilderImpl.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,28 @@ public int getJdbcBatchSize() {
4848
return globalBatchSize;
4949
}
5050

51+
private int batchSize(Integer explicitBatchSize) {
52+
return explicitBatchSize == null
53+
? globalBatchSize
54+
: explicitBatchSize;
55+
}
56+
5157
@Override
5258
public Batch buildBatch(
5359
BatchKey key,
5460
Integer explicitBatchSize,
5561
Supplier<PreparedStatementGroup> statementGroupSupplier,
5662
JdbcCoordinator jdbcCoordinator) {
57-
final int batchSize =
58-
explicitBatchSize == null
59-
? globalBatchSize
60-
: explicitBatchSize;
63+
final int batchSize = batchSize( explicitBatchSize );
6164
assert batchSize > 1;
6265
return new BatchImpl( key, statementGroupSupplier.get(), batchSize, jdbcCoordinator );
6366
}
6467

65-
6668
/**
6769
* Intended for use from tests
6870
*/
6971
@Internal
7072
public BatchImpl buildBatch(BatchKey batchKey, Integer sizeOverride, String table, SessionImplementor session, String sql) {
71-
final JdbcCoordinator jdbcCoordinator = session.getJdbcCoordinator();
72-
73-
final int batchSize = sizeOverride == null
74-
? globalBatchSize
75-
: sizeOverride;
76-
7773
return new BatchImpl(
7874
batchKey,
7975
new PreparedStatementGroupSingleTable(
@@ -137,8 +133,8 @@ public MutationDetails getDeleteDetails() {
137133
),
138134
session
139135
),
140-
batchSize,
141-
jdbcCoordinator
136+
batchSize( sizeOverride ),
137+
session.getJdbcCoordinator()
142138
);
143139
}
144140
}

hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/BatchBuilderInitiator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ public BatchBuilder initiateService(Map<String, Object> configurationValues, Ser
5151

5252
final String builderClassName = builder.toString();
5353
try {
54-
return (BatchBuilder) registry.requireService( ClassLoaderService.class )
55-
.classForName( builderClassName )
56-
.getConstructor()
57-
.newInstance();
54+
return (BatchBuilder)
55+
registry.requireService( ClassLoaderService.class )
56+
.classForName( builderClassName )
57+
.getConstructor()
58+
.newInstance();
5859
}
5960
catch (Exception e) {
6061
throw new ServiceException( "Could not build explicit BatchBuilder [" + builderClassName + "]", e );

hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/BatchImpl.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.hibernate.engine.jdbc.batch.internal;
66

7-
import java.sql.PreparedStatement;
87
import java.sql.SQLException;
98
import java.util.LinkedHashSet;
109

@@ -21,10 +20,6 @@
2120
import org.hibernate.engine.jdbc.spi.JdbcServices;
2221
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
2322
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
24-
import org.hibernate.event.monitor.spi.EventMonitor;
25-
import org.hibernate.event.monitor.spi.DiagnosticEvent;
26-
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
27-
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
2823

2924
import static java.util.Objects.requireNonNull;
3025
import static org.hibernate.engine.jdbc.JdbcLogging.JDBC_MESSAGE_LOGGER;
@@ -131,7 +126,7 @@ public void addToBatch(JdbcValueBindings jdbcValueBindings, TableInclusionChecke
131126
}
132127
else {
133128
//noinspection resource
134-
final PreparedStatement statement = statementDetails.resolveStatement();
129+
final var statement = statementDetails.resolveStatement();
135130
final String sqlString = statementDetails.getSqlString();
136131
sqlStatementLogger.logStatement( sqlString );
137132
jdbcValueBindings.beforeStatement( statementDetails );
@@ -169,7 +164,7 @@ protected void releaseStatements() {
169164
}
170165

171166
protected void clearBatch(PreparedStatementDetails statementDetails) {
172-
final PreparedStatement statement = statementDetails.getStatement();
167+
final var statement = statementDetails.getStatement();
173168
assert statement != null;
174169

175170
try {
@@ -192,7 +187,7 @@ protected void clearBatch(PreparedStatementDetails statementDetails) {
192187
* Convenience method to notify registered observers of an explicit execution of this batch.
193188
*/
194189
protected final void notifyObserversExplicitExecution() {
195-
for ( BatchObserver observer : observers ) {
190+
for ( var observer : observers ) {
196191
observer.batchExplicitlyExecuted();
197192
}
198193
}
@@ -201,7 +196,7 @@ protected final void notifyObserversExplicitExecution() {
201196
* Convenience method to notify registered observers of an implicit execution of this batch.
202197
*/
203198
protected final void notifyObserversImplicitExecution() {
204-
for ( BatchObserver observer : observers ) {
199+
for ( var observer : observers ) {
205200
observer.batchImplicitlyExecuted();
206201
}
207202
}
@@ -247,18 +242,18 @@ protected void performExecution() {
247242
);
248243
}
249244

250-
final JdbcSessionOwner jdbcSessionOwner = jdbcCoordinator.getJdbcSessionOwner();
251-
final JdbcEventHandler eventHandler = jdbcSessionOwner.getJdbcSessionContext().getEventHandler();
245+
final var jdbcSessionOwner = jdbcCoordinator.getJdbcSessionOwner();
246+
final var eventHandler = jdbcSessionOwner.getJdbcSessionContext().getEventHandler();
252247
try {
253248
getStatementGroup().forEachStatement( (tableName, statementDetails) -> {
254249
final String sql = statementDetails.getSqlString();
255-
final PreparedStatement statement = statementDetails.getStatement();
250+
final var statement = statementDetails.getStatement();
256251
if ( statement != null ) {
257252
try {
258253
if ( statementDetails.getMutatingTableDetails().isIdentifierTable() ) {
254+
final var eventMonitor = jdbcSessionOwner.getEventMonitor();
255+
final var executionEvent = eventMonitor.beginJdbcBatchExecutionEvent();
259256
final int[] rowCounts;
260-
final EventMonitor eventMonitor = jdbcSessionOwner.getEventMonitor();
261-
final DiagnosticEvent executionEvent = eventMonitor.beginJdbcBatchExecutionEvent();
262257
try {
263258
eventHandler.jdbcExecuteBatchStart();
264259
rowCounts = statement.executeBatch();
@@ -319,7 +314,7 @@ private void checkRowCounts(int[] rowCounts, PreparedStatementDetails statementD
319314
@Override
320315
public void release() {
321316
if ( BATCH_MESSAGE_LOGGER.isInfoEnabled() ) {
322-
final PreparedStatementGroup statementGroup = getStatementGroup();
317+
final var statementGroup = getStatementGroup();
323318
if ( statementGroup.getNumberOfStatements() != 0
324319
&& statementGroup.hasMatching( statementDetails -> statementDetails.getStatement() != null ) ) {
325320
BATCH_MESSAGE_LOGGER.batchContainedStatementsOnRelease();

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/LobCreationHelper.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44
*/
55
package org.hibernate.engine.jdbc.env.internal;
66

7-
import java.sql.Clob;
87
import java.sql.Connection;
9-
import java.sql.DatabaseMetaData;
108
import java.sql.SQLException;
119
import java.util.EnumSet;
1210
import java.util.Map;
1311

1412
import org.hibernate.cfg.Environment;
1513
import org.hibernate.dialect.Dialect;
16-
import org.hibernate.internal.util.config.ConfigurationHelper;
1714

1815
import static org.hibernate.engine.jdbc.env.internal.LobCreationLogging.LOB_LOGGER;
1916
import static org.hibernate.engine.jdbc.env.internal.LobCreationLogging.LOB_MESSAGE_LOGGER;
17+
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
2018

2119
/**
2220
* Utilities for LOB creation
@@ -37,7 +35,7 @@ public class LobCreationHelper {
3735
* @param jdbcConnection The connection which can be used in level-of-support testing.
3836
*/
3937
public static EnumSet<LobTypes> getSupportedContextualLobTypes(Dialect dialect, Map<String,Object> configValues, Connection jdbcConnection) {
40-
if ( ConfigurationHelper.getBoolean( Environment.NON_CONTEXTUAL_LOB_CREATION, configValues ) ) {
38+
if ( getBoolean( Environment.NON_CONTEXTUAL_LOB_CREATION, configValues ) ) {
4139
LOB_MESSAGE_LOGGER.disablingContextualLOBCreation( Environment.NON_CONTEXTUAL_LOB_CREATION );
4240
return NONE;
4341
}
@@ -48,14 +46,14 @@ public static EnumSet<LobTypes> getSupportedContextualLobTypes(Dialect dialect,
4846
}
4947

5048
try {
51-
final DatabaseMetaData meta = jdbcConnection.getMetaData();
49+
final var databaseMetaData = jdbcConnection.getMetaData();
5250
// if the jdbc driver version is less than 4, it shouldn't have createClob
53-
if ( meta.getJDBCMajorVersion() < 4 ) {
54-
LOB_MESSAGE_LOGGER.nonContextualLobCreationJdbcVersion( meta.getJDBCMajorVersion() );
51+
if ( databaseMetaData.getJDBCMajorVersion() < 4 ) {
52+
LOB_MESSAGE_LOGGER.nonContextualLobCreationJdbcVersion( databaseMetaData.getJDBCMajorVersion() );
5553
return NONE;
5654
}
5755

58-
if ( !dialect.supportsJdbcConnectionLobCreation( meta ) ) {
56+
if ( !dialect.supportsJdbcConnectionLobCreation( databaseMetaData ) ) {
5957
LOB_MESSAGE_LOGGER.nonContextualLobCreationDialect();
6058
return NONE;
6159
}
@@ -64,25 +62,23 @@ public static EnumSet<LobTypes> getSupportedContextualLobTypes(Dialect dialect,
6462
// ignore exception and continue
6563
}
6664

67-
// NOTE : for the time being we assume that the ability to call
68-
// `createClob` implies the ability to call `#createBlob`
65+
// NOTE: for the time being, we assume that the ability to call
66+
// createClob() implies the ability to call createBlob()
6967
if ( canCreateClob( jdbcConnection ) ) {
70-
if ( canCreateNClob( jdbcConnection ) ) {
71-
return EnumSet.of( LobTypes.BLOB, LobTypes.CLOB, LobTypes.NCLOB );
72-
}
73-
else {
74-
return EnumSet.of( LobTypes.BLOB, LobTypes.CLOB );
75-
}
68+
return canCreateNClob( jdbcConnection )
69+
? EnumSet.of( LobTypes.BLOB, LobTypes.CLOB, LobTypes.NCLOB )
70+
: EnumSet.of( LobTypes.BLOB, LobTypes.CLOB );
7671
}
7772

7873
return NONE;
7974
}
8075

8176
private static boolean canCreateClob(Connection jdbcConnection) {
8277
try {
83-
// we just want to see if the driver can create one. we can immediately free it.
84-
final Clob clob = jdbcConnection.createClob();
78+
// We just want to see if the driver can create one
79+
final var clob = jdbcConnection.createClob();
8580
try {
81+
// We can immediately free it
8682
clob.free();
8783
}
8884
catch (Throwable e) {
@@ -98,9 +94,10 @@ private static boolean canCreateClob(Connection jdbcConnection) {
9894

9995
private static boolean canCreateNClob(Connection jdbcConnection) {
10096
try {
101-
// we just want to see if the driver can create one. we can immediately free it.
102-
final Clob clob = jdbcConnection.createNClob();
97+
// We just want to see if the driver can create one
98+
final var clob = jdbcConnection.createNClob();
10399
try {
100+
// We can immediately free it
104101
clob.free();
105102
}
106103
catch (Throwable e) {

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/LobCreatorBuilderImpl.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ public LobCreator buildLobCreator(LobCreationContext lobCreationContext) {
7474
}
7575
else if ( supportedContextualLobTypes.contains( LobTypes.BLOB )
7676
&& supportedContextualLobTypes.contains( LobTypes.CLOB ) ){
77-
if ( !supportedContextualLobTypes.contains( LobTypes.NCLOB ) ) {
78-
return new BlobAndClobCreator( lobCreationContext, useConnectionToCreateLob );
79-
}
80-
else {
81-
return new StandardLobCreator( lobCreationContext, useConnectionToCreateLob );
82-
}
77+
return !supportedContextualLobTypes.contains( LobTypes.NCLOB )
78+
? new BlobAndClobCreator( lobCreationContext, useConnectionToCreateLob )
79+
: new StandardLobCreator( lobCreationContext, useConnectionToCreateLob );
8380
}
8481
else {
8582
LOB_LOGGER.debug( "Unexpected condition resolving type of LobCreator to use. Falling back to NonContextualLobCreator" );

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/NormalizingIdentifierHelperImpl.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ else if ( mustQuote( identifier ) ) {
7272
}
7373

7474
private boolean mustQuote(Identifier identifier) {
75+
final String identifierText = identifier.getText();
7576
return globallyQuoteIdentifiers
76-
|| autoQuoteKeywords && isReservedWord( identifier.getText() )
77-
|| autoQuoteInitialUnderscore && identifier.getText().startsWith( "_" )
78-
|| autoQuoteDollar && identifier.getText().contains( "$" );
77+
|| autoQuoteKeywords && isReservedWord( identifierText )
78+
|| autoQuoteInitialUnderscore && identifierText.startsWith( "_" )
79+
|| autoQuoteDollar && identifierText.contains( "$" );
7980
}
8081

8182
@Override
@@ -107,15 +108,13 @@ public String toMetaDataCatalogName(Identifier identifier) {
107108
// null is used to tell DatabaseMetaData to not limit results based on catalog.
108109
return null;
109110
}
110-
111-
if ( identifier == null ) {
112-
if ( jdbcEnvironment.getCurrentCatalog() == null ) {
113-
return "";
114-
}
115-
identifier = jdbcEnvironment.getCurrentCatalog();
111+
else {
112+
final var id =
113+
identifier == null
114+
? jdbcEnvironment.getCurrentCatalog()
115+
: identifier;
116+
return id == null ? "" : toMetaDataText( id );
116117
}
117-
118-
return toMetaDataText( identifier );
119118
}
120119

121120
private String toMetaDataText(Identifier identifier) {
@@ -149,15 +148,14 @@ public String toMetaDataSchemaName(Identifier identifier) {
149148
// null is used to tell DatabaseMetaData to not limit results based on schema.
150149
return null;
151150
}
152-
153-
if ( identifier == null ) {
154-
if ( jdbcEnvironment.getCurrentSchema() == null ) {
155-
return "";
156-
}
157-
identifier = jdbcEnvironment.getCurrentSchema();
151+
else {
152+
final var id =
153+
identifier == null
154+
? jdbcEnvironment.getCurrentSchema()
155+
: identifier;
156+
return id == null ? "" : toMetaDataText( id );
158157
}
159158

160-
return toMetaDataText( identifier );
161159
}
162160

163161
@Override

0 commit comments

Comments
 (0)