|
29 | 29 | import org.hibernate.engine.config.spi.ConfigurationService;
|
30 | 30 | import org.hibernate.engine.config.spi.StandardConverters;
|
31 | 31 | 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; |
32 | 34 | import org.hibernate.internal.CoreLogging;
|
33 | 35 | import org.hibernate.internal.CoreMessageLogger;
|
34 | 36 | import org.hibernate.internal.util.StringHelper;
|
@@ -59,12 +61,26 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information
|
59 | 61 |
|
60 | 62 | private final ExtractionContext extractionContext;
|
61 | 63 |
|
| 64 | + private final boolean useJdbcMetadataDefaultsSetting; |
| 65 | + |
| 66 | + private Identifier currentCatalog; |
| 67 | + private Identifier currentSchema; |
| 68 | + |
| 69 | + private String currentCatalogFilter; |
| 70 | + private String currentSchemaFilter; |
| 71 | + |
62 | 72 | public InformationExtractorJdbcDatabaseMetaDataImpl(ExtractionContext extractionContext) {
|
63 | 73 | this.extractionContext = extractionContext;
|
64 | 74 |
|
65 | 75 | ConfigurationService configService = extractionContext.getServiceRegistry()
|
66 | 76 | .getService( ConfigurationService.class );
|
67 | 77 |
|
| 78 | + useJdbcMetadataDefaultsSetting = configService.getSetting( |
| 79 | + "hibernate.temp.use_jdbc_metadata_defaults", |
| 80 | + StandardConverters.BOOLEAN, |
| 81 | + Boolean.TRUE |
| 82 | + ); |
| 83 | + |
68 | 84 | final String extraPhysycalTableTypesConfig = configService.getSetting(
|
69 | 85 | AvailableSettings.EXTRA_PHYSICAL_TABLE_TYPES,
|
70 | 86 | StandardConverters.STRING,
|
@@ -229,11 +245,14 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
|
229 | 245 | TableInformation tableInfo = null;
|
230 | 246 |
|
231 | 247 | // 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 ) { |
234 | 253 | tableInfo = locateTableInNamespace(
|
235 |
| - extractionContext.getJdbcEnvironment().getCurrentCatalog(), |
236 |
| - extractionContext.getJdbcEnvironment().getCurrentSchema(), |
| 254 | + currentCatalog, |
| 255 | + currentSchema, |
237 | 256 | tableName
|
238 | 257 | );
|
239 | 258 |
|
@@ -288,42 +307,125 @@ public TableInformation getTable(Identifier catalog, Identifier schema, Identifi
|
288 | 307 | }
|
289 | 308 | }
|
290 | 309 |
|
| 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 | + |
291 | 391 | public NameSpaceTablesInformation getTables(Identifier catalog, Identifier schema) {
|
292 | 392 |
|
293 | 393 | String catalogFilter = null;
|
294 | 394 | String schemaFilter = null;
|
295 | 395 |
|
296 |
| - if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsCatalogs() ) { |
| 396 | + final JdbcEnvironment jdbcEnvironment = extractionContext.getJdbcEnvironment(); |
| 397 | + final NameQualifierSupport nameQualifierSupport = jdbcEnvironment.getNameQualifierSupport(); |
| 398 | + if ( nameQualifierSupport.supportsCatalogs() ) { |
297 | 399 | 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 | + } |
308 | 410 | }
|
309 | 411 | }
|
310 | 412 | else {
|
311 | 413 | catalogFilter = toMetaDataObjectName( catalog );
|
312 | 414 | }
|
313 | 415 | }
|
314 | 416 |
|
315 |
| - if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsSchemas() ) { |
| 417 | + if ( nameQualifierSupport.supportsSchemas() ) { |
316 | 418 | 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 | + } |
327 | 429 | }
|
328 | 430 | }
|
329 | 431 | else {
|
|
0 commit comments