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  package org.dbunit.database;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import org.dbunit.dataset.Column;
27  import org.dbunit.dataset.DataSetException;
28  import org.dbunit.dataset.ITableMetaData;
29  import org.dbunit.dataset.RowOutOfBoundsException;
30  
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  
34  /**
35   * @author Manuel Laflamme
36   * @since Apr 10, 2003
37   * @version $Revision$
38   */
39  public class ForwardOnlyResultSetTable extends AbstractResultSetTable
40  {
41  
42      /**
43       * Logger for this class
44       */
45      private static final Logger logger = LoggerFactory.getLogger(ForwardOnlyResultSetTable.class);
46  
47      private int _lastRow = -1;
48      private boolean _eot = false; // End of table flag
49  
50      public ForwardOnlyResultSetTable(ITableMetaData metaData,
51              ResultSet resultSet) throws SQLException, DataSetException
52      {
53          super(metaData, resultSet);
54      }
55  
56      public ForwardOnlyResultSetTable(ITableMetaData metaData,
57              IDatabaseConnection connection) throws DataSetException, SQLException
58      {
59          super(metaData, connection);
60      }
61  
62      public ForwardOnlyResultSetTable(String tableName, String selectStatement,
63              IDatabaseConnection connection) throws DataSetException, SQLException
64      {
65          super(tableName, selectStatement, connection);
66      }
67  
68      // //////////////////////////////////////////////////////////////////////////
69      // ITable interface
70  
71      public int getRowCount()
72      {
73          throw new UnsupportedOperationException();
74      }
75  
76      public Object getValue(int row, String columnName) throws DataSetException
77      {
78          if(logger.isDebugEnabled())
79              logger.debug("getValue(row={}, columnName={}) - start", Integer.toString(row), columnName);
80  
81          try
82          {
83              // Move cursor forward up to specified row
84              while (!_eot && row > _lastRow)
85              {
86                  _eot = !_resultSet.next();
87                  _lastRow++;
88              }
89  
90              if (row < _lastRow)
91              {
92                  throw new UnsupportedOperationException("Cannot go backward!");
93              }
94  
95              if (_eot || row > _lastRow)
96              {
97                  // Proactively close the resultset
98                  close();
99                  throw new RowOutOfBoundsException(row + " > " + _lastRow);
100             }
101 
102             int columnIndex = getColumnIndex(columnName);
103             Column column = _metaData.getColumns()[columnIndex];
104             return column.getDataType().getSqlValue(columnIndex + 1, _resultSet);
105         }
106         catch (SQLException e)
107         {
108             throw new DataSetException(e);
109         }
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     public String toString()
116     {
117         StringBuilder sb = new StringBuilder(2000);
118 
119         sb.append(super.toString());
120         sb.append(", ");
121         sb.append(getClass().getName()).append("[");
122         sb.append("_eot=[").append(_eot).append("], ");
123         sb.append("_lastRow=[").append(_lastRow).append("]");
124         sb.append("]");
125 
126         return sb.toString();
127     }
128 }