KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.access;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedHashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.cayenne.CayenneRuntimeException;
31 import org.apache.cayenne.access.util.DefaultOperationObserver;
32 import org.apache.cayenne.query.Query;
33 import org.apache.cayenne.util.Util;
34
35 /**
36  * QueryResult encapsulates a result of execution of zero or more queries using
37  * QueryEngine. QueryResult supports queries with multiple mixed selects and updates, such
38  * as ProcedureQueries.
39  *
40  * @author Andrus Adamchik
41  */

42 public class QueryResult extends DefaultOperationObserver {
43
44     // a map with order of iteration == to the order of insertion
45
protected Map JavaDoc queries = new LinkedHashMap JavaDoc();
46
47     /**
48      * Clears any previously collected information.
49      */

50     public void clear() {
51         queries.clear();
52     }
53
54     /**
55      * Returns an iterator over all executed queries in the order they were executed.
56      */

57     public Iterator JavaDoc getQueries() {
58         return queries.keySet().iterator();
59     }
60
61     /**
62      * Returns a list of all results of a given query. This is potentially a mix of
63      * java.lang.Integer values for update operations and java.util.List for select
64      * operations. Results are returned in the order they were obtained.
65      */

66     public List JavaDoc getResults(Query query) {
67         List JavaDoc list = (List JavaDoc) queries.get(query);
68         return (list != null) ? list : Collections.EMPTY_LIST;
69     }
70
71     /**
72      * Returns the first update count for the query. This is a shortcut for
73      * <code>(Integer)getUpdates(query).get(0)<code>, kind of like Google's "I'm feeling lucky".
74      * Returns -1 if no update count is found for the query.
75      */

76     public int getFirstUpdateCount(Query query) {
77         List JavaDoc allResults = getResults(query);
78         int size = allResults.size();
79         if (size > 0) {
80             Iterator JavaDoc it = allResults.iterator();
81             while (it.hasNext()) {
82                 Object JavaDoc object = it.next();
83
84                 // if int
85
if (object instanceof Number JavaDoc) {
86                     return ((Number JavaDoc) object).intValue();
87                 }
88                 // if batch...
89
else if (object instanceof int[]) {
90                     int[] counts = (int[]) object;
91                     return counts.length > 0 ? counts[0] : -1;
92                 }
93             }
94         }
95         return -1;
96     }
97
98     /**
99      * Returns the first update count. Returns int[0] if there was no update results for
100      * the query.
101      *
102      * @since 1.2
103      */

104     public int[] getFirstUpdateCounts(Query query) {
105         List JavaDoc allResults = getResults(query);
106         int size = allResults.size();
107
108         if (size > 0) {
109             Iterator JavaDoc it = allResults.iterator();
110             while (it.hasNext()) {
111                 Object JavaDoc object = it.next();
112
113                 // if int
114
if (object instanceof Number JavaDoc) {
115                     return new int[] {
116                         ((Number JavaDoc) object).intValue()
117                     };
118                 }
119                 // if batch...
120
else if (object instanceof int[]) {
121                     return (int[]) object;
122                 }
123             }
124         }
125
126         return new int[0];
127     }
128
129     /**
130      * Returns the first results for the query. This is a shortcut for
131      * <code>(List)getRows(query).get(0)<code>, kind of like Google's "I'm feeling lucky".
132      */

133     public List JavaDoc getFirstRows(Query query) {
134         List JavaDoc allResults = getResults(query);
135         int size = allResults.size();
136         if (size == 0) {
137             return Collections.EMPTY_LIST;
138         }
139         else {
140             Iterator JavaDoc it = allResults.iterator();
141             while (it.hasNext()) {
142                 Object JavaDoc obj = it.next();
143                 if (obj instanceof List JavaDoc) {
144                     return (List JavaDoc) obj;
145                 }
146             }
147         }
148
149         return Collections.EMPTY_LIST;
150     }
151
152     /**
153      * Returns a List that itself contains Lists of data rows for each ResultSet returned
154      * by the query. ResultSets are returned in the oder they were obtained. Any updates
155      * that were performed are not included.
156      */

157     public List JavaDoc getRows(Query query) {
158         List JavaDoc allResults = getResults(query);
159         int size = allResults.size();
160         if (size == 0) {
161             return Collections.EMPTY_LIST;
162         }
163
164         List JavaDoc list = new ArrayList JavaDoc(size);
165         Iterator JavaDoc it = allResults.iterator();
166         while (it.hasNext()) {
167             Object JavaDoc obj = it.next();
168             if (obj instanceof List JavaDoc) {
169                 list.add(obj);
170             }
171         }
172
173         return list;
174     }
175
176     /**
177      * Returns a List that contains java.lang.Integer objects for each one of the update
178      * counts returned by the query. Update counts are returned in the order they were
179      * obtained. Batched and regular updates are combined together.
180      */

181     public List JavaDoc getUpdates(Query query) {
182         List JavaDoc allResults = getResults(query);
183         int size = allResults.size();
184         if (size == 0) {
185             return Collections.EMPTY_LIST;
186         }
187
188         List JavaDoc list = new ArrayList JavaDoc(size);
189         Iterator JavaDoc it = allResults.iterator();
190         while (it.hasNext()) {
191             Object JavaDoc object = it.next();
192             if (object instanceof Number JavaDoc) {
193                 list.add(object);
194             }
195             else if (object instanceof int[]) {
196                 int[] ints = (int[]) object;
197                 for (int i = 0; i < ints.length; i++) {
198                     list.add(new Integer JavaDoc(ints[i]));
199                 }
200             }
201         }
202
203         return list;
204     }
205
206     /**
207      * Overrides superclass implementation to rethrow an exception immediately.
208      */

209     public void nextQueryException(Query query, Exception JavaDoc ex) {
210         super.nextQueryException(query, ex);
211         throw new CayenneRuntimeException("Query exception.", Util.unwindException(ex));
212     }
213
214     /**
215      * Overrides superclass implementation to rethrow an exception immediately.
216      */

217     public void nextGlobalException(Exception JavaDoc ex) {
218         super.nextGlobalException(ex);
219         throw new CayenneRuntimeException("Global exception.", Util.unwindException(ex));
220     }
221
222     /**
223      * Always returns <code>false</code>, iterated results are not supported.
224      */

225     public boolean isIteratedResult() {
226         return false;
227     }
228
229     public void nextBatchCount(Query query, int[] resultCount) {
230         List JavaDoc list = (List JavaDoc) queries.get(query);
231         if (list == null) {
232             list = new ArrayList JavaDoc(5);
233             queries.put(query, list);
234         }
235
236         list.add(resultCount);
237     }
238
239     public void nextCount(Query query, int resultCount) {
240         super.nextCount(query, resultCount);
241
242         List JavaDoc list = (List JavaDoc) queries.get(query);
243         if (list == null) {
244             list = new ArrayList JavaDoc(5);
245             queries.put(query, list);
246         }
247
248         list.add(new Integer JavaDoc(resultCount));
249     }
250
251     public void nextDataRows(Query query, List JavaDoc dataRows) {
252         super.nextDataRows(query, dataRows);
253
254         List JavaDoc list = (List JavaDoc) queries.get(query);
255         if (list == null) {
256             list = new ArrayList JavaDoc(5);
257             queries.put(query, list);
258         }
259
260         list.add(dataRows);
261     }
262
263     public void nextDataRows(Query q, ResultIterator it) {
264         throw new CayenneRuntimeException("Iterated results are not supported by "
265                 + this.getClass().getName());
266     }
267
268 }
269
Popular Tags