KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.DataTableFilter 06 Apr 1998
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  * This object sits on top of a DataTable object filtering out certain types
31  * of calls. We could use this object to implement a ReferenceTable which
32  * can be used to declare a new table name with a DataTable type. We also
33  * use this object to implement a filter for column removals.
34  * <p>
35  * @author Tobias Downer
36  */

37
38 public class DataTableFilter extends AbstractDataTable {
39
40   /**
41    * The parent DataTable object.
42    */

43   protected AbstractDataTable parent;
44
45   /**
46    * The Constructor. A filter can only sit on top of a DataTable or
47    * DataTableFilter table.
48    * ISSUE: we could make an interface for this. This is a bit of a hack.
49    */

50   protected DataTableFilter(AbstractDataTable table) {
51     super();
52     parent = table;
53   }
54
55   /**
56    * Returns the Database context for this filtered table.
57    */

58   public Database getDatabase() {
59     return parent.getDatabase();
60   }
61
62   /**
63    * Returns the number of columns in the table.
64    */

65   public int getColumnCount() {
66     return parent.getColumnCount();
67   }
68
69   /**
70    * Returns the number of rows stored in the table.
71    */

72   public final int getRowCount() {
73     return parent.getRowCount();
74   }
75
76   /**
77    * Given a fully qualified variable field name, ie. 'APP.CUSTOMER.CUSTOMERID'
78    * this will return the column number the field is at. Returns -1 if the
79    * field does not exist in the table.
80    */

81   public int findFieldName(Variable v) {
82     return parent.findFieldName(v);
83   }
84
85   /**
86    * Returns a fully qualified Variable object that represents the name of
87    * the column at the given index. For example,
88    * new Variable(new TableName("APP", "CUSTOMER"), "ID")
89    */

90   public Variable getResolvedVariable(int column) {
91     return parent.getResolvedVariable(column);
92   }
93
94   /**
95    * Returns a SelectableScheme for the given column in the given VirtualTable
96    * row domain. Slight Hack: When we are asking for a selectable scheme for
97    * a reference table, we must defer the 'table' variable to the parent.
98    */

99   final SelectableScheme getSelectableSchemeFor(int column,
100                                            int original_column, Table table) {
101     if (table == this) {
102       return parent.getSelectableSchemeFor(column, original_column, parent);
103     }
104     else {
105       return parent.getSelectableSchemeFor(column, original_column, table);
106     }
107   }
108
109   /**
110    * Given a set, this trickles down through the Table hierarchy resolving
111    * the given row_set to a form that the given ancestor understands.
112    * Say you give the set { 0, 1, 2, 3, 4, 5, 6 }, this function may check
113    * down three levels and return a new 7 element set with the rows fully
114    * resolved to the given ancestors domain.
115    * <p>
116    * Slight Hack: When we are asking for a selectable scheme for a reference
117    * table, we must defer the 'table' variable to the parent.
118    */

119   final void setToRowTableDomain(int column, IntegerVector row_set,
120                                  TableDataSource ancestor) {
121     if (ancestor == this) {
122       parent.setToRowTableDomain(column, row_set, parent);
123     }
124     else {
125       parent.setToRowTableDomain(column, row_set, ancestor);
126     }
127   }
128
129   /**
130    * Return the list of DataTable and row sets that make up the raw information
131    * in this table. This is identical to the DataTable method except it
132    * puts this table as the owner of the row set.
133    */

134   final RawTableInformation resolveToRawTable(RawTableInformation info) {
135     IntegerVector row_set = new IntegerVector();
136     RowEnumeration e = rowEnumeration();
137     while (e.hasMoreRows()) {
138       row_set.addInt(e.nextRowIndex());
139     }
140     info.add(this, row_set);
141     return info;
142   }
143
144   /**
145    * Returns an object that represents the information in the given cell
146    * in the table. This will generally be an expensive algorithm, so calls
147    * to it should be kept to a minimum. Note that the offset between two
148    * rows is not necessarily 1.
149    */

150   public final TObject getCellContents(int column, int row) {
151     return parent.getCellContents(column, row);
152   }
153
154   /**
155    * Returns an Enumeration of the rows in this table.
156    * The Enumeration is a fast way of retrieving consequtive rows in the table.
157    */

158   public final RowEnumeration rowEnumeration() {
159     return parent.rowEnumeration();
160   }
161
162   /**
163    * Returns a DataTableDef object that defines the name of the table and the
164    * layout of the columns of the table. Note that for tables that are joined
165    * with other tables, the table name and schema for this object become
166    * mangled. For example, a table called 'PERSON' joined with a table called
167    * 'MUSIC' becomes a table called 'PERSON#MUSIC' in a null schema.
168    */

169   public DataTableDef getDataTableDef() {
170     return parent.getDataTableDef();
171   }
172
173   /**
174    * Adds a DataTableListener to the DataTable objects at the root of this
175    * table tree hierarchy. If this table represents the join of a number of
176    * tables then the DataTableListener is added to all the DataTable objects
177    * at the root.
178    * <p>
179    * A DataTableListener is notified of all modifications to the raw entries
180    * of the table. This listener can be used for detecting changes in VIEWs,
181    * for triggers or for caching of common queries.
182    */

183   void addDataTableListener(DataTableListener listener) {
184     parent.addDataTableListener(listener);
185   }
186
187   /**
188    * Removes a DataTableListener from the DataTable objects at the root of
189    * this table tree hierarchy. If this table represents the join of a
190    * number of tables, then the DataTableListener is removed from all the
191    * DataTable objects at the root.
192    */

193   void removeDataTableListener(DataTableListener listener) {
194     parent.removeDataTableListener(listener);
195   }
196
197
198   /**
199    * Locks the root table(s) of this table so that it is impossible to
200    * overwrite the underlying rows that may appear in this table.
201    * This is used when cells in the table need to be accessed 'outside' the
202    * lock. So we may have late access to cells in the table.
203    * 'lock_key' is a given key that will also unlock the root table(s).
204    * NOTE: This is nothing to do with the 'LockingMechanism' object.
205    */

206   public void lockRoot(int lock_key) {
207     parent.lockRoot(lock_key);
208   }
209
210   /**
211    * Unlocks the root tables so that the underlying rows may
212    * once again be used if they are not locked and have been removed. This
213    * should be called some time after the rows have been locked.
214    */

215   public void unlockRoot(int lock_key) {
216     parent.unlockRoot(lock_key);
217   }
218
219   /**
220    * Returns true if the table has its row roots locked (via the lockRoot(int)
221    * method.
222    */

223   public boolean hasRootsLocked() {
224     return parent.hasRootsLocked();
225   }
226
227 }
228
Popular Tags