View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       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 org.apache.ibatis.mapping;
17  
18  import java.sql.ResultSet;
19  
20  import org.apache.ibatis.session.Configuration;
21  import org.apache.ibatis.type.JdbcType;
22  import org.apache.ibatis.type.TypeHandler;
23  import org.apache.ibatis.type.TypeHandlerRegistry;
24  
25  /**
26   * @author Clinton Begin
27   */
28  public class ParameterMapping {
29  
30    private Configuration configuration;
31  
32    private String property;
33    private ParameterMode mode;
34    private Class<?> javaType = Object.class;
35    private JdbcType jdbcType;
36    private Integer numericScale;
37    private TypeHandler<?> typeHandler;
38    private String resultMapId;
39    private String jdbcTypeName;
40    private String expression;
41  
42    private ParameterMapping() {
43    }
44  
45    public static class Builder {
46      private ParameterMapping parameterMapping = new ParameterMapping();
47  
48      public Builder(Configuration configuration, String property, TypeHandler<?> typeHandler) {
49        parameterMapping.configuration = configuration;
50        parameterMapping.property = property;
51        parameterMapping.typeHandler = typeHandler;
52        parameterMapping.mode = ParameterMode.IN;
53      }
54  
55      public Builder(Configuration configuration, String property, Class<?> javaType) {
56        parameterMapping.configuration = configuration;
57        parameterMapping.property = property;
58        parameterMapping.javaType = javaType;
59        parameterMapping.mode = ParameterMode.IN;
60      }
61  
62      public Builder mode(ParameterMode mode) {
63        parameterMapping.mode = mode;
64        return this;
65      }
66  
67      public Builder javaType(Class<?> javaType) {
68        parameterMapping.javaType = javaType;
69        return this;
70      }
71  
72      public Builder jdbcType(JdbcType jdbcType) {
73        parameterMapping.jdbcType = jdbcType;
74        return this;
75      }
76  
77      public Builder numericScale(Integer numericScale) {
78        parameterMapping.numericScale = numericScale;
79        return this;
80      }
81  
82      public Builder resultMapId(String resultMapId) {
83        parameterMapping.resultMapId = resultMapId;
84        return this;
85      }
86  
87      public Builder typeHandler(TypeHandler<?> typeHandler) {
88        parameterMapping.typeHandler = typeHandler;
89        return this;
90      }
91  
92      public Builder jdbcTypeName(String jdbcTypeName) {
93        parameterMapping.jdbcTypeName = jdbcTypeName;
94        return this;
95      }
96  
97      public Builder expression(String expression) {
98        parameterMapping.expression = expression;
99        return this;
100     }
101 
102     public ParameterMapping build() {
103       resolveTypeHandler();
104       validate();
105       return parameterMapping;
106     }
107 
108     private void validate() {
109       if (ResultSet.class.equals(parameterMapping.javaType)) {
110         if (parameterMapping.resultMapId == null) {
111           throw new IllegalStateException("Missing resultmap in property '"
112               + parameterMapping.property + "'.  "
113               + "Parameters of type java.sql.ResultSet require a resultmap.");
114         }
115       } else {
116         if (parameterMapping.typeHandler == null) {
117           throw new IllegalStateException("Type handler was null on parameter mapping for property '"
118             + parameterMapping.property + "'. It was either not specified and/or could not be found for the javaType ("
119             + parameterMapping.javaType.getName() + ") : jdbcType (" + parameterMapping.jdbcType + ") combination.");
120         }
121       }
122     }
123 
124     private void resolveTypeHandler() {
125       if (parameterMapping.typeHandler == null && parameterMapping.javaType != null) {
126         Configuration configuration = parameterMapping.configuration;
127         TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
128         parameterMapping.typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.javaType, parameterMapping.jdbcType);
129       }
130     }
131 
132   }
133 
134   public String getProperty() {
135     return property;
136   }
137 
138   /**
139    * Used for handling output of callable statements.
140    *
141    * @return the mode
142    */
143   public ParameterMode getMode() {
144     return mode;
145   }
146 
147   /**
148    * Used for handling output of callable statements.
149    *
150    * @return the java type
151    */
152   public Class<?> getJavaType() {
153     return javaType;
154   }
155 
156   /**
157    * Used in the UnknownTypeHandler in case there is no handler for the property type.
158    *
159    * @return the jdbc type
160    */
161   public JdbcType getJdbcType() {
162     return jdbcType;
163   }
164 
165   /**
166    * Used for handling output of callable statements.
167    *
168    * @return the numeric scale
169    */
170   public Integer getNumericScale() {
171     return numericScale;
172   }
173 
174   /**
175    * Used when setting parameters to the PreparedStatement.
176    *
177    * @return the type handler
178    */
179   public TypeHandler<?> getTypeHandler() {
180     return typeHandler;
181   }
182 
183   /**
184    * Used for handling output of callable statements.
185    *
186    * @return the result map id
187    */
188   public String getResultMapId() {
189     return resultMapId;
190   }
191 
192   /**
193    * Used for handling output of callable statements.
194    *
195    * @return the jdbc type name
196    */
197   public String getJdbcTypeName() {
198     return jdbcTypeName;
199   }
200 
201   /**
202    * Expression 'Not used'.
203    *
204    * @return the expression
205    */
206   public String getExpression() {
207     return expression;
208   }
209 
210   @Override
211   public String toString() {
212     final StringBuilder sb = new StringBuilder("ParameterMapping{");
213     //sb.append("configuration=").append(configuration); // configuration doesn't have a useful .toString()
214     sb.append("property='").append(property).append('\'');
215     sb.append(", mode=").append(mode);
216     sb.append(", javaType=").append(javaType);
217     sb.append(", jdbcType=").append(jdbcType);
218     sb.append(", numericScale=").append(numericScale);
219     //sb.append(", typeHandler=").append(typeHandler); // typeHandler also doesn't have a useful .toString()
220     sb.append(", resultMapId='").append(resultMapId).append('\'');
221     sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
222     sb.append(", expression='").append(expression).append('\'');
223     sb.append('}');
224     return sb.toString();
225   }
226 }