org.springframework.jdbc.core.metadata.TableMetaDataContext Maven / Gradle / Ivy
/*
* Copyright 2002-2018 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
*
* http://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.LinkedHashMap;
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;
/**
* 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 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 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 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 reconcileColumnsToUse(List declaredColumns, String[] generatedKeyNames) {
if (generatedKeyNames.length > 0) {
this.generatedKeyColumnsUsed = true;
}
if (!declaredColumns.isEmpty()) {
return new ArrayList<>(declaredColumns);
}
Set keys = new LinkedHashSet<>(generatedKeyNames.length);
for (String key : generatedKeyNames) {
keys.add(key.toUpperCase());
}
List 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