11
11
import java .sql .SQLException ;
12
12
import java .util .Map ;
13
13
14
- import org .hibernate .HibernateException ;
15
14
import org .hibernate .boot .registry .StandardServiceInitiator ;
16
15
import org .hibernate .cfg .AvailableSettings ;
17
- import org .hibernate .dialect .Dialect ;
18
16
import org .hibernate .engine .jdbc .connections .spi .ConnectionProvider ;
19
17
import org .hibernate .engine .jdbc .connections .spi .JdbcConnectionAccess ;
20
18
import org .hibernate .engine .jdbc .connections .spi .MultiTenantConnectionProvider ;
21
- import org .hibernate .engine .jdbc .dialect .spi .DatabaseMetaDataDialectResolutionInfoAdapter ;
22
19
import org .hibernate .engine .jdbc .dialect .spi .DialectFactory ;
23
20
import org .hibernate .engine .jdbc .dialect .spi .DialectResolutionInfo ;
24
21
import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
25
22
import org .hibernate .internal .CoreMessageLogger ;
26
23
import org .hibernate .internal .log .DeprecationLogger ;
27
24
import org .hibernate .internal .util .NullnessHelper ;
25
+ import org .hibernate .internal .util .StringHelper ;
28
26
import org .hibernate .internal .util .config .ConfigurationHelper ;
29
27
import org .hibernate .service .spi .ServiceRegistryImplementor ;
30
28
@@ -63,8 +61,8 @@ public JdbcEnvironment initiateService(Map<String, Object> configurationValues,
63
61
true
64
62
);
65
63
66
- final Object dbName = NullnessHelper .coalesceSuppliedValues (
67
- () -> configurationValues .get ( AvailableSettings .JAKARTA_HBM2DDL_DB_NAME ),
64
+ String explicitDatabaseName = NullnessHelper .coalesceSuppliedValues (
65
+ () -> ( String ) configurationValues .get ( AvailableSettings .JAKARTA_HBM2DDL_DB_NAME ),
68
66
() -> {
69
67
final Object value = configurationValues .get ( AvailableSettings .DIALECT_DB_NAME );
70
68
if ( value != null ) {
@@ -73,105 +71,82 @@ public JdbcEnvironment initiateService(Map<String, Object> configurationValues,
73
71
AvailableSettings .JAKARTA_HBM2DDL_DB_NAME
74
72
);
75
73
}
76
- return value ;
74
+ return ( String ) value ;
77
75
}
78
76
);
79
77
80
- if ( dbName != null ) {
81
- final String dbVersion = NullnessHelper .coalesceSuppliedValues (
82
- () -> (String ) configurationValues .get ( AvailableSettings .JAKARTA_HBM2DDL_DB_VERSION ),
83
- () -> {
84
- final Object value = configurationValues .get ( AvailableSettings .DIALECT_DB_VERSION );
85
- if ( value != null ) {
86
- DeprecationLogger .DEPRECATION_LOGGER .deprecatedSetting (
87
- AvailableSettings .DIALECT_DB_VERSION ,
88
- AvailableSettings .JAKARTA_HBM2DDL_DB_VERSION
89
- );
90
- }
91
- return (String ) value ;
92
- },
93
- () -> "0"
94
- );
95
- final int dbMajorVersion = NullnessHelper .coalesceSuppliedValues (
96
- () -> ConfigurationHelper .getInteger ( AvailableSettings .JAKARTA_HBM2DDL_DB_MAJOR_VERSION , configurationValues ),
97
- () -> {
98
- final Integer value = ConfigurationHelper .getInteger (
78
+ Integer explicitDatabaseMajorVersion = NullnessHelper .coalesceSuppliedValues (
79
+ () -> ConfigurationHelper .getInteger ( AvailableSettings .JAKARTA_HBM2DDL_DB_MAJOR_VERSION , configurationValues ),
80
+ () -> {
81
+ final Integer value = ConfigurationHelper .getInteger (
82
+ AvailableSettings .DIALECT_DB_MAJOR_VERSION ,
83
+ configurationValues
84
+ );
85
+ if ( value != null ) {
86
+ DeprecationLogger .DEPRECATION_LOGGER .deprecatedSetting (
99
87
AvailableSettings .DIALECT_DB_MAJOR_VERSION ,
100
- configurationValues
88
+ AvailableSettings . JAKARTA_HBM2DDL_DB_MAJOR_VERSION
101
89
);
102
- if ( value != null ) {
103
- DeprecationLogger . DEPRECATION_LOGGER . deprecatedSetting (
104
- AvailableSettings . DIALECT_DB_MAJOR_VERSION ,
105
- AvailableSettings . JAKARTA_HBM2DDL_DB_MAJOR_VERSION
106
- );
107
- }
108
- return value ;
109
- },
110
- () -> 0
111
- );
112
- final int dbMinorVersion = NullnessHelper . coalesceSuppliedValues (
113
- () -> ConfigurationHelper . getInteger ( AvailableSettings . JAKARTA_HBM2DDL_DB_MINOR_VERSION , configurationValues ),
114
- () -> {
115
- final Integer value = ConfigurationHelper . getInteger (
90
+ }
91
+ return value ;
92
+ }
93
+ );
94
+
95
+ Integer explicitDatabaseMinorVersion = NullnessHelper . coalesceSuppliedValues (
96
+ () -> ConfigurationHelper . getInteger ( AvailableSettings . JAKARTA_HBM2DDL_DB_MINOR_VERSION , configurationValues ),
97
+ () -> {
98
+ final Integer value = ConfigurationHelper . getInteger (
99
+ AvailableSettings . DIALECT_DB_MINOR_VERSION ,
100
+ configurationValues
101
+ );
102
+ if ( value != null ) {
103
+ DeprecationLogger . DEPRECATION_LOGGER . deprecatedSetting (
116
104
AvailableSettings .DIALECT_DB_MINOR_VERSION ,
117
- configurationValues
105
+ AvailableSettings .JAKARTA_HBM2DDL_DB_MINOR_VERSION
106
+ );
107
+ }
108
+ return value ;
109
+ }
110
+ );
111
+
112
+ Integer configuredDatabaseMajorVersion = explicitDatabaseMajorVersion ;
113
+ Integer configuredDatabaseMinorVersion = explicitDatabaseMinorVersion ;
114
+ String explicitDatabaseVersion = NullnessHelper .coalesceSuppliedValues (
115
+ () -> (String ) configurationValues .get ( AvailableSettings .JAKARTA_HBM2DDL_DB_VERSION ),
116
+ () -> {
117
+ final Object value = configurationValues .get ( AvailableSettings .DIALECT_DB_VERSION );
118
+ if ( value != null ) {
119
+ DeprecationLogger .DEPRECATION_LOGGER .deprecatedSetting (
120
+ AvailableSettings .DIALECT_DB_VERSION ,
121
+ AvailableSettings .JAKARTA_HBM2DDL_DB_VERSION
118
122
);
119
- if ( value != null ) {
120
- DeprecationLogger .DEPRECATION_LOGGER .deprecatedSetting (
121
- AvailableSettings .DIALECT_DB_MINOR_VERSION ,
122
- AvailableSettings .JAKARTA_HBM2DDL_DB_MINOR_VERSION
123
- );
124
- }
125
- return value ;
126
- },
127
- () -> 0
128
- );
129
- return new JdbcEnvironmentImpl ( registry , dialectFactory .buildDialect (
130
- configurationValues ,
131
- () -> new DialectResolutionInfo () {
132
- @ Override
133
- public String getDatabaseName () {
134
- return (String ) dbName ;
135
- }
136
-
137
- @ Override
138
- public String getDatabaseVersion () {
139
- return dbVersion ;
140
- }
141
-
142
- @ Override
143
- public int getDatabaseMajorVersion () {
144
- return dbMajorVersion ;
145
- }
146
-
147
- @ Override
148
- public int getDatabaseMinorVersion () {
149
- return dbMinorVersion ;
150
- }
151
-
152
- @ Override
153
- public String getDriverName () {
154
- return "" ;
155
- }
156
-
157
- @ Override
158
- public int getDriverMajorVersion () {
159
- return 0 ;
160
- }
161
-
162
- @ Override
163
- public int getDriverMinorVersion () {
164
- return 0 ;
165
- }
166
-
167
- @ Override
168
- public String getSQLKeywords () {
169
- return "" ;
170
- }
171
123
}
172
- ) );
124
+ return (String ) value ;
125
+ }
126
+ ,
127
+ () -> {
128
+ if ( configuredDatabaseMajorVersion != null ) {
129
+ return configuredDatabaseMinorVersion == null ? configuredDatabaseMajorVersion .toString () : (configuredDatabaseMajorVersion + "." + configuredDatabaseMinorVersion );
130
+ }
131
+ return null ;
132
+ }
133
+ );
134
+
135
+ if ( explicitDatabaseMajorVersion == null && explicitDatabaseMinorVersion == null && explicitDatabaseVersion != null ) {
136
+ final String [] parts = explicitDatabaseVersion .split ( "\\ ." );
137
+ try {
138
+ final int potentialMajor = Integer .parseInt ( parts [0 ] );
139
+ if ( parts .length > 1 ) {
140
+ explicitDatabaseMinorVersion = Integer .parseInt ( parts [1 ] );
141
+ }
142
+ explicitDatabaseMajorVersion = potentialMajor ;
143
+ }
144
+ catch (NumberFormatException e ) {
145
+ // Ignore
146
+ }
173
147
}
174
- else if ( useJdbcMetadata ) {
148
+
149
+ if ( useJdbcMetadata ) {
175
150
final JdbcConnectionAccess jdbcConnectionAccess = buildJdbcConnectionAccess ( configurationValues , registry );
176
151
try {
177
152
final Connection connection = jdbcConnectionAccess .obtainConnection ();
@@ -203,23 +178,53 @@ else if ( useJdbcMetadata ) {
203
178
log .debugf ( "JDBC version : %s.%s" , dbmd .getJDBCMajorVersion (), dbmd .getJDBCMinorVersion () );
204
179
}
205
180
206
- final Dialect dialect = dialectFactory .buildDialect (
207
- configurationValues ,
208
- () -> {
209
- try {
210
- return new DatabaseMetaDataDialectResolutionInfoAdapter ( connection .getMetaData () );
211
- }
212
- catch ( SQLException sqlException ) {
213
- throw new HibernateException (
214
- "Unable to access java.sql.DatabaseMetaData to determine appropriate Dialect to use" ,
215
- sqlException
216
- );
217
- }
218
- }
181
+ final String databaseName ;
182
+ final String databaseVersion ;
183
+ final int databaseMajorVersion ;
184
+ final int databaseMinorVersion ;
185
+
186
+ if ( explicitDatabaseName == null ) {
187
+ databaseName = dbmd .getDatabaseProductName ();
188
+ }
189
+ else {
190
+ databaseName = explicitDatabaseName ;
191
+ }
192
+ if ( explicitDatabaseVersion == null ) {
193
+ databaseVersion = dbmd .getDatabaseProductVersion ();
194
+ }
195
+ else {
196
+ databaseVersion = explicitDatabaseVersion ;
197
+ }
198
+ if ( explicitDatabaseMajorVersion == null ) {
199
+ databaseMajorVersion = dbmd .getDatabaseMajorVersion ();
200
+ }
201
+ else {
202
+ databaseMajorVersion = explicitDatabaseMajorVersion ;
203
+ }
204
+ if ( explicitDatabaseMinorVersion == null ) {
205
+ databaseMinorVersion = dbmd .getDatabaseMinorVersion ();
206
+ }
207
+ else {
208
+ databaseMinorVersion = explicitDatabaseMinorVersion ;
209
+ }
210
+
211
+ final DialectResolutionInfo dialectResolutionInfo = new DialectResolutionInfoImpl (
212
+ dbmd ,
213
+ databaseName ,
214
+ databaseVersion ,
215
+ databaseMajorVersion ,
216
+ databaseMinorVersion ,
217
+ dbmd .getDriverName (),
218
+ dbmd .getDriverMajorVersion (),
219
+ dbmd .getDriverMinorVersion (),
220
+ dbmd .getSQLKeywords ()
219
221
);
220
222
return new JdbcEnvironmentImpl (
221
223
registry ,
222
- dialect ,
224
+ dialectFactory .buildDialect (
225
+ configurationValues ,
226
+ () -> dialectResolutionInfo
227
+ ),
223
228
dbmd ,
224
229
jdbcConnectionAccess
225
230
);
@@ -239,11 +244,40 @@ else if ( useJdbcMetadata ) {
239
244
log .unableToObtainConnectionToQueryMetadata ( e );
240
245
}
241
246
}
247
+ else {
248
+ if (
249
+ (StringHelper .isNotEmpty ( explicitDatabaseVersion ) || explicitDatabaseMajorVersion != null || explicitDatabaseMinorVersion != null )
250
+ && ( StringHelper .isNotEmpty ( explicitDatabaseName ) || isNotEmpty ( configurationValues .get ( AvailableSettings .DIALECT ) ) )
251
+ ) {
252
+ final DialectResolutionInfo dialectResolutionInfo = new DialectResolutionInfoImpl (
253
+ null ,
254
+ explicitDatabaseName ,
255
+ explicitDatabaseVersion != null ? explicitDatabaseVersion : "0" ,
256
+ explicitDatabaseMajorVersion != null ? explicitDatabaseMajorVersion : 0 ,
257
+ explicitDatabaseMinorVersion != null ? explicitDatabaseMinorVersion : 0 ,
258
+ null ,
259
+ 0 ,
260
+ 0 ,
261
+ null
262
+ );
263
+ return new JdbcEnvironmentImpl (
264
+ registry ,
265
+ dialectFactory .buildDialect (
266
+ configurationValues ,
267
+ () -> dialectResolutionInfo
268
+ )
269
+ );
270
+ }
271
+ }
242
272
243
273
// if we get here, either we were asked to not use JDBC metadata or accessing the JDBC metadata failed.
244
274
return new JdbcEnvironmentImpl ( registry , dialectFactory .buildDialect ( configurationValues , null ) );
245
275
}
246
276
277
+ private static boolean isNotEmpty (Object o ) {
278
+ return o != null && ( !(o instanceof String ) || !((String ) o ).isEmpty () );
279
+ }
280
+
247
281
private JdbcConnectionAccess buildJdbcConnectionAccess (Map <?,?> configValues , ServiceRegistryImplementor registry ) {
248
282
if ( !configValues .containsKey ( AvailableSettings .MULTI_TENANT_CONNECTION_PROVIDER ) ) {
249
283
ConnectionProvider connectionProvider = registry .getService ( ConnectionProvider .class );
@@ -321,4 +355,81 @@ public boolean supportsAggressiveRelease() {
321
355
return connectionProvider .supportsAggressiveRelease ();
322
356
}
323
357
}
358
+
359
+ private static class DialectResolutionInfoImpl implements DialectResolutionInfo {
360
+ private final DatabaseMetaData databaseMetadata ;
361
+ private final String databaseName ;
362
+ private final String databaseVersion ;
363
+ private final int databaseMajorVersion ;
364
+ private final int databaseMinorVersion ;
365
+ private final String driverName ;
366
+ private final int driverMajorVersion ;
367
+ private final int driverMinorVersion ;
368
+ private final String sqlKeywords ;
369
+
370
+ public DialectResolutionInfoImpl (
371
+ DatabaseMetaData databaseMetadata ,
372
+ String databaseName ,
373
+ String databaseVersion ,
374
+ int databaseMajorVersion ,
375
+ int databaseMinorVersion ,
376
+ String driverName ,
377
+ int driverMajorVersion ,
378
+ int driverMinorVersion ,
379
+ String sqlKeywords ) {
380
+ this .databaseMetadata = databaseMetadata ;
381
+ this .databaseName = databaseName ;
382
+ this .databaseVersion = databaseVersion ;
383
+ this .databaseMajorVersion = databaseMajorVersion ;
384
+ this .databaseMinorVersion = databaseMinorVersion ;
385
+ this .driverName = driverName ;
386
+ this .driverMajorVersion = driverMajorVersion ;
387
+ this .driverMinorVersion = driverMinorVersion ;
388
+ this .sqlKeywords = sqlKeywords ;
389
+ }
390
+
391
+ public String getSQLKeywords () {
392
+ return sqlKeywords ;
393
+ }
394
+
395
+ @ Override
396
+ public String getDatabaseName () {
397
+ return databaseName ;
398
+ }
399
+
400
+ @ Override
401
+ public String getDatabaseVersion () {
402
+ return databaseVersion ;
403
+ }
404
+
405
+ @ Override
406
+ public int getDatabaseMajorVersion () {
407
+ return databaseMajorVersion ;
408
+ }
409
+
410
+ @ Override
411
+ public int getDatabaseMinorVersion () {
412
+ return databaseMinorVersion ;
413
+ }
414
+
415
+ @ Override
416
+ public String getDriverName () {
417
+ return driverName ;
418
+ }
419
+
420
+ @ Override
421
+ public int getDriverMajorVersion () {
422
+ return driverMajorVersion ;
423
+ }
424
+
425
+ @ Override
426
+ public int getDriverMinorVersion () {
427
+ return driverMinorVersion ;
428
+ }
429
+
430
+ @ Override
431
+ public DatabaseMetaData getDatabaseMetadata () {
432
+ return databaseMetadata ;
433
+ }
434
+ }
324
435
}
0 commit comments