001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.database;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.Column;
028: import org.dbunit.dataset.DataSetException;
029: import org.dbunit.dataset.ITableMetaData;
030:
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033:
034: /**
035: * @author Manuel Laflamme
036: * @version $Revision: 554 $
037: * @since Feb 17, 2002
038: */
039: public class ScrollableResultSetTable extends AbstractResultSetTable {
040:
041: /**
042: * Logger for this class
043: */
044: private static final Logger logger = LoggerFactory
045: .getLogger(ScrollableResultSetTable.class);
046:
047: private final int _rowCount;
048:
049: public ScrollableResultSetTable(ITableMetaData metaData,
050: ResultSet resultSet) throws SQLException, DataSetException {
051: super (metaData, resultSet);
052:
053: try {
054: if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) {
055: throw new SQLException(
056: "Forward only ResultSet not supported");
057: }
058:
059: _resultSet.last();
060: _rowCount = _resultSet.getRow();
061: } catch (SQLException e) {
062: logger.error("ScrollableResultSetTable()", e);
063:
064: close();
065: throw e;
066: }
067: }
068:
069: public ScrollableResultSetTable(ITableMetaData metaData,
070: IDatabaseConnection connection) throws DataSetException,
071: SQLException {
072: super (metaData, connection);
073:
074: try {
075: if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) {
076: throw new SQLException(
077: "Forward only ResultSet not supported");
078: }
079:
080: _resultSet.last();
081: _rowCount = _resultSet.getRow();
082: } catch (SQLException e) {
083: logger.error("ScrollableResultSetTable()", e);
084:
085: close();
086: throw e;
087: }
088: }
089:
090: public ScrollableResultSetTable(String tableName,
091: String selectStatement, IDatabaseConnection connection)
092: throws DataSetException, SQLException {
093: super (tableName, selectStatement, connection);
094:
095: try {
096: if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) {
097: throw new SQLException(
098: "Forward only ResultSet not supported");
099: }
100:
101: _resultSet.last();
102: _rowCount = _resultSet.getRow();
103: } catch (SQLException e) {
104: logger.error("ScrollableResultSetTable()", e);
105:
106: close();
107: throw e;
108: }
109: }
110:
111: ////////////////////////////////////////////////////////////////////////////
112: // ITable interface
113:
114: public int getRowCount() {
115: logger.debug("getRowCount() - start");
116:
117: return _rowCount;
118: }
119:
120: public Object getValue(int row, String columnName)
121: throws DataSetException {
122: logger.debug("getValue(row=" + row + ", columnName="
123: + columnName + ") - start");
124:
125: assertValidRowIndex(row);
126:
127: try {
128: _resultSet.absolute(row + 1);
129:
130: int columnIndex = getColumnIndex(columnName);
131: Column column = _metaData.getColumns()[columnIndex];
132: return column.getDataType().getSqlValue(columnIndex + 1,
133: _resultSet);
134: } catch (SQLException e) {
135: logger.error("getValue()", e);
136:
137: throw new DataSetException(e);
138: }
139: }
140: }
|