-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
TableMetaDataContext.java
393 lines (349 loc) · 12 KB
/
TableMetaDataContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.metadata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Class to manage context meta-data used for the configuration
* and execution of operations on a database table.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 2.5
*/
public class TableMetaDataContext {
// Logger available to subclasses
protected final Log logger = LogFactory.getLog(getClass());
// Name of table for this context
@Nullable
private String tableName;
// Name of catalog for this context
@Nullable
private String catalogName;
// Name of schema for this context
@Nullable
private String schemaName;
// List of columns objects to be used in this context
private List<String> tableColumns = new ArrayList<>();
// Should we access insert parameter meta-data info or not
private boolean accessTableColumnMetaData = true;
// Should we override default for including synonyms for meta-data lookups
private boolean overrideIncludeSynonymsDefault = false;
// The provider of table meta-data
@Nullable
private TableMetaDataProvider metaDataProvider;
// Are we using generated key columns
private boolean generatedKeyColumnsUsed = false;
/**
* Set the name of the table for this context.
*/
public void setTableName(@Nullable String tableName) {
this.tableName = tableName;
}
/**
* Get the name of the table for this context.
*/
@Nullable
public String getTableName() {
return this.tableName;
}
/**
* Set the name of the catalog for this context.
*/
public void setCatalogName(@Nullable String catalogName) {
this.catalogName = catalogName;
}
/**
* Get the name of the catalog for this context.
*/
@Nullable
public String getCatalogName() {
return this.catalogName;
}
/**
* Set the name of the schema for this context.
*/
public void setSchemaName(@Nullable String schemaName) {
this.schemaName = schemaName;
}
/**
* Get the name of the schema for this context.
*/
@Nullable
public String getSchemaName() {
return this.schemaName;
}
/**
* Specify whether we should access table column meta-data.
*/
public void setAccessTableColumnMetaData(boolean accessTableColumnMetaData) {
this.accessTableColumnMetaData = accessTableColumnMetaData;
}
/**
* Are we accessing table meta-data?
*/
public boolean isAccessTableColumnMetaData() {
return this.accessTableColumnMetaData;
}
/**
* Specify whether we should override default for accessing synonyms.
*/
public void setOverrideIncludeSynonymsDefault(boolean override) {
this.overrideIncludeSynonymsDefault = override;
}
/**
* Are we overriding include synonyms default?
*/
public boolean isOverrideIncludeSynonymsDefault() {
return this.overrideIncludeSynonymsDefault;
}
/**
* Get a List of the table column names.
*/
public List<String> getTableColumns() {
return this.tableColumns;
}
/**
* Process the current meta-data with the provided configuration options.
* @param dataSource the DataSource being used
* @param declaredColumns any columns that are declared
* @param generatedKeyNames name of generated keys
*/
public void processMetaData(DataSource dataSource, List<String> declaredColumns, String[] generatedKeyNames) {
this.metaDataProvider = TableMetaDataProviderFactory.createMetaDataProvider(dataSource, this);
this.tableColumns = reconcileColumnsToUse(declaredColumns, generatedKeyNames);
}
private TableMetaDataProvider obtainMetaDataProvider() {
Assert.state(this.metaDataProvider != null, "No TableMetaDataProvider - call processMetaData first");
return this.metaDataProvider;
}
/**
* Compare columns created from meta-data with declared columns and return a reconciled list.
* @param declaredColumns declared column names
* @param generatedKeyNames names of generated key columns
*/
protected List<String> reconcileColumnsToUse(List<String> declaredColumns, String[] generatedKeyNames) {
if (generatedKeyNames.length > 0) {
this.generatedKeyColumnsUsed = true;
}
if (!declaredColumns.isEmpty()) {
return new ArrayList<>(declaredColumns);
}
Set<String> keys = new LinkedHashSet<>(generatedKeyNames.length);
for (String key : generatedKeyNames) {
keys.add(key.toUpperCase());
}
List<String> columns = new ArrayList<>();
for (TableParameterMetaData meta : obtainMetaDataProvider().getTableParameterMetaData()) {
if (!keys.contains(meta.getParameterName().toUpperCase())) {
columns.add(meta.getParameterName());
}
}
return columns;
}
/**
* Match the provided column names and values with the list of columns used.
* @param parameterSource the parameter names and values
*/
public List<Object> matchInParameterValuesWithInsertColumns(SqlParameterSource parameterSource) {
List<Object> values = new ArrayList<>();
// For parameter source lookups we need to provide case-insensitive lookup support since the
// database meta-data is not necessarily providing case-sensitive column names
Map<String, String> caseInsensitiveParameterNames =
SqlParameterSourceUtils.extractCaseInsensitiveParameterNames(parameterSource);
for (String column : this.tableColumns) {
if (parameterSource.hasValue(column)) {
values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, column));
}
else {
String lowerCaseName = column.toLowerCase();
if (parameterSource.hasValue(lowerCaseName)) {
values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, lowerCaseName));
}
else {
String propertyName = JdbcUtils.convertUnderscoreNameToPropertyName(column);
if (parameterSource.hasValue(propertyName)) {
values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, propertyName));
}
else {
if (caseInsensitiveParameterNames.containsKey(lowerCaseName)) {
values.add(SqlParameterSourceUtils.getTypedValue(
parameterSource, caseInsensitiveParameterNames.get(lowerCaseName)));
}
else {
values.add(null);
}
}
}
}
}
return values;
}
/**
* Match the provided column names and values with the list of columns used.
* @param inParameters the parameter names and values
*/
public List<Object> matchInParameterValuesWithInsertColumns(Map<String, ?> inParameters) {
List<Object> values = new ArrayList<>(inParameters.size());
for (String column : this.tableColumns) {
Object value = inParameters.get(column);
if (value == null) {
value = inParameters.get(column.toLowerCase());
if (value == null) {
for (Map.Entry<String, ?> entry : inParameters.entrySet()) {
if (column.equalsIgnoreCase(entry.getKey())) {
value = entry.getValue();
break;
}
}
}
}
values.add(value);
}
return values;
}
/**
* Build the insert string based on configuration and meta-data information.
* @return the insert string to be used
*/
public String createInsertString(String... generatedKeyNames) {
Set<String> keys = new LinkedHashSet<>(generatedKeyNames.length);
for (String key : generatedKeyNames) {
keys.add(key.toUpperCase());
}
StringBuilder insertStatement = new StringBuilder();
insertStatement.append("INSERT INTO ");
if (getSchemaName() != null) {
insertStatement.append(getSchemaName());
insertStatement.append(".");
}
insertStatement.append(getTableName());
insertStatement.append(" (");
int columnCount = 0;
for (String columnName : getTableColumns()) {
if (!keys.contains(columnName.toUpperCase())) {
columnCount++;
if (columnCount > 1) {
insertStatement.append(", ");
}
insertStatement.append(columnName);
}
}
insertStatement.append(") VALUES(");
if (columnCount < 1) {
if (this.generatedKeyColumnsUsed) {
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate non-key columns for table '" +
getTableName() + "' so an empty insert statement is generated");
}
}
else {
throw new InvalidDataAccessApiUsageException("Unable to locate columns for table '" +
getTableName() + "' so an insert statement can't be generated");
}
}
String params = String.join(", ", Collections.nCopies(columnCount, "?"));
insertStatement.append(params);
insertStatement.append(")");
return insertStatement.toString();
}
/**
* Build the array of {@link java.sql.Types} based on configuration and meta-data information.
* @return the array of types to be used
*/
public int[] createInsertTypes() {
int[] types = new int[getTableColumns().size()];
List<TableParameterMetaData> parameters = obtainMetaDataProvider().getTableParameterMetaData();
Map<String, TableParameterMetaData> parameterMap = CollectionUtils.newLinkedHashMap(parameters.size());
for (TableParameterMetaData tpmd : parameters) {
parameterMap.put(tpmd.getParameterName().toUpperCase(), tpmd);
}
int typeIndx = 0;
for (String column : getTableColumns()) {
if (column == null) {
types[typeIndx] = SqlTypeValue.TYPE_UNKNOWN;
}
else {
TableParameterMetaData tpmd = parameterMap.get(column.toUpperCase());
if (tpmd != null) {
types[typeIndx] = tpmd.getSqlType();
}
else {
types[typeIndx] = SqlTypeValue.TYPE_UNKNOWN;
}
}
typeIndx++;
}
return types;
}
/**
* Does this database support the JDBC 3.0 feature of retrieving generated keys:
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
*/
public boolean isGetGeneratedKeysSupported() {
return obtainMetaDataProvider().isGetGeneratedKeysSupported();
}
/**
* Does this database support simple query to retrieve generated keys
* when the JDBC 3.0 feature is not supported:
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
*/
public boolean isGetGeneratedKeysSimulated() {
return obtainMetaDataProvider().isGetGeneratedKeysSimulated();
}
/**
* Does this database support a simple query to retrieve generated keys
* when the JDBC 3.0 feature is not supported:
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
* @deprecated as of 4.3.15, in favor of {@link #getSimpleQueryForGetGeneratedKey}
*/
@Deprecated
@Nullable
public String getSimulationQueryForGetGeneratedKey(String tableName, String keyColumnName) {
return getSimpleQueryForGetGeneratedKey(tableName, keyColumnName);
}
/**
* Does this database support a simple query to retrieve generated keys
* when the JDBC 3.0 feature is not supported:
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
*/
@Nullable
public String getSimpleQueryForGetGeneratedKey(String tableName, String keyColumnName) {
return obtainMetaDataProvider().getSimpleQueryForGetGeneratedKey(tableName, keyColumnName);
}
/**
* Is a column name String array for retrieving generated keys supported:
* {@link java.sql.Connection#createStruct(String, Object[])}?
*/
public boolean isGeneratedKeysColumnNameArraySupported() {
return obtainMetaDataProvider().isGeneratedKeysColumnNameArraySupported();
}
}