KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.FilterTable 13 Jul 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 com.mckoi.util.IntegerVector;
28
29 /**
30  * A table that is a filter for another table. By default, all Table methods
31  * are implemented to call the parent. This class should be used when we
32  * want to implement a Table filter of some kind. For example, a filter
33  * for specific columns, or even rows, etc.
34  * <p>
35  * NOTE: For efficiency reasons, this will store SelectableScheme objects
36  * generated by the parent like VirtualTable.
37  *
38  * @author Tobias Downer
39  */

40
41 public class FilterTable extends Table {
42
43   /**
44    * The Table we are filtering the columns from.
45    */

46   protected Table parent;
47
48   /**
49    * The schemes to describe the entity relation in the given column.
50    */

51   private SelectableScheme[] column_scheme;
52
53   /**
54    * The Constructor.
55    */

56   public FilterTable(Table parent) {
57     this.parent = parent;
58   }
59
60   /**
61    * Returns the parent table.
62    */

63   protected Table getParent() {
64     return parent;
65   }
66
67   /**
68    * Returns the parent Database object.
69    */

70   public Database getDatabase() {
71     return parent.getDatabase();
72   }
73
74   /**
75    * Returns the number of columns in the table.
76    */

77   public int getColumnCount() {
78     return parent.getColumnCount();
79   }
80
81   /**
82    * Returns the number of rows stored in the table.
83    */

84   public int getRowCount() {
85     return parent.getRowCount();
86   }
87
88   /**
89    * Given a fully qualified variable field name, ie. 'APP.CUSTOMER.CUSTOMERID'
90    * this will return the column number the field is at. Returns -1 if the
91    * field does not exist in the table.
92    */

93   public int findFieldName(Variable v) {
94     return parent.findFieldName(v);
95   }
96
97   /**
98    * Returns a fully qualified Variable object that represents the name of
99    * the column at the given index. For example,
100    * new Variable(new TableName("APP", "CUSTOMER"), "ID")
101    */

102   public Variable getResolvedVariable(int column) {
103     return parent.getResolvedVariable(column);
104   }
105
106   /**
107    * Returns a SelectableScheme for the given column in the given VirtualTable
108    * row domain.
109    */

110   SelectableScheme getSelectableSchemeFor(int column,
111                                           int original_column, Table table) {
112
113     if (column_scheme == null) {
114       column_scheme = new SelectableScheme[parent.getColumnCount()];
115     }
116
117     // Is there a local scheme available?
118
SelectableScheme scheme = column_scheme[column];
119     if (scheme == null) {
120
121       // If we are asking for the selectable schema of this table we must
122
// tell the parent we are looking for its selectable scheme.
123
Table t = table;
124       if (table == this) {
125         t = parent;
126       }
127
128       // Scheme is not cached in this table so ask the parent.
129
scheme = parent.getSelectableSchemeFor(column, original_column, t);
130       if (table == this) {
131         column_scheme[column] = scheme;
132       }
133
134     }
135     else {
136       // If this has a cached scheme and we are in the correct domain then
137
// return it.
138
if (table == this) {
139         return scheme;
140       }
141       else {
142         // Otherwise we must calculate the subset of the scheme
143
return scheme.getSubsetScheme(table, original_column);
144       }
145     }
146     return scheme;
147   }
148
149   /**
150    * Given a set, this trickles down through the Table hierarchy resolving
151    * the given row_set to a form that the given ancestor understands.
152    * Say you give the set { 0, 1, 2, 3, 4, 5, 6 }, this function may check
153    * down three levels and return a new 7 element set with the rows fully
154    * resolved to the given ancestors domain.
155    */

156   void setToRowTableDomain(int column, IntegerVector row_set,
157                            TableDataSource ancestor) {
158     if (ancestor == this || ancestor == parent) {
159       return;
160     }
161     else {
162       parent.setToRowTableDomain(column, row_set, ancestor);
163     }
164   }
165
166   /**
167    * Return the list of DataTable and row sets that make up the raw information
168    * in this table.
169    */

170   RawTableInformation resolveToRawTable(RawTableInformation info) {
171     return parent.resolveToRawTable(info);
172   }
173
174   /**
175    * Returns an object that represents the information in the given cell
176    * in the table. This will generally be an expensive algorithm, so calls
177    * to it should be kept to a minimum. Note that the offset between two
178    * rows is not necessarily 1.
179    */

180   public TObject getCellContents(int column, int row) {
181     return parent.getCellContents(column, row);
182   }
183
184   /**
185    * Returns an Enumeration of the rows in this table.
186    * The Enumeration is a fast way of retrieving consequtive rows in the table.
187    */

188   public RowEnumeration rowEnumeration() {
189     return parent.rowEnumeration();
190   }
191
192   /**
193    * Returns a DataTableDef object that defines the name of the table and the
194    * layout of the columns of the table. Note that for tables that are joined
195    * with other tables, the table name and schema for this object become
196    * mangled. For example, a table called 'PERSON' joined with a table called
197    * 'MUSIC' becomes a table called 'PERSON#MUSIC' in a null schema.
198    */

199   public DataTableDef getDataTableDef() {
200     return parent.getDataTableDef();
201   }
202
203   /**
204    * Adds a DataTableListener to the DataTable objects at the root of this
205    * table tree hierarchy. If this table represents the join of a number of
206    * tables then the DataTableListener is added to all the DataTable objects
207    * at the root.
208    * <p>
209    * A DataTableListener is notified of all modifications to the raw entries
210    * of the table. This listener can be used for detecting changes in VIEWs,
211    * for triggers or for caching of common queries.
212    */

213   void addDataTableListener(DataTableListener listener) {
214     parent.addDataTableListener(listener);
215   }
216
217   /**
218    * Removes a DataTableListener from the DataTable objects at the root of
219    * this table tree hierarchy. If this table represents the join of a
220    * number of tables, then the DataTableListener is removed from all the
221    * DataTable objects at the root.
222    */

223   void removeDataTableListener(DataTableListener listener) {
224     parent.removeDataTableListener(listener);
225   }
226
227
228   /**
229    * Locks the root table(s) of this table so that it is impossible to
230    * overwrite the underlying rows that may appear in this table.
231    * This is used when cells in the table need to be accessed 'outside' the
232    * lock. So we may have late access to cells in the table.
233    * 'lock_key' is a given key that will also unlock the root table(s).
234    * NOTE: This is nothing to do with the 'LockingMechanism' object.
235    */

236   public void lockRoot(int lock_key) {
237     parent.lockRoot(lock_key);
238   }
239
240   /**
241    * Unlocks the root tables so that the underlying rows may
242    * once again be used if they are not locked and have been removed. This
243    * should be called some time after the rows have been locked.
244    */

245   public void unlockRoot(int lock_key) {
246     parent.unlockRoot(lock_key);
247   }
248
249   /**
250    * Returns true if the table has its row roots locked (via the lockRoot(int)
251    * method.
252    */

253   public boolean hasRootsLocked() {
254     return parent.hasRootsLocked();
255   }
256
257
258
259   /**
260    * Prints a graph of the table hierarchy to the stream.
261    */

262   public void printGraph(java.io.PrintStream JavaDoc out, int indent) {
263     for (int i = 0; i < indent; ++i) {
264       out.print(' ');
265     }
266     out.println("F[" + getClass());
267
268     parent.printGraph(out, indent + 2);
269
270     for (int i = 0; i < indent; ++i) {
271       out.print(' ');
272     }
273     out.println("]");
274   }
275
276 }
277
Popular Tags