KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > QueryTableIterator


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 org.dbunit.dataset.*;
24
25 import java.sql.SQLException JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * @author Manuel Laflamme
30  * @since Sep 15, 2003
31  * @version $Revision: 1.4 $
32  */

33 public class QueryTableIterator implements ITableIterator
34 {
35     private final List JavaDoc _tableEntries;
36     private final IDatabaseConnection _connection;
37     private IResultSetTable _currentTable;
38     private int _index = -1;
39
40     public QueryTableIterator(List JavaDoc tableEntries, IDatabaseConnection connection)
41     {
42         _tableEntries = tableEntries;
43         _connection = connection;
44         _currentTable = null;
45     }
46
47     ////////////////////////////////////////////////////////////////////////////
48
// ITableIterator interface
49

50     public boolean next() throws DataSetException
51     {
52         _index++;
53
54         // Ensure previous table is closed
55
if (_currentTable != null)
56         {
57             _currentTable.close();
58             _currentTable = null;
59         }
60
61         return _index < _tableEntries.size();
62     }
63
64     public ITableMetaData getTableMetaData() throws DataSetException
65     {
66         QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);
67
68         // No query specified, use metadata from dataset
69
if (entry.getQuery() == null)
70         {
71             try
72             {
73                 IDataSet dataSet = _connection.createDataSet();
74                 return dataSet.getTableMetaData(entry.getTableName());
75             }
76             catch (SQLException JavaDoc e)
77             {
78                 throw new DataSetException(e);
79             }
80         }
81         else
82         {
83             return getTable().getTableMetaData();
84         }
85     }
86
87     public ITable getTable() throws DataSetException
88     {
89         if (_currentTable == null)
90         {
91             try
92             {
93                 QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry)_tableEntries.get(_index);
94
95                 // No query specified, use table from dataset
96
if (entry.getQuery() == null)
97                 {
98                     IDataSet dataSet = _connection.createDataSet();
99                     _currentTable = (IResultSetTable)dataSet.getTable(entry.getTableName());
100                 }
101                 else
102                 {
103                     DatabaseConfig config = _connection.getConfig();
104                     IResultSetTableFactory factory = (IResultSetTableFactory)config.getProperty(
105                             DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
106
107                     _currentTable = factory.createTable(entry.getTableName(), entry.getQuery(), _connection);
108                 }
109             }
110             catch (SQLException JavaDoc e)
111             {
112                 throw new DataSetException(e);
113             }
114         }
115         return _currentTable;
116     }
117 }
118
Popular Tags