KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > namedparam > MapSqlParameterSource


1 /*
2  * Copyright 2002-2006 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.jdbc.core.namedparam;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.springframework.util.Assert;
24
25 /**
26  * {@link SqlParameterSource} implementation that holds a given Map of parameters.
27  *
28  * <p>This class is intended for passing in a simple Map of parameter values
29  * to the methods of the {@link NamedParameterJdbcTemplate} class.
30  *
31  * <p>The <code>addValue</code> methods on this class will make adding several
32  * values easier. The methods return a reference to the {@link MapSqlParameterSource}
33  * itself, so you can chain several method calls together within a single statement.
34  *
35  * @author Thomas Risberg
36  * @author Juergen Hoeller
37  * @since 2.0
38  * @see #addValue(String, Object)
39  * @see #addValue(String, Object, int)
40  * @see #registerSqlType
41  * @see NamedParameterJdbcTemplate
42  */

43 public class MapSqlParameterSource extends AbstractSqlParameterSource {
44
45     private final Map JavaDoc values = new HashMap JavaDoc();
46
47
48     /**
49      * Create an empty MapSqlParameterSource,
50      * with values to be added via <code>addValue</code>.
51      * @see #addValue(String, Object)
52      */

53     public MapSqlParameterSource() {
54     }
55
56     /**
57      * Create a new MapSqlParameterSource, with one value
58      * comprised of the supplied arguments.
59      * @param paramName the name of the parameter
60      * @param value the value of the parameter
61      * @see #addValue(String, Object)
62      */

63     public MapSqlParameterSource(String JavaDoc paramName, Object JavaDoc value) {
64         addValue(paramName, value);
65     }
66
67     /**
68      * Create a new MapSqlParameterSource based on a Map.
69      * @param values a Map holding existing parameter values (can be <code>null</code>)
70      */

71     public MapSqlParameterSource(Map JavaDoc values) {
72         addValues(values);
73     }
74
75
76     /**
77      * Add a parameter to this parameter source.
78      * @param paramName the name of the parameter
79      * @param value the value of the parameter
80      * @return a reference to this parameter source,
81      * so it's possible to chain several calls together
82      */

83     public MapSqlParameterSource addValue(String JavaDoc paramName, Object JavaDoc value) {
84         Assert.notNull(paramName, "Parameter name must not be null");
85         this.values.put(paramName, value);
86         return this;
87     }
88
89     /**
90      * Add a parameter to this parameter source.
91      * @param paramName the name of the parameter
92      * @param value the value of the parameter
93      * @param sqlType the SQL type of the parameter
94      * @return a reference to this parameter source,
95      * so it's possible to chain several calls together
96      */

97     public MapSqlParameterSource addValue(String JavaDoc paramName, Object JavaDoc value, int sqlType) {
98         Assert.notNull(paramName, "Parameter name must not be null");
99         this.values.put(paramName, value);
100         registerSqlType(paramName, sqlType);
101         return this;
102     }
103
104     /**
105      * Add a Map of parameters to this parameter source.
106      * @param values a Map holding existing parameter values (can be <code>null</code>)
107      * @return a reference to this parameter source,
108      * so it's possible to chain several calls together
109      */

110     public MapSqlParameterSource addValues(Map JavaDoc values) {
111         if (values != null) {
112             this.values.putAll(values);
113         }
114         return this;
115     }
116
117     /**
118      * Expose the current parameter values as read-only Map.
119      */

120     public Map JavaDoc getValues() {
121         return Collections.unmodifiableMap(this.values);
122     }
123
124
125     public boolean hasValue(String JavaDoc paramName) {
126         return this.values.containsKey(paramName);
127     }
128
129     public Object JavaDoc getValue(String JavaDoc paramName) {
130         if (!hasValue(paramName)) {
131             throw new IllegalArgumentException JavaDoc("No value registered for key '" + paramName + "'");
132         }
133         return this.values.get(paramName);
134     }
135
136 }
137
Popular Tags