QueryDataSet.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.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;

  25. import org.dbunit.dataset.AbstractDataSet;
  26. import org.dbunit.dataset.DataSetException;
  27. import org.dbunit.dataset.ITableIterator;
  28. import org.dbunit.dataset.OrderedTableNameMap;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * Holds collection of tables resulting from database query.
  33.  *
  34.  * @author Eric Pugh
  35.  * @author gommma
  36.  * @author Last changed by: $Author$
  37.  * @version $Revision$ $Date$
  38.  * @since Dec 4, 2002
  39.  */
  40. public class QueryDataSet extends AbstractDataSet
  41. {

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

  46.     private final IDatabaseConnection _connection;
  47.     private final OrderedTableNameMap _tables;


  48.     /**
  49.      * Create a QueryDataSet by passing in the connection to the database to use.
  50.      *
  51.      * @param connection The connection object to the database.
  52.      */
  53.     public QueryDataSet(IDatabaseConnection connection)
  54.     {
  55.         this(connection, connection.getConfig().getFeature(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES));
  56.     }

  57.     /**
  58.      * Create a QueryDataSet by passing in the connection to the database to use.
  59.      *
  60.      * @param connection The connection object to the database.
  61.      * @param caseSensitiveTableNames Whether or not this dataset should use case sensitive table names
  62.      * @since 2.4.2
  63.      */
  64.     public QueryDataSet(IDatabaseConnection connection, boolean caseSensitiveTableNames)
  65.     {
  66.         super(caseSensitiveTableNames);
  67.         if (connection == null) {
  68.             throw new NullPointerException("The parameter 'connection' must not be null");
  69.         }
  70.         _connection = connection;
  71.         _tables = super.createTableNameMap();
  72.     }

  73.     /**
  74.      *  Adds a table and it's associated query to this dataset.
  75.      *
  76.      * @param tableName The name of the table
  77.      * @param query The query to retrieve data with for this table. Can be null which will select
  78.      * all data (see {@link #addTable(String)} for details)
  79.      * @throws AmbiguousTableNameException
  80.      */
  81.     public void addTable(String tableName, String query) throws AmbiguousTableNameException
  82.     {
  83.         logger.debug("addTable(tableName={}, query={}) - start", tableName, query);
  84.         _tables.add(tableName, new TableEntry(tableName, query));
  85.     }

  86.     /**
  87.      *  Adds a table with using 'SELECT * FROM <code>tableName</code>' as query.
  88.      *
  89.      * @param tableName The name of the table
  90.      * @throws AmbiguousTableNameException
  91.      */
  92.     public void addTable(String tableName) throws AmbiguousTableNameException
  93.     {
  94.         logger.debug("addTable(tableName={}) - start", tableName);
  95.         this.addTable(tableName, null);
  96.     }

  97.     ////////////////////////////////////////////////////////////////////////////
  98.     // AbstractDataSet class

  99.     protected ITableIterator createIterator(boolean reversed) throws DataSetException
  100.     {
  101.         if(logger.isDebugEnabled())
  102.             logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
  103.        
  104.         List tableEntries = new ArrayList(_tables.orderedValues());
  105.         if (reversed)
  106.         {
  107.             Collections.reverse(tableEntries);
  108.         }

  109.         return new QueryTableIterator(tableEntries, _connection);
  110.     }

  111.     ////////////////////////////////////////////////////////////////////////////
  112.     // IDataSet interface

  113.     public String[] getTableNames() throws DataSetException
  114.     {
  115.         logger.debug("getTableNames() - start");
  116.         return this._tables.getTableNames();
  117.     }

  118.     /**
  119.      * Represents a table and a SQL query that should be used to retrieve the
  120.      * data for this table.
  121.      */
  122.     static class TableEntry
  123.     {
  124.         private final String _tableName;
  125.         private final String _query;

  126.         public TableEntry(String tableName, String query)
  127.         {
  128.             _tableName = tableName;
  129.             _query = query;
  130.         }

  131.         public String getTableName()
  132.         {
  133.             return _tableName;
  134.         }

  135.         public String getQuery()
  136.         {
  137.             return _query;
  138.         }
  139.     }
  140. }