KickJava   Java API By Example, From Geeks To Geeks.

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


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, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.queryframework;
23
24 import java.util.*;
25 import oracle.toplink.essentials.internal.helper.*;
26 import oracle.toplink.essentials.exceptions.*;
27 import oracle.toplink.essentials.internal.queryframework.*;
28 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
29
30 /**
31  * <p><b>Purpose</b>:
32  * Concrete class to perform read using raw SQL.
33  * <p>
34  * <p><b>Responsibilities</b>:
35  * Execute a selecting raw SQL string.
36  * This returns a Collection of the DatabaseRows representing the result set.
37  *
38  * @author Yvon Lavoie
39  * @since TOPLink/Java 1.0
40  */

41 public class DataReadQuery extends ReadQuery {
42     protected ContainerPolicy containerPolicy;
43     
44     //** For EJB 3 support returns results without using the AbstractRecord */
45
protected boolean useAbstractRecord = true;
46
47     /**
48      * PUBLIC:
49      * Initialize the state of the query.
50      */

51     public DataReadQuery() {
52         super();
53         this.shouldMaintainCache = false;
54         useAbstractRecord = true;
55         setContainerPolicy(ContainerPolicy.buildPolicyFor(ClassConstants.Vector_class));
56     }
57
58     /**
59      * PUBLIC:
60      * Initialize the query to use the specified SQL string.
61      */

62     public DataReadQuery(String JavaDoc sqlString) {
63         this();
64         setSQLString(sqlString);
65     }
66
67     /**
68      * PUBLIC:
69      * Initialize the query to use the specified call.
70      */

71     public DataReadQuery(Call call) {
72         this();
73         setCall(call);
74     }
75
76     /**
77      * INTERNAL:
78      * Clone the query.
79      */

80     public Object JavaDoc clone() {
81         DataReadQuery cloneQuery = (DataReadQuery)super.clone();
82         cloneQuery.setContainerPolicy(getContainerPolicy().clone(cloneQuery));
83         return cloneQuery;
84     }
85
86     /**
87      * INTERNAL:
88      * Execute the query.
89      * Perform the work to execute the SQL string.
90      * @exception DatabaseException an error has occurred on the database
91      * @return a collection or cursor of DatabaseRows representing the result set
92      */

93     public Object JavaDoc executeDatabaseQuery() throws DatabaseException {
94         if (getContainerPolicy().overridesRead()) {
95             return getContainerPolicy().execute();
96         }
97         return executeNonCursor();
98     }
99
100     /**
101      * INTERNAL:
102      * The results are *not* in a cursor, build the collection.
103      */

104     protected Object JavaDoc executeNonCursor() throws DatabaseException {
105         Vector rows = getQueryMechanism().executeSelect();
106         if (useAbstractRecord ){
107             Object JavaDoc results = getContainerPolicy().buildContainerFromVector(rows, getSession());
108             return results;
109         }
110         ContainerPolicy containerPolicy = getContainerPolicy();
111         Object JavaDoc reportResults = containerPolicy.containerInstance(rows.size());
112         for (Iterator rowsEnum = rows.iterator(); rowsEnum.hasNext();) {
113             containerPolicy.addInto( ((AbstractRecord)rowsEnum.next()).getValues() , reportResults, getSession());
114         }
115         return reportResults;
116     }
117
118     /**
119      * PUBLIC:
120      * Return the query's ContainerPolicy.
121      * @return oracle.toplink.essentials.internal.queryframework.ContainerPolicy
122      */

123     public ContainerPolicy getContainerPolicy() {
124         return containerPolicy;
125     }
126
127     /**
128      * PUBLIC:
129      * Return if this is a data read query.
130      */

131     public boolean isDataReadQuery() {
132         return true;
133     }
134
135     /**
136      * INTERNAL:
137      * Prepare the receiver for execution in a session.
138      */

139     protected void prepare() {
140         super.prepare();
141         getContainerPolicy().prepare(this, getSession());
142         if (getContainerPolicy().overridesRead()) {
143             return;
144         }
145         getQueryMechanism().prepareExecuteSelect();
146     }
147
148     /**
149      * INTERNAL:
150      * Prepare the receiver for execution in a session.
151      */

152     public void prepareForExecution() throws QueryException {
153         super.prepareForExecution();
154         getContainerPolicy().prepareForExecution();
155     }
156
157     /**
158      * PUBLIC:
159      * Set the container policy.
160      */

161     public void setContainerPolicy(ContainerPolicy containerPolicy) {
162         // Fix for BUG 3337003 - TopLink OX will try to set this to null if
163
// it is not set in the deployment XML. So don't allow it to do that.
164
if (containerPolicy == null) {
165             return;
166         }
167
168         this.containerPolicy = containerPolicy;
169     }
170
171     /**
172      * PUBLIC:
173      * Configure the query to use an instance of the specified container class
174      * to hold the target objects.
175      * The container class must implement (directly or indirectly) the Collection interface.
176      */

177     public void useCollectionClass(Class JavaDoc concreteClass) {
178         setContainerPolicy(ContainerPolicy.buildPolicyFor(concreteClass));
179     }
180     
181     /**
182      * INTERNAL:
183      * Allow changing the default behaviour so that AbstractRecords are not returned as query results.
184      */

185     public void setUseAbstractRecord(boolean useAbstractRecord){
186         this.useAbstractRecord = useAbstractRecord;
187     }
188 }
189
Popular Tags