Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/ibatis/sqlmap/engine/mapping/parameter/BasicParameterMap.java


1   /*
2    *  Copyright 2004 Clinton Begin
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  package com.ibatis.sqlmap.engine.mapping.parameter;
17  
18  import com.ibatis.sqlmap.engine.cache.CacheKey;
19  import com.ibatis.sqlmap.engine.exchange.DataExchange;
20  import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
21  import com.ibatis.sqlmap.engine.scope.ErrorContext;
22  import com.ibatis.sqlmap.engine.scope.RequestScope;
23  import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
24  import com.ibatis.sqlmap.engine.type.JdbcTypeRegistry;
25  import com.ibatis.sqlmap.engine.type.TypeHandler;
26  
27  import java.sql.PreparedStatement;
28  import java.sql.SQLException;
29  import java.sql.Types;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  public class BasicParameterMap implements ParameterMap {
35  
36    private String id;
37    private Class parameterClass;
38  
39    private ParameterMapping[] parameterMappings;
40    private DataExchange dataExchange;
41  
42    private String resource;
43  
44    private Map parameterMappingIndex = new HashMap();
45  
46    private SqlMapExecutorDelegate delegate;
47  
48    public BasicParameterMap(SqlMapExecutorDelegate delegate) {
49      this.delegate = delegate;
50    }
51  
52    public SqlMapExecutorDelegate getDelegate() {
53      return delegate;
54    }
55  
56    public String getId() {
57      return id;
58    }
59  
60    public void setId(String id) {
61      this.id = id;
62    }
63  
64    public Class getParameterClass() {
65      return parameterClass;
66    }
67  
68    public void setParameterClass(Class parameterClass) {
69      this.parameterClass = parameterClass;
70    }
71  
72    public DataExchange getDataExchange() {
73      return dataExchange;
74    }
75  
76    public void setDataExchange(DataExchange dataExchange) {
77      this.dataExchange = dataExchange;
78    }
79  
80    public ParameterMapping[] getParameterMappings() {
81      return parameterMappings;
82    }
83  
84    public void setParameterMappingList(List parameterMappingList) {
85      this.parameterMappings = (BasicParameterMapping[]) parameterMappingList.toArray(new BasicParameterMapping[parameterMappingList.size()]);
86      for (int i = 0; i < parameterMappings.length; i++) {
87        parameterMappingIndex.put(parameterMappings[i].getPropertyName(), new Integer(i));
88      }
89      Map props = new HashMap();
90      props.put("map", this);
91  
92      dataExchange = delegate.getDataExchangeFactory().getDataExchangeForClass(parameterClass);
93      dataExchange.initialize(props);
94    }
95  
96    public int getParameterIndex(String propertyName) {
97      Integer idx = null;
98      idx = (Integer) parameterMappingIndex.get(propertyName);
99      return idx == null ? -1 : idx.intValue();
100   }
101 
102   public int getParameterCount() {
103     return this.parameterMappings.length;
104   }
105 
106   /**
107    * @param ps
108    * @param parameters
109    * @throws java.sql.SQLException
110    */
111   public void setParameters(RequestScope request, PreparedStatement ps, Object[] parameters)
112       throws SQLException {
113 
114     ErrorContext errorContext = request.getErrorContext();
115     errorContext.setActivity("applying a parameter map");
116     errorContext.setObjectId(this.getId());
117     errorContext.setResource(this.getResource());
118     errorContext.setMoreInfo("Check the parameter map.");
119 
120     if (parameterMappings != null) {
121       for (int i = 0; i < parameterMappings.length; i++) {
122         BasicParameterMapping mapping = (BasicParameterMapping) parameterMappings[i];
123         errorContext.setMoreInfo(mapping.getErrorString());
124         if (mapping.isInputAllowed()) {
125           setParameter(ps, mapping, parameters, i);
126         }
127       }
128     }
129   }
130 
131   public Object[] getParameterObjectValues(RequestScope request, Object parameterObject) {
132     return dataExchange.getData(request, this, parameterObject);
133   }
134 
135   public CacheKey getCacheKey(RequestScope request, Object parameterObject) {
136     return dataExchange.getCacheKey(request, this, parameterObject);
137   }
138 
139   public void refreshParameterObjectValues(RequestScope request, Object parameterObject, Object[] values) {
140     dataExchange.setData(request, this, parameterObject, values);
141   }
142 
143   public String getResource() {
144     return resource;
145   }
146 
147   public void setResource(String resource) {
148     this.resource = resource;
149   }
150 
151   protected void setParameter(PreparedStatement ps, BasicParameterMapping mapping, Object[] parameters, int i) throws SQLException {
152     Object value = parameters[i];
153     // Apply Null Value
154     String nullValueString = mapping.getNullValue();
155     if (nullValueString != null) {
156       TypeHandler handler = mapping.getTypeHandler();
157       if (handler.equals(value, nullValueString)) {
158         value = null;
159       }
160     }
161 
162     // Set Parameter
163     TypeHandler typeHandler = mapping.getTypeHandler();
164     if (value != null) {
165       typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
166     } else if (typeHandler instanceof CustomTypeHandler) {
167       typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
168     } else {
169       int jdbcType = mapping.getJdbcType();
170       if (jdbcType != JdbcTypeRegistry.UNKNOWN_TYPE) {
171         ps.setNull(i + 1, jdbcType);
172       } else {
173         ps.setNull(i + 1, Types.OTHER);
174       }
175     }
176   }
177 
178 }