Skip to content

Commit b17e17c

Browse files
committed
HHH-13788 Schema update try to recreate existing tables
1 parent dfdc439 commit b17e17c

File tree

1 file changed

+128
-26
lines changed

1 file changed

+128
-26
lines changed

hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/InformationExtractorJdbcDatabaseMetaDataImpl.java

Lines changed: 128 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.hibernate.engine.config.spi.ConfigurationService;
3030
import org.hibernate.engine.config.spi.StandardConverters;
3131
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
32+
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
33+
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
3234
import org.hibernate.internal.CoreLogging;
3335
import org.hibernate.internal.CoreMessageLogger;
3436
import org.hibernate.internal.util.StringHelper;
@@ -59,12 +61,26 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information
5961

6062
private final ExtractionContext extractionContext;
6163

64+
private final boolean useJdbcMetadataDefaultsSetting;
65+
66+
private Identifier currentCatalog;
67+
private Identifier currentSchema;
68+
69+
private String currentCatalogFilter;
70+
private String currentSchemaFilter;
71+
6272
public InformationExtractorJdbcDatabaseMetaDataImpl(ExtractionContext extractionContext) {
6373
this.extractionContext = extractionContext;
6474

6575
ConfigurationService configService = extractionContext.getServiceRegistry()
6676
.getService( ConfigurationService.class );
6777

78+
useJdbcMetadataDefaultsSetting = configService.getSetting(
79+
"hibernate.temp.use_jdbc_metadata_defaults",
80+
StandardConverters.BOOLEAN,
81+
Boolean.TRUE
82+
);
83+
6884
final String extraPhysycalTableTypesConfig = configService.getSetting(
6985
AvailableSettings.EXTRA_PHYSICAL_TABLE_TYPES,
7086
StandardConverters.STRING,
@@ -229,11 +245,14 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
229245
TableInformation tableInfo = null;
230246

231247
// 1) look in current namespace
232-
if ( extractionContext.getJdbcEnvironment().getCurrentCatalog() != null
233-
|| extractionContext.getJdbcEnvironment().getCurrentSchema() != null ) {
248+
final JdbcEnvironment jdbcEnvironment = extractionContext.getJdbcEnvironment();
249+
final Identifier currentSchema = getCurrentSchema( jdbcEnvironment );
250+
final Identifier currentCatalog = getCurrentCatalog( jdbcEnvironment );
251+
if ( currentCatalog != null
252+
|| currentSchema != null ) {
234253
tableInfo = locateTableInNamespace(
235-
extractionContext.getJdbcEnvironment().getCurrentCatalog(),
236-
extractionContext.getJdbcEnvironment().getCurrentSchema(),
254+
currentCatalog,
255+
currentSchema,
237256
tableName
238257
);
239258

@@ -288,42 +307,125 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
288307
}
289308
}
290309

310+
private Identifier getCurrentSchema(JdbcEnvironment jdbcEnvironment) {
311+
if ( currentSchema != null ) {
312+
return currentSchema;
313+
}
314+
final Identifier schema = jdbcEnvironment.getCurrentSchema();
315+
if ( schema != null ) {
316+
currentSchema = schema;
317+
}
318+
if ( !useJdbcMetadataDefaultsSetting ) {
319+
try {
320+
currentSchema = extractionContext.getJdbcEnvironment()
321+
.getIdentifierHelper()
322+
.toIdentifier( extractionContext.getJdbcConnection().getSchema() );
323+
}
324+
catch (SQLException ignore) {
325+
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
326+
}
327+
}
328+
return currentCatalog;
329+
}
330+
331+
private Identifier getCurrentCatalog(JdbcEnvironment jdbcEnvironment) {
332+
if ( currentCatalog != null ) {
333+
return currentCatalog;
334+
}
335+
final Identifier catalog = jdbcEnvironment.getCurrentCatalog();
336+
if ( catalog != null ) {
337+
currentCatalog = catalog;
338+
}
339+
if ( !useJdbcMetadataDefaultsSetting ) {
340+
try {
341+
currentCatalog = extractionContext.getJdbcEnvironment()
342+
.getIdentifierHelper()
343+
.toIdentifier( extractionContext.getJdbcConnection().getCatalog() );
344+
}
345+
catch (SQLException ignore) {
346+
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
347+
}
348+
}
349+
return currentCatalog;
350+
}
351+
352+
private String getCurrentCatalogFilter(JdbcEnvironment jdbcEnvironment) {
353+
if ( currentCatalogFilter != null ) {
354+
return currentCatalogFilter;
355+
}
356+
final Identifier currentCatalog = jdbcEnvironment.getCurrentCatalog();
357+
if ( currentCatalog != null ) {
358+
currentCatalogFilter = toMetaDataObjectName( currentCatalog );
359+
}
360+
if ( !useJdbcMetadataDefaultsSetting ) {
361+
try {
362+
currentCatalogFilter = extractionContext.getJdbcConnection().getCatalog();
363+
}
364+
catch (SQLException ignore) {
365+
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
366+
}
367+
}
368+
return currentCatalogFilter;
369+
}
370+
371+
private String getCurrentSchemaFilter(JdbcEnvironment jdbcEnvironment) {
372+
if ( currentSchemaFilter != null ) {
373+
return currentSchemaFilter;
374+
}
375+
final Identifier currentSchema = jdbcEnvironment.getCurrentSchema();
376+
if ( currentSchema != null ) {
377+
currentSchemaFilter = toMetaDataObjectName( currentSchema );
378+
}
379+
380+
if ( !useJdbcMetadataDefaultsSetting ) {
381+
try {
382+
currentSchemaFilter = extractionContext.getJdbcConnection().getSchema();
383+
}
384+
catch (SQLException ignore) {
385+
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
386+
}
387+
}
388+
return currentSchemaFilter;
389+
}
390+
291391
public NameSpaceTablesInformation getTables(Identifier catalog, Identifier schema) {
292392

293393
String catalogFilter = null;
294394
String schemaFilter = null;
295395

296-
if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsCatalogs() ) {
396+
final JdbcEnvironment jdbcEnvironment = extractionContext.getJdbcEnvironment();
397+
final NameQualifierSupport nameQualifierSupport = jdbcEnvironment.getNameQualifierSupport();
398+
if ( nameQualifierSupport.supportsCatalogs() ) {
297399
if ( catalog == null ) {
298-
if ( extractionContext.getJdbcEnvironment().getCurrentCatalog() != null ) {
299-
// 1) look in current namespace
300-
catalogFilter = toMetaDataObjectName( extractionContext.getJdbcEnvironment().getCurrentCatalog() );
301-
}
302-
else if ( extractionContext.getDefaultCatalog() != null ) {
303-
// 2) look in default namespace
304-
catalogFilter = toMetaDataObjectName( extractionContext.getDefaultCatalog() );
305-
}
306-
else {
307-
catalogFilter = "";
400+
// look in the current namespace
401+
catalogFilter = getCurrentCatalogFilter(jdbcEnvironment);
402+
if ( catalogFilter == null ) {
403+
if ( extractionContext.getDefaultCatalog() != null ) {
404+
// 2) look in default namespace
405+
catalogFilter = toMetaDataObjectName( extractionContext.getDefaultCatalog() );
406+
}
407+
else {
408+
catalogFilter = "";
409+
}
308410
}
309411
}
310412
else {
311413
catalogFilter = toMetaDataObjectName( catalog );
312414
}
313415
}
314416

315-
if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsSchemas() ) {
417+
if ( nameQualifierSupport.supportsSchemas() ) {
316418
if ( schema == null ) {
317-
if ( extractionContext.getJdbcEnvironment().getCurrentSchema() != null ) {
318-
// 1) look in current namespace
319-
schemaFilter = toMetaDataObjectName( extractionContext.getJdbcEnvironment().getCurrentSchema() );
320-
}
321-
else if ( extractionContext.getDefaultSchema() != null ) {
322-
// 2) look in default namespace
323-
schemaFilter = toMetaDataObjectName( extractionContext.getDefaultSchema() );
324-
}
325-
else {
326-
schemaFilter = "";
419+
// 1) look in current namespace
420+
schemaFilter = getCurrentSchemaFilter( jdbcEnvironment );
421+
if ( schemaFilter == null ) {
422+
if ( extractionContext.getDefaultSchema() != null ) {
423+
// 2) look in default namespace
424+
schemaFilter = toMetaDataObjectName( extractionContext.getDefaultSchema() );
425+
}
426+
else {
427+
schemaFilter = "";
428+
}
327429
}
328430
}
329431
else {

0 commit comments

Comments
 (0)