16
16
17
17
package org .springframework .jdbc .core .namedparam ;
18
18
19
+ import java .util .Arrays ;
20
+ import java .util .Collection ;
19
21
import java .util .HashMap ;
20
22
import java .util .Map ;
21
23
22
24
import org .springframework .jdbc .core .SqlParameterValue ;
23
25
import org .springframework .lang .Nullable ;
24
26
25
27
/**
26
- * Class that provides helper methods for the use of {@link SqlParameterSource}
27
- * with {@code SimpleJdbc} classes .
28
+ * Class that provides helper methods for the use of {@link SqlParameterSource},
29
+ * in particular with {@link NamedParameterJdbcTemplate} .
28
30
*
29
31
* @author Thomas Risberg
32
+ * @author Juergen Hoeller
30
33
* @since 2.5
31
34
*/
32
- public class SqlParameterSourceUtils {
35
+ public abstract class SqlParameterSourceUtils {
33
36
34
37
/**
35
- * Create an array of MapSqlParameterSource objects populated with data from the
36
- * values passed in. This will define what is included in a batch operation.
37
- * @param valueMaps array of Maps containing the values to be used
38
- * @return an array of SqlParameterSource
38
+ * Create an array of {@link SqlParameterSource} objects populated with data
39
+ * from the values passed in (either a {@link Map} or a bean object).
40
+ * This will define what is included in a batch operation.
41
+ * @param candidates object array of objects containing the values to be used
42
+ * @return an array of {@link SqlParameterSource}
43
+ * @see MapSqlParameterSource
44
+ * @see BeanPropertySqlParameterSource
45
+ * @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]))
39
46
*/
40
- public static SqlParameterSource [] createBatch (Map <String , ?>[] valueMaps ) {
41
- MapSqlParameterSource [] batch = new MapSqlParameterSource [valueMaps .length ];
42
- for (int i = 0 ; i < valueMaps .length ; i ++) {
43
- Map <String , ?> valueMap = valueMaps [i ];
44
- batch [i ] = new MapSqlParameterSource (valueMap );
47
+ @ SuppressWarnings ("unchecked" )
48
+ public static SqlParameterSource [] createBatch (Object ... candidates ) {
49
+ return createBatch (Arrays .asList (candidates ));
50
+ }
51
+
52
+ /**
53
+ * Create an array of {@link SqlParameterSource} objects populated with data
54
+ * from the values passed in (either a {@link Map} or a bean object).
55
+ * This will define what is included in a batch operation.
56
+ * @param candidates collection of objects containing the values to be used
57
+ * @return an array of {@link SqlParameterSource}
58
+ * @since 5.0.2
59
+ * @see MapSqlParameterSource
60
+ * @see BeanPropertySqlParameterSource
61
+ * @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]))
62
+ */
63
+ @ SuppressWarnings ("unchecked" )
64
+ public static SqlParameterSource [] createBatch (Collection <?> candidates ) {
65
+ SqlParameterSource [] batch = new SqlParameterSource [candidates .size ()];
66
+ int i = 0 ;
67
+ for (Object candidate : candidates ) {
68
+ batch [i ] = (candidate instanceof Map ? new MapSqlParameterSource ((Map <String , ?>) candidate ) :
69
+ new BeanPropertySqlParameterSource (candidate ));
70
+ i ++;
45
71
}
46
72
return batch ;
47
73
}
48
74
49
75
/**
50
- * Create an array of BeanPropertySqlParameterSource objects populated with data
51
- * from the values passed in. This will define what is included in a batch operation.
52
- * @param beans object array of beans containing the values to be used
53
- * @return an array of SqlParameterSource
76
+ * Create an array of {@link MapSqlParameterSource} objects populated with data from
77
+ * the values passed in. This will define what is included in a batch operation.
78
+ * @param valueMaps array of {@link Map} instances containing the values to be used
79
+ * @return an array of {@link SqlParameterSource}
80
+ * @see MapSqlParameterSource
81
+ * @see NamedParameterJdbcTemplate#batchUpdate(String, Map[])
54
82
*/
55
- public static SqlParameterSource [] createBatch (Object [] beans ) {
56
- BeanPropertySqlParameterSource [] batch = new BeanPropertySqlParameterSource [beans .length ];
57
- for (int i = 0 ; i < beans .length ; i ++) {
58
- Object bean = beans [i ];
59
- batch [i ] = new BeanPropertySqlParameterSource (bean );
83
+ public static SqlParameterSource [] createBatch (Map <String , ?>[] valueMaps ) {
84
+ SqlParameterSource [] batch = new SqlParameterSource [valueMaps .length ];
85
+ for (int i = 0 ; i < valueMaps .length ; i ++) {
86
+ batch [i ] = new MapSqlParameterSource (valueMaps [i ]);
60
87
}
61
88
return batch ;
62
89
}
63
90
64
91
/**
65
92
* Create a wrapped value if parameter has type information, plain object if not.
66
- * @param source the source of paramer values and type information
93
+ * @param source the source of parameter values and type information
67
94
* @param parameterName the name of the parameter
68
95
* @return the value object
69
96
*/
@@ -85,13 +112,13 @@ public static Object getTypedValue(SqlParameterSource source, String parameterNa
85
112
86
113
/**
87
114
* Create a Map of case insensitive parameter names together with the original name.
88
- * @param parameterSource the source of paramer names
115
+ * @param parameterSource the source of parameter names
89
116
* @return the Map that can be used for case insensitive matching of parameter names
90
117
*/
91
118
public static Map <String , String > extractCaseInsensitiveParameterNames (SqlParameterSource parameterSource ) {
92
119
Map <String , String > caseInsensitiveParameterNames = new HashMap <>();
93
120
if (parameterSource instanceof BeanPropertySqlParameterSource ) {
94
- String [] propertyNames = ((BeanPropertySqlParameterSource )parameterSource ).getReadablePropertyNames ();
121
+ String [] propertyNames = ((BeanPropertySqlParameterSource ) parameterSource ).getReadablePropertyNames ();
95
122
for (String name : propertyNames ) {
96
123
caseInsensitiveParameterNames .put (name .toLowerCase (), name );
97
124
}
0 commit comments