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.config;
17  
18  import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
19  import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
20  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
21  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;
22  import com.ibatis.sqlmap.engine.scope.ErrorContext;
23  import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
24  import com.ibatis.sqlmap.engine.type.TypeHandler;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /**
30   * The Class ParameterMapConfig.
31   */
32  public class ParameterMapConfig {
33  
34    /** The Constant MODE_IN. */
35    public static final String MODE_IN = "IN";
36  
37    /** The Constant MODE_OUT. */
38    public static final String MODE_OUT = "OUT";
39  
40    /** The Constant MODE_INOUT. */
41    public static final String MODE_INOUT = "INUOT";
42  
43    /** The config. */
44    private SqlMapConfiguration config;
45  
46    /** The error context. */
47    private ErrorContext errorContext;
48  
49    /** The client. */
50    private SqlMapClientImpl client;
51  
52    /** The parameter map. */
53    private ParameterMap parameterMap;
54  
55    /** The parameter mapping list. */
56    private List parameterMappingList;
57  
58    /**
59     * Instantiates a new parameter map config.
60     *
61     * @param config
62     *          the config
63     * @param id
64     *          the id
65     * @param parameterClass
66     *          the parameter class
67     */
68    ParameterMapConfig(SqlMapConfiguration config, String id, Class parameterClass) {
69      this.config = config;
70      this.errorContext = config.getErrorContext();
71      this.client = config.getClient();
72      errorContext.setActivity("building a parameter map");
73      parameterMap = new ParameterMap(client.getDelegate());
74      parameterMap.setId(id);
75      parameterMap.setResource(errorContext.getResource());
76      errorContext.setObjectId(id + " parameter map");
77      parameterMap.setParameterClass(parameterClass);
78      errorContext.setMoreInfo("Check the parameter mappings.");
79      this.parameterMappingList = new ArrayList();
80      client.getDelegate().addParameterMap(parameterMap);
81    }
82  
83    /**
84     * Adds the parameter mapping.
85     *
86     * @param propertyName
87     *          the property name
88     * @param javaClass
89     *          the java class
90     * @param jdbcType
91     *          the jdbc type
92     * @param nullValue
93     *          the null value
94     * @param mode
95     *          the mode
96     * @param outParamType
97     *          the out param type
98     * @param numericScale
99     *          the numeric scale
100    * @param typeHandlerImpl
101    *          the type handler impl
102    * @param resultMap
103    *          the result map
104    */
105   public void addParameterMapping(String propertyName, Class javaClass, String jdbcType, String nullValue, String mode,
106       String outParamType, Integer numericScale, Object typeHandlerImpl, String resultMap) {
107     errorContext.setObjectId(propertyName + " mapping of the " + parameterMap.getId() + " parameter map");
108     TypeHandler handler;
109     if (typeHandlerImpl != null) {
110       errorContext.setMoreInfo("Check the parameter mapping typeHandler attribute '" + typeHandlerImpl
111           + "' (must be a TypeHandler or TypeHandlerCallback implementation).");
112       if (typeHandlerImpl instanceof TypeHandlerCallback) {
113         handler = new CustomTypeHandler((TypeHandlerCallback) typeHandlerImpl);
114       } else if (typeHandlerImpl instanceof TypeHandler) {
115         handler = (TypeHandler) typeHandlerImpl;
116       } else {
117         throw new RuntimeException(
118             "The class '" + typeHandlerImpl + "' is not a valid implementation of TypeHandler or TypeHandlerCallback");
119       }
120     } else {
121       errorContext.setMoreInfo("Check the parameter mapping property type or name.");
122       handler = config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(),
123           parameterMap.getParameterClass(), propertyName, javaClass, jdbcType);
124     }
125     ParameterMapping mapping = new ParameterMapping();
126     mapping.setPropertyName(propertyName);
127     mapping.setJdbcTypeName(jdbcType);
128     mapping.setTypeName(outParamType);
129     mapping.setResultMapName(resultMap);
130     mapping.setNullValue(nullValue);
131     if (mode != null && mode.length() > 0) {
132       mapping.setMode(mode);
133     }
134     mapping.setTypeHandler(handler);
135     mapping.setJavaType(javaClass);
136     mapping.setNumericScale(numericScale);
137     parameterMappingList.add(mapping);
138     parameterMap.setParameterMappingList(parameterMappingList);
139   }
140 
141 }