14
14
import org .hibernate .boot .model .naming .Identifier ;
15
15
import org .hibernate .boot .model .relational .Database ;
16
16
import org .hibernate .boot .model .relational .InitCommand ;
17
- import org .hibernate .boot .model .relational .Namespace ;
18
17
import org .hibernate .boot .model .relational .QualifiedName ;
19
18
import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
20
- import org .hibernate .dialect .Dialect ;
21
19
import org .hibernate .engine .jdbc .internal .FormatStyle ;
22
20
import org .hibernate .engine .jdbc .spi .SqlStatementLogger ;
23
21
import org .hibernate .engine .spi .SessionEventListenerManager ;
24
22
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
25
- import org .hibernate .event .monitor .spi .EventMonitor ;
26
- import org .hibernate .event .monitor .spi .DiagnosticEvent ;
27
- import org .hibernate .mapping .Column ;
28
23
import org .hibernate .id .IdentifierGenerationException ;
29
24
import org .hibernate .id .IntegralDataTypeHolder ;
30
25
import org .hibernate .jdbc .AbstractReturningWork ;
31
26
import org .hibernate .mapping .Table ;
32
- import org .hibernate .stat .spi .StatisticsImplementor ;
33
- import org .hibernate .type .BasicType ;
34
27
import org .hibernate .type .StandardBasicTypes ;
35
28
36
- import org .hibernate .type .spi .TypeConfiguration ;
37
-
38
29
import static org .hibernate .LockMode .PESSIMISTIC_WRITE ;
39
30
import static org .hibernate .id .IdentifierGeneratorHelper .getIntegralDataTypeHolder ;
40
31
import static org .hibernate .id .enhanced .TableGeneratorLogger .TABLE_GENERATOR_LOGGER ;
@@ -148,8 +139,8 @@ public AccessCallback buildCallback(final SharedSessionContractImplementor sessi
148
139
throw new AssertionFailure ( "SequenceStyleGenerator's TableStructure was not properly initialized" );
149
140
}
150
141
151
- final SessionEventListenerManager statsCollector = session .getEventListenerManager ();
152
- final SqlStatementLogger statementLogger =
142
+ final var statsCollector = session .getEventListenerManager ();
143
+ final var statementLogger =
153
144
session .getFactory ().getJdbcServices ()
154
145
.getSqlStatementLogger ();
155
146
@@ -160,48 +151,48 @@ public IntegralDataTypeHolder getNextValue() {
160
151
new AbstractReturningWork <>() {
161
152
@ Override
162
153
public IntegralDataTypeHolder execute (Connection connection ) throws SQLException {
163
- final IntegralDataTypeHolder value = makeValue ();
154
+ final var value = makeValue ();
164
155
int rows ;
165
156
do {
166
- try ( PreparedStatement selectStatement = prepareStatement (
157
+ try ( var selectStatement = prepareStatement (
167
158
connection ,
168
159
selectQuery ,
169
160
statementLogger ,
170
161
statsCollector ,
171
162
session
172
163
) ) {
173
- final ResultSet selectRS = executeQuery (
164
+ final var resultSet = executeQuery (
174
165
selectStatement ,
175
166
statsCollector ,
176
167
selectQuery ,
177
168
session
178
169
);
179
- if ( !selectRS .next () ) {
170
+ if ( !resultSet .next () ) {
180
171
throw new IdentifierGenerationException (
181
172
"Could not read a hi value, populate the table: "
182
173
+ physicalTableName );
183
174
}
184
- value .initialize ( selectRS , 1 );
185
- selectRS .close ();
175
+ value .initialize ( resultSet , 1 );
176
+ resultSet .close ();
186
177
}
187
178
catch (SQLException sqle ) {
188
179
TABLE_GENERATOR_LOGGER .unableToReadHiValue ( physicalTableName .render (), sqle );
189
180
throw sqle ;
190
181
}
191
182
192
183
193
- try ( PreparedStatement updatePS = prepareStatement (
184
+ try ( var updateStatement = prepareStatement (
194
185
connection ,
195
186
updateQuery ,
196
187
statementLogger ,
197
188
statsCollector ,
198
189
session
199
190
) ) {
200
191
final int increment = applyIncrementSizeToSourceValues ? incrementSize : 1 ;
201
- final IntegralDataTypeHolder updateValue = value .copy ().add ( increment );
202
- updateValue .bind ( updatePS , 1 );
203
- value .bind ( updatePS , 2 );
204
- rows = executeUpdate ( updatePS , statsCollector , updateQuery , session );
192
+ final var updateValue = value .copy ().add ( increment );
193
+ updateValue .bind ( updateStatement , 1 );
194
+ value .bind ( updateStatement , 2 );
195
+ rows = executeUpdate ( updateStatement , statsCollector , updateQuery , session );
205
196
}
206
197
catch (SQLException e ) {
207
198
TABLE_GENERATOR_LOGGER .unableToUpdateHiValue ( physicalTableName .render (), e );
@@ -232,9 +223,9 @@ private PreparedStatement prepareStatement(
232
223
SessionEventListenerManager statsCollector ,
233
224
SharedSessionContractImplementor session ) throws SQLException {
234
225
logger .logStatement ( sql , FormatStyle .BASIC .getFormatter () );
235
- final EventMonitor eventMonitor = session .getEventMonitor ();
236
- final DiagnosticEvent creationEvent = eventMonitor .beginJdbcPreparedStatementCreationEvent ();
237
- final StatisticsImplementor stats = session .getFactory ().getStatistics ();
226
+ final var eventMonitor = session .getEventMonitor ();
227
+ final var creationEvent = eventMonitor .beginJdbcPreparedStatementCreationEvent ();
228
+ final var stats = session .getFactory ().getStatistics ();
238
229
try {
239
230
statsCollector .jdbcPrepareStatementStart ();
240
231
if ( stats != null && stats .isStatisticsEnabled () ) {
@@ -256,8 +247,8 @@ private int executeUpdate(
256
247
SessionEventListenerManager statsCollector ,
257
248
String sql ,
258
249
SharedSessionContractImplementor session ) throws SQLException {
259
- final EventMonitor eventMonitor = session .getEventMonitor ();
260
- final DiagnosticEvent executionEvent = eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
250
+ final var eventMonitor = session .getEventMonitor ();
251
+ final var executionEvent = eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
261
252
try {
262
253
statsCollector .jdbcExecuteStatementStart ();
263
254
return ps .executeUpdate ();
@@ -274,8 +265,8 @@ private ResultSet executeQuery(
274
265
SessionEventListenerManager statsCollector ,
275
266
String sql ,
276
267
SharedSessionContractImplementor session ) throws SQLException {
277
- final EventMonitor eventMonitor = session .getEventMonitor ();
278
- final DiagnosticEvent executionEvent = eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
268
+ final var eventMonitor = session .getEventMonitor ();
269
+ final var executionEvent = eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
279
270
try {
280
271
statsCollector .jdbcExecuteStatementStart ();
281
272
return ps .executeQuery ();
@@ -293,43 +284,35 @@ public boolean isPhysicalSequence() {
293
284
294
285
@ Override
295
286
public void registerExportables (Database database ) {
296
-
297
- final Namespace namespace = database .locateNamespace (
287
+ final var namespace = database .locateNamespace (
298
288
logicalQualifiedTableName .getCatalogName (),
299
289
logicalQualifiedTableName .getSchemaName ()
300
290
);
301
291
302
- Table table = namespace .locateTable ( logicalQualifiedTableName .getObjectName () );
292
+ final var objectName = logicalQualifiedTableName .getObjectName ();
293
+ Table table = namespace .locateTable ( objectName );
303
294
final boolean tableCreated ;
304
295
if ( table == null ) {
305
- table = namespace .createTable (
306
- logicalQualifiedTableName .getObjectName (),
307
- (identifier ) -> new Table ( contributor , namespace , identifier , false )
308
- );
296
+ table = namespace .createTable ( objectName ,
297
+ identifier -> new Table ( contributor , namespace , identifier , false ) );
309
298
tableCreated = true ;
310
299
}
311
300
else {
312
301
tableCreated = false ;
313
302
}
314
303
physicalTableName = table .getQualifiedTableName ();
315
304
316
- valueColumnNameText = logicalValueColumnNameIdentifier .render ( database .getJdbcEnvironment (). getDialect () );
305
+ valueColumnNameText = logicalValueColumnNameIdentifier .render ( database .getDialect () );
317
306
if ( tableCreated ) {
318
- final TypeConfiguration typeConfiguration = database .getTypeConfiguration ();
319
- final BasicType <Long > type = typeConfiguration .getBasicTypeRegistry ().resolve ( StandardBasicTypes .LONG );
320
- final Column valueColumn = ExportableColumnHelper .column (
321
- database ,
322
- table ,
323
- valueColumnNameText ,
324
- type ,
307
+ final var typeConfiguration = database .getTypeConfiguration ();
308
+ final var type = typeConfiguration .getBasicTypeRegistry ().resolve ( StandardBasicTypes .LONG );
309
+ final String typeName =
325
310
typeConfiguration .getDdlTypeRegistry ()
326
- .getTypeName ( type .getJdbcType ().getDdlTypeCode (), database .getDialect () )
327
- );
328
-
311
+ .getTypeName ( type .getJdbcType ().getDdlTypeCode (), database .getDialect () );
312
+ final var valueColumn =
313
+ ExportableColumnHelper . column ( database , table , valueColumnNameText , type , typeName );
329
314
table .addColumn ( valueColumn );
330
-
331
315
table .setOptions ( options );
332
-
333
316
table .addInitCommand ( context -> new InitCommand (
334
317
"insert into " + context .format ( physicalTableName )
335
318
+ " ( " + valueColumnNameText + " ) values ( " + initialValue + " )"
@@ -339,14 +322,13 @@ public void registerExportables(Database database) {
339
322
340
323
@ Override
341
324
public void initialize (SqlStringGenerationContext context ) {
342
- final Dialect dialect = context .getDialect ();
325
+ final var dialect = context .getDialect ();
343
326
final String formattedPhysicalTableName = context .format ( physicalTableName );
344
327
final String lockedTable =
345
328
dialect .appendLockHint ( new LockOptions ( PESSIMISTIC_WRITE ), formattedPhysicalTableName )
346
329
+ dialect .getForUpdateString ();
347
330
selectQuery = "select " + valueColumnNameText + " as id_val" +
348
331
" from " + lockedTable ;
349
-
350
332
updateQuery = "update " + formattedPhysicalTableName +
351
333
" set " + valueColumnNameText + "= ?" +
352
334
" where " + valueColumnNameText + "=?" ;
0 commit comments