001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.core.simple;
018
019import java.util.Arrays;
020import java.util.Map;
021import javax.sql.DataSource;
022
023import org.springframework.jdbc.core.JdbcTemplate;
024import org.springframework.jdbc.core.namedparam.SqlParameterSource;
025import org.springframework.jdbc.support.KeyHolder;
026import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
027
028/**
029 * A SimpleJdbcInsert is a multi-threaded, reusable object providing easy insert
030 * capabilities for a table. It provides meta-data processing to simplify the code
031 * needed to construct a basic insert statement. All you need to provide is the
032 * name of the table and a Map containing the column names and the column values.
033 *
034 * <p>The meta-data processing is based on the DatabaseMetaData provided by the
035 * JDBC driver. As long as the JDBC driver can provide the names of the columns
036 * for a specified table than we can rely on this auto-detection feature. If that
037 * is not the case, then the column names must be specified explicitly.
038 *
039 * <p>The actual insert is being handled using Spring's {@link JdbcTemplate}.
040 *
041 * <p>Many of the configuration methods return the current instance of the
042 * SimpleJdbcInsert to provide the ability to chain multiple ones together
043 * in a "fluent" interface style.
044 *
045 * @author Thomas Risberg
046 * @author Juergen Hoeller
047 * @since 2.5
048 * @see java.sql.DatabaseMetaData
049 * @see org.springframework.jdbc.core.JdbcTemplate
050 */
051public class SimpleJdbcInsert extends AbstractJdbcInsert implements SimpleJdbcInsertOperations {
052
053        /**
054         * Constructor that takes one parameter with the JDBC DataSource to use when creating the
055         * JdbcTemplate.
056         * @param dataSource the {@code DataSource} to use
057         * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource
058         */
059        public SimpleJdbcInsert(DataSource dataSource) {
060                super(dataSource);
061        }
062
063        /**
064         * Alternative Constructor that takes one parameter with the JdbcTemplate to be used.
065         * @param jdbcTemplate the {@code JdbcTemplate} to use
066         * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource
067         */
068        public SimpleJdbcInsert(JdbcTemplate jdbcTemplate) {
069                super(jdbcTemplate);
070        }
071
072
073        @Override
074        public SimpleJdbcInsert withTableName(String tableName) {
075                setTableName(tableName);
076                return this;
077        }
078
079        @Override
080        public SimpleJdbcInsert withSchemaName(String schemaName) {
081                setSchemaName(schemaName);
082                return this;
083        }
084
085        @Override
086        public SimpleJdbcInsert withCatalogName(String catalogName) {
087                setCatalogName(catalogName);
088                return this;
089        }
090
091        @Override
092        public SimpleJdbcInsert usingColumns(String... columnNames) {
093                setColumnNames(Arrays.asList(columnNames));
094                return this;
095        }
096
097        @Override
098        public SimpleJdbcInsert usingGeneratedKeyColumns(String... columnNames) {
099                setGeneratedKeyNames(columnNames);
100                return this;
101        }
102
103        @Override
104        public SimpleJdbcInsertOperations withoutTableColumnMetaDataAccess() {
105                setAccessTableColumnMetaData(false);
106                return this;
107        }
108
109        @Override
110        public SimpleJdbcInsertOperations includeSynonymsForTableColumnMetaData() {
111                setOverrideIncludeSynonymsDefault(true);
112                return this;
113        }
114
115        @Override
116        public SimpleJdbcInsertOperations useNativeJdbcExtractorForMetaData(NativeJdbcExtractor nativeJdbcExtractor) {
117                setNativeJdbcExtractor(nativeJdbcExtractor);
118                return this;
119        }
120
121        @Override
122        public int execute(Map<String, ?> args) {
123                return doExecute(args);
124        }
125
126        @Override
127        public int execute(SqlParameterSource parameterSource) {
128                return doExecute(parameterSource);
129        }
130
131        @Override
132        public Number executeAndReturnKey(Map<String, ?> args) {
133                return doExecuteAndReturnKey(args);
134        }
135
136        @Override
137        public Number executeAndReturnKey(SqlParameterSource parameterSource) {
138                return doExecuteAndReturnKey(parameterSource);
139        }
140
141        @Override
142        public KeyHolder executeAndReturnKeyHolder(Map<String, ?> args) {
143                return doExecuteAndReturnKeyHolder(args);
144        }
145
146        @Override
147        public KeyHolder executeAndReturnKeyHolder(SqlParameterSource parameterSource) {
148                return doExecuteAndReturnKeyHolder(parameterSource);
149        }
150
151        @Override
152        @SuppressWarnings("unchecked")
153        public int[] executeBatch(Map<String, ?>... batch) {
154                return doExecuteBatch(batch);
155        }
156
157        @Override
158        public int[] executeBatch(SqlParameterSource... batch) {
159                return doExecuteBatch(batch);
160        }
161
162}