KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > OuterTable


1 /**
2  * com.mckoi.database.OuterTable 21 Sep 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program 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
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.util.ArrayList JavaDoc;
28 import com.mckoi.util.IntegerVector;
29
30 /**
31  * A Table class for forming OUTER type results. This takes as its constructor
32  * the base table (with no outer NULL fields) that is what the result is based
33  * on. It is then possible to merge in tables that are ancestors
34  *
35  * @author Tobias Downer
36  */

37
38 class OuterTable extends VirtualTable implements RootTable {
39
40   /**
41    * The merged rows.
42    */

43   public IntegerVector[] outer_rows;
44
45   /**
46    * The row count of the outer rows.
47    */

48   private int outer_row_count;
49
50   /**
51    * Constructs the OuterTable given the base table.
52    */

53   public OuterTable(Table input_table) {
54     super();
55
56     RawTableInformation base_table =
57                       input_table.resolveToRawTable(new RawTableInformation());
58     Table[] tables = base_table.getTables();
59     IntegerVector[] rows = base_table.getRows();
60
61     outer_rows = new IntegerVector[rows.length];
62
63     // Set up the VirtualTable with this base table information,
64
init(tables);
65     set(tables, rows);
66
67   }
68
69   /**
70    * Merges the given table in with this table.
71    */

72   public void mergeIn(Table outside_table) {
73     RawTableInformation raw_table_info =
74                     outside_table.resolveToRawTable(new RawTableInformation());
75
76     // Get the base information,
77
Table[] base_tables = getReferenceTables();
78     IntegerVector[] base_rows = getReferenceRows();
79
80     // The tables and rows being merged in.
81
Table[] tables = raw_table_info.getTables();
82     IntegerVector[] rows = raw_table_info.getRows();
83     // The number of rows being merged in.
84
outer_row_count = rows[0].size();
85
86     for (int i = 0; i < base_tables.length; ++i) {
87       Table btable = base_tables[i];
88       int index = -1;
89       for (int n = 0; n < tables.length && index == -1; ++n) {
90         if (btable == tables[n]) {
91           index = n;
92         }
93       }
94
95       // If the table wasn't found, then set 'NULL' to this base_table
96
if (index == -1) {
97         outer_rows[i] = null;
98       }
99       else {
100         IntegerVector list = new IntegerVector(outer_row_count);
101         outer_rows[i] = list;
102         // Merge in the rows from the input table,
103
IntegerVector to_merge = rows[index];
104         if (to_merge.size() != outer_row_count) {
105           throw new Error JavaDoc("Wrong size for rows being merged in.");
106         }
107         list.append(to_merge);
108       }
109
110     }
111
112   }
113
114   // ---------- Implemented from DefaultDataTable ----------
115

116   /**
117    * Returns the modified row count.
118    */

119   public int getRowCount() {
120     return super.getRowCount() + outer_row_count;
121   }
122
123   /**
124    * Returns a SelectableScheme for the given column in the given VirtualTable
125    * row domain. This searches down through the tables ancestors until it
126    * comes across a table with a SelectableScheme where the given column is
127    * fully resolved. In most cases, this will be the root DataTable.
128    * <p>
129    * For an OuterTable, this must also include any rows with an index of -1
130    * which indicates they are NULL. NULL rows are put at the top of the
131    * index list.
132    */

133   SelectableScheme getSelectableSchemeFor(int column, int original_column,
134                                           Table table) {
135
136     if (column_scheme[column] == null) {
137       // EFFICIENCY: We implement this with a blind search...
138
SelectableScheme scheme = new BlindSearch(this, column);
139       column_scheme[column] = scheme.getSubsetScheme(this, column);
140     }
141
142     if (table == this) {
143       return column_scheme[column];
144     }
145     else {
146       return column_scheme[column].getSubsetScheme(table, original_column);
147     }
148
149   }
150
151   /**
152    * Returns an object that represents the information in the given cell
153    * in the table.
154    */

155   public TObject getCellContents(int column, int row) {
156     int table_num = column_table[column];
157     Table parent_table = reference_list[table_num];
158     if (row >= outer_row_count) {
159       row = row_list[table_num].intAt(row - outer_row_count);
160       return parent_table.getCellContents(column_filter[column], row);
161     }
162     else {
163       if (outer_rows[table_num] == null) {
164         // Special case, handling outer entries (NULL)
165
return new TObject(getColumnDefAt(column).getTType(), null);
166       }
167       else {
168         row = outer_rows[table_num].intAt(row);
169         return parent_table.getCellContents(column_filter[column], row);
170       }
171     }
172   }
173
174   // ---------- Implemented from RootTable ----------
175

176   /**
177    * This function is used to check that two tables are identical. This
178    * is used in operations like 'union' that need to determine that the
179    * roots are infact of the same type.
180    */

181   public boolean typeEquals(RootTable table) {
182     return (this == table);
183   }
184
185 }
186
Popular Tags