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.sql.SQLException;
24  import java.util.List;
25  
26  import org.dbunit.database.QueryDataSet.TableEntry;
27  import org.dbunit.dataset.DataSetException;
28  import org.dbunit.dataset.ITable;
29  import org.dbunit.dataset.ITableIterator;
30  import org.dbunit.dataset.ITableMetaData;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Iterator used to iterate over a list of tables using a specific query for retrieving
36   * data for every table.
37   * 
38   * @author Manuel Laflamme
39   * @author gommma (gommma AT users.sourceforge.net)
40   * @author Last changed by: $Author$
41   * @version $Revision$ $Date$
42   * @since 1.5.6 (Sep 15, 2003)
43   */
44  public class QueryTableIterator implements ITableIterator
45  {
46  
47      /**
48       * Logger for this class
49       */
50      private static final Logger logger = LoggerFactory.getLogger(QueryTableIterator.class);
51  
52      private final List _tableEntries;
53      private final IDatabaseConnection _connection;
54      private IResultSetTable _currentTable;
55      private int _index = -1;
56  
57      /**
58       * @param tableEntries list of {@link TableEntry} objects
59       * @param connection The database connection needed to load data
60       */
61      public QueryTableIterator(List tableEntries, IDatabaseConnection connection)
62      {
63      	if (tableEntries == null) {
64  			throw new NullPointerException(
65  					"The parameter 'tableEntries' must not be null");
66  		}
67      	if (connection == null) {
68  			throw new NullPointerException(
69  					"The parameter 'connection' must not be null");
70  		}
71      	
72          _tableEntries = tableEntries;
73          _connection = connection;
74          _currentTable = null;
75      }
76  
77      ////////////////////////////////////////////////////////////////////////////
78      // ITableIterator interface
79  
80      /**
81       * {@inheritDoc}
82       */
83      public boolean next() throws DataSetException
84      {
85          logger.debug("next() - start");
86  
87          _index++;
88  
89          // Ensure previous table is closed
90          if (_currentTable != null)
91          {
92              _currentTable.close();
93              _currentTable = null;
94          }
95  
96          return _index < _tableEntries.size();
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     public ITableMetaData getTableMetaData() throws DataSetException
103     {
104         logger.debug("getTableMetaData() - start");
105 
106         QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);
107 
108         // No query specified, use metadata from dataset
109         if (entry.getQuery() == null)
110         {
111             try
112             {
113                 ITable table = _connection.createTable(entry.getTableName());
114                 return table.getTableMetaData();
115             }
116             catch (SQLException e)
117             {
118                 throw new DataSetException(e);
119             }
120         }
121         else
122         {
123             return getTable().getTableMetaData();
124         }
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     public ITable getTable() throws DataSetException
131     {
132         logger.debug("getTable() - start");
133 
134         if (_currentTable == null)
135         {
136             try
137             {
138                 QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);
139 
140                 // No query specified, use table from dataset
141                 if (entry.getQuery() == null)
142                 {
143                     _currentTable = (IResultSetTable)_connection.createTable(entry.getTableName());
144                 }
145                 else
146                 {
147                     DatabaseConfig config = _connection.getConfig();
148                     IResultSetTableFactory factory = (IResultSetTableFactory)config.getProperty(
149                             DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
150 
151                     _currentTable = factory.createTable(entry.getTableName(), entry.getQuery(), _connection);
152                 }
153             }
154             catch (SQLException e)
155             {
156                 throw new DataSetException(e);
157             }
158         }
159         return _currentTable;
160     }
161 }