CompositeTable.java

  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.dataset;

  22. import java.util.Arrays;

  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;

  25. /**
  26.  * @author Manuel Laflamme
  27.  * @version $Revision$
  28.  * @since Feb 17, 2002
  29.  */
  30. public class CompositeTable extends AbstractTable {

  31.     /**
  32.      * Logger for this class
  33.      */
  34.     private static final Logger logger =
  35.             LoggerFactory.getLogger(CompositeTable.class);

  36.     private final ITableMetaData _metaData;
  37.     private final ITable[] _tables;

  38.     /**
  39.      * Creates a composite table that combines the specified metadata with the
  40.      * specified table.
  41.      */
  42.     public CompositeTable(ITableMetaData metaData, ITable table) {
  43.         _metaData = metaData;
  44.         _tables = new ITable[] {table};
  45.     }

  46.     /**
  47.      * Creates a composite table that combines the specified metadata with the
  48.      * specified tables.
  49.      */
  50.     public CompositeTable(ITableMetaData metaData, ITable[] tables) {
  51.         _metaData = metaData;
  52.         _tables = tables;
  53.     }

  54.     /**
  55.      * Creates a composite table that combines the specified specified tables.
  56.      * The metadata from the first table is used as metadata for the new table.
  57.      */
  58.     public CompositeTable(ITable table1, ITable table2) {
  59.         _metaData = table1.getTableMetaData();
  60.         _tables = new ITable[] {table1, table2};
  61.     }

  62.     /**
  63.      * Creates a composite dataset that encapsulate the specified table with a
  64.      * new name.
  65.      */
  66.     public CompositeTable(String newName, ITable table) throws DataSetException {
  67.         ITableMetaData metaData = table.getTableMetaData();
  68.         _metaData =
  69.                 new DefaultTableMetaData(newName, metaData.getColumns(),
  70.                         metaData.getPrimaryKeys());
  71.         _tables = new ITable[] {table};
  72.     }

  73.     // //////////////////////////////////////////////////////////////////////////
  74.     // ITable interface

  75.     public ITableMetaData getTableMetaData() {
  76.         return _metaData;
  77.     }

  78.     public int getRowCount() {
  79.         logger.debug("getRowCount() - start");

  80.         int totalCount = 0;
  81.         for (int i = 0; i < _tables.length; i++) {
  82.             ITable table = _tables[i];
  83.             totalCount += table.getRowCount();
  84.         }

  85.         return totalCount;
  86.     }

  87.     public Object getValue(int row, String columnName) throws DataSetException {
  88.         if (logger.isDebugEnabled()) {
  89.             logger.debug("getValue(row={}, columnName={}) - start", Integer
  90.                     .toString(row), columnName);
  91.         }

  92.         if (row < 0) {
  93.             throw new RowOutOfBoundsException(row + " < 0 ");
  94.         }

  95.         int totalCount = 0;
  96.         for (int i = 0; i < _tables.length; i++) {
  97.             ITable table = _tables[i];

  98.             int count = table.getRowCount();
  99.             if (totalCount + count > row) {
  100.                 return table.getValue(row - totalCount, columnName);
  101.             }
  102.             totalCount += count;
  103.         }

  104.         throw new RowOutOfBoundsException(row + " > " + totalCount);
  105.     }

  106.     /**
  107.      * {@inheritDoc}
  108.      */
  109.     public String toString() {
  110.         StringBuilder sb = new StringBuilder(2000);

  111.         sb.append(getClass().getName()).append("[");
  112.         sb.append("_metaData=[").append(_metaData).append("], ");
  113.         sb.append("_tables=[").append(Arrays.toString(_tables)).append("]");
  114.         sb.append("]");

  115.         return sb.toString();
  116.     }
  117. }