View Javadoc
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  
22  package org.dbunit.dataset;
23  
24  import java.util.Arrays;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * @author Manuel Laflamme
31   * @version $Revision$
32   * @since Feb 17, 2002
33   */
34  public class CompositeTable extends AbstractTable {
35  
36      /**
37       * Logger for this class
38       */
39      private static final Logger logger =
40              LoggerFactory.getLogger(CompositeTable.class);
41  
42      private final ITableMetaData _metaData;
43      private final ITable[] _tables;
44  
45      /**
46       * Creates a composite table that combines the specified metadata with the
47       * specified table.
48       */
49      public CompositeTable(ITableMetaData metaData, ITable table) {
50          _metaData = metaData;
51          _tables = new ITable[] {table};
52      }
53  
54      /**
55       * Creates a composite table that combines the specified metadata with the
56       * specified tables.
57       */
58      public CompositeTable(ITableMetaData metaData, ITable[] tables) {
59          _metaData = metaData;
60          _tables = tables;
61      }
62  
63      /**
64       * Creates a composite table that combines the specified specified tables.
65       * The metadata from the first table is used as metadata for the new table.
66       */
67      public CompositeTable(ITable table1, ITable table2) {
68          _metaData = table1.getTableMetaData();
69          _tables = new ITable[] {table1, table2};
70      }
71  
72      /**
73       * Creates a composite dataset that encapsulate the specified table with a
74       * new name.
75       */
76      public CompositeTable(String newName, ITable table) throws DataSetException {
77          ITableMetaData metaData = table.getTableMetaData();
78          _metaData =
79                  new DefaultTableMetaData(newName, metaData.getColumns(),
80                          metaData.getPrimaryKeys());
81          _tables = new ITable[] {table};
82      }
83  
84      // //////////////////////////////////////////////////////////////////////////
85      // ITable interface
86  
87      public ITableMetaData getTableMetaData() {
88          return _metaData;
89      }
90  
91      public int getRowCount() {
92          logger.debug("getRowCount() - start");
93  
94          int totalCount = 0;
95          for (int i = 0; i < _tables.length; i++) {
96              ITable table = _tables[i];
97              totalCount += table.getRowCount();
98          }
99  
100         return totalCount;
101     }
102 
103     public Object getValue(int row, String columnName) throws DataSetException {
104         if (logger.isDebugEnabled()) {
105             logger.debug("getValue(row={}, columnName={}) - start", Integer
106                     .toString(row), columnName);
107         }
108 
109         if (row < 0) {
110             throw new RowOutOfBoundsException(row + " < 0 ");
111         }
112 
113         int totalCount = 0;
114         for (int i = 0; i < _tables.length; i++) {
115             ITable table = _tables[i];
116 
117             int count = table.getRowCount();
118             if (totalCount + count > row) {
119                 return table.getValue(row - totalCount, columnName);
120             }
121             totalCount += count;
122         }
123 
124         throw new RowOutOfBoundsException(row + " > " + totalCount);
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     public String toString() {
131         StringBuilder sb = new StringBuilder(2000);
132 
133         sb.append(getClass().getName()).append("[");
134         sb.append("_metaData=[").append(_metaData).append("], ");
135         sb.append("_tables=[").append(Arrays.toString(_tables)).append("]");
136         sb.append("]");
137 
138         return sb.toString();
139     }
140 }