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      public boolean nextWithoutClosing()
100     {
101         _index++;
102         _currentTable = null;
103 
104         return _index < _tableEntries.size();
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     public ITableMetaData getTableMetaData() throws DataSetException
111     {
112         logger.debug("getTableMetaData() - start");
113 
114         QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);
115 
116         // No query specified, use metadata from dataset
117         if (entry.getQuery() == null)
118         {
119             try
120             {
121                 ITable table = _connection.createTable(entry.getTableName());
122                 return table.getTableMetaData();
123             }
124             catch (SQLException e)
125             {
126                 throw new DataSetException(e);
127             }
128         }
129         else
130         {
131             return getTable().getTableMetaData();
132         }
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     public ITable getTable() throws DataSetException
139     {
140         logger.debug("getTable() - start");
141 
142         if (_currentTable == null)
143         {
144             try
145             {
146                 QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);
147 
148                 // No query specified, use table from dataset
149                 if (entry.getQuery() == null)
150                 {
151                     _currentTable = (IResultSetTable)_connection.createTable(entry.getTableName());
152                 }
153                 else
154                 {
155                     DatabaseConfig config = _connection.getConfig();
156                     IResultSetTableFactory factory = (IResultSetTableFactory)config.getProperty(
157                             DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
158 
159                     _currentTable = factory.createTable(entry.getTableName(), entry.getQuery(), _connection);
160                 }
161             }
162             catch (SQLException e)
163             {
164                 throw new DataSetException(e);
165             }
166         }
167         return _currentTable;
168     }
169 }