View Javadoc
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  
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   * @author Manuel Laflamme
36   * @version $Revision$
37   * @since Feb 17, 2002
38   */
39  public class ScrollableResultSetTable extends AbstractResultSetTable
40  {
41  
42      /**
43       * Logger for this class
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     // ITable interface
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      * {@inheritDoc}
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 }