KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > queryframework > SQLCall


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.queryframework;
23
24 import java.util.Vector JavaDoc;
25 import java.io.*;
26 import oracle.toplink.essentials.internal.databaseaccess.*;
27 import oracle.toplink.essentials.internal.helper.DatabaseField;
28 import oracle.toplink.essentials.internal.sessions.AbstractSession;
29 import oracle.toplink.essentials.exceptions.ValidationException;
30 import oracle.toplink.essentials.internal.expressions.ParameterExpression;
31
32 /**
33  * <b>Purpose</b>: Used as an abstraction of an SQL call.
34  * A call is an SQL string with parameters.
35  */

36 public class SQLCall extends DatabaseCall implements QueryStringCall {
37     protected boolean hasCustomSQLArguments;
38
39     /**
40      * PUBLIC:
41      * Create a new SQL call.
42      */

43     public SQLCall() {
44         super();
45         this.hasCustomSQLArguments = false;
46     }
47
48     /**
49      * PUBLIC:
50      * Create a new SQL call.
51      */

52     public SQLCall(String JavaDoc sqlString) {
53         this();
54         setSQLString(sqlString);
55     }
56
57     /**
58      * INTERNAL:
59      * Set the data passed through setCustomSQLArgumentType and useCustomSQLCursorOutputAsResultSet methods.
60      */

61     protected void afterTranslateCustomQuery(Vector JavaDoc updatedParameters, Vector JavaDoc updatedParameterTypes) {
62         for (int i = 0; i < getParameters().size(); i++) {
63             Integer JavaDoc parameterType = (Integer JavaDoc)getParameterTypes().elementAt(i);
64             Object JavaDoc parameter = getParameters().elementAt(i);
65             if ((parameterType == MODIFY) || (parameterType == OUT) || (parameterType == OUT_CURSOR) || ((parameterType == IN) && parameter instanceof DatabaseField)) {
66                 DatabaseField field = (DatabaseField)parameter;
67                 afterTranslateCustomQueryUpdateParameter(field, i, parameterType, updatedParameters, updatedParameterTypes);
68             } else if (parameterType == INOUT) {
69                 DatabaseField outField = (DatabaseField)((Object JavaDoc[])parameter)[1];
70                 afterTranslateCustomQueryUpdateParameter(outField, i, parameterType, updatedParameters, updatedParameterTypes);
71                 if ((((Object JavaDoc[])parameter)[0] instanceof DatabaseField) && (((Object JavaDoc[])parameter)[0] != ((Object JavaDoc[])parameter)[1])) {
72                     DatabaseField inField = (DatabaseField)((Object JavaDoc[])parameter)[0];
73                     afterTranslateCustomQueryUpdateParameter(inField, i, parameterType, updatedParameters, updatedParameterTypes);
74                 }
75             }
76         }
77     }
78
79     /**
80      * INTERNAL:
81      * Set the data passed through setCustomSQLArgumentType and useCustomSQLCursorOutputAsResultSet methods.
82      */

83     protected void afterTranslateCustomQueryUpdateParameter(DatabaseField field, int index, Integer JavaDoc parameterType, Vector JavaDoc updatedParameters, Vector JavaDoc updatedParameterTypes) {
84         for (int j = 0; j < updatedParameters.size(); j++) {
85             DatabaseField updateField = (DatabaseField)updatedParameters.elementAt(j);
86             if (field.equals(updateField)) {
87                 Integer JavaDoc updateParameterType = (Integer JavaDoc)updatedParameterTypes.elementAt(j);
88                 if (updateParameterType == null) {
89                     field.setType(updateField.getType());
90                 } else if (updateParameterType == OUT_CURSOR) {
91                     if (parameterType == OUT) {
92                         getParameterTypes().setElementAt(OUT_CURSOR, index);
93                     } else {
94                         throw ValidationException.cannotSetCursorForParameterTypeOtherThanOut(field.getName(), toString());
95                     }
96                 }
97                 break;
98             }
99         }
100     }
101
102     /**
103      * INTERNAL:
104      * Used to avoid misiterpreting the # in custom SQL.
105      */

106     public boolean hasCustomSQLArguments() {
107         return hasCustomSQLArguments;
108     }
109
110     public boolean isSQLCall() {
111         return true;
112     }
113
114     public boolean isQueryStringCall() {
115         return true;
116     }
117
118     /**
119      * INTERNAL:
120      * Called by prepare method only.
121      */

122     protected void prepareInternal(AbstractSession session) {
123         if (hasCustomSQLArguments()) {
124             // hold results of setCustomSQLArgumentType and useCustomSQLCursorOutputAsResultSet methods
125
Vector JavaDoc updatedParameters = null;
126             Vector JavaDoc updatedParameterTypes = null;
127             if (getParameters().size() > 0) {
128                 updatedParameters = getParameters();
129                 setParameters(oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance());
130                 updatedParameterTypes = getParameterTypes();
131                 setParameterTypes(oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance());
132             }
133
134             translateCustomQuery();
135
136             if (updatedParameters != null) {
137                 afterTranslateCustomQuery(updatedParameters, updatedParameterTypes);
138             }
139         }
140
141         super.prepareInternal(session);
142     }
143
144     /**
145      * INTERNAL:
146      * Used to avoid misiterpreting the # in custom SQL.
147      */

148     public void setHasCustomSQLArguments(boolean hasCustomSQLArguments) {
149         this.hasCustomSQLArguments = hasCustomSQLArguments;
150     }
151
152     /**
153      * PUBLIC:
154      * This method should only be used with custom SQL:
155      * it sets a type to OUT or INOUT parameter (prefixed with ### or #### in custom SQL string).
156      */

157     public void setCustomSQLArgumentType(String JavaDoc customParameterName, Class JavaDoc type) {
158         DatabaseField field = new DatabaseField(customParameterName);
159         field.setType(type);
160         getParameters().add(field);
161         getParameterTypes().add(null);
162     }
163
164     /**
165      * Set the SQL string.
166      */

167     public void setSQLString(String JavaDoc sqlString) {
168         setSQLStringInternal(sqlString);
169     }
170
171     /**
172      * INTERNAL:
173      * All values are printed as ? to allow for parameter binding or translation during the execute of the call.
174      */

175     public void appendTranslationParameter(Writer writer, ParameterExpression expression, DatabasePlatform platform) throws IOException {
176         try {
177             platform.writeParameterMarker(writer, expression);
178         } catch (IOException exception) {
179             throw ValidationException.fileError(exception);
180         }
181         getParameters().addElement(expression);
182         getParameterTypes().addElement(TRANSLATION);
183     }
184
185     /**
186      * PUBLIC:
187      * This method should only be used with custom SQL:
188      * Used for Oracle result sets through procedures.
189      * It defines OUT parameter (prefixed with ### in custom SQL string)
190      * as a cursor output.
191      */

192     public void useCustomSQLCursorOutputAsResultSet(String JavaDoc customParameterName) {
193         DatabaseField field = new DatabaseField(customParameterName);
194         getParameters().add(field);
195         getParameterTypes().add(OUT_CURSOR);
196         setIsCursorOutputProcedure(true);
197     }
198 }
199
Popular Tags