Source code: javatools/db/DbCachedTable.java
1 /*
2 * DbCachedTable.java
3 *
4 * Created on 6 marzo 2002, 17.45
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.db;
26
27 import java.sql.*;
28 import java.util.LinkedList;
29
30 /**
31 * It's a "cached" table, that represents a result from SELECT in a cached way,
32 * that is it can be accessed more than once.
33 * @author Antonio Petrelli
34 * @version 0.0.1
35 */
36 public class DbCachedTable extends javatools.db.DbTable {
37
38
39 /** Creates new DbCachedTable
40 * @param db The database for this operation.
41 * @throws DbException If something goes wrong.
42 */
43 public DbCachedTable(DbDatabase db) throws DbException {
44 super(db);
45 resultList = new LinkedList();
46 }
47
48 /**
49 * Return an iterator to iterate over the rows in this table. <P>
50 *
51 * BUGS: Currently you can only iterate over a table that was returned from a
52 * DbSelector.
53 *
54 * @return The needed iterator.
55 */
56 public DbIterator iterator() {
57 return new DbCachedIterator(this);
58 }
59
60 /** Sets the result set to be representend in this class.
61 * @param stmt The source statement of the result set. It can be <CODE>null</CODE>.
62 * @param rs The needed result set.
63 * @throws DbException If something goes wrong.
64 */
65 public void setResultSet(PreparedStatement stmt, ResultSet rs) throws DbException {
66 DbIterator resIt;
67 DbRow tempRow;
68 int i, numColumns;
69
70 try {
71 super.setResultSet(stmt, rs);
72 numColumns = columnCount;
73 while (rs.next()) {
74 tempRow = new DbRow(this);
75 for (i=0; i < numColumns; i++)
76 tempRow.values[i] = rs.getObject(i+1);
77 resultList.add(tempRow);
78 }
79 }
80 catch (SQLException e) {
81 throw new DbException (e.getMessage());
82 }
83 }
84
85 /** Contains the result list as a linked list instead of a result set.
86 */
87 protected LinkedList resultList;
88 }