View Javadoc
1   /**
2    *    Copyright 2009-2019 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 ParameterMappingParameterMapping">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    * @return
141    */
142   public ParameterMode getMode() {
143     return mode;
144   }
145 
146   /**
147    * Used for handling output of callable statements.
148    * @return
149    */
150   public Class<?> getJavaType() {
151     return javaType;
152   }
153 
154   /**
155    * Used in the UnknownTypeHandler in case there is no handler for the property type.
156    * @return
157    */
158   public JdbcType getJdbcType() {
159     return jdbcType;
160   }
161 
162   /**
163    * Used for handling output of callable statements.
164    * @return
165    */
166   public Integer getNumericScale() {
167     return numericScale;
168   }
169 
170   /**
171    * Used when setting parameters to the PreparedStatement.
172    * @return
173    */
174   public TypeHandler<?> getTypeHandler() {
175     return typeHandler;
176   }
177 
178   /**
179    * Used for handling output of callable statements.
180    * @return
181    */
182   public String getResultMapId() {
183     return resultMapId;
184   }
185 
186   /**
187    * Used for handling output of callable statements.
188    * @return
189    */
190   public String getJdbcTypeName() {
191     return jdbcTypeName;
192   }
193 
194   /**
195    * Not used
196    * @return
197    */
198   public String getExpression() {
199     return expression;
200   }
201 
202   @Override
203   public String toString() {
204     final StringBuilder sb = new StringBuilder("ParameterMapping{");
205     //sb.append("configuration=").append(configuration); // configuration doesn't have a useful .toString()
206     sb.append("property='").append(property).append('\'');
207     sb.append(", mode=").append(mode);
208     sb.append(", javaType=").append(javaType);
209     sb.append(", jdbcType=").append(jdbcType);
210     sb.append(", numericScale=").append(numericScale);
211     //sb.append(", typeHandler=").append(typeHandler); // typeHandler also doesn't have a useful .toString()
212     sb.append(", resultMapId='").append(resultMapId).append('\'');
213     sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
214     sb.append(", expression='").append(expression).append('\'');
215     sb.append('}');
216     return sb.toString();
217   }
218 }