001: /*
002: * Copyright 2004 Clinton Begin
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: package com.ibatis.sqlmap.engine.mapping.parameter;
017:
018: import com.ibatis.sqlmap.engine.cache.CacheKey;
019: import com.ibatis.sqlmap.engine.exchange.DataExchange;
020: import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
021: import com.ibatis.sqlmap.engine.scope.ErrorContext;
022: import com.ibatis.sqlmap.engine.scope.RequestScope;
023: import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
024: import com.ibatis.sqlmap.engine.type.JdbcTypeRegistry;
025: import com.ibatis.sqlmap.engine.type.TypeHandler;
026:
027: import java.sql.PreparedStatement;
028: import java.sql.SQLException;
029: import java.sql.Types;
030: import java.util.HashMap;
031: import java.util.List;
032: import java.util.Map;
033:
034: public class BasicParameterMap implements ParameterMap {
035:
036: private String id;
037: private Class parameterClass;
038:
039: private ParameterMapping[] parameterMappings;
040: private DataExchange dataExchange;
041:
042: private String resource;
043:
044: private Map parameterMappingIndex = new HashMap();
045:
046: private SqlMapExecutorDelegate delegate;
047:
048: public BasicParameterMap(SqlMapExecutorDelegate delegate) {
049: this .delegate = delegate;
050: }
051:
052: public SqlMapExecutorDelegate getDelegate() {
053: return delegate;
054: }
055:
056: public String getId() {
057: return id;
058: }
059:
060: public void setId(String id) {
061: this .id = id;
062: }
063:
064: public Class getParameterClass() {
065: return parameterClass;
066: }
067:
068: public void setParameterClass(Class parameterClass) {
069: this .parameterClass = parameterClass;
070: }
071:
072: public DataExchange getDataExchange() {
073: return dataExchange;
074: }
075:
076: public void setDataExchange(DataExchange dataExchange) {
077: this .dataExchange = dataExchange;
078: }
079:
080: public ParameterMapping[] getParameterMappings() {
081: return parameterMappings;
082: }
083:
084: public void setParameterMappingList(List parameterMappingList) {
085: this .parameterMappings = (BasicParameterMapping[]) parameterMappingList
086: .toArray(new BasicParameterMapping[parameterMappingList
087: .size()]);
088: for (int i = 0; i < parameterMappings.length; i++) {
089: parameterMappingIndex.put(parameterMappings[i]
090: .getPropertyName(), new Integer(i));
091: }
092: Map props = new HashMap();
093: props.put("map", this );
094:
095: dataExchange = delegate.getDataExchangeFactory()
096: .getDataExchangeForClass(parameterClass);
097: dataExchange.initialize(props);
098: }
099:
100: public int getParameterIndex(String propertyName) {
101: Integer idx = null;
102: idx = (Integer) parameterMappingIndex.get(propertyName);
103: return idx == null ? -1 : idx.intValue();
104: }
105:
106: public int getParameterCount() {
107: return this .parameterMappings.length;
108: }
109:
110: /**
111: * @param ps
112: * @param parameters
113: * @throws java.sql.SQLException
114: */
115: public void setParameters(RequestScope request,
116: PreparedStatement ps, Object[] parameters)
117: throws SQLException {
118:
119: ErrorContext errorContext = request.getErrorContext();
120: errorContext.setActivity("applying a parameter map");
121: errorContext.setObjectId(this .getId());
122: errorContext.setResource(this .getResource());
123: errorContext.setMoreInfo("Check the parameter map.");
124:
125: if (parameterMappings != null) {
126: for (int i = 0; i < parameterMappings.length; i++) {
127: BasicParameterMapping mapping = (BasicParameterMapping) parameterMappings[i];
128: errorContext.setMoreInfo(mapping.getErrorString());
129: if (mapping.isInputAllowed()) {
130: setParameter(ps, mapping, parameters, i);
131: }
132: }
133: }
134: }
135:
136: public Object[] getParameterObjectValues(RequestScope request,
137: Object parameterObject) {
138: return dataExchange.getData(request, this , parameterObject);
139: }
140:
141: public CacheKey getCacheKey(RequestScope request,
142: Object parameterObject) {
143: return dataExchange.getCacheKey(request, this , parameterObject);
144: }
145:
146: public void refreshParameterObjectValues(RequestScope request,
147: Object parameterObject, Object[] values) {
148: dataExchange.setData(request, this , parameterObject, values);
149: }
150:
151: public String getResource() {
152: return resource;
153: }
154:
155: public void setResource(String resource) {
156: this .resource = resource;
157: }
158:
159: protected void setParameter(PreparedStatement ps,
160: BasicParameterMapping mapping, Object[] parameters, int i)
161: throws SQLException {
162: Object value = parameters[i];
163: // Apply Null Value
164: String nullValueString = mapping.getNullValue();
165: if (nullValueString != null) {
166: TypeHandler handler = mapping.getTypeHandler();
167: if (handler.equals(value, nullValueString)) {
168: value = null;
169: }
170: }
171:
172: // Set Parameter
173: TypeHandler typeHandler = mapping.getTypeHandler();
174: if (value != null) {
175: typeHandler.setParameter(ps, i + 1, value, mapping
176: .getJdbcTypeName());
177: } else if (typeHandler instanceof CustomTypeHandler) {
178: typeHandler.setParameter(ps, i + 1, value, mapping
179: .getJdbcTypeName());
180: } else {
181: int jdbcType = mapping.getJdbcType();
182: if (jdbcType != JdbcTypeRegistry.UNKNOWN_TYPE) {
183: ps.setNull(i + 1, jdbcType);
184: } else {
185: ps.setNull(i + 1, Types.OTHER);
186: }
187: }
188: }
189:
190: }
|