KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > queryframework > CallQueryMechanism


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.internal.queryframework;
23
24 import java.util.*;
25 import oracle.toplink.essentials.internal.helper.*;
26 import oracle.toplink.essentials.internal.databaseaccess.*;
27 import oracle.toplink.essentials.internal.expressions.*;
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.queryframework.*;
30 import oracle.toplink.essentials.internal.sessions.AbstractSession;
31
32 /**
33  * <p><b>Purpose</b>:
34  * Mechanism used for custom SQL and stored procedure queries.
35  * <p>
36  * <p><b>Responsibilities</b>:
37  * Executes the appropriate call.
38  *
39  * @author James Sutherland
40  * @since TOPLink/Java 2.0
41  */

42 public class CallQueryMechanism extends DatasourceCallQueryMechanism {
43
44     /**
45      * Initialize the state of the query
46      * @param query - owner of mechanism
47      */

48     public CallQueryMechanism(DatabaseQuery query) {
49         super(query);
50     }
51
52     /**
53      * Initialize the state of the query
54      * @param query - owner of mechanism
55      * @param call - sql call
56      */

57     public CallQueryMechanism(DatabaseQuery query, DatabaseCall call) {
58         super(query, call);
59         call.setIsFieldMatchingRequired(true);
60     }
61
62     /**
63      * Return the call.
64      */

65     public DatabaseCall getDatabaseCall() {
66         return (DatabaseCall)call;
67     }
68
69     /**
70      * INTERNAL:
71      * This is different from 'prepareForExecution' in that this is called on the original query,
72      * and the other is called on the copy of the query.
73      * This query is copied for concurrency so this prepare can only setup things that
74      * will apply to any future execution of this query.
75      */

76     public void prepareCall() throws QueryException {
77         DatabaseQuery query = getQuery();
78         AbstractSession executionSession = getSession().getExecutionSession(query);
79         if (hasMultipleCalls()) {
80             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
81                 DatabaseCall call = (DatabaseCall)callsEnum.nextElement();
82                 if (!query.shouldIgnoreBindAllParameters()) {
83                     call.setUsesBinding(query.shouldBindAllParameters());
84                 }
85                 if (!query.shouldIgnoreCacheStatement()) {
86                     call.setShouldCacheStatement(query.shouldCacheStatement());
87                 }
88                 if (query.isReadQuery()) {
89                     ReadQuery readQuery = (ReadQuery)query;
90                     call.setMaxRows(readQuery.getMaxRows());
91                     if (readQuery.getFirstResult() != 0) {
92                         call.setFirstResult(readQuery.getFirstResult());
93                         call.setIsResultSetScrollable(true);
94                         call.setResultSetType(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE);
95                         call.setResultSetConcurrency(java.sql.ResultSet.CONCUR_READ_ONLY);
96                     }
97                 }
98                 call.prepare(executionSession);
99             }
100         } else if (getCall() != null) {
101             DatabaseCall call = getDatabaseCall();
102             if (!query.shouldIgnoreBindAllParameters()) {
103                 call.setUsesBinding(query.shouldBindAllParameters());
104             }
105             if (!query.shouldIgnoreCacheStatement()) {
106                 call.setShouldCacheStatement(query.shouldCacheStatement());
107             }
108             if (query.isReadQuery()) {
109                 ReadQuery readQuery = (ReadQuery)query;
110                 call.setMaxRows(readQuery.getMaxRows());
111                 if (readQuery.getFirstResult() != 0) {
112                     call.setFirstResult(readQuery.getFirstResult());
113                     call.setIsResultSetScrollable(true);
114                     call.setResultSetType(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE);
115                     call.setResultSetConcurrency(java.sql.ResultSet.CONCUR_READ_ONLY);
116                 }
117             }
118             call.prepare(executionSession);
119         }
120     }
121
122     /**
123      * Pre-build configure the SQL call.
124      */

125     public void prepareDeleteAll() {
126         if (hasMultipleCalls()) {
127             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
128                 DatabaseCall call = (DatabaseCall)callsEnum.nextElement();
129                 call.returnNothing();
130                 if (getQuery().getDescriptor().usesOptimisticLocking()) {
131                     call.setHasOptimisticLock(true);
132                 }
133             }
134         } else {
135             getCall().returnNothing();
136             if (getQuery().getDescriptor().usesOptimisticLocking()) {
137                 getDatabaseCall().setHasOptimisticLock(true);
138             }
139         }
140
141         prepareCall();
142     }
143
144     /**
145      * Pre-build configure the SQL call.
146      */

147     public void prepareDeleteObject() {
148         if (hasMultipleCalls()) {
149             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
150                 DatabaseCall call = (DatabaseCall)callsEnum.nextElement();
151                 call.returnNothing();
152                 if (getQuery().getDescriptor().usesOptimisticLocking()) {
153                     call.setHasOptimisticLock(true);
154                 }
155             }
156         } else {
157             getCall().returnNothing();
158             if (getQuery().getDescriptor().usesOptimisticLocking()) {
159                 getDatabaseCall().setHasOptimisticLock(true);
160             }
161         }
162         prepareCall();
163     }
164
165     /**
166      * Pre-build configure the SQL call.
167      */

168     public void prepareDoesExist(DatabaseField field) {
169         getCall().returnOneRow();
170         Vector fields = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1);
171         fields.addElement(field);
172         getDatabaseCall().setFields(fields);
173         prepareCall();
174     }
175
176     /**
177      * Pre-build configure the SQL call.
178      */

179     public void prepareExecuteSelect() {
180         if (hasMultipleCalls()) {
181             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
182                 DatabaseCall databseCall = (DatabaseCall)callsEnum.nextElement();
183                 databseCall.returnManyRows();
184                 databseCall.setIsFieldMatchingRequired(isCallQueryMechanism());
185             }
186         } else {
187             DatabaseCall call = getDatabaseCall();
188             call.returnManyRows();
189             call.setIsFieldMatchingRequired(isCallQueryMechanism());
190         }
191         prepareCall();
192     }
193
194     /**
195      * Pre-build configure the SQL call.
196      */

197     public void prepareSelectAllRows() {
198         if (hasMultipleCalls()) {
199             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
200                 DatabaseCall call = (DatabaseCall)callsEnum.nextElement();
201                 call.returnManyRows();
202                 if (isCallQueryMechanism()) {
203                     call.setIsFieldMatchingRequired(true);
204                     // Set the fieldsincluding joined and partial fields and compute joined indexes,
205
// this requires and assume that the custom SQL returns the fields in the correct order.
206
call.setFields(((ObjectLevelReadQuery)getQuery()).getSelectionFields());
207                     ((ObjectLevelReadQuery)getQuery()).getJoinedAttributeManager().computeJoiningMappingIndexes(true, getSession(), 0);
208                 }
209             }
210         } else {
211             getCall().returnManyRows();
212             if (isCallQueryMechanism()) {
213                 DatabaseCall call = getDatabaseCall();
214                 call.setIsFieldMatchingRequired(true);
215                 // Set the fieldsincluding joined and partial fields and compute joined indexes,
216
// this requires and assume that the custom SQL returns the fields in the correct order.
217
call.setFields(((ObjectLevelReadQuery)getQuery()).getSelectionFields());
218                 ((ObjectLevelReadQuery)getQuery()).getJoinedAttributeManager().computeJoiningMappingIndexes(true, getSession(), 0);
219             }
220         }
221         prepareCall();
222     }
223
224     /**
225      * Pre-build configure the SQL call.
226      */

227     public void prepareSelectOneRow() {
228         if (hasMultipleCalls()) {
229             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
230                 DatabaseCall call = (DatabaseCall)callsEnum.nextElement();
231                 call.returnOneRow();
232                 if (isCallQueryMechanism()) {
233                     call.setIsFieldMatchingRequired(true);
234                     // Set the fieldsincluding joined and partial fields and compute joined indexes,
235
// this requires and assume that the custom SQL returns the fields in the correct order.
236
call.setFields(((ObjectLevelReadQuery)getQuery()).getSelectionFields());
237                     ((ObjectLevelReadQuery)getQuery()).getJoinedAttributeManager().computeJoiningMappingIndexes(true, getSession(), 0);
238                 }
239             }
240         } else {
241             getCall().returnOneRow();
242             if (isCallQueryMechanism()) {
243                 DatabaseCall call = getDatabaseCall();
244                 call.setIsFieldMatchingRequired(true);
245                 // Set the fieldsincluding joined and partial fields and compute joined indexes,
246
// this requires and assume that the custom SQL returns the fields in the correct order.
247
call.setFields(((ObjectLevelReadQuery)getQuery()).getSelectionFields());
248                 ((ObjectLevelReadQuery)getQuery()).getJoinedAttributeManager().computeJoiningMappingIndexes(true, getSession(), 0);
249             }
250         }
251         prepareCall();
252     }
253
254     /**
255      * Pre-build configure the SQL call.
256      */

257     public void prepareUpdateObject() {
258         if (hasMultipleCalls()) {
259             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
260                 DatabaseCall call = (DatabaseCall)callsEnum.nextElement();
261                 call.returnNothing();
262                 if (getQuery().getDescriptor().usesOptimisticLocking()) {
263                     call.setHasOptimisticLock(true);
264                 }
265             }
266         } else if (getCall() != null) {
267             getCall().returnNothing();
268             if (getQuery().getDescriptor().usesOptimisticLocking()) {
269                 getDatabaseCall().setHasOptimisticLock(true);
270             }
271         }
272         prepareCall();
273     }
274
275     /**
276      * INTERNAL:
277      * Configure the call to be a dynamic custom SQL call, so that it ignore the # token.
278      */

279     public void setCallHasCustomSQLArguments() {
280         if (hasMultipleCalls()) {
281             for (Enumeration callsEnum = getCalls().elements(); callsEnum.hasMoreElements();) {
282                 DatabaseCall databseCall = (DatabaseCall)callsEnum.nextElement();
283                 if (databseCall.isSQLCall()) {
284                     ((SQLCall)databseCall).setHasCustomSQLArguments(true);
285                 }
286             }
287         } else if (getCall().isSQLCall()) {
288             ((SQLCall)getCall()).setHasCustomSQLArguments(true);
289         }
290     }
291
292     /**
293      * Update the foreign key fields when resolving a bi-directonal reference in a UOW.
294      * This must always be dynamic as it is called within an insert query and is really part of the insert
295      * and does not fire update events or worry about locking.
296      */

297     protected void updateForeignKeyFieldAfterInsert(WriteObjectQuery writeQuery) {
298         for (Enumeration tablesEnum = getDescriptor().getTables().elements();
299                  tablesEnum.hasMoreElements();) {
300             DatabaseTable table = (DatabaseTable)tablesEnum.nextElement();
301             SQLUpdateStatement updateStatement = new SQLUpdateStatement();
302             updateStatement.setModifyRow(getDescriptor().getObjectBuilder().buildRowForUpdate(writeQuery));
303             updateStatement.setTranslationRow(getTranslationRow());
304             updateStatement.setTable(table);
305             updateStatement.setWhereClause(getDescriptor().getObjectBuilder().buildPrimaryKeyExpression(table));// Must not check version, ok as just inserted it.
306
// Bug 2996585
307
StatementQueryMechanism updateMechanism = new StatementQueryMechanism(writeQuery, updateStatement);
308             writeQuery.setModifyRow(updateStatement.getModifyRow());
309             updateMechanism.updateObject();
310
311         }
312     }
313 }
314
Popular Tags