| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package org.springframework.batch.item.database.support; |
| 5 | |
| 6 | import java.sql.PreparedStatement; |
| 7 | import java.sql.SQLException; |
| 8 | import java.util.Iterator; |
| 9 | import java.util.Map; |
| 10 | import java.util.Set; |
| 11 | import java.util.Map.Entry; |
| 12 | |
| 13 | import org.springframework.batch.item.database.ItemPreparedStatementSetter; |
| 14 | import org.springframework.jdbc.core.ColumnMapRowMapper; |
| 15 | import org.springframework.jdbc.core.SqlTypeValue; |
| 16 | import org.springframework.jdbc.core.StatementCreatorUtils; |
| 17 | import org.springframework.util.Assert; |
| 18 | |
| 19 | /** |
| 20 | * </p>Implementation of the {@link ItemPreparedStatementSetter} interface that assumes all |
| 21 | * keys are contained within a {@link Map} with the column name as the key. It assumes nothing |
| 22 | * about ordering, and assumes that the order the entry set can be iterated over is the same as |
| 23 | * the PreparedStatement should be set.</p> |
| 24 | * |
| 25 | * @author Lucas Ward |
| 26 | * @author Dave Syer |
| 27 | * @see ItemPreparedStatementSetter |
| 28 | * @see ColumnMapRowMapper |
| 29 | */ |
| 30 | public class ColumnMapItemPreparedStatementSetter implements ItemPreparedStatementSetter { |
| 31 | |
| 32 | public void setValues(Object item, PreparedStatement ps) throws SQLException { |
| 33 | Assert.isInstanceOf(Map.class, item, "Input to map PreparedStatement parameters must be of type Map."); |
| 34 | Set keySet = ((Map)item).entrySet(); |
| 35 | int counter = 1; |
| 36 | for(Iterator it = keySet.iterator(); it.hasNext();){ |
| 37 | Entry entry = (Entry)it.next(); |
| 38 | StatementCreatorUtils.setParameterValue(ps, counter, SqlTypeValue.TYPE_UNKNOWN, entry.getValue()); |
| 39 | counter++; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | } |