KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > binding > MapperCommand


1 package com.ibatis.sqlmap.engine.binding;
2
3 import com.ibatis.sqlmap.client.SqlMapClient;
4 import com.ibatis.sqlmap.client.SqlMapException;
5 import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
6 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
7 import com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement;
8 import com.ibatis.sqlmap.engine.mapping.statement.StatementType;
9
10 import java.lang.reflect.Method JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 public class MapperCommand {
16
17   private SqlMapClient client;
18   private SqlMapExecutorDelegate delegate;
19
20   private boolean hasSuppliedValueObject;
21
22   private Method JavaDoc method;
23   private int argCount;
24
25   private boolean returnsList;
26   private boolean hasListBounds;
27   private boolean hasPageSize;
28
29   private boolean returnsMap;
30   private boolean hasMapValueKey;
31
32   private StatementType type;
33   private String JavaDoc statementName;
34   private Class JavaDoc parameterClass;
35   private Class JavaDoc resultClass;
36
37   public MapperCommand(Method JavaDoc method, SqlMapClient client) {
38
39     this.client = client;
40     this.method = method;
41
42     setupFields();
43     determineStatementType();
44     determineSelectMethod();
45     validateStatement();
46     validateResult();
47     validateParameter();
48   }
49
50   private void setupFields() {
51     this.statementName = method.getName();
52     this.delegate = ((ExtendedSqlMapClient) client).getDelegate();
53     GeneralStatement statement = (GeneralStatement) delegate.getMappedStatement(statementName);
54
55     this.parameterClass = statement.getParameterClass();
56     this.resultClass = statement.getResultMap() == null ? null : statement.getResultMap().getResultClass();
57     this.type = statement.getStatementType();
58
59     this.argCount = method.getParameterTypes().length;
60
61   }
62
63   private void determineStatementType() {
64     // If proc or unkown, determine which type of statement we should execute using method names
65
if (StatementType.PROCEDURE == type || StatementType.UNKNOWN == type) {
66       if (statementName.startsWith("insert")) {
67         type = StatementType.INSERT;
68       } else if (statementName.startsWith("create")) {
69         type = StatementType.INSERT;
70       } else if (statementName.startsWith("update")) {
71         type = StatementType.UPDATE;
72       } else if (statementName.startsWith("save")) {
73         type = StatementType.UPDATE;
74       } else if (statementName.startsWith("delete")) {
75         type = StatementType.DELETE;
76       } else if (statementName.startsWith("remove")) {
77         type = StatementType.DELETE;
78       } else if (statementName.startsWith("select")) {
79         type = StatementType.SELECT;
80       } else if (statementName.startsWith("query")) {
81         type = StatementType.SELECT;
82       } else if (statementName.startsWith("get")) {
83         type = StatementType.SELECT;
84       } else if (statementName.startsWith("fetch")) {
85         type = StatementType.SELECT;
86       }
87     }
88   }
89
90   private void determineSelectMethod() {
91     if (StatementType.SELECT == type) {
92       if (List JavaDoc.class.isAssignableFrom(method.getReturnType())) {
93         // queryForList
94
returnsList = true;
95         if (argCount == 2) {
96           hasPageSize = true;
97         } else if (argCount == 3) {
98           hasListBounds = true;
99         }
100       } else if (Map JavaDoc.class.isAssignableFrom(method.getReturnType())
101           && argCount > 1
102           && String JavaDoc.class.isAssignableFrom(method.getParameterTypes()[1])) {
103         // queryForMap
104
if (argCount == 2) {
105           returnsMap = true;
106           hasMapValueKey = false;
107         } else if (argCount == 3) {
108           returnsMap = true;
109           hasMapValueKey = true;
110         }
111       } else {
112         // queryForObject
113
if (argCount == 2) {
114           hasSuppliedValueObject = true;
115         }
116       }
117     }
118   }
119
120   private void validateStatement() {
121     try {
122       delegate.getMappedStatement(statementName);
123     } catch (Exception JavaDoc e) {
124       throw new SqlMapException("Invalid bound statement (not found): " + statementName);
125     }
126
127     if (StatementType.UNKNOWN == type || StatementType.PROCEDURE == type) {
128       throw new SqlMapException("Unkown statement type for statement: " + statementName);
129     }
130   }
131
132   private void validateResult() {
133     if (!returnsList && !returnsMap) {
134 // if (method.getReturnType() == null || method.getReturnType() == Class.class) {
135
// // Make sure both aren't null
136
// if (resultClass != null) {
137
// throw new SqlMapException("Invalid bound result for statement (mismatched null/void): " + statementName);
138
// }
139
// } else if (resultClass == null) {
140
// // Make sure both aren't null
141
// if (method.getReturnType() != null || method.getReturnType() == Class.class) {
142
// System.out.println (method.getReturnType().getClass());
143
// throw new SqlMapException("Invalid bound result for statement (mismatched null/void): " + statementName);
144
// }
145
// } else {
146
// // Make sure types are compatible
147
// if (!method.getReturnType().isAssignableFrom(resultClass)) {
148
// throw new SqlMapException("Invalid bound result for statement (incompatible types): " + statementName);
149
// }
150
// }
151
}
152   }
153
154   private void validateParameter() {
155     if (argCount > 0) {
156       // Make sure types are compatible
157
Class JavaDoc paramType = method.getParameterTypes()[0];
158       if (parameterClass != null) {
159 // if (!parameterClass.isAssignableFrom(paramType.getClass())) {
160
// throw new SqlMapException("Invalid bound parameter for statement (incompatible types): " + statementName);
161
// }
162
}
163     }
164     if (argCount > 1 && type != StatementType.SELECT) {
165       // Only select statements can have multiple parameters
166
throw new SqlMapException("Too many parameters for statement (must be 1 or 0): " + statementName);
167     }
168     if (argCount > 1 && type != StatementType.SELECT) {
169       // Only select statements can have multiple parameters
170
throw new SqlMapException("Too many parameters for statement (must be 1 or 0): " + statementName);
171     }
172   }
173
174   public Object JavaDoc execute(Object JavaDoc[] args) throws SQLException JavaDoc {
175     Object JavaDoc result = null;
176     if (StatementType.INSERT == type) {
177       Object JavaDoc param = getParam(args);
178       result = client.insert(statementName, param);
179     } else if (StatementType.UPDATE == type) {
180       Object JavaDoc param = getParam(args);
181       result = new Integer JavaDoc(client.update(statementName, param));
182     } else if (StatementType.DELETE == type) {
183       Object JavaDoc param = getParam(args);
184       result = new Integer JavaDoc(client.delete(statementName, param));
185     } else if (StatementType.SELECT == type) {
186       if (returnsList) {
187         result = executeForList(args);
188       } else if (returnsMap) {
189         result = executeForMap(args);
190       } else {
191         result = executeForObject(args);
192       }
193     } else {
194       throw new SqlMapException("Unkown execution method for: " + statementName);
195     }
196
197     return result;
198   }
199
200   private Object JavaDoc executeForObject(Object JavaDoc[] args) throws SQLException JavaDoc {
201     Object JavaDoc result;
202     if (hasSuppliedValueObject) {
203       Object JavaDoc param = getParam(args);
204       Object JavaDoc valueObject = args[1];
205       result = client.queryForObject(statementName, param, valueObject);
206     } else {
207       Object JavaDoc param = getParam(args);
208       result = client.queryForObject(statementName, param);
209     }
210     return result;
211   }
212
213   private Object JavaDoc executeForMap(Object JavaDoc[] args) throws SQLException JavaDoc {
214     Object JavaDoc result;
215     if (hasMapValueKey) {
216       Object JavaDoc param = getParam(args);
217       String JavaDoc keyProp = (String JavaDoc) args[1];
218       String JavaDoc valueProp = (String JavaDoc) args[2];
219       result = client.queryForMap(statementName, param, keyProp, valueProp);
220     } else {
221       Object JavaDoc param = getParam(args);
222       String JavaDoc keyProp = (String JavaDoc) args[1];
223       result = client.queryForMap(statementName, param, keyProp);
224     }
225     return result;
226   }
227
228   private Object JavaDoc executeForList(Object JavaDoc[] args) throws SQLException JavaDoc {
229     Object JavaDoc result;
230     if (hasListBounds) {
231       Object JavaDoc param = getParam(args);
232       int skip = ((Integer JavaDoc) args[1]).intValue();
233       int max = ((Integer JavaDoc) args[2]).intValue();
234       result = client.queryForList(statementName, param, skip, max);
235     } else if (hasPageSize) {
236       Object JavaDoc param = getParam(args);
237       int pageSize = ((Integer JavaDoc) args[1]).intValue();
238       result = client.queryForPaginatedList(statementName, param, pageSize);
239     } else {
240       Object JavaDoc param = getParam(args);
241       result = client.queryForList(statementName, param);
242     }
243     return result;
244   }
245
246   private Object JavaDoc getParam(Object JavaDoc[] args) {
247     if (args == null) {
248       return null;
249     }
250     return args.length > 0 ? args[0] : null;
251
252   }
253
254 }
255
Popular Tags