ForwardOnlyResultSetTable.java

  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. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import org.dbunit.dataset.Column;
  25. import org.dbunit.dataset.DataSetException;
  26. import org.dbunit.dataset.ITableMetaData;
  27. import org.dbunit.dataset.RowOutOfBoundsException;

  28. import java.sql.ResultSet;
  29. import java.sql.SQLException;

  30. /**
  31.  * @author Manuel Laflamme
  32.  * @since Apr 10, 2003
  33.  * @version $Revision$
  34.  */
  35. public class ForwardOnlyResultSetTable extends AbstractResultSetTable
  36. {

  37.     /**
  38.      * Logger for this class
  39.      */
  40.     private static final Logger logger = LoggerFactory.getLogger(ForwardOnlyResultSetTable.class);

  41.     private int _lastRow = -1;
  42.     private boolean _eot = false; // End of table flag

  43.     public ForwardOnlyResultSetTable(ITableMetaData metaData,
  44.             ResultSet resultSet) throws SQLException, DataSetException
  45.     {
  46.         super(metaData, resultSet);
  47.     }

  48.     public ForwardOnlyResultSetTable(ITableMetaData metaData,
  49.             IDatabaseConnection connection) throws DataSetException, SQLException
  50.     {
  51.         super(metaData, connection);
  52.     }

  53.     public ForwardOnlyResultSetTable(String tableName, String selectStatement,
  54.             IDatabaseConnection connection) throws DataSetException, SQLException
  55.     {
  56.         super(tableName, selectStatement, connection);
  57.     }

  58.     // //////////////////////////////////////////////////////////////////////////
  59.     // ITable interface

  60.     public int getRowCount()
  61.     {
  62.         throw new UnsupportedOperationException();
  63.     }

  64.     public Object getValue(int row, String columnName) throws DataSetException
  65.     {
  66.         if(logger.isDebugEnabled())
  67.             logger.debug("getValue(row={}, columnName={}) - start", Integer.toString(row), columnName);

  68.         try
  69.         {
  70.             // Move cursor forward up to specified row
  71.             while (!_eot && row > _lastRow)
  72.             {
  73.                 _eot = !_resultSet.next();
  74.                 _lastRow++;
  75.             }

  76.             if (row < _lastRow)
  77.             {
  78.                 throw new UnsupportedOperationException("Cannot go backward!");
  79.             }

  80.             if (_eot || row > _lastRow)
  81.             {
  82.                 // Proactively close the resultset
  83.                 close();
  84.                 throw new RowOutOfBoundsException(row + " > " + _lastRow);
  85.             }

  86.             int columnIndex = getColumnIndex(columnName);
  87.             Column column = _metaData.getColumns()[columnIndex];
  88.             return column.getDataType().getSqlValue(columnIndex + 1, _resultSet);
  89.         }
  90.         catch (SQLException e)
  91.         {
  92.             throw new DataSetException(e);
  93.         }
  94.     }

  95.     /**
  96.      * {@inheritDoc}
  97.      */
  98.     public String toString()
  99.     {
  100.         StringBuilder sb = new StringBuilder(2000);

  101.         sb.append(super.toString());
  102.         sb.append(", ");
  103.         sb.append(getClass().getName()).append("[");
  104.         sb.append("_eot=[").append(_eot).append("], ");
  105.         sb.append("_lastRow=[").append(_lastRow).append("]");
  106.         sb.append("]");

  107.         return sb.toString();
  108.     }
  109. }