View Javadoc
1   /*
2    * Copyright 2004-2023 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    *    https://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.common.resources.Resources;
19  import com.ibatis.sqlmap.client.SqlMapException;
20  import com.ibatis.sqlmap.engine.type.JdbcTypeRegistry;
21  import com.ibatis.sqlmap.engine.type.TypeHandler;
22  
23  /**
24   * The Class ParameterMapping.
25   */
26  public class ParameterMapping {
27  
28    /** The Constant MODE_INOUT. */
29    private static final String MODE_INOUT = "INOUT";
30  
31    /** The Constant MODE_OUT. */
32    private static final String MODE_OUT = "OUT";
33  
34    /** The Constant MODE_IN. */
35    private static final String MODE_IN = "IN";
36  
37    /** The property name. */
38    private String propertyName;
39  
40    /** The type handler. */
41    private TypeHandler typeHandler;
42  
43    /** The type name. */
44    private String typeName; // this is used for REF types or user-defined types
45  
46    /** The jdbc type. */
47    private int jdbcType;
48  
49    /** The jdbc type name. */
50    private String jdbcTypeName;
51  
52    /** The null value. */
53    private String nullValue;
54  
55    /** The mode. */
56    private String mode;
57  
58    /** The input allowed. */
59    private boolean inputAllowed;
60  
61    /** The output allowed. */
62    private boolean outputAllowed;
63  
64    /** The java type. */
65    private Class javaType;
66  
67    /** The result map name. */
68    private String resultMapName;
69  
70    /** The numeric scale. */
71    private Integer numericScale;
72  
73    /** The error string. */
74    private String errorString;
75  
76    /**
77     * Instantiates a new parameter mapping.
78     */
79    public ParameterMapping() {
80      mode = "IN";
81      inputAllowed = true;
82      outputAllowed = false;
83      // Default JDBC type if UNKNOWN_TYPE
84      jdbcType = JdbcTypeRegistry.UNKNOWN_TYPE;
85    }
86  
87    /**
88     * Gets the null value.
89     *
90     * @return the null value
91     */
92    public String getNullValue() {
93      return nullValue;
94    }
95  
96    /**
97     * Sets the null value.
98     *
99     * @param nullValue
100    *          the new null value
101    */
102   public void setNullValue(String nullValue) {
103     this.nullValue = nullValue;
104   }
105 
106   /**
107    * Gets the property name.
108    *
109    * @return the property name
110    */
111   public String getPropertyName() {
112     return propertyName;
113   }
114 
115   /**
116    * Sets the property name.
117    *
118    * @param propertyName
119    *          the new property name
120    */
121   public void setPropertyName(String propertyName) {
122     this.errorString = "Check the parameter mapping for the '" + propertyName + "' property.";
123     this.propertyName = propertyName;
124   }
125 
126   /**
127    * Gets the error string.
128    *
129    * @return the error string
130    */
131   public String getErrorString() {
132     return errorString;
133   }
134 
135   /**
136    * Gets the type handler.
137    *
138    * @return the type handler
139    */
140   public TypeHandler getTypeHandler() {
141     return typeHandler;
142   }
143 
144   /**
145    * Sets the type handler.
146    *
147    * @param typeHandler
148    *          the new type handler
149    */
150   public void setTypeHandler(TypeHandler typeHandler) {
151     this.typeHandler = typeHandler;
152   }
153 
154   /**
155    * Gets the java type.
156    *
157    * @return the java type
158    */
159   public Class getJavaType() {
160     return javaType;
161   }
162 
163   /**
164    * Sets the java type.
165    *
166    * @param javaType
167    *          the new java type
168    */
169   public void setJavaType(Class javaType) {
170     this.javaType = javaType;
171   }
172 
173   /**
174    * Gets the java type name.
175    *
176    * @return the java type name
177    */
178   public String getJavaTypeName() {
179     if (javaType == null) {
180       return null;
181     } else {
182       return javaType.getName();
183     }
184   }
185 
186   /**
187    * Sets the java type name.
188    *
189    * @param javaTypeName
190    *          the new java type name
191    */
192   public void setJavaTypeName(String javaTypeName) {
193     try {
194       if (javaTypeName == null) {
195         this.javaType = null;
196       } else {
197         this.javaType = Resources.classForName(javaTypeName);
198       }
199     } catch (ClassNotFoundException e) {
200       throw new SqlMapException("Error setting javaType property of ParameterMap.  Cause: " + e, e);
201     }
202   }
203 
204   /**
205    * Gets the jdbc type.
206    *
207    * @return the jdbc type
208    */
209   public int getJdbcType() {
210     return jdbcType;
211   }
212 
213   /**
214    * Gets the jdbc type name.
215    *
216    * @return the jdbc type name
217    */
218   public String getJdbcTypeName() {
219     return jdbcTypeName;
220   }
221 
222   /**
223    * Sets the jdbc type name.
224    *
225    * @param jdbcTypeName
226    *          the new jdbc type name
227    */
228   public void setJdbcTypeName(String jdbcTypeName) {
229     this.jdbcTypeName = jdbcTypeName;
230     this.jdbcType = JdbcTypeRegistry.getType(jdbcTypeName);
231   }
232 
233   /**
234    * Gets the mode.
235    *
236    * @return the mode
237    */
238   public String getMode() {
239     return mode;
240   }
241 
242   /**
243    * Sets the mode.
244    *
245    * @param mode
246    *          the new mode
247    */
248   public void setMode(String mode) {
249     this.mode = mode;
250     inputAllowed = MODE_IN.equals(mode) || MODE_INOUT.equals(mode);
251     outputAllowed = MODE_OUT.equals(mode) || MODE_INOUT.equals(mode);
252   }
253 
254   /**
255    * Checks if is input allowed.
256    *
257    * @return true, if is input allowed
258    */
259   public boolean isInputAllowed() {
260     return inputAllowed;
261   }
262 
263   /**
264    * Checks if is output allowed.
265    *
266    * @return true, if is output allowed
267    */
268   public boolean isOutputAllowed() {
269     return outputAllowed;
270   }
271 
272   /**
273    * user-defined or REF types.
274    *
275    * @return typeName
276    */
277   public String getTypeName() {
278     return typeName;
279   }
280 
281   /**
282    * for user-defined or REF types.
283    *
284    * @param typeName
285    *          the new type name
286    */
287   public void setTypeName(String typeName) {
288     this.typeName = typeName;
289   }
290 
291   /**
292    * Gets the result map name.
293    *
294    * @return the result map name
295    */
296   public String getResultMapName() {
297     return resultMapName;
298   }
299 
300   /**
301    * Sets the result map name.
302    *
303    * @param resultMapName
304    *          the new result map name
305    */
306   public void setResultMapName(String resultMapName) {
307     this.resultMapName = resultMapName;
308   }
309 
310   /**
311    * Gets the numeric scale.
312    *
313    * @return the numeric scale
314    */
315   public Integer getNumericScale() {
316     return numericScale;
317   }
318 
319   /**
320    * Sets the numeric scale.
321    *
322    * @param numericScale
323    *          the new numeric scale
324    */
325   public void setNumericScale(Integer numericScale) {
326     if (numericScale != null && numericScale.intValue() < 0) {
327       throw new RuntimeException(
328           "Error setting numericScale on parameter mapping.  Cause: scale must be greater than or equal to zero");
329     }
330     this.numericScale = numericScale;
331   }
332 
333 }