1 | /* |
2 | * Copyright 2006-2013 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 | package org.springframework.batch.core.resource; |
17 | |
18 | import java.sql.PreparedStatement; |
19 | import java.sql.SQLException; |
20 | import java.util.List; |
21 | |
22 | import org.springframework.batch.item.database.JdbcCursorItemReader; |
23 | import org.springframework.beans.factory.InitializingBean; |
24 | import org.springframework.jdbc.core.PreparedStatementSetter; |
25 | import org.springframework.jdbc.core.SqlTypeValue; |
26 | import org.springframework.jdbc.core.StatementCreatorUtils; |
27 | import org.springframework.util.Assert; |
28 | |
29 | /** |
30 | * Implementation of the {@link PreparedStatementSetter} interface that accepts |
31 | * a list of values to be set on a PreparedStatement. This is usually used in |
32 | * conjunction with the {@link JdbcCursorItemReader} to allow for the replacement |
33 | * of bind variables when generating the cursor. The order of the list will be |
34 | * used to determine the ordering of setting variables. For example, the first |
35 | * item in the list will be the first bind variable set. (i.e. it will |
36 | * correspond to the first '?' in the SQL statement) |
37 | * |
38 | * @author Lucas Ward |
39 | * |
40 | */ |
41 | public class ListPreparedStatementSetter implements |
42 | PreparedStatementSetter, InitializingBean { |
43 | |
44 | private List<?> parameters; |
45 | |
46 | @Override |
47 | public void setValues(PreparedStatement ps) throws SQLException { |
48 | for (int i = 0; i < parameters.size(); i++) { |
49 | StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, parameters.get(i)); |
50 | } |
51 | } |
52 | |
53 | /** |
54 | * The parameter values that will be set on the PreparedStatement. |
55 | * It is assumed that their order in the List is the order of the parameters |
56 | * in the PreparedStatement. |
57 | */ |
58 | public void setParameters(List<?> parameters) { |
59 | this.parameters = parameters; |
60 | } |
61 | |
62 | @Override |
63 | public void afterPropertiesSet() throws Exception { |
64 | Assert.notNull(parameters, "Parameters must be provided"); |
65 | } |
66 | } |