KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > object > SqlCall


1 /*
2  * Copyright 2002-2006 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
17 package org.springframework.jdbc.object;
18
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
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 /**
31  * RdbmsOperation using a JdbcTemplate and representing a SQL-based
32  * call such as a stored procedure or a stored function.
33  *
34  * <p>Configures a CallableStatementCreatorFactory based on the declared
35  * parameters.
36  *
37  * @author Rod Johnson
38  * @author Thomas Risberg
39  * @see CallableStatementCreatorFactory
40  */

41 public abstract class SqlCall extends RdbmsOperation {
42
43     /**
44      * Object enabling us to create CallableStatementCreators
45      * efficiently, based on this class's declared parameters.
46      */

47     private CallableStatementCreatorFactory callableStatementFactory;
48
49     /**
50      * Flag used to indicate that this call is for a function and to
51      * use the {? = call get_invoice_count(?)} syntax.
52      */

53     private boolean function = false;
54
55     /**
56      * Flag used to indicate that the sql for this call should be used exactly as it is
57      * defined. No need to add the escape syntax and parameter place holders.
58      */

59     private boolean sqlReadyForUse = false;
60
61     /**
62      * Call string as defined in java.sql.CallableStatement.
63      * String of form {call add_invoice(?, ?, ?)}
64      * or {? = call get_invoice_count(?)} if isFunction is set to true
65      * Updated after each parameter is added.
66      */

67     private String JavaDoc callString;
68
69
70     /**
71      * Constructor to allow use as a JavaBean.
72      * A DataSource, SQL and any parameters must be supplied before
73      * invoking the <code>compile</code> method and using this object.
74      * @see #setDataSource
75      * @see #setSql
76      * @see #compile
77      */

78     public SqlCall() {
79     }
80
81     /**
82      * Create a new SqlCall object with SQL, but without parameters.
83      * Must add parameters or settle with none.
84      * @param ds DataSource to obtain connections from
85      * @param sql SQL to execute
86      */

87     public SqlCall(DataSource JavaDoc ds, String JavaDoc sql) {
88         setDataSource(ds);
89         setSql(sql);
90     }
91
92
93     /**
94      * Set whether this call is for a function.
95      */

96     public void setFunction(boolean function) {
97         this.function = function;
98     }
99
100     /**
101      * Return whether this call is for a function.
102      */

103     public boolean isFunction() {
104         return function;
105     }
106
107     /**
108      * Set whether the SQL can be used as is.
109      */

110     public void setSqlReadyForUse(boolean sqlReadyForUse) {
111         this.sqlReadyForUse = sqlReadyForUse;
112     }
113
114     /**
115      * Return whether the SQL can be used as is.
116      */

117     public boolean isSqlReadyForUse() {
118         return sqlReadyForUse;
119     }
120
121
122     /**
123      * Overridden method to configure the CallableStatementCreatorFactory
124      * based on our declared parameters.
125      * @see RdbmsOperation#compileInternal()
126      */

127     protected final void compileInternal() {
128         if (isSqlReadyForUse()) {
129             this.callString = getSql();
130         }
131         else {
132             List JavaDoc 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     /**
168      * Hook method that subclasses may override to react to compilation.
169      * This implementation does nothing.
170      */

171     protected void onCompileInternal() {
172     }
173
174     /**
175      * Get the call string.
176      */

177     public String JavaDoc getCallString() {
178         return this.callString;
179     }
180
181     /**
182      * Return a CallableStatementCreator to perform an operation
183      * with this parameters.
184      * @param inParams parameters. May be <code>null</code>.
185      */

186     protected CallableStatementCreator newCallableStatementCreator(Map JavaDoc inParams) {
187         return this.callableStatementFactory.newCallableStatementCreator(inParams);
188     }
189
190     /**
191      * Return a CallableStatementCreator to perform an operation
192      * with the parameters returned from this ParameterMapper.
193      * @param inParamMapper parametermapper. May not be <code>null</code>.
194      */

195     protected CallableStatementCreator newCallableStatementCreator(ParameterMapper inParamMapper) {
196         return this.callableStatementFactory.newCallableStatementCreator(inParamMapper);
197     }
198
199 }
200
Popular Tags