ScrollableResultSetTable.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 java.sql.ResultSet;
  28. import java.sql.SQLException;

  29. /**
  30.  * @author Manuel Laflamme
  31.  * @version $Revision$
  32.  * @since Feb 17, 2002
  33.  */
  34. public class ScrollableResultSetTable extends AbstractResultSetTable
  35. {

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

  40.     private final int _rowCount;

  41.     public ScrollableResultSetTable(ITableMetaData metaData, ResultSet resultSet)
  42.             throws SQLException, DataSetException
  43.     {
  44.         super(metaData, resultSet);

  45.         try
  46.         {
  47.             if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
  48.             {
  49.                 throw new SQLException("Forward only ResultSet not supported");
  50.             }

  51.             _resultSet.last();
  52.             _rowCount = _resultSet.getRow();
  53.         }
  54.         catch (SQLException e)
  55.         {
  56.             close();
  57.             throw e;
  58.         }
  59.     }

  60.     public ScrollableResultSetTable(ITableMetaData metaData,
  61.             IDatabaseConnection connection) throws DataSetException, SQLException
  62.     {
  63.         super(metaData, connection);

  64.         try
  65.         {
  66.             if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
  67.             {
  68.                 throw new SQLException("Forward only ResultSet not supported");
  69.             }

  70.             _resultSet.last();
  71.             _rowCount = _resultSet.getRow();
  72.         }
  73.         catch (SQLException e)
  74.         {
  75.             close();
  76.             throw e;
  77.         }
  78.     }

  79.     public ScrollableResultSetTable(String tableName, String selectStatement,
  80.             IDatabaseConnection connection) throws DataSetException, SQLException
  81.     {
  82.         super(tableName, selectStatement, connection);

  83.         try
  84.         {
  85.             if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
  86.             {
  87.                 throw new SQLException("Forward only ResultSet not supported");
  88.             }

  89.             _resultSet.last();
  90.             _rowCount = _resultSet.getRow();
  91.         }
  92.         catch (SQLException e)
  93.         {
  94.             close();
  95.             throw e;
  96.         }
  97.     }

  98.     ////////////////////////////////////////////////////////////////////////////
  99.     // ITable interface

  100.     public int getRowCount()
  101.     {
  102.         return _rowCount;
  103.     }

  104.     public Object getValue(int row, String columnName) throws DataSetException
  105.     {
  106.         if(logger.isDebugEnabled())
  107.             logger.debug("getValue(row={}, columnName={}) - start", Integer.toString(row), columnName);

  108.         assertValidRowIndex(row);

  109.         try
  110.         {
  111.             _resultSet.absolute(row + 1);

  112.             int columnIndex = getColumnIndex(columnName);
  113.             Column column = _metaData.getColumns()[columnIndex];
  114.             return column.getDataType().getSqlValue(columnIndex + 1, _resultSet);
  115.         }
  116.         catch (SQLException e)
  117.         {
  118.             throw new DataSetException(e);
  119.         }
  120.     }

  121.     /**
  122.      * {@inheritDoc}
  123.      */
  124.     public String toString()
  125.     {
  126.         StringBuilder sb = new StringBuilder(2000);

  127.         sb.append(super.toString());
  128.         sb.append(", ");
  129.         sb.append(getClass().getName()).append("[");
  130.         sb.append("_rowCount=[").append(_rowCount).append("]");
  131.         sb.append("]");

  132.         return sb.toString();
  133.     }
  134. }