KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > access > QueryResult


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.access;
58
59 import java.util.ArrayList JavaDoc;
60 import java.util.Collection JavaDoc;
61 import java.util.Collections JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64
65 import org.apache.commons.collections.map.LinkedMap;
66 import org.objectstyle.cayenne.CayenneRuntimeException;
67 import org.objectstyle.cayenne.QueryResponse;
68 import org.objectstyle.cayenne.access.util.DefaultOperationObserver;
69 import org.objectstyle.cayenne.query.Query;
70 import org.objectstyle.cayenne.util.Util;
71
72 /**
73  * QueryResult encapsulates a result of execution of zero or more queries using
74  * QueryEngine. QueryResult supporting queries with multiple mixed selects and updates,
75  * such as ProcedureQueries.
76  *
77  * @author Andrei Adamchik
78  */

79 public class QueryResult extends DefaultOperationObserver implements QueryResponse {
80
81     // LinkedMap guarantees that iterating over
82
// its keys is done in the original insertion order-
83
// something that is needed to return executed queries in
84
// the right order. (Java 1.4 adds LinkedHashMap that has
85
// the same functionality, but we have to stay 1.3 comatible)
86
protected LinkedMap queries = new LinkedMap();
87
88     /** Clears any previously collected information. */
89     public void clear() {
90         queries.clear();
91     }
92
93     /**
94      * Returns an iterator over all executed queries in the order they were executed.
95      */

96     public Iterator JavaDoc getQueries() {
97         return queries.asList().iterator();
98     }
99     
100     /**
101      * Returns all results regardless of the query.
102      *
103      * @since 1.2
104      */

105     public Collection JavaDoc getResults() {
106         if (queries.isEmpty()) {
107             return Collections.EMPTY_LIST;
108         }
109
110         Collection JavaDoc results = new ArrayList JavaDoc(5);
111
112         Iterator JavaDoc it = queries.values().iterator();
113         while (it.hasNext()) {
114             Collection JavaDoc queryResult = (Collection JavaDoc) it.next();
115             results.addAll(queryResult);
116         }
117
118         return results;
119     }
120
121     /**
122      * Returns a list of all results of a given query. This is potentially a mix of
123      * java.lang.Integer values for update operations and java.util.List for select
124      * operations. Results are returned in the order they were obtained.
125      */

126     public List JavaDoc getResults(Query query) {
127         List JavaDoc list = (List JavaDoc) queries.get(query);
128         return (list != null) ? list : Collections.EMPTY_LIST;
129     }
130
131     /**
132     * Returns the first update count for the query. This is a shortcut for
133     * <code>(Integer)getUpdates(query).get(0)<code>, kind of like Google's "I'm feeling lucky".
134     * Returns -1 if no update count is found for the query.
135     */

136     public int getFirstUpdateCount(Query query) {
137         List JavaDoc allResults = getResults(query);
138         int size = allResults.size();
139         if (size > 0) {
140             Iterator JavaDoc it = allResults.iterator();
141             while (it.hasNext()) {
142                 Object JavaDoc obj = it.next();
143                 if (obj instanceof Number JavaDoc) {
144                     return ((Number JavaDoc) obj).intValue();
145                 }
146             }
147         }
148         return -1;
149     }
150
151     /**
152      * Returns the first results for the query. This is a shortcut for
153      * <code>(List)getRows(query).get(0)<code>, kind of like Google's "I'm feeling lucky".
154      */

155     public List JavaDoc getFirstRows(Query query) {
156         List JavaDoc allResults = getResults(query);
157         int size = allResults.size();
158         if (size == 0) {
159             return Collections.EMPTY_LIST;
160         } else {
161             Iterator JavaDoc it = allResults.iterator();
162             while (it.hasNext()) {
163                 Object JavaDoc obj = it.next();
164                 if (obj instanceof List JavaDoc) {
165                     return (List JavaDoc) obj;
166                 }
167             }
168         }
169
170         return Collections.EMPTY_LIST;
171     }
172
173     /**
174      * Returns a List that itself contains Lists of data rows for each ResultSet
175      * returned by the query. ResultSets are returned in the oder they were obtained.
176      * Any updates that were performed are not included.
177      */

178     public List JavaDoc getRows(Query query) {
179         List JavaDoc allResults = getResults(query);
180         int size = allResults.size();
181         if (size == 0) {
182             return Collections.EMPTY_LIST;
183         }
184
185         List JavaDoc list = new ArrayList JavaDoc(size);
186         Iterator JavaDoc it = allResults.iterator();
187         while (it.hasNext()) {
188             Object JavaDoc obj = it.next();
189             if (obj instanceof List JavaDoc) {
190                 list.add(obj);
191             }
192         }
193
194         return list;
195     }
196
197     /**
198      * Returns a List that contains java.lang.Integer objects for each one of the update
199      * counts returned by the query. Update counts are returned in the order they were obtained.
200      * Data rows are not included.
201      */

202     public List JavaDoc getUpdates(Query query) {
203         List JavaDoc allResults = getResults(query);
204         int size = allResults.size();
205         if (size == 0) {
206             return Collections.EMPTY_LIST;
207         }
208
209         List JavaDoc list = new ArrayList JavaDoc(size);
210         Iterator JavaDoc it = allResults.iterator();
211         while (it.hasNext()) {
212             Object JavaDoc obj = it.next();
213             if (obj instanceof Number JavaDoc) {
214                 list.add(obj);
215             }
216         }
217
218         return list;
219     }
220
221     /**
222      * Overrides superclass implementation to rethrow an exception
223      * immediately.
224      */

225     public void nextQueryException(Query query, Exception JavaDoc ex) {
226         super.nextQueryException(query, ex);
227         throw new CayenneRuntimeException(
228             "Query exception.",
229             Util.unwindException(ex));
230     }
231
232     /**
233      * Overrides superclass implementation to rethrow an exception
234      * immediately.
235      */

236     public void nextGlobalException(Exception JavaDoc ex) {
237         super.nextGlobalException(ex);
238         throw new CayenneRuntimeException(
239             "Global exception.",
240             Util.unwindException(ex));
241     }
242
243     /**
244      * Always returns <code>false</code>, iterated results are not supported.
245      */

246     public boolean isIteratedResult() {
247         return false;
248     }
249
250     public void nextBatchCount(Query query, int[] resultCount) {
251         for (int i = 0; i < resultCount.length; i++) {
252             nextCount(query, resultCount[i]);
253         }
254     }
255
256     public void nextCount(Query query, int resultCount) {
257         super.nextCount(query, resultCount);
258
259         List JavaDoc list = (List JavaDoc) queries.get(query);
260         if (list == null) {
261             list = new ArrayList JavaDoc(5);
262             queries.put(query, list);
263         }
264
265         list.add(new Integer JavaDoc(resultCount));
266     }
267
268     public void nextDataRows(Query query, List JavaDoc dataRows) {
269         super.nextDataRows(query, dataRows);
270
271         List JavaDoc list = (List JavaDoc) queries.get(query);
272         if (list == null) {
273             list = new ArrayList JavaDoc(5);
274             queries.put(query, list);
275         }
276
277         list.add(dataRows);
278     }
279
280     public void nextDataRows(Query q, ResultIterator it) {
281         throw new CayenneRuntimeException(
282             "Iterated results are not supported by "
283                 + this.getClass().getName());
284     }
285 }
286
Popular Tags