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.AbstractDataSet;
027: import org.dbunit.dataset.DataSetException;
028: import org.dbunit.dataset.ITableIterator;
029:
030: import java.sql.SQLException;
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.Iterator;
034: import java.util.List;
035:
036: /**
037: * Holds collection of tables resulting from database query.
038: *
039: * @author Eric Pugh
040: * @since Dec 4, 2002
041: * @version $Revision: 554 $
042: */
043: public class QueryDataSet extends AbstractDataSet {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger logger = LoggerFactory
049: .getLogger(QueryDataSet.class);
050:
051: private final IDatabaseConnection _connection;
052: private final List _tableEntries = new ArrayList();
053:
054: /**
055: * Create a QueryDataSet by passing in the connection to the database to use.
056: *
057: * @param connection The connection object to the database.
058: * @exception java.sql.SQLException Description of the Exception
059: */
060: public QueryDataSet(IDatabaseConnection connection)
061: throws SQLException {
062: _connection = connection;
063: }
064:
065: /**
066: * Adds a table and it's associted query to this dataset.
067: *
068: * @param tableName The name of the table
069: * @param query The query to retrieve data with for this table
070: */
071: public void addTable(String tableName, String query) {
072: logger.debug("addTable(tableName=" + tableName + ", query="
073: + query + ") - start");
074:
075: _tableEntries.add(new TableEntry(tableName, query));
076: }
077:
078: /**
079: * Adds a table with using 'SELECT * FROM <code>tableName</code>' as query.
080: *
081: * @param tableName The name of the table
082: */
083: public void addTable(String tableName) {
084: logger.debug("addTable(tableName=" + tableName + ") - start");
085:
086: _tableEntries.add(new TableEntry(tableName, null));
087: }
088:
089: ////////////////////////////////////////////////////////////////////////////
090: // AbstractDataSet class
091:
092: protected ITableIterator createIterator(boolean reversed)
093: throws DataSetException {
094: logger.debug("createIterator(reversed=" + reversed
095: + ") - start");
096:
097: List tableEntries = new ArrayList(_tableEntries);
098: if (reversed) {
099: Collections.reverse(tableEntries);
100: }
101:
102: return new QueryTableIterator(tableEntries, _connection);
103: }
104:
105: ////////////////////////////////////////////////////////////////////////////
106: // IDataSet interface
107:
108: public String[] getTableNames() throws DataSetException {
109: logger.debug("getTableNames() - start");
110:
111: List names = new ArrayList();
112: for (Iterator it = _tableEntries.iterator(); it.hasNext();) {
113: TableEntry entry = (TableEntry) it.next();
114: names.add(entry.getTableName());
115: }
116:
117: return (String[]) names.toArray(new String[0]);
118: }
119:
120: static class TableEntry {
121:
122: /**
123: * Logger for this class
124: */
125: private static final Logger logger = LoggerFactory
126: .getLogger(TableEntry.class);
127:
128: private final String _tableName;
129: private final String _query;
130:
131: public TableEntry(String tableName, String query) {
132: _tableName = tableName;
133: _query = query;
134: }
135:
136: public String getTableName() {
137: logger.debug("getTableName() - start");
138:
139: return _tableName;
140: }
141:
142: public String getQuery() {
143: logger.debug("getQuery() - start");
144:
145: return _query;
146: }
147: }
148: }
|