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:
022: package org.dbunit.dataset;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: /**
028: * @author Manuel Laflamme
029: * @version $Revision: 554 $
030: * @since Feb 17, 2002
031: */
032: public class CompositeTable extends AbstractTable {
033:
034: /**
035: * Logger for this class
036: */
037: private static final Logger logger = LoggerFactory
038: .getLogger(CompositeTable.class);
039:
040: private final ITableMetaData _metaData;
041: private final ITable[] _tables;
042:
043: /**
044: * Creates a composite table that combines the specified metadata with the
045: * specified table.
046: */
047: public CompositeTable(ITableMetaData metaData, ITable table) {
048: _metaData = metaData;
049: _tables = new ITable[] { table };
050: }
051:
052: /**
053: * Creates a composite table that combines the specified metadata with the
054: * specified tables.
055: */
056: public CompositeTable(ITableMetaData metaData, ITable[] tables) {
057: _metaData = metaData;
058: _tables = tables;
059: }
060:
061: /**
062: * Creates a composite table that combines the specified specified tables.
063: * The metadata from the first table is used as metadata for the new table.
064: */
065: public CompositeTable(ITable table1, ITable table2) {
066: _metaData = table1.getTableMetaData();
067: _tables = new ITable[] { table1, table2 };
068: }
069:
070: /**
071: * Creates a composite dataset that encapsulate the specified table with
072: * a new name.
073: */
074: public CompositeTable(String newName, ITable table)
075: throws DataSetException {
076: ITableMetaData metaData = table.getTableMetaData();
077: _metaData = new DefaultTableMetaData(newName, metaData
078: .getColumns(), metaData.getPrimaryKeys());
079: _tables = new ITable[] { table };
080: }
081:
082: ////////////////////////////////////////////////////////////////////////////
083: // ITable interface
084:
085: public ITableMetaData getTableMetaData() {
086: logger.debug("getTableMetaData() - start");
087:
088: return _metaData;
089: }
090:
091: public int getRowCount() {
092: logger.debug("getRowCount() - start");
093:
094: int totalCount = 0;
095: for (int i = 0; i < _tables.length; i++) {
096: ITable table = _tables[i];
097: totalCount += table.getRowCount();
098: }
099:
100: return totalCount;
101: }
102:
103: public Object getValue(int row, String column)
104: throws DataSetException {
105: logger.debug("getValue(row=" + row + ", column=" + column
106: + ") - start");
107:
108: if (row < 0) {
109: throw new RowOutOfBoundsException(row + " < 0 ");
110: }
111:
112: int totalCount = 0;
113: for (int i = 0; i < _tables.length; i++) {
114: ITable table = _tables[i];
115:
116: int count = table.getRowCount();
117: if (totalCount + count > row) {
118: return table.getValue(row - totalCount, column);
119: }
120: totalCount += count;
121: }
122:
123: throw new RowOutOfBoundsException(row + " > " + totalCount);
124: }
125: }
|