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 ; 11 import java.sql.SQLException ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 public class MapperCommand { 16 17 private SqlMapClient client; 18 private SqlMapExecutorDelegate delegate; 19 20 private boolean hasSuppliedValueObject; 21 22 private Method 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 statementName; 34 private Class parameterClass; 35 private Class resultClass; 36 37 public MapperCommand(Method 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 (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 .class.isAssignableFrom(method.getReturnType())) { 93 returnsList = true; 95 if (argCount == 2) { 96 hasPageSize = true; 97 } else if (argCount == 3) { 98 hasListBounds = true; 99 } 100 } else if (Map .class.isAssignableFrom(method.getReturnType()) 101 && argCount > 1 102 && String .class.isAssignableFrom(method.getParameterTypes()[1])) { 103 if (argCount == 2) { 105 returnsMap = true; 106 hasMapValueKey = false; 107 } else if (argCount == 3) { 108 returnsMap = true; 109 hasMapValueKey = true; 110 } 111 } else { 112 if (argCount == 2) { 114 hasSuppliedValueObject = true; 115 } 116 } 117 } 118 } 119 120 private void validateStatement() { 121 try { 122 delegate.getMappedStatement(statementName); 123 } catch (Exception 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 } 152 } 153 154 private void validateParameter() { 155 if (argCount > 0) { 156 Class paramType = method.getParameterTypes()[0]; 158 if (parameterClass != null) { 159 } 163 } 164 if (argCount > 1 && type != StatementType.SELECT) { 165 throw new SqlMapException("Too many parameters for statement (must be 1 or 0): " + statementName); 167 } 168 if (argCount > 1 && type != StatementType.SELECT) { 169 throw new SqlMapException("Too many parameters for statement (must be 1 or 0): " + statementName); 171 } 172 } 173 174 public Object execute(Object [] args) throws SQLException { 175 Object result = null; 176 if (StatementType.INSERT == type) { 177 Object param = getParam(args); 178 result = client.insert(statementName, param); 179 } else if (StatementType.UPDATE == type) { 180 Object param = getParam(args); 181 result = new Integer (client.update(statementName, param)); 182 } else if (StatementType.DELETE == type) { 183 Object param = getParam(args); 184 result = new Integer (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 executeForObject(Object [] args) throws SQLException { 201 Object result; 202 if (hasSuppliedValueObject) { 203 Object param = getParam(args); 204 Object valueObject = args[1]; 205 result = client.queryForObject(statementName, param, valueObject); 206 } else { 207 Object param = getParam(args); 208 result = client.queryForObject(statementName, param); 209 } 210 return result; 211 } 212 213 private Object executeForMap(Object [] args) throws SQLException { 214 Object result; 215 if (hasMapValueKey) { 216 Object param = getParam(args); 217 String keyProp = (String ) args[1]; 218 String valueProp = (String ) args[2]; 219 result = client.queryForMap(statementName, param, keyProp, valueProp); 220 } else { 221 Object param = getParam(args); 222 String keyProp = (String ) args[1]; 223 result = client.queryForMap(statementName, param, keyProp); 224 } 225 return result; 226 } 227 228 private Object executeForList(Object [] args) throws SQLException { 229 Object result; 230 if (hasListBounds) { 231 Object param = getParam(args); 232 int skip = ((Integer ) args[1]).intValue(); 233 int max = ((Integer ) args[2]).intValue(); 234 result = client.queryForList(statementName, param, skip, max); 235 } else if (hasPageSize) { 236 Object param = getParam(args); 237 int pageSize = ((Integer ) args[1]).intValue(); 238 result = client.queryForPaginatedList(statementName, param, pageSize); 239 } else { 240 Object param = getParam(args); 241 result = client.queryForList(statementName, param); 242 } 243 return result; 244 } 245 246 private Object getParam(Object [] args) { 247 if (args == null) { 248 return null; 249 } 250 return args.length > 0 ? args[0] : null; 251 252 } 253 254 } 255 | Popular Tags |