SqlCall.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.jdbc.object;
  17. import java.util.List;
  18. import java.util.Map;
  19. import org.springframework.jdbc.core.CallableStatementCreator;
  20. import org.springframework.jdbc.core.CallableStatementCreatorFactory;
  21. import org.springframework.jdbc.core.ParameterMapper;
  22. import org.springframework.jdbc.core.SqlParameter;
  23. import org.springframework.jdbc.core.SqlReturnResultSet;
  24. /**
  25.  * RdbmsOperation using a JdbcTemplate and representing a SQL-based
  26.  * call such as a stored procedure or a stored function.
  27.  *
  28.  * <p>Configures a CallableStatementCreatorFactory based on the declared
  29.  * parameters.
  30.  *
  31.  * @author Rod Johnson
  32.  * @author Thomas Risberg
  33.  */
  34. public abstract class SqlCall extends RdbmsOperation {
  35. /**
  36.  * Object enabling us to create CallableStatementCreators
  37.  * efficiently, based on this class's declared parameters.
  38.  */
  39. private CallableStatementCreatorFactory callableStatementFactory;
  40. /**
  41.  * Flag used to indicate that this call is for a function and to
  42.  * use the {? = call get_invoice_count(?)} syntax.
  43.  */
  44. private boolean function = false;
  45. /**
  46.  * Flag used to indicate that the sql for this call should be used exactly as it is
  47.  * defined.  No need to add the escape syntax and parameter place holders.
  48.  */
  49. private boolean sqlReadyForUse = false;
  50. /**
  51.  * Call string as defined in java.sql.CallableStatement.
  52.  * String of form {call add_invoice(?, ?, ?)}
  53.  * or {? = call get_invoice_count(?)} if isFunction is set to true
  54.  * Updated after each parameter is added.
  55.  */
  56. private String callString;
  57. /**
  58.  * Set whether this call is for a function.
  59.  */
  60. public void setFunction(boolean function) {
  61. this.function = function;
  62. }
  63. /**
  64.  * Get whether this call is for a function.
  65.  */
  66. public boolean isFunction() {
  67. return function;
  68. }
  69. /**
  70.  * Set whether the SQL can be used as is.
  71.  */
  72. public void setSqlReadyForUse(boolean sqlReadyForUse) {
  73. this.sqlReadyForUse = sqlReadyForUse;
  74. }
  75. /**
  76.  * Return whether the SQL can be used as is.
  77.  */
  78. public boolean isSqlReadyForUse() {
  79. return sqlReadyForUse;
  80. }
  81. /**
  82.  * Overridden method to configure the CallableStatementCreatorFactory
  83.  * based on our declared parameters.
  84.  * @see RdbmsOperation#compileInternal()
  85.  */
  86. protected final void compileInternal() {
  87. if (isSqlReadyForUse()) {
  88. this.callString = getSql();
  89. }
  90. else {
  91. List parameters = getDeclaredParameters();
  92. int firstParameter = 0;
  93. if (isFunction()) {
  94. this.callString = "{? = call " + getSql() + "(";
  95. firstParameter = 1;
  96. }
  97. else {
  98. this.callString = "{call " + getSql() + "(";
  99. }
  100. for (int i = firstParameter; i < parameters.size(); i++) {
  101. SqlParameter p = (SqlParameter) parameters.get(i);
  102. if ((p instanceof SqlReturnResultSet)) {
  103. firstParameter++;
  104. }
  105. else {
  106. if (i > firstParameter) {
  107. this.callString += ", ";
  108. }
  109. this.callString += "?";
  110. }
  111. }
  112. this.callString += ")}";
  113. }
  114. if (logger.isInfoEnabled()) {
  115. logger.info("Compiled stored procedure. Call string is [" + getCallString() + "]");
  116. }
  117. this.callableStatementFactory = new CallableStatementCreatorFactory(getCallString(), getDeclaredParameters());
  118. this.callableStatementFactory.setResultSetType(getResultSetType());
  119. this.callableStatementFactory.setUpdatableResults(isUpdatableResults());
  120. this.callableStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());
  121. onCompileInternal();
  122. }
  123. /**
  124.  * Hook method that subclasses may override to react to compilation.
  125.  * This implementation does nothing.
  126.  */
  127. protected void onCompileInternal() {
  128. }
  129. /**
  130.  * Get the call string.
  131.  */
  132. public String getCallString() {
  133. return this.callString;
  134. }
  135. /**
  136.  * Return a CallableStatementCreator to perform an operation
  137.  * with this parameters.
  138.  * @param inParams parameters. May be null.
  139.  */
  140. protected CallableStatementCreator newCallableStatementCreator(Map inParams) {
  141. return this.callableStatementFactory.newCallableStatementCreator(inParams);
  142. }
  143. /**
  144.  * Return a CallableStatementCreator to perform an operation
  145.  * with the parameters returned from this ParameterMapper.
  146.  * @param inParamMapper parametermapper. May not be null.
  147.  */
  148. protected CallableStatementCreator newCallableStatementCreator(ParameterMapper inParamMapper) {
  149. return this.callableStatementFactory.newCallableStatementCreator(inParamMapper);
  150. }
  151. }