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 java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.dbunit.dataset.AbstractDataSet;
28  import org.dbunit.dataset.DataSetException;
29  import org.dbunit.dataset.ITableIterator;
30  import org.dbunit.dataset.OrderedTableNameMap;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Holds collection of tables resulting from database query.
36   *
37   * @author Eric Pugh
38   * @author gommma
39   * @author Last changed by: $Author$
40   * @version $Revision$ $Date$
41   * @since Dec 4, 2002
42   */
43  public class QueryDataSet extends AbstractDataSet
44  {
45  
46      /**
47       * Logger for this class
48       */
49      private static final Logger logger = LoggerFactory.getLogger(QueryDataSet.class);
50  
51      private final IDatabaseConnection _connection;
52      private final OrderedTableNameMap _tables;
53  
54  
55      /**
56       * Create a QueryDataSet by passing in the connection to the database to use.
57       *
58       * @param connection The connection object to the database.
59       */
60      public QueryDataSet(IDatabaseConnection connection)
61      {
62          this(connection, connection.getConfig().getFeature(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES));
63      }
64  
65      /**
66       * Create a QueryDataSet by passing in the connection to the database to use.
67       *
68       * @param connection The connection object to the database.
69       * @param caseSensitiveTableNames Whether or not this dataset should use case sensitive table names
70       * @since 2.4.2
71       */
72      public QueryDataSet(IDatabaseConnection connection, boolean caseSensitiveTableNames)
73      {
74          super(caseSensitiveTableNames);
75          if (connection == null) {
76              throw new NullPointerException("The parameter 'connection' must not be null");
77          }
78          _connection = connection;
79          _tables = super.createTableNameMap();
80      }
81  
82      /**
83       *  Adds a table and it's associated query to this dataset.
84       *
85       * @param tableName The name of the table
86       * @param query The query to retrieve data with for this table. Can be null which will select
87       * all data (see {@link #addTable(String)} for details)
88       * @throws AmbiguousTableNameException 
89       */
90      public void addTable(String tableName, String query) throws AmbiguousTableNameException
91      {
92          logger.debug("addTable(tableName={}, query={}) - start", tableName, query);
93          _tables.add(tableName, new TableEntry(tableName, query));
94      }
95  
96      /**
97       *  Adds a table with using 'SELECT * FROM <code>tableName</code>' as query.
98       *
99       * @param tableName The name of the table
100      * @throws AmbiguousTableNameException 
101      */
102     public void addTable(String tableName) throws AmbiguousTableNameException
103     {
104         logger.debug("addTable(tableName={}) - start", tableName);
105         this.addTable(tableName, null);
106     }
107 
108     ////////////////////////////////////////////////////////////////////////////
109     // AbstractDataSet class
110 
111     protected ITableIterator createIterator(boolean reversed) throws DataSetException
112     {
113     	if(logger.isDebugEnabled())
114     		logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
115     	
116         List tableEntries = new ArrayList(_tables.orderedValues());
117         if (reversed)
118         {
119             Collections.reverse(tableEntries);
120         }
121 
122         return new QueryTableIterator(tableEntries, _connection);
123     }
124 
125     ////////////////////////////////////////////////////////////////////////////
126     // IDataSet interface
127 
128     public String[] getTableNames() throws DataSetException
129     {
130         logger.debug("getTableNames() - start");
131         return this._tables.getTableNames();
132     }
133 
134     /**
135      * Represents a table and a SQL query that should be used to retrieve the
136      * data for this table.
137      */
138     static class TableEntry
139     {
140         private final String _tableName;
141         private final String _query;
142 
143         public TableEntry(String tableName, String query)
144         {
145             _tableName = tableName;
146             _query = query;
147         }
148 
149         public String getTableName()
150         {
151             return _tableName;
152         }
153 
154         public String getQuery()
155         {
156             return _query;
157         }
158     }
159 }
160 
161