View Javadoc
1   /*
2    *    Copyright 2009-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 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 final 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 '" + parameterMapping.property + "'.  "
112               + "Parameters of type java.sql.ResultSet require a resultmap.");
113         }
114       } else if (parameterMapping.typeHandler == null) {
115         throw new IllegalStateException("Type handler was null on parameter mapping for property '"
116             + parameterMapping.property + "'. It was either not specified and/or could not be found for the javaType ("
117             + parameterMapping.javaType.getName() + ") : jdbcType (" + parameterMapping.jdbcType + ") combination.");
118       }
119     }
120 
121     private void resolveTypeHandler() {
122       if (parameterMapping.typeHandler == null && parameterMapping.javaType != null) {
123         Configuration configuration = parameterMapping.configuration;
124         TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
125         parameterMapping.typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.javaType,
126             parameterMapping.jdbcType);
127       }
128     }
129 
130   }
131 
132   public String getProperty() {
133     return property;
134   }
135 
136   /**
137    * Used for handling output of callable statements.
138    *
139    * @return the mode
140    */
141   public ParameterMode getMode() {
142     return mode;
143   }
144 
145   /**
146    * Used for handling output of callable statements.
147    *
148    * @return the java type
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    *
157    * @return the jdbc type
158    */
159   public JdbcType getJdbcType() {
160     return jdbcType;
161   }
162 
163   /**
164    * Used for handling output of callable statements.
165    *
166    * @return the numeric scale
167    */
168   public Integer getNumericScale() {
169     return numericScale;
170   }
171 
172   /**
173    * Used when setting parameters to the PreparedStatement.
174    *
175    * @return the type handler
176    */
177   public TypeHandler<?> getTypeHandler() {
178     return typeHandler;
179   }
180 
181   /**
182    * Used for handling output of callable statements.
183    *
184    * @return the result map id
185    */
186   public String getResultMapId() {
187     return resultMapId;
188   }
189 
190   /**
191    * Used for handling output of callable statements.
192    *
193    * @return the jdbc type name
194    */
195   public String getJdbcTypeName() {
196     return jdbcTypeName;
197   }
198 
199   /**
200    * Expression 'Not used'.
201    *
202    * @return the expression
203    */
204   public String getExpression() {
205     return expression;
206   }
207 
208   @Override
209   public String toString() {
210     final StringBuilder sb = new StringBuilder("ParameterMapping{");
211     // sb.append("configuration=").append(configuration); // configuration doesn't have a useful .toString()
212     sb.append("property='").append(property).append('\'');
213     sb.append(", mode=").append(mode);
214     sb.append(", javaType=").append(javaType);
215     sb.append(", jdbcType=").append(jdbcType);
216     sb.append(", numericScale=").append(numericScale);
217     // sb.append(", typeHandler=").append(typeHandler); // typeHandler also doesn't have a useful .toString()
218     sb.append(", resultMapId='").append(resultMapId).append('\'');
219     sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
220     sb.append(", expression='").append(expression).append('\'');
221     sb.append('}');
222     return sb.toString();
223   }
224 }