KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > storage > rdbms > TableRowIterator


1 /*
2  * TableRowIterator.java
3  *
4  * Version: $Revision: 1.8 $
5  *
6  * Date: $Date: 2006/05/26 14:08:49 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.storage.rdbms;
41
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.sql.Statement JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.List JavaDoc;
47
48 /**
49  * Represents the results of a database query
50  *
51  * @author Peter Breton
52  * @version $Revision: 1.8 $
53  */

54 public class TableRowIterator
55 {
56     /**
57      * Results from a query
58      */

59     private ResultSet JavaDoc results;
60
61     /**
62      * Statement used to submit the query
63      */

64     private Statement JavaDoc statemt = null;
65
66     /**
67      * The name of the RDBMS table
68      */

69     private String JavaDoc table;
70
71     /**
72      * True if there is a next row
73      */

74     private boolean hasNext = true;
75
76     /**
77      * True if we have already advanced to the next row.
78      */

79     private boolean hasAdvanced = false;
80
81     /**
82      * Constructor
83      *
84      * @param results -
85      * A JDBC ResultSet
86      */

87     TableRowIterator(ResultSet JavaDoc results)
88     {
89         this(results, null);
90         statemt = null;
91     }
92
93     /**
94      * Constructor
95      *
96      * @param results -
97      * A JDBC ResultSet
98      * @param table -
99      * The name of the table
100      */

101     TableRowIterator(ResultSet JavaDoc results, String JavaDoc table)
102     {
103         this.results = results;
104         this.table = table;
105         statemt = null;
106     }
107
108     /**
109      * Finalize -- this method is called when this object is GC-ed.
110      */

111     public void finalize()
112     {
113         close();
114     }
115
116     /**
117      * setStatement -- this method saves the statement used to do the query. We
118      * must keep this so that the statement can be closed when we are finished.
119      *
120      * @param st -
121      * The statement used to do the query that created this
122      * TableRowIterator
123      */

124     public void setStatement(Statement JavaDoc st)
125     {
126         statemt = st;
127     }
128
129     /**
130      * Advance to the next row and return it. Returns null if there are no more
131      * rows.
132      *
133      * @return - The next row, or null if no more rows
134      * @exception SQLException -
135      * If a database error occurs while fetching values
136      */

137     public TableRow next() throws SQLException JavaDoc
138     {
139         if (results == null)
140         {
141             return null;
142         }
143
144         if (!hasNext())
145         {
146             return null;
147         }
148
149         hasAdvanced = false;
150
151         return DatabaseManager.process(results, table);
152     }
153
154     /**
155      * Return true if there are more rows, false otherwise
156      *
157      * @return - true if there are more rows, false otherwise
158      * @exception SQLException -
159      * If a database error occurs while fetching values
160      */

161     public boolean hasNext() throws SQLException JavaDoc
162     {
163         if (results == null)
164         {
165             return false;
166         }
167
168         if (hasAdvanced)
169         {
170             return hasNext;
171         }
172
173         hasAdvanced = true;
174         hasNext = results.next();
175
176         // No more results
177
if (!hasNext)
178         {
179             close();
180         }
181
182         return hasNext;
183     }
184
185     /**
186      * Saves all the values returned by iterator into a list.
187      *
188      * As a side effect the result set is closed and no more
189      * operations can be preformed on this object.
190      *
191      * @return - A list of all the values returned by the iterator.
192      * @exception SQLException -
193      * If a database error occurs while fetching values
194      */

195     public List JavaDoc toList() throws SQLException JavaDoc
196     {
197         List JavaDoc resultsList = new ArrayList JavaDoc();
198
199         while (hasNext())
200         {
201             resultsList.add(next());
202         }
203
204         // Close the connection after converting it to a list.
205
this.close();
206         
207         return resultsList;
208     }
209
210     /**
211      * Close the Iterator and release any associated resources
212      */

213     public void close()
214     {
215         try
216         {
217             results.close();
218             if (results != null)
219                 results.close();
220         }
221         catch (SQLException JavaDoc sqle)
222         {
223         }
224
225         // try to close the statement if we have one
226
try
227         {
228             if (statemt != null)
229                 statemt.close();
230             statemt = null;
231         }
232         catch (SQLException JavaDoc sqle)
233         {
234         }
235     }
236 }
237
Popular Tags