1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.database;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import org.dbunit.dataset.Column;
28 import org.dbunit.dataset.DataSetException;
29 import org.dbunit.dataset.ITableMetaData;
30
31 import java.sql.ResultSet;
32 import java.sql.SQLException;
33
34
35
36
37
38
39 public class ScrollableResultSetTable extends AbstractResultSetTable
40 {
41
42
43
44
45 private static final Logger logger = LoggerFactory.getLogger(ScrollableResultSetTable.class);
46
47 private final int _rowCount;
48
49 public ScrollableResultSetTable(ITableMetaData metaData, ResultSet resultSet)
50 throws SQLException, DataSetException
51 {
52 super(metaData, resultSet);
53
54 try
55 {
56 if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
57 {
58 throw new SQLException("Forward only ResultSet not supported");
59 }
60
61 _resultSet.last();
62 _rowCount = _resultSet.getRow();
63 }
64 catch (SQLException e)
65 {
66 close();
67 throw e;
68 }
69 }
70
71 public ScrollableResultSetTable(ITableMetaData metaData,
72 IDatabaseConnection connection) throws DataSetException, SQLException
73 {
74 super(metaData, connection);
75
76 try
77 {
78 if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
79 {
80 throw new SQLException("Forward only ResultSet not supported");
81 }
82
83 _resultSet.last();
84 _rowCount = _resultSet.getRow();
85 }
86 catch (SQLException e)
87 {
88 close();
89 throw e;
90 }
91 }
92
93 public ScrollableResultSetTable(String tableName, String selectStatement,
94 IDatabaseConnection connection) throws DataSetException, SQLException
95 {
96 super(tableName, selectStatement, connection);
97
98 try
99 {
100 if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
101 {
102 throw new SQLException("Forward only ResultSet not supported");
103 }
104
105 _resultSet.last();
106 _rowCount = _resultSet.getRow();
107 }
108 catch (SQLException e)
109 {
110 close();
111 throw e;
112 }
113 }
114
115
116
117
118 public int getRowCount()
119 {
120 return _rowCount;
121 }
122
123 public Object getValue(int row, String columnName) throws DataSetException
124 {
125 if(logger.isDebugEnabled())
126 logger.debug("getValue(row={}, columnName={}) - start", Integer.toString(row), columnName);
127
128 assertValidRowIndex(row);
129
130 try
131 {
132 _resultSet.absolute(row + 1);
133
134 int columnIndex = getColumnIndex(columnName);
135 Column column = _metaData.getColumns()[columnIndex];
136 return column.getDataType().getSqlValue(columnIndex + 1, _resultSet);
137 }
138 catch (SQLException e)
139 {
140 throw new DataSetException(e);
141 }
142 }
143
144
145
146
147 public String toString()
148 {
149 StringBuilder sb = new StringBuilder(2000);
150
151 sb.append(super.toString());
152 sb.append(", ");
153 sb.append(getClass().getName()).append("[");
154 sb.append("_rowCount=[").append(_rowCount).append("]");
155 sb.append("]");
156
157 return sb.toString();
158 }
159 }