View Javadoc

1   /*
2    * Copyright 2006-2007 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.batch.item.database.support;
18  
19  import java.sql.PreparedStatement;
20  import java.sql.SQLException;
21  import java.util.Map;
22  
23  import org.springframework.batch.item.database.ItemPreparedStatementSetter;
24  import org.springframework.jdbc.core.ColumnMapRowMapper;
25  import org.springframework.jdbc.core.SqlTypeValue;
26  import org.springframework.jdbc.core.StatementCreatorUtils;
27  import org.springframework.util.Assert;
28  
29  /**
30   * </p>Implementation of the {@link ItemPreparedStatementSetter} interface that assumes all
31   * keys are contained within a {@link Map} with the column name as the key.  It assumes nothing 
32   * about ordering, and assumes that the order the entry set can be iterated over is the same as
33   * the PreparedStatement should be set.</p>
34   * 
35   * @author Lucas Ward
36   * @author Dave Syer
37   * @see ItemPreparedStatementSetter
38   * @see ColumnMapRowMapper
39   */
40  public class ColumnMapItemPreparedStatementSetter implements ItemPreparedStatementSetter<Map<String, Object>> {
41  
42      @Override
43  	public void setValues(Map<String, Object> item, PreparedStatement ps) throws SQLException {
44  		Assert.isInstanceOf(Map.class, item, "Input to map PreparedStatement parameters must be of type Map.");
45  		int counter = 1;
46  		for(Object value : item.values()){
47  			StatementCreatorUtils.setParameterValue(ps, counter, SqlTypeValue.TYPE_UNKNOWN, value);
48  			counter++;
49  		}
50  	}
51  
52  }