1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32 public class ParameterMapConfig {
33
34
35 public static final String MODE_IN = "IN";
36
37
38 public static final String MODE_OUT = "OUT";
39
40
41 public static final String MODE_INOUT = "INUOT";
42
43
44 private SqlMapConfiguration config;
45
46
47 private ErrorContext errorContext;
48
49
50 private SqlMapClientImpl client;
51
52
53 private ParameterMap parameterMap;
54
55
56 private List parameterMappingList;
57
58
59
60
61
62
63
64
65
66
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 }