01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.jdbc.core.namedparam;
18:
19: import java.util.Map;
20:
21: import org.springframework.jdbc.core.SqlParameterValue;
22:
23: /**
24: * Class that provides helper methods for the use of {@link SqlParameterSource}
25: * with <code>SimpleJdbc</code> classes.
26: *
27: * @author Thomas Risberg
28: * @since 2.5
29: * @see org.springframework.jdbc.core.simple.SimpleJdbcTemplate
30: */
31: public class SqlParameterSourceUtils {
32:
33: /**
34: * Create an array of MapSqlParameterSource objects populated with data from the
35: * values passed in. This will define what is included in a batch operation.
36: * @param valueMaps array of Maps containing the values to be used
37: * @return an array of SqlParameterSource
38: */
39: public static SqlParameterSource[] createBatch(Map[] valueMaps) {
40: MapSqlParameterSource[] batch = new MapSqlParameterSource[valueMaps.length];
41: for (int i = 0; i < valueMaps.length; i++) {
42: Map valueMap = valueMaps[i];
43: batch[i] = new MapSqlParameterSource(valueMap);
44: }
45: return batch;
46: }
47:
48: /**
49: * Create an array of BeanPropertySqlParameterSource objects populated with data
50: * from the values passed in. This will define what is included in a batch operation.
51: * @param beans object array of beans containing the values to be used
52: * @return an array of SqlParameterSource
53: */
54: public static SqlParameterSource[] createBatch(Object[] beans) {
55: BeanPropertySqlParameterSource[] batch = new BeanPropertySqlParameterSource[beans.length];
56: for (int i = 0; i < beans.length; i++) {
57: Object bean = beans[i];
58: batch[i] = new BeanPropertySqlParameterSource(bean);
59: }
60: return batch;
61: }
62:
63: /**
64: * Create a wrapped value if parameter has type information, plain object if not.
65: * @param source the source of paramer values and type information
66: * @param parameterName the name of the parameter
67: * @return the value object
68: */
69: public static Object getTypedValue(SqlParameterSource source,
70: String parameterName) {
71: int sqlType = source.getSqlType(parameterName);
72: if (sqlType != SqlParameterSource.TYPE_UNKNOWN) {
73: return new SqlParameterValue(sqlType, source
74: .getValue(parameterName));
75: } else {
76: return source.getValue(parameterName);
77: }
78: }
79:
80: }
|