QueryTableIterator.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 java.sql.SQLException;
  23. import java.util.List;

  24. import org.dbunit.database.QueryDataSet.TableEntry;
  25. import org.dbunit.dataset.DataSetException;
  26. import org.dbunit.dataset.ITable;
  27. import org.dbunit.dataset.ITableIterator;
  28. import org.dbunit.dataset.ITableMetaData;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * Iterator used to iterate over a list of tables using a specific query for retrieving
  33.  * data for every table.
  34.  *
  35.  * @author Manuel Laflamme
  36.  * @author gommma (gommma AT users.sourceforge.net)
  37.  * @author Last changed by: $Author$
  38.  * @version $Revision$ $Date$
  39.  * @since 1.5.6 (Sep 15, 2003)
  40.  */
  41. public class QueryTableIterator implements ITableIterator
  42. {

  43.     /**
  44.      * Logger for this class
  45.      */
  46.     private static final Logger logger = LoggerFactory.getLogger(QueryTableIterator.class);

  47.     private final List _tableEntries;
  48.     private final IDatabaseConnection _connection;
  49.     private IResultSetTable _currentTable;
  50.     private int _index = -1;

  51.     /**
  52.      * @param tableEntries list of {@link TableEntry} objects
  53.      * @param connection The database connection needed to load data
  54.      */
  55.     public QueryTableIterator(List tableEntries, IDatabaseConnection connection)
  56.     {
  57.         if (tableEntries == null) {
  58.             throw new NullPointerException(
  59.                     "The parameter 'tableEntries' must not be null");
  60.         }
  61.         if (connection == null) {
  62.             throw new NullPointerException(
  63.                     "The parameter 'connection' must not be null");
  64.         }
  65.        
  66.         _tableEntries = tableEntries;
  67.         _connection = connection;
  68.         _currentTable = null;
  69.     }

  70.     ////////////////////////////////////////////////////////////////////////////
  71.     // ITableIterator interface

  72.     /**
  73.      * {@inheritDoc}
  74.      */
  75.     public boolean next() throws DataSetException
  76.     {
  77.         logger.debug("next() - start");

  78.         _index++;

  79.         // Ensure previous table is closed
  80.         if (_currentTable != null)
  81.         {
  82.             _currentTable.close();
  83.             _currentTable = null;
  84.         }

  85.         return _index < _tableEntries.size();
  86.     }

  87.     public boolean nextWithoutClosing()
  88.     {
  89.         _index++;
  90.         _currentTable = null;

  91.         return _index < _tableEntries.size();
  92.     }

  93.     /**
  94.      * {@inheritDoc}
  95.      */
  96.     public ITableMetaData getTableMetaData() throws DataSetException
  97.     {
  98.         logger.debug("getTableMetaData() - start");

  99.         QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);

  100.         // No query specified, use metadata from dataset
  101.         if (entry.getQuery() == null)
  102.         {
  103.             try
  104.             {
  105.                 ITable table = _connection.createTable(entry.getTableName());
  106.                 return table.getTableMetaData();
  107.             }
  108.             catch (SQLException e)
  109.             {
  110.                 throw new DataSetException(e);
  111.             }
  112.         }
  113.         else
  114.         {
  115.             return getTable().getTableMetaData();
  116.         }
  117.     }

  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     public ITable getTable() throws DataSetException
  122.     {
  123.         logger.debug("getTable() - start");

  124.         if (_currentTable == null)
  125.         {
  126.             try
  127.             {
  128.                 QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);

  129.                 // No query specified, use table from dataset
  130.                 if (entry.getQuery() == null)
  131.                 {
  132.                     _currentTable = (IResultSetTable)_connection.createTable(entry.getTableName());
  133.                 }
  134.                 else
  135.                 {
  136.                     DatabaseConfig config = _connection.getConfig();
  137.                     IResultSetTableFactory factory = (IResultSetTableFactory)config.getProperty(
  138.                             DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);

  139.                     _currentTable = factory.createTable(entry.getTableName(), entry.getQuery(), _connection);
  140.                 }
  141.             }
  142.             catch (SQLException e)
  143.             {
  144.                 throw new DataSetException(e);
  145.             }
  146.         }
  147.         return _currentTable;
  148.     }
  149. }