1 /* 2 * Copyright 2002-2008 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.jdbc.core.simple; 18 19 import java.util.Arrays; 20 import java.util.Map; 21 22 import javax.sql.DataSource; 23 24 import org.springframework.jdbc.core.JdbcTemplate; 25 import org.springframework.jdbc.core.namedparam.SqlParameterSource; 26 import org.springframework.jdbc.support.KeyHolder; 27 import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor; 28 29 /** 30 * A SimpleJdbcInsert is a multi-threaded, reusable object providing easy insert 31 * capabilities for a table. It provides meta data processing to simplify the code 32 * needed to construct a basic insert statement. All you need to provide is the 33 * name of the table and a Map containing the column names and the column values. 34 * 35 * <p>The meta data processing is based on the DatabaseMetaData provided by the 36 * JDBC driver. As long as the JBDC driver can provide the names of the columns 37 * for a specified table than we can rely on this auto-detection feature. If that 38 * is not the case then the column names must be specified explicitly. 39 * 40 * <p>The actual insert is being handled using Spring's 41 * {@link org.springframework.jdbc.core.JdbcTemplate}. 42 * 43 * <p>Many of the configuration methods return the current instance of the SimpleJdbcInsert 44 * to provide the ability to string multiple ones together in a "fluid" interface style. 45 * 46 * @author Thomas Risberg 47 * @since 2.5 48 * @see java.sql.DatabaseMetaData 49 * @see org.springframework.jdbc.core.JdbcTemplate 50 */ 51 public class SimpleJdbcInsert extends AbstractJdbcInsert implements SimpleJdbcInsertOperations { 52 53 /** 54 * Constructor that takes one parameter with the JDBC DataSource to use when creating the 55 * JdbcTemplate. 56 * @param dataSource the <code>DataSource</code> to use 57 * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource 58 */ SimpleJdbcInsert(DataSource dataSource)59 public SimpleJdbcInsert(DataSource dataSource) { 60 super(dataSource); 61 } 62 63 /** 64 * Alternative Constructor that takes one parameter with the JdbcTemplate to be used. 65 * @param jdbcTemplate the <code>JdbcTemplate</code> to use 66 * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource 67 */ SimpleJdbcInsert(JdbcTemplate jdbcTemplate)68 public SimpleJdbcInsert(JdbcTemplate jdbcTemplate) { 69 super(jdbcTemplate); 70 } 71 72 withTableName(String tableName)73 public SimpleJdbcInsert withTableName(String tableName) { 74 setTableName(tableName); 75 return this; 76 } 77 withSchemaName(String schemaName)78 public SimpleJdbcInsert withSchemaName(String schemaName) { 79 setSchemaName(schemaName); 80 return this; 81 } 82 withCatalogName(String catalogName)83 public SimpleJdbcInsert withCatalogName(String catalogName) { 84 setCatalogName(catalogName); 85 return this; 86 } 87 usingColumns(String... columnNames)88 public SimpleJdbcInsert usingColumns(String... columnNames) { 89 setColumnNames(Arrays.asList(columnNames)); 90 return this; 91 } 92 usingGeneratedKeyColumns(String... columnNames)93 public SimpleJdbcInsert usingGeneratedKeyColumns(String... columnNames) { 94 setGeneratedKeyNames(columnNames); 95 return this; 96 } 97 withoutTableColumnMetaDataAccess()98 public SimpleJdbcInsertOperations withoutTableColumnMetaDataAccess() { 99 setAccessTableColumnMetaData(false); 100 return this; 101 } 102 includeSynonymsForTableColumnMetaData()103 public SimpleJdbcInsertOperations includeSynonymsForTableColumnMetaData() { 104 setOverrideIncludeSynonymsDefault(true); 105 return this; 106 } 107 useNativeJdbcExtractorForMetaData(NativeJdbcExtractor nativeJdbcExtractor)108 public SimpleJdbcInsertOperations useNativeJdbcExtractorForMetaData(NativeJdbcExtractor nativeJdbcExtractor) { 109 setNativeJdbcExtractor(nativeJdbcExtractor); 110 return this; 111 } 112 execute(Map<String, Object> args)113 public int execute(Map<String, Object> args) { 114 return doExecute(args); 115 } 116 execute(SqlParameterSource parameterSource)117 public int execute(SqlParameterSource parameterSource) { 118 return doExecute(parameterSource); 119 } 120 executeAndReturnKey(Map<String, Object> args)121 public Number executeAndReturnKey(Map<String, Object> args) { 122 return doExecuteAndReturnKey(args); 123 } 124 executeAndReturnKey(SqlParameterSource parameterSource)125 public Number executeAndReturnKey(SqlParameterSource parameterSource) { 126 return doExecuteAndReturnKey(parameterSource); 127 } 128 executeAndReturnKeyHolder(Map<String, Object> args)129 public KeyHolder executeAndReturnKeyHolder(Map<String, Object> args) { 130 return doExecuteAndReturnKeyHolder(args); 131 } 132 executeAndReturnKeyHolder(SqlParameterSource parameterSource)133 public KeyHolder executeAndReturnKeyHolder(SqlParameterSource parameterSource) { 134 return doExecuteAndReturnKeyHolder(parameterSource); 135 } 136 executeBatch(Map<String, Object>[] batch)137 public int[] executeBatch(Map<String, Object>[] batch) { 138 return doExecuteBatch(batch); 139 } 140 executeBatch(SqlParameterSource[] batch)141 public int[] executeBatch(SqlParameterSource[] batch) { 142 return doExecuteBatch(batch); 143 } 144 145 } 146