1 16 17 package org.springframework.jdbc.object; 18 19 import java.util.List ; 20 import java.util.Map ; 21 22 import javax.sql.DataSource ; 23 24 import org.springframework.jdbc.core.CallableStatementCreator; 25 import org.springframework.jdbc.core.CallableStatementCreatorFactory; 26 import org.springframework.jdbc.core.ParameterMapper; 27 import org.springframework.jdbc.core.SqlParameter; 28 import org.springframework.jdbc.core.SqlReturnResultSet; 29 30 41 public abstract class SqlCall extends RdbmsOperation { 42 43 47 private CallableStatementCreatorFactory callableStatementFactory; 48 49 53 private boolean function = false; 54 55 59 private boolean sqlReadyForUse = false; 60 61 67 private String callString; 68 69 70 78 public SqlCall() { 79 } 80 81 87 public SqlCall(DataSource ds, String sql) { 88 setDataSource(ds); 89 setSql(sql); 90 } 91 92 93 96 public void setFunction(boolean function) { 97 this.function = function; 98 } 99 100 103 public boolean isFunction() { 104 return function; 105 } 106 107 110 public void setSqlReadyForUse(boolean sqlReadyForUse) { 111 this.sqlReadyForUse = sqlReadyForUse; 112 } 113 114 117 public boolean isSqlReadyForUse() { 118 return sqlReadyForUse; 119 } 120 121 122 127 protected final void compileInternal() { 128 if (isSqlReadyForUse()) { 129 this.callString = getSql(); 130 } 131 else { 132 List parameters = getDeclaredParameters(); 133 int parameterCount = 0; 134 if (isFunction()) { 135 this.callString = "{? = call " + getSql() + "("; 136 parameterCount = -1; 137 } 138 else { 139 this.callString = "{call " + getSql() + "("; 140 } 141 for (int i = 0; i < parameters.size(); i++) { 142 SqlParameter parameter = (SqlParameter) parameters.get(i); 143 if (!(parameter instanceof SqlReturnResultSet)) { 144 if (parameterCount > 0) { 145 this.callString += ", "; 146 } 147 if (parameterCount >= 0) { 148 this.callString += "?"; 149 } 150 parameterCount++; 151 } 152 } 153 this.callString += ")}"; 154 } 155 if (logger.isDebugEnabled()) { 156 logger.debug("Compiled stored procedure. Call string is [" + getCallString() + "]"); 157 } 158 159 this.callableStatementFactory = new CallableStatementCreatorFactory(getCallString(), getDeclaredParameters()); 160 this.callableStatementFactory.setResultSetType(getResultSetType()); 161 this.callableStatementFactory.setUpdatableResults(isUpdatableResults()); 162 this.callableStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor()); 163 164 onCompileInternal(); 165 } 166 167 171 protected void onCompileInternal() { 172 } 173 174 177 public String getCallString() { 178 return this.callString; 179 } 180 181 186 protected CallableStatementCreator newCallableStatementCreator(Map inParams) { 187 return this.callableStatementFactory.newCallableStatementCreator(inParams); 188 } 189 190 195 protected CallableStatementCreator newCallableStatementCreator(ParameterMapper inParamMapper) { 196 return this.callableStatementFactory.newCallableStatementCreator(inParamMapper); 197 } 198 199 } 200 | Popular Tags |