1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.dbunit.database;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import org.dbunit.dataset.Column;
27 import org.dbunit.dataset.DataSetException;
28 import org.dbunit.dataset.ITableMetaData;
29 import org.dbunit.dataset.RowOutOfBoundsException;
30
31 import java.sql.ResultSet;
32 import java.sql.SQLException;
33
34
35
36
37
38
39 public class ForwardOnlyResultSetTable extends AbstractResultSetTable
40 {
41
42
43
44
45 private static final Logger logger = LoggerFactory.getLogger(ForwardOnlyResultSetTable.class);
46
47 private int _lastRow = -1;
48 private boolean _eot = false;
49
50 public ForwardOnlyResultSetTable(ITableMetaData metaData,
51 ResultSet resultSet) throws SQLException, DataSetException
52 {
53 super(metaData, resultSet);
54 }
55
56 public ForwardOnlyResultSetTable(ITableMetaData metaData,
57 IDatabaseConnection connection) throws DataSetException, SQLException
58 {
59 super(metaData, connection);
60 }
61
62 public ForwardOnlyResultSetTable(String tableName, String selectStatement,
63 IDatabaseConnection connection) throws DataSetException, SQLException
64 {
65 super(tableName, selectStatement, connection);
66 }
67
68
69
70
71 public int getRowCount()
72 {
73 throw new UnsupportedOperationException();
74 }
75
76 public Object getValue(int row, String columnName) throws DataSetException
77 {
78 if(logger.isDebugEnabled())
79 logger.debug("getValue(row={}, columnName={}) - start", Integer.toString(row), columnName);
80
81 try
82 {
83
84 while (!_eot && row > _lastRow)
85 {
86 _eot = !_resultSet.next();
87 _lastRow++;
88 }
89
90 if (row < _lastRow)
91 {
92 throw new UnsupportedOperationException("Cannot go backward!");
93 }
94
95 if (_eot || row > _lastRow)
96 {
97
98 close();
99 throw new RowOutOfBoundsException(row + " > " + _lastRow);
100 }
101
102 int columnIndex = getColumnIndex(columnName);
103 Column column = _metaData.getColumns()[columnIndex];
104 return column.getDataType().getSqlValue(columnIndex + 1, _resultSet);
105 }
106 catch (SQLException e)
107 {
108 throw new DataSetException(e);
109 }
110 }
111
112
113
114
115 public String toString()
116 {
117 StringBuilder sb = new StringBuilder(2000);
118
119 sb.append(super.toString());
120 sb.append(", ");
121 sb.append(getClass().getName()).append("[");
122 sb.append("_eot=[").append(_eot).append("], ");
123 sb.append("_lastRow=[").append(_lastRow).append("]");
124 sb.append("]");
125
126 return sb.toString();
127 }
128 }