KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > QueryResult


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: QueryResult.java,v 1.5 2003/10/10 22:22:15 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.sql.ResultSet JavaDoc;
14 import java.sql.SQLException JavaDoc;
15 import java.util.AbstractCollection JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20 import javax.jdo.JDODataStoreException;
21 import javax.jdo.JDOUserException;
22
23
24 public final class QueryResult extends AbstractCollection JavaDoc
25 {
26     private final Query query;
27     private final Query.ResultObjectFactory rof;
28     private ResultSet JavaDoc rs;
29     private final StoreManager storeMgr;
30
31     private boolean moreResultSetRows;
32     private ArrayList JavaDoc resultObjs = new ArrayList JavaDoc();
33
34     public QueryResult(Query query, Query.ResultObjectFactory rof, ResultSet JavaDoc rs) throws SQLException JavaDoc
35     {
36         this.query = query;
37         this.rof = rof;
38         this.rs = rs;
39
40         storeMgr = query.getStoreManager();
41
42         if (!(moreResultSetRows = rs.next()))
43             closeResultSet();
44         else if (!query.getPersistenceManager().currentTransaction().isActive())
45             advanceToEndOfResultSet();
46     }
47
48     private void advanceToEndOfResultSet()
49     {
50         /* Advance through any remaining result set rows. */
51         while (moreResultSetRows)
52             nextResultSetElement();
53     }
54
55     private boolean isOpen()
56     {
57         return resultObjs != null;
58     }
59
60     private void assertIsOpen()
61     {
62         if( ! isOpen() )
63             throw new JDOUserException("Query result has been closed");
64     }
65
66     private Object JavaDoc nextResultSetElement()
67     {
68         Object JavaDoc nextElement = rof.getObject(rs);
69
70         storeMgr.logSQLWarnings(rs);
71
72         resultObjs.add(nextElement);
73
74         try
75         {
76             if (!(moreResultSetRows = rs.next()))
77                 closeResultSet();
78         }
79         catch (SQLException JavaDoc e)
80         {
81             throw storeMgr.getDatabaseAdapter().newDataStoreException("Failed reading result set", e);
82         }
83
84         return nextElement;
85     }
86
87     public Iterator JavaDoc iterator()
88     {
89         assertIsOpen();
90
91         return new QueryResultIterator();
92     }
93
94     private class QueryResultIterator implements Iterator JavaDoc
95     {
96         private int nextRowNum = 0;
97
98         public QueryResultIterator()
99         {
100         }
101
102         public boolean hasNext()
103         {
104             synchronized (QueryResult.this)
105             {
106                 if( ! isOpen() )
107                     return false;
108
109                 return nextRowNum < resultObjs.size() ? true : moreResultSetRows;
110             }
111         }
112
113         public Object JavaDoc next()
114         {
115             synchronized (QueryResult.this)
116             {
117                 if( ! isOpen() )
118                     throw new NoSuchElementException JavaDoc("This query result has been closed");
119
120                 Object JavaDoc nextElement = null;
121
122                 if (nextRowNum < resultObjs.size())
123                     nextElement = resultObjs.get(nextRowNum);
124                 else if (moreResultSetRows)
125                     nextElement = nextResultSetElement();
126                 else
127                     throw new NoSuchElementException JavaDoc("No more elements in query result");
128
129                 ++nextRowNum;
130
131                 return nextElement;
132             }
133         }
134
135         public void remove()
136         {
137             throw new UnsupportedOperationException JavaDoc("Query result sets are not modifiable");
138         }
139     }
140
141     public synchronized boolean contains(Object JavaDoc o)
142     {
143         assertIsOpen();
144         advanceToEndOfResultSet();
145
146         return resultObjs.contains(o);
147     }
148
149     public synchronized boolean containsAll(Collection JavaDoc c)
150     {
151         assertIsOpen();
152         advanceToEndOfResultSet();
153
154         return resultObjs.containsAll(c);
155     }
156
157     public synchronized boolean isEmpty()
158     {
159         assertIsOpen();
160
161         return resultObjs.isEmpty() && !moreResultSetRows;
162     }
163
164     public synchronized int size()
165     {
166         assertIsOpen();
167         advanceToEndOfResultSet();
168
169         return resultObjs.size();
170     }
171
172     public synchronized Object JavaDoc[] toArray()
173     {
174         assertIsOpen();
175         advanceToEndOfResultSet();
176
177         return resultObjs.toArray();
178     }
179
180     public synchronized Object JavaDoc[] toArray(Object JavaDoc[] a)
181     {
182         assertIsOpen();
183         advanceToEndOfResultSet();
184
185         return resultObjs.toArray(a);
186     }
187
188     private void closeResultSet()
189     {
190         if (rs != null)
191         {
192             try
193             {
194                 try
195                 {
196                     rs.getStatement().close();
197                 }
198                 catch (SQLException JavaDoc e)
199                 {
200                     throw storeMgr.getDatabaseAdapter().newDataStoreException("Closing query result failed", e);
201                 }
202             }
203             finally
204             {
205                 rs = null;
206             }
207         }
208     }
209
210     public synchronized void close()
211     {
212         resultObjs = null;
213         moreResultSetRows = false;
214
215         closeResultSet();
216     }
217 }
218
Popular Tags