001/*
002 * Copyright 2006-2007 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.batch.item.database.support;
018
019import org.springframework.batch.item.database.ItemPreparedStatementSetter;
020import org.springframework.jdbc.core.ColumnMapRowMapper;
021import org.springframework.jdbc.core.SqlTypeValue;
022import org.springframework.jdbc.core.StatementCreatorUtils;
023import org.springframework.util.Assert;
024
025import java.sql.PreparedStatement;
026import java.sql.SQLException;
027import java.util.Map;
028
029/**
030 * <p>Implementation of the {@link ItemPreparedStatementSetter} interface that assumes all
031 * keys are contained within a {@link Map} with the column name as the key.  It assumes nothing 
032 * about ordering, and assumes that the order the entry set can be iterated over is the same as
033 * the PreparedStatement should be set.</p>
034 * 
035 * @author Lucas Ward
036 * @author Dave Syer
037 * @see ItemPreparedStatementSetter
038 * @see ColumnMapRowMapper
039 */
040public class ColumnMapItemPreparedStatementSetter implements ItemPreparedStatementSetter<Map<String, Object>> {
041
042    @Override
043        public void setValues(Map<String, Object> item, PreparedStatement ps) throws SQLException {
044                Assert.isInstanceOf(Map.class, item, "Input to map PreparedStatement parameters must be of type Map.");
045                int counter = 1;
046                for(Object value : item.values()){
047                        StatementCreatorUtils.setParameterValue(ps, counter, SqlTypeValue.TYPE_UNKNOWN, value);
048                        counter++;
049                }
050        }
051
052}