001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jdbc.core.namedparam;
018:
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Iterator;
023:
024: import org.springframework.util.Assert;
025: import org.springframework.jdbc.core.SqlParameterValue;
026:
027: /**
028: * {@link SqlParameterSource} implementation that holds a given Map of parameters.
029: *
030: * <p>This class is intended for passing in a simple Map of parameter values
031: * to the methods of the {@link NamedParameterJdbcTemplate} class.
032: *
033: * <p>The <code>addValue</code> methods on this class will make adding several
034: * values easier. The methods return a reference to the {@link MapSqlParameterSource}
035: * itself, so you can chain several method calls together within a single statement.
036: *
037: * @author Thomas Risberg
038: * @author Juergen Hoeller
039: * @since 2.0
040: * @see #addValue(String, Object)
041: * @see #addValue(String, Object, int)
042: * @see #registerSqlType
043: * @see NamedParameterJdbcTemplate
044: */
045: public class MapSqlParameterSource extends AbstractSqlParameterSource {
046:
047: private final Map values = new HashMap();
048:
049: /**
050: * Create an empty MapSqlParameterSource,
051: * with values to be added via <code>addValue</code>.
052: * @see #addValue(String, Object)
053: */
054: public MapSqlParameterSource() {
055: }
056:
057: /**
058: * Create a new MapSqlParameterSource, with one value
059: * comprised of the supplied arguments.
060: * @param paramName the name of the parameter
061: * @param value the value of the parameter
062: * @see #addValue(String, Object)
063: */
064: public MapSqlParameterSource(String paramName, Object value) {
065: addValue(paramName, value);
066: }
067:
068: /**
069: * Create a new MapSqlParameterSource based on a Map.
070: * @param values a Map holding existing parameter values (can be <code>null</code>)
071: */
072: public MapSqlParameterSource(Map values) {
073: addValues(values);
074: }
075:
076: /**
077: * Add a parameter to this parameter source.
078: * @param paramName the name of the parameter
079: * @param value the value of the parameter
080: * @return a reference to this parameter source,
081: * so it's possible to chain several calls together
082: */
083: public MapSqlParameterSource addValue(String paramName, Object value) {
084: Assert.notNull(paramName, "Parameter name must not be null");
085: this .values.put(paramName, value);
086: if (value != null && value instanceof SqlParameterValue) {
087: registerSqlType(paramName, ((SqlParameterValue) value)
088: .getSqlType());
089: }
090: return this ;
091: }
092:
093: /**
094: * Add a parameter to this parameter source.
095: * @param paramName the name of the parameter
096: * @param value the value of the parameter
097: * @param sqlType the SQL type of the parameter
098: * @return a reference to this parameter source,
099: * so it's possible to chain several calls together
100: */
101: public MapSqlParameterSource addValue(String paramName,
102: Object value, int sqlType) {
103: Assert.notNull(paramName, "Parameter name must not be null");
104: this .values.put(paramName, value);
105: registerSqlType(paramName, sqlType);
106: return this ;
107: }
108:
109: /**
110: * Add a Map of parameters to this parameter source.
111: * @param values a Map holding existing parameter values (can be <code>null</code>)
112: * @return a reference to this parameter source,
113: * so it's possible to chain several calls together
114: */
115: public MapSqlParameterSource addValues(Map values) {
116: if (values != null) {
117: this .values.putAll(values);
118: for (Iterator iter = values.keySet().iterator(); iter
119: .hasNext();) {
120: Object k = iter.next();
121: Object o = values.get(k);
122: if (o != null && k instanceof String
123: && o instanceof SqlParameterValue) {
124: registerSqlType((String) k, ((SqlParameterValue) o)
125: .getSqlType());
126: }
127: }
128: }
129: return this ;
130: }
131:
132: /**
133: * Expose the current parameter values as read-only Map.
134: */
135: public Map getValues() {
136: return Collections.unmodifiableMap(this .values);
137: }
138:
139: public boolean hasValue(String paramName) {
140: return this .values.containsKey(paramName);
141: }
142:
143: public Object getValue(String paramName) {
144: if (!hasValue(paramName)) {
145: throw new IllegalArgumentException(
146: "No value registered for key '" + paramName + "'");
147: }
148: return this.values.get(paramName);
149: }
150:
151: }
|