1 16 17 package org.springframework.jdbc.core; 18 19 import java.sql.CallableStatement ; 20 import java.sql.Connection ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.util.HashMap ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.springframework.dao.InvalidDataAccessApiUsageException; 29 import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor; 30 31 40 public class CallableStatementCreatorFactory { 41 42 43 private final String callString; 44 45 46 private final List declaredParameters; 47 48 private int resultSetType = ResultSet.TYPE_FORWARD_ONLY; 49 50 private boolean updatableResults = false; 51 52 private NativeJdbcExtractor nativeJdbcExtractor; 53 54 55 59 public CallableStatementCreatorFactory(String callString) { 60 this.callString = callString; 61 this.declaredParameters = new LinkedList (); 62 } 63 64 69 public CallableStatementCreatorFactory(String callString, List declaredParameters) { 70 this.callString = callString; 71 this.declaredParameters = declaredParameters; 72 } 73 74 78 public void addParameter(SqlParameter param) { 79 this.declaredParameters.add(param); 80 } 81 82 90 public void setResultSetType(int resultSetType) { 91 this.resultSetType = resultSetType; 92 } 93 94 98 public void setUpdatableResults(boolean updatableResults) { 99 this.updatableResults = updatableResults; 100 } 101 102 106 public void setNativeJdbcExtractor(NativeJdbcExtractor nativeJdbcExtractor) { 107 this.nativeJdbcExtractor = nativeJdbcExtractor; 108 } 109 110 111 115 public CallableStatementCreator newCallableStatementCreator(Map inParams) { 116 return new CallableStatementCreatorImpl(inParams != null ? inParams : new HashMap ()); 117 } 118 119 123 public CallableStatementCreator newCallableStatementCreator(ParameterMapper inParamMapper) { 124 return new CallableStatementCreatorImpl(inParamMapper); 125 } 126 127 128 131 private class CallableStatementCreatorImpl 132 implements CallableStatementCreator, SqlProvider, ParameterDisposer { 133 134 private ParameterMapper inParameterMapper; 135 136 private Map inParameters; 137 138 143 public CallableStatementCreatorImpl(ParameterMapper inParamMapper) { 144 this.inParameterMapper = inParamMapper; 145 } 146 147 151 public CallableStatementCreatorImpl(Map inParams) { 152 this.inParameters = inParams; 153 } 154 155 public CallableStatement createCallableStatement(Connection con) throws SQLException { 156 if (this.inParameterMapper != null) { 158 this.inParameters = this.inParameterMapper.createMap(con); 159 } 160 else { 161 if (this.inParameters == null) { 162 throw new InvalidDataAccessApiUsageException( 163 "A ParameterMapper or a Map of parameters must be provided"); 164 } 165 } 166 167 CallableStatement cs = null; 168 if (resultSetType == ResultSet.TYPE_FORWARD_ONLY && !updatableResults) { 169 cs = con.prepareCall(callString); 170 } 171 else { 172 cs = con.prepareCall(callString, resultSetType, 173 updatableResults ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY); 174 } 175 176 CallableStatement csToUse = cs; 178 if (nativeJdbcExtractor != null) { 179 csToUse = nativeJdbcExtractor.getNativeCallableStatement(cs); 180 } 181 182 int sqlColIndx = 1; 183 for (int i = 0; i < declaredParameters.size(); i++) { 184 SqlParameter declaredParameter = (SqlParameter) declaredParameters.get(i); 185 if (!this.inParameters.containsKey(declaredParameter.getName()) && 186 !(declaredParameter instanceof ResultSetSupportingSqlParameter)) { 187 throw new InvalidDataAccessApiUsageException( 188 "Required input parameter '" + declaredParameter.getName() + "' is missing"); 189 } 190 Object inValue = this.inParameters.get(declaredParameter.getName()); 192 if (!(declaredParameter instanceof ResultSetSupportingSqlParameter)) { 193 StatementCreatorUtils.setParameterValue(csToUse, sqlColIndx, declaredParameter, inValue); 194 } 195 else { 196 if (declaredParameter instanceof SqlOutParameter) { 199 if (declaredParameter.getTypeName() != null) { 200 cs.registerOutParameter(sqlColIndx, declaredParameter.getSqlType(), declaredParameter.getTypeName()); 201 } 202 else { 203 cs.registerOutParameter(sqlColIndx, declaredParameter.getSqlType()); 204 } 205 if (((SqlOutParameter) declaredParameter).isInputValueProvided() || inValue != null) { 206 StatementCreatorUtils.setParameterValue(csToUse, sqlColIndx, declaredParameter, inValue); 207 } 208 } 209 } 210 if (!(declaredParameter instanceof SqlReturnResultSet)) { 211 sqlColIndx++; 212 } 213 } 214 215 return cs; 216 } 217 218 public String getSql() { 219 return callString; 220 } 221 222 public void cleanupParameters() { 223 if (this.inParameters != null) { 224 StatementCreatorUtils.cleanupParameters(this.inParameters.values()); 225 } 226 } 227 228 public String toString() { 229 StringBuffer buf = new StringBuffer ("CallableStatementCreatorFactory.CallableStatementCreatorImpl: sql=["); 230 buf.append(callString).append("]; parameters=").append(this.inParameters); 231 return buf.toString(); 232 } 233 } 234 235 } 236 | Popular Tags |