17
17
import org .hibernate .HibernateException ;
18
18
import org .hibernate .LockMode ;
19
19
import org .hibernate .MappingException ;
20
- import org .hibernate .boot .model .naming .ObjectNameNormalizer ;
20
+ import org .hibernate .boot .model .naming .Identifier ;
21
21
import org .hibernate .boot .model .relational .Database ;
22
22
import org .hibernate .boot .model .relational .Namespace ;
23
23
import org .hibernate .boot .model .relational .QualifiedName ;
@@ -94,7 +94,8 @@ public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenera
94
94
95
95
private QualifiedName qualifiedTableName ;
96
96
private String tableName ;
97
- private String pkColumnName ;
97
+ private String segmentColumnName ;
98
+ private String segmentName ;
98
99
private String valueColumnName ;
99
100
private String query ;
100
101
private String insert ;
@@ -254,34 +255,105 @@ private ResultSet executeQuery(PreparedStatement ps, SessionEventListenerManager
254
255
255
256
@ SuppressWarnings ({"StatementWithEmptyBody" , "deprecation" })
256
257
public void configure (Type type , Properties params , ServiceRegistry serviceRegistry ) throws MappingException {
258
+ returnClass = type .getReturnedClass ();
259
+
257
260
final JdbcEnvironment jdbcEnvironment = serviceRegistry .getService ( JdbcEnvironment .class );
258
- final ObjectNameNormalizer normalizer = (ObjectNameNormalizer ) params .get ( IDENTIFIER_NORMALIZER );
259
261
260
- qualifiedTableName = QualifiedNameParser .INSTANCE .parse (
261
- ConfigurationHelper .getString ( ID_TABLE , params , DEFAULT_TABLE ),
262
- normalizer .normalizeIdentifierQuoting ( params .getProperty ( CATALOG ) ),
263
- normalizer .normalizeIdentifierQuoting ( params .getProperty ( SCHEMA ) )
262
+ qualifiedTableName = determineGeneratorTableName ( params , jdbcEnvironment );
263
+
264
+ segmentColumnName = determineSegmentColumnName ( params , jdbcEnvironment );
265
+ keySize = ConfigurationHelper .getInt ( PK_LENGTH_NAME , params , DEFAULT_PK_LENGTH );
266
+ segmentName = ConfigurationHelper .getString ( PK_VALUE_NAME , params , params .getProperty ( TABLE ) );
267
+
268
+ valueColumnName = determineValueColumnName ( params , jdbcEnvironment );
269
+
270
+ //hilo config
271
+ maxLo = ConfigurationHelper .getInt ( MAX_LO , params , Short .MAX_VALUE );
272
+
273
+ if ( maxLo >= 1 ) {
274
+ hiloOptimizer = new LegacyHiLoAlgorithmOptimizer ( returnClass , maxLo );
275
+ }
276
+ }
277
+
278
+ protected QualifiedName determineGeneratorTableName (Properties params , JdbcEnvironment jdbcEnvironment ) {
279
+ final String tableName = ConfigurationHelper .getString ( ID_TABLE , params , DEFAULT_TABLE );
280
+
281
+ if ( tableName .contains ( "." ) ) {
282
+ return QualifiedNameParser .INSTANCE .parse ( tableName );
283
+ }
284
+ else {
285
+ // todo : need to incorporate implicit catalog and schema names
286
+ final Identifier catalog = jdbcEnvironment .getIdentifierHelper ().toIdentifier (
287
+ ConfigurationHelper .getString ( CATALOG , params )
288
+ );
289
+ final Identifier schema = jdbcEnvironment .getIdentifierHelper ().toIdentifier (
290
+ ConfigurationHelper .getString ( SCHEMA , params )
291
+ );
292
+ return new QualifiedNameParser .NameParts (
293
+ catalog ,
294
+ schema ,
295
+ jdbcEnvironment .getIdentifierHelper ().toIdentifier ( tableName )
296
+ );
297
+ }
298
+ }
299
+
300
+ protected String determineSegmentColumnName (Properties params , JdbcEnvironment jdbcEnvironment ) {
301
+ final String name = ConfigurationHelper .getString ( PK_COLUMN_NAME , params , DEFAULT_PK_COLUMN );
302
+ return jdbcEnvironment .getIdentifierHelper ().toIdentifier ( name ).render ( jdbcEnvironment .getDialect () );
303
+ }
304
+
305
+ protected String determineValueColumnName (Properties params , JdbcEnvironment jdbcEnvironment ) {
306
+ final String name = ConfigurationHelper .getString ( VALUE_COLUMN_NAME , params , DEFAULT_VALUE_COLUMN );
307
+ return jdbcEnvironment .getIdentifierHelper ().toIdentifier ( name ).render ( jdbcEnvironment .getDialect () );
308
+ }
309
+
310
+ @ Override
311
+ public void registerExportables (Database database ) {
312
+ final Namespace namespace = database .locateNamespace (
313
+ qualifiedTableName .getCatalogName (),
314
+ qualifiedTableName .getSchemaName ()
264
315
);
265
316
317
+ Table table = namespace .locateTable ( qualifiedTableName .getObjectName () );
318
+ if ( table == null ) {
319
+ table = namespace .createTable ( qualifiedTableName .getObjectName (), false );
320
+
321
+ // todo : note sure the best solution here. do we add the columns if missing? other?
322
+ table .setPrimaryKey ( new PrimaryKey () );
323
+
324
+ final Column pkColumn = new ExportableColumn (
325
+ database ,
326
+ table ,
327
+ segmentColumnName ,
328
+ StringType .INSTANCE ,
329
+ database .getDialect ().getTypeName ( Types .VARCHAR , keySize , 0 , 0 )
330
+ );
331
+ pkColumn .setNullable ( false );
332
+ table .addColumn ( pkColumn );
333
+ table .getPrimaryKey ().addColumn ( pkColumn );
334
+
335
+ final Column valueColumn = new ExportableColumn (
336
+ database ,
337
+ table ,
338
+ valueColumnName ,
339
+ LongType .INSTANCE
340
+ );
341
+ table .addColumn ( valueColumn );
342
+ }
343
+
344
+ final JdbcEnvironment jdbcEnvironment = database .getJdbcEnvironment ();
345
+
346
+ // allow physical naming strategies a chance to kick in
266
347
tableName = jdbcEnvironment .getQualifiedObjectNameFormatter ().format (
267
- qualifiedTableName ,
348
+ table . getQualifiedTableName () ,
268
349
jdbcEnvironment .getDialect ()
269
350
);
270
- pkColumnName = normalizer .toDatabaseIdentifierText (
271
- ConfigurationHelper .getString ( PK_COLUMN_NAME , params , DEFAULT_PK_COLUMN )
272
- );
273
- valueColumnName = normalizer .toDatabaseIdentifierText (
274
- ConfigurationHelper .getString ( VALUE_COLUMN_NAME , params , DEFAULT_VALUE_COLUMN )
275
- );
276
-
277
- keySize = ConfigurationHelper .getInt ( PK_LENGTH_NAME , params , DEFAULT_PK_LENGTH );
278
- String keyValue = ConfigurationHelper .getString ( PK_VALUE_NAME , params , params .getProperty ( TABLE ) );
279
351
280
352
query = "select " +
281
353
valueColumnName +
282
354
" from " +
283
355
jdbcEnvironment .getDialect ().appendLockHint ( LockMode .PESSIMISTIC_WRITE , tableName ) +
284
- " where " + pkColumnName + " = '" + keyValue + "'" +
356
+ " where " + segmentColumnName + " = '" + segmentName + "'" +
285
357
jdbcEnvironment .getDialect ().getForUpdateString ();
286
358
287
359
update = "update " +
@@ -291,59 +363,24 @@ public void configure(Type type, Properties params, ServiceRegistry serviceRegis
291
363
" = ? where " +
292
364
valueColumnName +
293
365
" = ? and " +
294
- pkColumnName +
366
+ segmentColumnName +
295
367
" = '" +
296
- keyValue
368
+ segmentName
297
369
+ "'" ;
298
370
299
371
insert = "insert into " + tableName +
300
- "(" + pkColumnName + ", " + valueColumnName + ") " +
301
- "values('" + keyValue + "', ?)" ;
302
-
303
-
304
- //hilo config
305
- maxLo = ConfigurationHelper .getInt ( MAX_LO , params , Short .MAX_VALUE );
306
- returnClass = type .getReturnedClass ();
372
+ "(" + segmentColumnName + ", " + valueColumnName + ") " +
373
+ "values('" + segmentName + "', ?)" ;
307
374
308
- if ( maxLo >= 1 ) {
309
- hiloOptimizer = new LegacyHiLoAlgorithmOptimizer ( returnClass , maxLo );
310
- }
311
- }
312
375
313
- @ Override
314
- public void registerExportables (Database database ) {
315
- final Namespace namespace = database .locateNamespace (
316
- qualifiedTableName .getCatalogName (),
317
- qualifiedTableName .getSchemaName ()
318
- );
319
376
320
- final Table table = namespace .createTable ( qualifiedTableName .getObjectName (), false );
321
- table .setPrimaryKey ( new PrimaryKey () );
322
-
323
- final Column pkColumn = new ExportableColumn (
324
- database ,
325
- table ,
326
- pkColumnName ,
327
- StringType .INSTANCE ,
328
- database .getDialect ().getTypeName ( Types .VARCHAR , keySize , 0 , 0 )
329
- );
330
- table .addColumn ( pkColumn );
331
- table .getPrimaryKey ().addColumn ( pkColumn );
332
-
333
- final Column valueColumn = new ExportableColumn (
334
- database ,
335
- table ,
336
- valueColumnName ,
337
- LongType .INSTANCE
338
- );
339
- table .addColumn ( valueColumn );
340
377
}
341
378
342
379
public String [] sqlCreateStrings (Dialect dialect ) throws HibernateException {
343
380
return new String [] {
344
381
dialect .getCreateTableString ()
345
382
+ ' ' + tableName + " ( "
346
- + pkColumnName + ' ' + dialect .getTypeName ( Types .VARCHAR , keySize , 0 , 0 ) + ", "
383
+ + segmentColumnName + ' ' + dialect .getTypeName ( Types .VARCHAR , keySize , 0 , 0 ) + ", "
347
384
+ valueColumnName + ' ' + dialect .getTypeName ( Types .INTEGER )
348
385
+ " )" + dialect .getTableTypeString ()
349
386
};
0 commit comments