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: package org.dbunit.database;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.Column;
027: import org.dbunit.dataset.DataSetException;
028: import org.dbunit.dataset.ITableMetaData;
029: import org.dbunit.dataset.RowOutOfBoundsException;
030:
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033:
034: /**
035: * @author Manuel Laflamme
036: * @since Apr 10, 2003
037: * @version $Revision: 554 $
038: */
039: public class ForwardOnlyResultSetTable extends AbstractResultSetTable {
040:
041: /**
042: * Logger for this class
043: */
044: private static final Logger logger = LoggerFactory
045: .getLogger(ForwardOnlyResultSetTable.class);
046:
047: private int _lastRow = -1;
048: private boolean _eot = false; // End of table flag
049:
050: public ForwardOnlyResultSetTable(ITableMetaData metaData,
051: ResultSet resultSet) throws SQLException, DataSetException {
052: super (metaData, resultSet);
053: }
054:
055: public ForwardOnlyResultSetTable(ITableMetaData metaData,
056: IDatabaseConnection connection) throws DataSetException,
057: SQLException {
058: super (metaData, connection);
059: }
060:
061: public ForwardOnlyResultSetTable(String tableName,
062: String selectStatement, IDatabaseConnection connection)
063: throws DataSetException, SQLException {
064: super (tableName, selectStatement, connection);
065: }
066:
067: ////////////////////////////////////////////////////////////////////////////
068: // ITable interface
069:
070: public int getRowCount() {
071: logger.debug("getRowCount() - start");
072:
073: throw new UnsupportedOperationException();
074: }
075:
076: public Object getValue(int row, String columnName)
077: throws DataSetException {
078: logger.debug("getValue(row=" + row + ", columnName="
079: + columnName + ") - start");
080:
081: try {
082: // Move cursor forward up to specified row
083: while (!_eot && row > _lastRow) {
084: _eot = !_resultSet.next();
085: _lastRow++;
086: }
087:
088: if (row < _lastRow) {
089: throw new UnsupportedOperationException(
090: "Cannot go backward!");
091: }
092:
093: if (_eot || row > _lastRow) {
094: // Proactively close the resultset
095: close();
096: throw new RowOutOfBoundsException(row + " > "
097: + _lastRow);
098: }
099:
100: int columnIndex = getColumnIndex(columnName);
101: Column column = _metaData.getColumns()[columnIndex];
102: return column.getDataType().getSqlValue(columnIndex + 1,
103: _resultSet);
104: } catch (SQLException e) {
105: logger.error("getValue()", e);
106:
107: throw new DataSetException(e);
108: }
109: }
110: }
|