001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.database;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.*;
027:
028: import java.sql.SQLException;
029: import java.util.List;
030:
031: /**
032: * @author Manuel Laflamme
033: * @since Sep 15, 2003
034: * @version $Revision: 554 $
035: */
036: public class QueryTableIterator implements ITableIterator {
037:
038: /**
039: * Logger for this class
040: */
041: private static final Logger logger = LoggerFactory
042: .getLogger(QueryTableIterator.class);
043:
044: private final List _tableEntries;
045: private final IDatabaseConnection _connection;
046: private IResultSetTable _currentTable;
047: private int _index = -1;
048:
049: public QueryTableIterator(List tableEntries,
050: IDatabaseConnection connection) {
051: _tableEntries = tableEntries;
052: _connection = connection;
053: _currentTable = null;
054: }
055:
056: ////////////////////////////////////////////////////////////////////////////
057: // ITableIterator interface
058:
059: public boolean next() throws DataSetException {
060: logger.debug("next() - start");
061:
062: _index++;
063:
064: // Ensure previous table is closed
065: if (_currentTable != null) {
066: _currentTable.close();
067: _currentTable = null;
068: }
069:
070: return _index < _tableEntries.size();
071: }
072:
073: public ITableMetaData getTableMetaData() throws DataSetException {
074: logger.debug("getTableMetaData() - start");
075:
076: QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry) _tableEntries
077: .get(_index);
078:
079: // No query specified, use metadata from dataset
080: if (entry.getQuery() == null) {
081: try {
082: IDataSet dataSet = _connection.createDataSet();
083: return dataSet.getTableMetaData(entry.getTableName());
084: } catch (SQLException e) {
085: logger.error("getTableMetaData()", e);
086:
087: throw new DataSetException(e);
088: }
089: } else {
090: return getTable().getTableMetaData();
091: }
092: }
093:
094: public ITable getTable() throws DataSetException {
095: logger.debug("getTable() - start");
096:
097: if (_currentTable == null) {
098: try {
099: QueryDataSet.TableEntry entry = (QueryDataSet.TableEntry) _tableEntries
100: .get(_index);
101:
102: // No query specified, use table from dataset
103: if (entry.getQuery() == null) {
104: IDataSet dataSet = _connection.createDataSet();
105: _currentTable = (IResultSetTable) dataSet
106: .getTable(entry.getTableName());
107: } else {
108: DatabaseConfig config = _connection.getConfig();
109: IResultSetTableFactory factory = (IResultSetTableFactory) config
110: .getProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY);
111:
112: _currentTable = factory.createTable(entry
113: .getTableName(), entry.getQuery(),
114: _connection);
115: }
116: } catch (SQLException e) {
117: logger.error("getTable()", e);
118:
119: throw new DataSetException(e);
120: }
121: }
122: return _currentTable;
123: }
124: }
|