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:
023: import org.springframework.util.Assert;
024:
025: /**
026: * {@link SqlParameterSource} implementation that holds a given Map of parameters.
027: *
028: * <p>This class is intended for passing in a simple Map of parameter values
029: * to the methods of the {@link NamedParameterJdbcTemplate} class.
030: *
031: * <p>The <code>addValue</code> methods on this class will make adding several
032: * values easier. The methods return a reference to the {@link MapSqlParameterSource}
033: * itself, so you can chain several method calls together within a single statement.
034: *
035: * @author Thomas Risberg
036: * @author Juergen Hoeller
037: * @since 2.0
038: * @see #addValue(String, Object)
039: * @see #addValue(String, Object, int)
040: * @see #registerSqlType
041: * @see NamedParameterJdbcTemplate
042: */
043: public class MapSqlParameterSource extends AbstractSqlParameterSource {
044:
045: private final Map values = new HashMap();
046:
047: /**
048: * Create an empty MapSqlParameterSource,
049: * with values to be added via <code>addValue</code>.
050: * @see #addValue(String, Object)
051: */
052: public MapSqlParameterSource() {
053: }
054:
055: /**
056: * Create a new MapSqlParameterSource, with one value
057: * comprised of the supplied arguments.
058: * @param paramName the name of the parameter
059: * @param value the value of the parameter
060: * @see #addValue(String, Object)
061: */
062: public MapSqlParameterSource(String paramName, Object value) {
063: addValue(paramName, value);
064: }
065:
066: /**
067: * Create a new MapSqlParameterSource based on a Map.
068: * @param values a Map holding existing parameter values (can be <code>null</code>)
069: */
070: public MapSqlParameterSource(Map values) {
071: addValues(values);
072: }
073:
074: /**
075: * Add a parameter to this parameter source.
076: * @param paramName the name of the parameter
077: * @param value the value of the parameter
078: * @return a reference to this parameter source,
079: * so it's possible to chain several calls together
080: */
081: public MapSqlParameterSource addValue(String paramName, Object value) {
082: Assert.notNull(paramName, "Parameter name must not be null");
083: this .values.put(paramName, value);
084: return this ;
085: }
086:
087: /**
088: * Add a parameter to this parameter source.
089: * @param paramName the name of the parameter
090: * @param value the value of the parameter
091: * @param sqlType the SQL type of the parameter
092: * @return a reference to this parameter source,
093: * so it's possible to chain several calls together
094: */
095: public MapSqlParameterSource addValue(String paramName,
096: Object value, int sqlType) {
097: Assert.notNull(paramName, "Parameter name must not be null");
098: this .values.put(paramName, value);
099: registerSqlType(paramName, sqlType);
100: return this ;
101: }
102:
103: /**
104: * Add a Map of parameters to this parameter source.
105: * @param values a Map holding existing parameter values (can be <code>null</code>)
106: * @return a reference to this parameter source,
107: * so it's possible to chain several calls together
108: */
109: public MapSqlParameterSource addValues(Map values) {
110: if (values != null) {
111: this .values.putAll(values);
112: }
113: return this ;
114: }
115:
116: /**
117: * Expose the current parameter values as read-only Map.
118: */
119: public Map getValues() {
120: return Collections.unmodifiableMap(this .values);
121: }
122:
123: public boolean hasValue(String paramName) {
124: return this .values.containsKey(paramName);
125: }
126:
127: public Object getValue(String paramName) {
128: if (!hasValue(paramName)) {
129: throw new IllegalArgumentException(
130: "No value registered for key '" + paramName + "'");
131: }
132: return this.values.get(paramName);
133: }
134:
135: }
|