1 19 20 package org.apache.cayenne.access.trans; 21 22 import java.sql.CallableStatement ; 23 import java.sql.PreparedStatement ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.cayenne.access.QueryLogger; 30 import org.apache.cayenne.access.QueryTranslator; 31 import org.apache.cayenne.map.Procedure; 32 import org.apache.cayenne.map.ProcedureParameter; 33 import org.apache.cayenne.query.ProcedureQuery; 34 35 40 public class ProcedureTranslator extends QueryTranslator { 41 42 45 static class NotInParam { 46 47 protected String type; 48 49 public NotInParam(String type) { 50 this.type = type; 51 } 52 53 public String toString() { 54 return type; 55 } 56 } 57 58 private static NotInParam OUT_PARAM = new NotInParam("[OUT]"); 59 60 protected List callParams; 61 protected List values; 62 63 66 protected String createSqlString() { 67 Procedure procedure = getProcedure(); 68 69 StringBuffer buf = new StringBuffer (); 70 71 int totalParams = callParams.size(); 72 73 if (procedure.isReturningValue()) { 75 totalParams--; 76 buf.append("{? = call "); 77 } 78 else { 79 buf.append("{call "); 80 } 81 82 buf.append(procedure.getFullyQualifiedName()); 83 84 if (totalParams > 0) { 85 buf.append("(?"); 87 88 for (int i = 1; i < totalParams; i++) { 89 buf.append(", ?"); 90 } 91 92 buf.append(")"); 93 } 94 95 buf.append("}"); 96 return buf.toString(); 97 } 98 99 public PreparedStatement createStatement() throws Exception { 100 long t1 = System.currentTimeMillis(); 101 102 this.callParams = getProcedure().getCallParameters(); 103 this.values = new ArrayList (callParams.size()); 104 105 initValues(); 106 String sqlStr = createSqlString(); 107 108 if (QueryLogger.isLoggable()) { 109 long time = System.currentTimeMillis() - t1; 111 112 List loggableParameters = new ArrayList (values.size()); 113 Iterator it = values.iterator(); 114 while (it.hasNext()) { 115 Object val = it.next(); 116 if (val instanceof NotInParam) { 117 val = val.toString(); 118 } 119 loggableParameters.add(val); 120 } 121 122 QueryLogger.logQuery(sqlStr, loggableParameters, time); 123 } 124 CallableStatement stmt = connection.prepareCall(sqlStr); 125 initStatement(stmt); 126 return stmt; 127 } 128 129 public Procedure getProcedure() { 130 return getEntityResolver().lookupProcedure(query); 131 } 132 133 public ProcedureQuery getProcedureQuery() { 134 return (ProcedureQuery) query; 135 } 136 137 140 protected void initStatement(CallableStatement stmt) throws Exception { 141 if (values != null && values.size() > 0) { 142 List params = getProcedure().getCallParameters(); 143 144 int len = values.size(); 145 for (int i = 0; i < len; i++) { 146 ProcedureParameter param = (ProcedureParameter) params.get(i); 147 148 if (param.isOutParam()) { 151 setOutParam(stmt, param, i + 1); 152 } 153 154 if (param.isInParameter()) { 155 setInParam(stmt, param, values.get(i), i + 1); 156 } 157 } 158 } 159 } 160 161 protected void initValues() { 162 Map queryValues = getProcedureQuery().getParameters(); 163 164 168 Iterator it = callParams.iterator(); 169 while (it.hasNext()) { 170 ProcedureParameter param = (ProcedureParameter) it.next(); 171 172 if (param.getDirection() == ProcedureParameter.OUT_PARAMETER) { 173 values.add(OUT_PARAM); 174 } 175 else { 176 values.add(queryValues.get(param.getName())); 177 } 178 } 179 } 180 181 184 protected void setInParam( 185 CallableStatement stmt, 186 ProcedureParameter param, 187 Object val, 188 int pos) throws Exception { 189 190 int type = param.getType(); 191 adapter.bindParameter(stmt, val, pos, type, param.getPrecision()); 192 } 193 194 197 protected void setOutParam(CallableStatement stmt, ProcedureParameter param, int pos) 198 throws Exception { 199 200 int precision = param.getPrecision(); 201 if (precision >= 0) { 202 stmt.registerOutParameter(pos, param.getType(), precision); 203 } 204 else { 205 stmt.registerOutParameter(pos, param.getType()); 206 } 207 } 208 } 209 | Popular Tags |