org.springframework.jdbc.core.simple.AbstractJdbcInsert 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.simple;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.core.StatementCreatorUtils;
import org.springframework.jdbc.core.metadata.TableMetaDataContext;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Abstract class to provide base functionality for easy inserts
* based on configuration options and database meta-data.
*
* This class provides the base SPI for {@link SimpleJdbcInsert}.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 2.5
*/
public abstract class AbstractJdbcInsert {
/** Logger available to subclasses. */
protected final Log logger = LogFactory.getLog(getClass());
/** Lower-level class used to execute SQL. */
private final JdbcTemplate jdbcTemplate;
/** Context used to retrieve and manage database meta-data. */
private final TableMetaDataContext tableMetaDataContext = new TableMetaDataContext();
/** List of columns objects to be used in insert statement. */
private final List declaredColumns = new ArrayList<>();
/** The names of the columns holding the generated key. */
private String[] generatedKeyNames = new String[0];
/**
* Has this operation been compiled? Compilation means at least checking
* that a DataSource or JdbcTemplate has been provided.
*/
private volatile boolean compiled = false;
/** The generated string used for insert statement. */
private String insertString = "";
/** The SQL type information for the insert columns. */
private int[] insertTypes = new int[0];
/**
* Constructor to be used when initializing using a {@link DataSource}.
* @param dataSource the DataSource to be used
*/
protected AbstractJdbcInsert(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/**
* Constructor to be used when initializing using a {@link JdbcTemplate}.
* @param jdbcTemplate the JdbcTemplate to use
*/
protected AbstractJdbcInsert(JdbcTemplate jdbcTemplate) {
Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null");
this.jdbcTemplate = jdbcTemplate;
}
//-------------------------------------------------------------------------
// Methods dealing with configuration properties
//-------------------------------------------------------------------------
/**
* Get the configured {@link JdbcTemplate}.
*/
public JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
/**
* Set the name of the table for this insert.
*/
public void setTableName(@Nullable String tableName) {
checkIfConfigurationModificationIsAllowed();
this.tableMetaDataContext.setTableName(tableName);
}
/**
* Get the name of the table for this insert.
*/
@Nullable
public String getTableName() {
return this.tableMetaDataContext.getTableName();
}
/**
* Set the name of the schema for this insert.
*/
public void setSchemaName(@Nullable String schemaName) {
checkIfConfigurationModificationIsAllowed();
this.tableMetaDataContext.setSchemaName(schemaName);
}
/**
* Get the name of the schema for this insert.
*/
@Nullable
public String getSchemaName() {
return this.tableMetaDataContext.getSchemaName();
}
/**
* Set the name of the catalog for this insert.
*/
public void setCatalogName(@Nullable String catalogName) {
checkIfConfigurationModificationIsAllowed();
this.tableMetaDataContext.setCatalogName(catalogName);
}
/**
* Get the name of the catalog for this insert.
*/
@Nullable
public String getCatalogName() {
return this.tableMetaDataContext.getCatalogName();
}
/**
* Set the names of the columns to be used.
*/
public void setColumnNames(List columnNames) {
checkIfConfigurationModificationIsAllowed();
this.declaredColumns.clear();
this.declaredColumns.addAll(columnNames);
}
/**
* Get the names of the columns used.
*/
public List getColumnNames() {
return Collections.unmodifiableList(this.declaredColumns);
}
/**
* Specify the name of a single generated key column.
*/
public void setGeneratedKeyName(String generatedKeyName) {
checkIfConfigurationModificationIsAllowed();
this.generatedKeyNames = new String[] {generatedKeyName};
}
/**
* Set the names of any generated keys.
*/
public void setGeneratedKeyNames(String... generatedKeyNames) {
checkIfConfigurationModificationIsAllowed();
this.generatedKeyNames = generatedKeyNames;
}
/**
* Get the names of any generated keys.
*/
public String[] getGeneratedKeyNames() {
return this.generatedKeyNames;
}
/**
* Specify whether the parameter meta-data for the call should be used.
* The default is {@code true}.
*/
public void setAccessTableColumnMetaData(boolean accessTableColumnMetaData) {
this.tableMetaDataContext.setAccessTableColumnMetaData(accessTableColumnMetaData);
}
/**
* Specify whether the default for including synonyms should be changed.
* The default is {@code false}.
*/
public void setOverrideIncludeSynonymsDefault(boolean override) {
this.tableMetaDataContext.setOverrideIncludeSynonymsDefault(override);
}
/**
* Get the insert string to be used.
*/
public String getInsertString() {
return this.insertString;
}
/**
* Get the array of {@link java.sql.Types} to be used for insert.
*/
public int[] getInsertTypes() {
return this.insertTypes;
}
//-------------------------------------------------------------------------
// Methods handling compilation issues
//-------------------------------------------------------------------------
/**
* Compile this JdbcInsert using provided parameters and meta-data plus other settings.
* This finalizes the configuration for this object and subsequent attempts to compile are
* ignored. This will be implicitly called the first time an un-compiled insert is executed.
* @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized,
* for example if no DataSource has been provided
*/
public final synchronized void compile() throws InvalidDataAccessApiUsageException {
if (!isCompiled()) {
if (getTableName() == null) {
throw new InvalidDataAccessApiUsageException("Table name is required");
}
try {
this.jdbcTemplate.afterPropertiesSet();
}
catch (IllegalArgumentException ex) {
throw new InvalidDataAccessApiUsageException(ex.getMessage());
}
compileInternal();
this.compiled = true;
if (logger.isDebugEnabled()) {
logger.debug("JdbcInsert for table [" + getTableName() + "] compiled");
}
}
}
/**
* Delegate method to perform the actual compilation.
* Subclasses can override this template method to perform their own compilation.
* Invoked after this base class's compilation is complete.
*/
protected void compileInternal() {
DataSource dataSource = getJdbcTemplate().getDataSource();
Assert.state(dataSource != null, "No DataSource set");
this.tableMetaDataContext.processMetaData(dataSource, getColumnNames(), getGeneratedKeyNames());
this.insertString = this.tableMetaDataContext.createInsertString(getGeneratedKeyNames());
this.insertTypes = this.tableMetaDataContext.createInsertTypes();
if (logger.isDebugEnabled()) {
logger.debug("Compiled insert object: insert string is [" + this.insertString + "]");
}
onCompileInternal();
}
/**
* Hook method that subclasses may override to react to compilation.
*
This implementation is empty.
*/
protected void onCompileInternal() {
}
/**
* Is this operation "compiled"?
* @return whether this operation is compiled and ready to use
*/
public boolean isCompiled() {
return this.compiled;
}
/**
* Check whether this operation has been compiled already;
* lazily compile it if not already compiled.
*
Automatically called by {@code validateParameters}.
*/
protected void checkCompiled() {
if (!isCompiled()) {
logger.debug("JdbcInsert not compiled before execution - invoking compile");
compile();
}
}
/**
* Method to check whether we are allowed to make any configuration changes at this time.
* If the class has been compiled, then no further changes to the configuration are allowed.
*/
protected void checkIfConfigurationModificationIsAllowed() {
if (isCompiled()) {
throw new InvalidDataAccessApiUsageException(
"Configuration cannot be altered once the class has been compiled or used");
}
}
//-------------------------------------------------------------------------
// Methods handling execution
//-------------------------------------------------------------------------
/**
* Delegate method that executes the insert using the passed-in Map of parameters.
* @param args a Map with parameter names and values to be used in insert
* @return the number of rows affected
*/
protected int doExecute(Map args) {
checkCompiled();
List