1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
31
32
33
34 public class CompositeTable extends AbstractTable {
35
36
37
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
47
48
49 public CompositeTable(ITableMetaData metaData, ITable table) {
50 _metaData = metaData;
51 _tables = new ITable[] {table};
52 }
53
54
55
56
57
58 public CompositeTable(ITableMetaData metaData, ITable[] tables) {
59 _metaData = metaData;
60 _tables = tables;
61 }
62
63
64
65
66
67 public CompositeTable(ITable table1, ITable table2) {
68 _metaData = table1.getTableMetaData();
69 _tables = new ITable[] {table1, table2};
70 }
71
72
73
74
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
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
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 }