1
1
/*
2
- * Copyright 2002-2007 the original author or authors.
2
+ * Copyright 2002-2010 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
33
33
import org .springframework .jdbc .support .nativejdbc .NativeJdbcExtractor ;
34
34
35
35
/**
36
- * A generic implementation of the {@link TableMetaDataProvider} that should provide enough features for all supported
37
- * databases.
36
+ * A generic implementation of the {@link TableMetaDataProvider} that should provide
37
+ * enough features for all supported databases.
38
38
*
39
39
* @author Thomas Risberg
40
40
* @since 2.5
@@ -67,30 +67,23 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
67
67
68
68
/** database products we know not supporting the use of a String[] for generated keys */
69
69
private List productsNotSupportingGeneratedKeysColumnNameArray =
70
- Arrays .asList (new String [] { "Apache Derby" , "HSQL Database Engine" } );
70
+ Arrays .asList ("Apache Derby" , "HSQL Database Engine" );
71
71
72
72
/** Collection of TableParameterMetaData objects */
73
73
private List <TableParameterMetaData > insertParameterMetaData = new ArrayList <TableParameterMetaData >();
74
74
75
75
/** NativeJdbcExtractor that can be used to retrieve the native connection */
76
- protected NativeJdbcExtractor nativeJdbcExtractor = null ;
76
+ private NativeJdbcExtractor nativeJdbcExtractor ;
77
77
78
78
79
79
/**
80
80
* Constructor used to initialize with provided database meta data.
81
81
* @param databaseMetaData meta data to be used
82
- * @throws SQLException
83
82
*/
84
83
protected GenericTableMetaDataProvider (DatabaseMetaData databaseMetaData ) throws SQLException {
85
- userName = databaseMetaData .getUserName ();
84
+ this . userName = databaseMetaData .getUserName ();
86
85
}
87
86
88
- /**
89
- * Get whether identifiers use upper case
90
- */
91
- public boolean isStoresUpperCaseIdentifiers () {
92
- return storesUpperCaseIdentifiers ;
93
- }
94
87
95
88
/**
96
89
* Specify whether identifiers use upper case
@@ -100,29 +93,36 @@ public void setStoresUpperCaseIdentifiers(boolean storesUpperCaseIdentifiers) {
100
93
}
101
94
102
95
/**
103
- * Get whether identifiers use lower case
96
+ * Get whether identifiers use upper case
104
97
*/
105
- public boolean isStoresLowerCaseIdentifiers () {
106
- return storesLowerCaseIdentifiers ;
98
+ public boolean isStoresUpperCaseIdentifiers () {
99
+ return this . storesUpperCaseIdentifiers ;
107
100
}
108
101
109
102
/**
110
- * Specify whether identifiers use lower case
103
+ * Specify whether identifiers use lower case.
111
104
*/
112
105
public void setStoresLowerCaseIdentifiers (boolean storesLowerCaseIdentifiers ) {
113
106
this .storesLowerCaseIdentifiers = storesLowerCaseIdentifiers ;
114
107
}
115
108
109
+ /**
110
+ * Get whether identifiers use lower case
111
+ */
112
+ public boolean isStoresLowerCaseIdentifiers () {
113
+ return this .storesLowerCaseIdentifiers ;
114
+ }
115
+
116
116
public boolean isTableColumnMetaDataUsed () {
117
- return tableColumnMetaDataUsed ;
117
+ return this . tableColumnMetaDataUsed ;
118
118
}
119
119
120
120
public List <TableParameterMetaData > getTableParameterMetaData () {
121
- return insertParameterMetaData ;
121
+ return this . insertParameterMetaData ;
122
122
}
123
123
124
124
public boolean isGetGeneratedKeysSupported () {
125
- return getGeneratedKeysSupported ;
125
+ return this . getGeneratedKeysSupported ;
126
126
}
127
127
128
128
public boolean isGetGeneratedKeysSimulated (){
@@ -140,23 +140,27 @@ public void setGetGeneratedKeysSupported(boolean getGeneratedKeysSupported) {
140
140
this .getGeneratedKeysSupported = getGeneratedKeysSupported ;
141
141
}
142
142
143
- public boolean isGeneratedKeysColumnNameArraySupported () {
144
- return generatedKeysColumnNameArraySupported ;
145
- }
146
-
147
143
/**
148
144
* Specify whether a column name array is supported for generated keys
149
145
*/
150
146
public void setGeneratedKeysColumnNameArraySupported (boolean generatedKeysColumnNameArraySupported ) {
151
147
this .generatedKeysColumnNameArraySupported = generatedKeysColumnNameArraySupported ;
152
148
}
153
149
150
+ public boolean isGeneratedKeysColumnNameArraySupported () {
151
+ return this .generatedKeysColumnNameArraySupported ;
152
+ }
153
+
154
154
public void setNativeJdbcExtractor (NativeJdbcExtractor nativeJdbcExtractor ) {
155
155
this .nativeJdbcExtractor = nativeJdbcExtractor ;
156
156
}
157
157
158
- public void initializeWithMetaData (DatabaseMetaData databaseMetaData ) throws SQLException {
158
+ protected NativeJdbcExtractor getNativeJdbcExtractor () {
159
+ return this .nativeJdbcExtractor ;
160
+ }
159
161
162
+
163
+ public void initializeWithMetaData (DatabaseMetaData databaseMetaData ) throws SQLException {
160
164
try {
161
165
if (databaseMetaData .supportsGetGeneratedKeys ()) {
162
166
logger .debug ("GetGeneratedKeys is supported" );
@@ -172,7 +176,7 @@ public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQL
172
176
}
173
177
try {
174
178
String databaseProductName = databaseMetaData .getDatabaseProductName ();
175
- if (productsNotSupportingGeneratedKeysColumnNameArray .contains (databaseProductName )) {
179
+ if (this . productsNotSupportingGeneratedKeysColumnNameArray .contains (databaseProductName )) {
176
180
logger .debug ("GeneratedKeysColumnNameArray is not supported for " + databaseProductName );
177
181
setGeneratedKeysColumnNameArraySupported (false );
178
182
}
@@ -185,7 +189,7 @@ public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQL
185
189
logger .warn ("Error retrieving 'DatabaseMetaData.getDatabaseProductName' - " + se .getMessage ());
186
190
}
187
191
try {
188
- databaseVersion = databaseMetaData .getDatabaseProductVersion ();
192
+ this . databaseVersion = databaseMetaData .getDatabaseProductVersion ();
189
193
}
190
194
catch (SQLException se ) {
191
195
logger .warn ("Error retrieving 'DatabaseMetaData.getDatabaseProductVersion' - " + se .getMessage ());
@@ -205,47 +209,56 @@ public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQL
205
209
206
210
}
207
211
208
- public void initializeWithTableColumnMetaData (DatabaseMetaData databaseMetaData , String catalogName , String schemaName , String tableName )
209
- throws SQLException {
210
-
211
- tableColumnMetaDataUsed = true ;
212
+ public void initializeWithTableColumnMetaData (DatabaseMetaData databaseMetaData , String catalogName ,
213
+ String schemaName , String tableName ) throws SQLException {
212
214
215
+ this .tableColumnMetaDataUsed = true ;
213
216
locateTableAndProcessMetaData (databaseMetaData , catalogName , schemaName , tableName );
214
-
215
-
216
217
}
217
218
218
219
public String tableNameToUse (String tableName ) {
219
- if (tableName == null )
220
+ if (tableName == null ) {
220
221
return null ;
221
- else if (isStoresUpperCaseIdentifiers ())
222
+ }
223
+ else if (isStoresUpperCaseIdentifiers ()) {
222
224
return tableName .toUpperCase ();
223
- else if (isStoresLowerCaseIdentifiers ())
225
+ }
226
+ else if (isStoresLowerCaseIdentifiers ()) {
224
227
return tableName .toLowerCase ();
225
- else
228
+ }
229
+ else {
226
230
return tableName ;
231
+ }
227
232
}
228
233
229
234
public String catalogNameToUse (String catalogName ) {
230
- if (catalogName == null )
235
+ if (catalogName == null ) {
231
236
return null ;
232
- else if (isStoresUpperCaseIdentifiers ())
237
+ }
238
+ else if (isStoresUpperCaseIdentifiers ()) {
233
239
return catalogName .toUpperCase ();
234
- else if (isStoresLowerCaseIdentifiers ())
240
+ }
241
+ else if (isStoresLowerCaseIdentifiers ()) {
235
242
return catalogName .toLowerCase ();
236
- else
237
- return catalogName ;
243
+ }
244
+ else {
245
+ return catalogName ;
246
+ }
238
247
}
239
248
240
249
public String schemaNameToUse (String schemaName ) {
241
- if (schemaName == null )
250
+ if (schemaName == null ) {
242
251
return null ;
243
- else if (isStoresUpperCaseIdentifiers ())
252
+ }
253
+ else if (isStoresUpperCaseIdentifiers ()) {
244
254
return schemaName .toUpperCase ();
245
- else if (isStoresLowerCaseIdentifiers ())
255
+ }
256
+ else if (isStoresLowerCaseIdentifiers ()) {
246
257
return schemaName .toLowerCase ();
247
- else
248
- return schemaName ;
258
+ }
259
+ else {
260
+ return schemaName ;
261
+ }
249
262
}
250
263
251
264
public String metaDataCatalogNameToUse (String catalogName ) {
@@ -261,19 +274,20 @@ public String metaDataSchemaNameToUse(String schemaName) {
261
274
262
275
263
276
/**
264
- * Provide access to version info for subclasses
277
+ * Provide access to version info for subclasses.
265
278
*/
266
279
protected String getDatabaseVersion () {
267
- return databaseVersion ;
280
+ return this . databaseVersion ;
268
281
}
269
282
270
283
/**
271
- * Method supporting the metedata processing for a table
284
+ * Method supporting the metedata processing for a table.
272
285
*/
273
- private void locateTableAndProcessMetaData (DatabaseMetaData databaseMetaData , String catalogName , String schemaName , String tableName ) {
286
+ private void locateTableAndProcessMetaData (DatabaseMetaData databaseMetaData , String catalogName ,
287
+ String schemaName , String tableName ) {
288
+
274
289
Map <String , TableMetaData > tableMeta = new HashMap <String , TableMetaData >();
275
290
ResultSet tables = null ;
276
-
277
291
try {
278
292
tables = databaseMetaData .getTables (
279
293
catalogNameToUse (catalogName ),
@@ -311,7 +325,7 @@ private void locateTableAndProcessMetaData(DatabaseMetaData databaseMetaData, St
311
325
logger .warn ("Unable to locate table meta data for '" + tableName +"' -- column names must be provided" );
312
326
}
313
327
else {
314
- TableMetaData tmd = null ;
328
+ TableMetaData tmd ;
315
329
if (schemaName == null ) {
316
330
tmd = tableMeta .get (userName .toUpperCase ());
317
331
if (tmd == null ) {
@@ -320,14 +334,16 @@ private void locateTableAndProcessMetaData(DatabaseMetaData databaseMetaData, St
320
334
tmd = tableMeta .get ("DBO" );
321
335
}
322
336
if (tmd == null ) {
323
- throw new DataAccessResourceFailureException ("Unable to locate table meta data for '" + tableName + "' in the default schema" );
337
+ throw new DataAccessResourceFailureException ("Unable to locate table meta data for '" +
338
+ tableName + "' in the default schema" );
324
339
}
325
340
}
326
341
}
327
342
else {
328
343
tmd = tableMeta .get (schemaName .toUpperCase ());
329
344
if (tmd == null ) {
330
- throw new DataAccessResourceFailureException ("Unable to locate table meta data for '" + tableName + "' in the '" + schemaName + "' schema" );
345
+ throw new DataAccessResourceFailureException ("Unable to locate table meta data for '" +
346
+ tableName + "' in the '" + schemaName + "' schema" );
331
347
}
332
348
}
333
349
@@ -378,7 +394,7 @@ private void processTableColumns(DatabaseMetaData databaseMetaData, TableMetaDat
378
394
dataType ,
379
395
nullable
380
396
);
381
- insertParameterMetaData .add (meta );
397
+ this . insertParameterMetaData .add (meta );
382
398
if (logger .isDebugEnabled ()) {
383
399
logger .debug ("Retrieved metadata: "
384
400
+ meta .getParameterName () +
@@ -389,61 +405,65 @@ private void processTableColumns(DatabaseMetaData databaseMetaData, TableMetaDat
389
405
}
390
406
}
391
407
catch (SQLException se ) {
392
- logger .warn ("Error while retreiving metadata for table columns: " + se .getMessage ());
408
+ logger .warn ("Error while retrieving metadata for table columns: " + se .getMessage ());
393
409
}
394
410
finally {
395
411
try {
396
412
if (tableColumns != null )
397
413
tableColumns .close ();
398
414
}
399
415
catch (SQLException se ) {
400
- logger .warn ("Problem closing resultset for table column metadata " + se .getMessage ());
416
+ logger .warn ("Problem closing ResultSet for table column metadata " + se .getMessage ());
401
417
}
402
418
}
403
419
404
420
}
405
421
406
422
407
423
/**
408
- * Class representing table meta data
424
+ * Inner class representing table meta data.
409
425
*/
410
- private class TableMetaData {
426
+ private static class TableMetaData {
427
+
411
428
private String catalogName ;
429
+
412
430
private String schemaName ;
413
- private String tableName ;
414
- private String type ;
415
431
432
+ private String tableName ;
416
433
417
- public String getCatalogName () {
418
- return catalogName ;
419
- }
434
+ private String type ;
420
435
421
436
public void setCatalogName (String catalogName ) {
422
437
this .catalogName = catalogName ;
423
438
}
424
439
425
- public String getSchemaName () {
426
- return schemaName ;
440
+ public String getCatalogName () {
441
+ return this . catalogName ;
427
442
}
428
443
429
444
public void setSchemaName (String schemaName ) {
430
445
this .schemaName = schemaName ;
431
446
}
432
447
433
- public String getTableName () {
434
- return tableName ;
448
+ public String getSchemaName () {
449
+ return this . schemaName ;
435
450
}
436
451
437
452
public void setTableName (String tableName ) {
438
453
this .tableName = tableName ;
439
454
}
440
455
441
- public String getType () {
442
- return type ;
456
+ public String getTableName () {
457
+ return this . tableName ;
443
458
}
444
459
445
460
public void setType (String type ) {
446
461
this .type = type ;
447
462
}
463
+
464
+ public String getType () {
465
+ return this .type ;
466
+ }
448
467
}
468
+
449
469
}
0 commit comments