KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > ForwardOnlyResultSetTable


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21 package org.dbunit.database;
22
23 import org.dbunit.dataset.Column;
24 import org.dbunit.dataset.DataSetException;
25 import org.dbunit.dataset.ITableMetaData;
26 import org.dbunit.dataset.RowOutOfBoundsException;
27
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 /**
32  * @author Manuel Laflamme
33  * @since Apr 10, 2003
34  * @version $Revision: 1.7 $
35  */

36 public class ForwardOnlyResultSetTable extends AbstractResultSetTable
37 {
38     private int _lastRow = -1;
39     private boolean _eot = false; // End of table flag
40

41     public ForwardOnlyResultSetTable(ITableMetaData metaData,
42             ResultSet JavaDoc resultSet) throws SQLException JavaDoc, DataSetException
43     {
44         super(metaData, resultSet);
45     }
46
47     public ForwardOnlyResultSetTable(ITableMetaData metaData,
48             IDatabaseConnection connection) throws DataSetException, SQLException JavaDoc
49     {
50         super(metaData, connection);
51     }
52
53     public ForwardOnlyResultSetTable(String JavaDoc tableName, String JavaDoc selectStatement,
54             IDatabaseConnection connection) throws DataSetException, SQLException JavaDoc
55     {
56         super(tableName, selectStatement, connection);
57     }
58
59     ////////////////////////////////////////////////////////////////////////////
60
// ITable interface
61

62     public int getRowCount()
63     {
64         throw new UnsupportedOperationException JavaDoc();
65     }
66
67     public Object JavaDoc getValue(int row, String JavaDoc columnName) throws DataSetException
68     {
69         try
70         {
71             // Move cursor forward up to specified row
72
while (!_eot && row > _lastRow)
73             {
74                 _eot = !_resultSet.next();
75                 _lastRow++;
76             }
77
78             if (row < _lastRow)
79             {
80                 throw new UnsupportedOperationException JavaDoc("Cannot go backward!");
81             }
82
83             if (_eot || row > _lastRow)
84             {
85                 // Proactively close the resultset
86
close();
87                 throw new RowOutOfBoundsException(row + " > " + _lastRow);
88             }
89
90             int columnIndex = getColumnIndex(columnName);
91             Column column = _metaData.getColumns()[columnIndex];
92             return column.getDataType().getSqlValue(columnIndex + 1, _resultSet);
93         }
94         catch (SQLException JavaDoc e)
95         {
96             throw new DataSetException(e);
97         }
98     }
99 }
100
Popular Tags