9
9
import java .sql .DatabaseMetaData ;
10
10
import java .sql .SQLException ;
11
11
import java .util .Arrays ;
12
- import java .util .Collections ;
13
12
import java .util .HashSet ;
14
13
import java .util .LinkedHashSet ;
15
14
import java .util .Set ;
16
- import java .util .TreeSet ;
17
15
18
16
import org .hibernate .boot .model .naming .Identifier ;
19
17
import org .hibernate .boot .registry .selector .spi .StrategySelector ;
22
20
import org .hibernate .engine .config .spi .ConfigurationService ;
23
21
import org .hibernate .engine .config .spi .StandardConverters ;
24
22
import org .hibernate .engine .jdbc .env .spi .ExtractedDatabaseMetaData ;
23
+ import org .hibernate .engine .jdbc .env .spi .IdentifierCaseStrategy ;
25
24
import org .hibernate .engine .jdbc .env .spi .IdentifierHelper ;
25
+ import org .hibernate .engine .jdbc .env .spi .IdentifierHelperBuilder ;
26
26
import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
27
27
import org .hibernate .engine .jdbc .env .spi .LobCreatorBuilder ;
28
28
import org .hibernate .engine .jdbc .env .spi .NameQualifierSupport ;
36
36
import org .hibernate .service .ServiceRegistry ;
37
37
import org .hibernate .service .spi .ServiceRegistryImplementor ;
38
38
39
+ import org .jboss .logging .Logger ;
40
+
39
41
/**
40
42
* @author Steve Ebersole
41
43
*/
42
44
public class JdbcEnvironmentImpl implements JdbcEnvironment {
45
+ private static final Logger log = Logger .getLogger ( JdbcEnvironmentImpl .class );
46
+
43
47
private final Dialect dialect ;
44
48
45
49
private final SqlExceptionHelper sqlExceptionHelper ;
@@ -51,7 +55,6 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
51
55
private final LobCreatorBuilderImpl lobCreatorBuilder ;
52
56
53
57
private final LinkedHashSet <TypeInfo > typeInfoSet = new LinkedHashSet <TypeInfo >();
54
- private final Set <String > reservedWords = new TreeSet <String >( String .CASE_INSENSITIVE_ORDER );
55
58
56
59
/**
57
60
* Constructor form used when the JDBC {@link java.sql.DatabaseMetaData} is not available.
@@ -62,6 +65,8 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
62
65
public JdbcEnvironmentImpl (ServiceRegistryImplementor serviceRegistry , Dialect dialect ) {
63
66
this .dialect = dialect ;
64
67
68
+ final ConfigurationService cfgService = serviceRegistry .getService ( ConfigurationService .class );
69
+
65
70
NameQualifierSupport nameQualifierSupport = dialect .getNameQualifierSupport ();
66
71
if ( nameQualifierSupport == null ) {
67
72
// assume both catalogs and schemas are supported
@@ -71,39 +76,43 @@ public JdbcEnvironmentImpl(ServiceRegistryImplementor serviceRegistry, Dialect d
71
76
this .sqlExceptionHelper = buildSqlExceptionHelper ( dialect );
72
77
this .extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl .Builder ( this ).build ();
73
78
74
- reservedWords .addAll ( dialect .determineKeywordsForAutoQuoting ( Collections .<String >emptySet () ) );
79
+ final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder .from ( this );
80
+ identifierHelperBuilder .setGloballyQuoteIdentifiers ( globalQuoting ( cfgService ) );
81
+ identifierHelperBuilder .setNameQualifierSupport ( nameQualifierSupport );
75
82
76
- final boolean globallyQuoteIdentifiers = serviceRegistry .getService ( ConfigurationService .class )
77
- .getSetting ( AvailableSettings .GLOBALLY_QUOTED_IDENTIFIERS , StandardConverters .BOOLEAN , false );
78
-
79
- // a simple impl that works on H2
80
- this .identifierHelper = new NormalizingIdentifierHelperImpl (
81
- this ,
82
- nameQualifierSupport ,
83
- globallyQuoteIdentifiers ,
84
- true , // storesMixedCaseQuotedIdentifiers
85
- false , // storesLowerCaseQuotedIdentifiers
86
- false , // storesUpperCaseQuotedIdentifiers
87
- false , // storesMixedCaseIdentifiers
88
- true , // storesUpperCaseIdentifiers
89
- false // storesLowerCaseIdentifiers
90
- );
83
+ IdentifierHelper identifierHelper = null ;
84
+ try {
85
+ identifierHelper = dialect .buildIdentifierHelper ( identifierHelperBuilder , null );
86
+ }
87
+ catch (SQLException sqle ) {
88
+ // should never ever happen
89
+ log .debug ( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment" , sqle );
90
+ }
91
+ if ( identifierHelper == null ) {
92
+ identifierHelper = identifierHelperBuilder .build ();
93
+ }
94
+ this .identifierHelper = identifierHelper ;
91
95
92
96
this .currentCatalog = identifierHelper .toIdentifier (
93
- serviceRegistry .getService ( ConfigurationService .class )
94
- .getSetting ( AvailableSettings .DEFAULT_CATALOG , StandardConverters .STRING )
97
+ cfgService .getSetting ( AvailableSettings .DEFAULT_CATALOG , StandardConverters .STRING )
95
98
);
96
99
this .currentSchema = Identifier .toIdentifier (
97
- serviceRegistry .getService ( ConfigurationService .class )
98
- .getSetting ( AvailableSettings .DEFAULT_SCHEMA , StandardConverters .STRING )
100
+ cfgService .getSetting ( AvailableSettings .DEFAULT_SCHEMA , StandardConverters .STRING )
99
101
);
100
102
101
- // again, a simple impl that works on H2
102
103
this .qualifiedObjectNameFormatter = new QualifiedObjectNameFormatterStandardImpl ( nameQualifierSupport );
103
104
104
105
this .lobCreatorBuilder = LobCreatorBuilderImpl .makeLobCreatorBuilder ();
105
106
}
106
107
108
+ private static boolean globalQuoting (ConfigurationService cfgService ) {
109
+ return cfgService .getSetting (
110
+ AvailableSettings .GLOBALLY_QUOTED_IDENTIFIERS ,
111
+ StandardConverters .BOOLEAN ,
112
+ false
113
+ );
114
+ }
115
+
107
116
/**
108
117
* Constructor form used from testing
109
118
*
@@ -123,22 +132,20 @@ public JdbcEnvironmentImpl(DatabaseMetaData databaseMetaData, Dialect dialect) t
123
132
nameQualifierSupport = determineNameQualifierSupport ( databaseMetaData );
124
133
}
125
134
126
- reservedWords .addAll ( dialect .determineKeywordsForAutoQuoting ( extractedMetaDataSupport .getExtraKeywords () ) );
127
-
128
- final boolean globallyQuoteIdentifiers = false ;
129
-
130
- // a simple impl that works on H2
131
- this .identifierHelper = new NormalizingIdentifierHelperImpl (
132
- this ,
133
- nameQualifierSupport ,
134
- globallyQuoteIdentifiers ,
135
- true , // storesMixedCaseQuotedIdentifiers
136
- false , // storesLowerCaseQuotedIdentifiers
137
- false , // storesUpperCaseQuotedIdentifiers
138
- false , // storesMixedCaseIdentifiers
139
- true , // storesUpperCaseIdentifiers
140
- false // storesLowerCaseIdentifiers
141
- );
135
+ final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder .from ( this );
136
+ identifierHelperBuilder .setNameQualifierSupport ( nameQualifierSupport );
137
+ IdentifierHelper identifierHelper = null ;
138
+ try {
139
+ identifierHelper = dialect .buildIdentifierHelper ( identifierHelperBuilder , databaseMetaData );
140
+ }
141
+ catch (SQLException sqle ) {
142
+ // should never ever happen
143
+ log .debug ( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment" , sqle );
144
+ }
145
+ if ( identifierHelper == null ) {
146
+ identifierHelper = identifierHelperBuilder .build ();
147
+ }
148
+ this .identifierHelper = identifierHelper ;
142
149
143
150
this .currentCatalog = null ;
144
151
this .currentSchema = null ;
@@ -184,6 +191,8 @@ public JdbcEnvironmentImpl(
184
191
DatabaseMetaData databaseMetaData ) throws SQLException {
185
192
this .dialect = dialect ;
186
193
194
+ final ConfigurationService cfgService = serviceRegistry .getService ( ConfigurationService .class );
195
+
187
196
this .sqlExceptionHelper = buildSqlExceptionHelper ( dialect );
188
197
189
198
this .extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl .Builder ( this )
@@ -196,22 +205,21 @@ public JdbcEnvironmentImpl(
196
205
nameQualifierSupport = determineNameQualifierSupport ( databaseMetaData );
197
206
}
198
207
199
- reservedWords .addAll ( dialect .determineKeywordsForAutoQuoting ( extractedMetaDataSupport .getExtraKeywords () ) );
200
-
201
- final boolean globallyQuoteIdentifiers = serviceRegistry .getService ( ConfigurationService .class )
202
- .getSetting ( AvailableSettings .GLOBALLY_QUOTED_IDENTIFIERS , StandardConverters .BOOLEAN , false );
203
-
204
- this .identifierHelper = new NormalizingIdentifierHelperImpl (
205
- this ,
206
- nameQualifierSupport ,
207
- globallyQuoteIdentifiers ,
208
- databaseMetaData .storesMixedCaseQuotedIdentifiers (),
209
- databaseMetaData .storesLowerCaseQuotedIdentifiers (),
210
- databaseMetaData .storesUpperCaseQuotedIdentifiers (),
211
- databaseMetaData .storesMixedCaseIdentifiers (),
212
- databaseMetaData .storesUpperCaseIdentifiers (),
213
- databaseMetaData .storesLowerCaseIdentifiers ()
214
- );
208
+ final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder .from ( this );
209
+ identifierHelperBuilder .setGloballyQuoteIdentifiers ( globalQuoting ( cfgService ) );
210
+ identifierHelperBuilder .setNameQualifierSupport ( nameQualifierSupport );
211
+ IdentifierHelper identifierHelper = null ;
212
+ try {
213
+ identifierHelper = dialect .buildIdentifierHelper ( identifierHelperBuilder , databaseMetaData );
214
+ }
215
+ catch (SQLException sqle ) {
216
+ // should never ever happen
217
+ log .debug ( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment" , sqle );
218
+ }
219
+ if ( identifierHelper == null ) {
220
+ identifierHelper = identifierHelperBuilder .build ();
221
+ }
222
+ this .identifierHelper = identifierHelper ;
215
223
216
224
// and that current-catalog and current-schema happen after it
217
225
this .currentCatalog = identifierHelper .toIdentifier ( extractedMetaDataSupport .getConnectionCatalogName () );
@@ -225,7 +233,7 @@ public JdbcEnvironmentImpl(
225
233
this .typeInfoSet .addAll ( TypeInfo .extractTypeInfo ( databaseMetaData ) );
226
234
227
235
this .lobCreatorBuilder = LobCreatorBuilderImpl .makeLobCreatorBuilder (
228
- serviceRegistry . getService ( ConfigurationService . class ) .getSettings (),
236
+ cfgService .getSettings (),
229
237
databaseMetaData .getConnection ()
230
238
);
231
239
}
@@ -307,11 +315,6 @@ public IdentifierHelper getIdentifierHelper() {
307
315
return identifierHelper ;
308
316
}
309
317
310
- @ Override
311
- public boolean isReservedWord (String word ) {
312
- return reservedWords .contains ( word );
313
- }
314
-
315
318
@ Override
316
319
public SqlExceptionHelper getSqlExceptionHelper () {
317
320
return sqlExceptionHelper ;
0 commit comments