KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.TemporaryTable 11 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 java.util.ArrayList JavaDoc;
28 import com.mckoi.util.IntegerVector;
29 import com.mckoi.debug.*;
30 import com.mckoi.database.global.TypeUtil;
31
32 /**
33  * This class represents a temporary table that is built from data that is
34  * not related to any underlying DataTable object from the database.
35  * <p>
36  * For example, an aggregate function generates data would be put into a
37  * TemporaryTable.
38  *
39  * @author Tobias Downer
40  */

41
42 public final class TemporaryTable extends DefaultDataTable {
43
44   /**
45    * The DataTableDef object that describes the columns in this table.
46    */

47   private DataTableDef table_def;
48
49   /**
50    * A Vector that represents the storage of TObject[] arrays for each row
51    * of the table.
52    */

53   private ArrayList JavaDoc table_storage;
54
55   /**
56    * The Constructor.
57    */

58   public TemporaryTable(Database database,
59                         String JavaDoc name, DataTableColumnDef[] fields) {
60     super(database);
61
62     table_storage = new ArrayList JavaDoc();
63
64     table_def = new DataTableDef();
65     table_def.setTableName(new TableName(null, name));
66     for (int i = 0; i < fields.length; ++i) {
67       table_def.addVirtualColumn(new DataTableColumnDef(fields[i]));
68     }
69     table_def.setImmutable();
70   }
71
72   /**
73    * Constructs this TemporaryTable based on the fields from the given
74    * Table object.
75    */

76   public TemporaryTable(String JavaDoc name, Table based_on) {
77     super(based_on.getDatabase());
78
79     table_def = new DataTableDef(based_on.getDataTableDef());
80     table_def.setTableName(new TableName(null, name));
81     table_def.setImmutable();
82   }
83
84   /**
85    * Constructs this TemporaryTable based on the given Table object.
86    */

87   public TemporaryTable(DefaultDataTable based_on) {
88     super(based_on.getDatabase());
89     
90     table_def = new DataTableDef(based_on.getDataTableDef());
91     table_def.setImmutable();
92   }
93
94
95
96   /* ====== Methods that are only for TemporaryTable interface ====== */
97
98   /**
99    * Resolves the given column name (eg 'id' or 'Customer.id' or
100    * 'APP.Customer.id') to a column in this table.
101    */

102   private Variable resolveToVariable(String JavaDoc col_name) {
103     Variable partial = Variable.resolve(col_name);
104     return partial;
105 // return partial.resolveTableName(TableName.resolve(getName()));
106
}
107
108   /**
109    * Creates a new row where cells can be inserted into.
110    */

111   public void newRow() {
112     table_storage.add(new TObject[getColumnCount()]);
113     ++row_count;
114   }
115
116   /**
117    * Sets the cell in the given column / row to the given value.
118    */

119   public void setRowCell(TObject cell, int column, int row) {
120     TObject[] cells = (TObject[]) table_storage.get(row);
121     cells[column] = cell;
122   }
123
124   /**
125    * Sets the cell in the column of the last row of this table to the given
126    * TObject.
127    */

128   public void setRowCell(TObject cell, String JavaDoc col_name) {
129     Variable v = resolveToVariable(col_name);
130     setRowCell(cell, findFieldName(v), row_count - 1);
131   }
132
133   /**
134    * Sets the cell in the column of the last row of this table to the given
135    * TObject.
136    */

137   public void setRowObject(TObject ob, int col_index, int row) {
138     setRowCell(ob, col_index, row);
139   }
140
141   /**
142    * Sets the cell in the column of the last row of this table to the given
143    * TObject.
144    */

145   public void setRowObject(TObject ob, String JavaDoc col_name) {
146     Variable v = resolveToVariable(col_name);
147     setRowObject(ob, findFieldName(v));
148   }
149
150   /**
151    * Sets the cell in the column of the last row of this table to the given
152    * TObject.
153    */

154   public void setRowObject(TObject ob, int col_index) {
155     setRowObject(ob, col_index, row_count - 1);
156   }
157
158   /**
159    * Copies the cell from the given table (src_col, src_row) to the last row
160    * of the column specified of this table.
161    */

162   public void setCellFrom(Table table, int src_col, int src_row,
163                           String JavaDoc to_col) {
164     Variable v = resolveToVariable(to_col);
165     TObject cell = table.getCellContents(src_col, src_row);
166     setRowCell(cell, findFieldName(v), row_count - 1);
167   }
168
169   /**
170    * Copies the contents of the row of the given Table onto the end of this
171    * table. Only copies columns that exist in both tables.
172    */

173   public void copyFrom(Table table, int row) {
174     newRow();
175
176     Variable[] vars = new Variable[table.getColumnCount()];
177     for (int i = 0; i < vars.length; ++i) {
178       vars[i] = table.getResolvedVariable(i);
179     }
180
181     for (int i = 0; i < getColumnCount(); ++i) {
182       Variable v = getResolvedVariable(i);
183       String JavaDoc col_name = v.getName();
184       try {
185         int tcol_index = -1;
186         for (int n = 0; n < vars.length || tcol_index == -1; ++n) {
187           if (vars[n].getName().equals(col_name)) {
188             tcol_index = n;
189           }
190         }
191         setRowCell(table.getCellContents(tcol_index, row), i, row_count - 1);
192       }
193       catch (Exception JavaDoc e) {
194         Debug().writeException(e);
195         throw new Error JavaDoc(e.getMessage());
196       }
197     }
198
199   }
200
201
202
203
204
205   /**
206    * This should be called if you want to perform table operations on this
207    * TemporaryTable. It should be called *after* all the rows have been set.
208    * It generates SelectableScheme object which sorts the columns of the table
209    * and lets us execute Table operations on this table.
210    * NOTE: After this method is called, the table must not change in any way.
211    */

212   public void setupAllSelectableSchemes() {
213     blankSelectableSchemes(1); // <- blind search
214
for (int row_number = 0; row_number < row_count; ++row_number) {
215       addRowToColumnSchemes(row_number);
216     }
217   }
218
219   /* ====== Methods that are implemented for Table interface ====== */
220
221   public DataTableDef getDataTableDef() {
222     return table_def;
223   }
224
225   /**
226    * Returns an object that represents the information in the given cell
227    * in the table. This can be used to obtain information about the given
228    * table cells.
229    */

230   public TObject getCellContents(int column, int row) {
231     TObject[] cells = (TObject[]) table_storage.get(row);
232     TObject cell = cells[column];
233     if (cell == null) {
234       throw new Error JavaDoc("NULL cell! (" + column + ", " + row + ")");
235     }
236     return cell;
237   }
238
239   /**
240    * Returns an Enumeration of the rows in this table.
241    * Each call to 'nextRowIndex' returns the next valid row index in the table.
242    */

243   public RowEnumeration rowEnumeration() {
244     return new SimpleRowEnumeration(row_count);
245   }
246
247   /**
248    * Adds a DataTableListener to the DataTable objects at the root of this
249    * table tree hierarchy. If this table represents the join of a number of
250    * tables then the DataTableListener is added to all the DataTable objects
251    * at the root.
252    * <p>
253    * A DataTableListener is notified of all modifications to the raw entries
254    * of the table. This listener can be used for detecting changes in VIEWs,
255    * for triggers or for caching of common queries.
256    */

257   void addDataTableListener(DataTableListener listener) {
258     // Nothing to be notified on with a Temporary table...
259
}
260
261   /**
262    * Removes a DataTableListener from the DataTable objects at the root of
263    * this table tree hierarchy. If this table represents the join of a
264    * number of tables, then the DataTableListener is removed from all the
265    * DataTable objects at the root.
266    */

267   void removeDataTableListener(DataTableListener listener) {
268     // No listeners can be in a TemporaryTable.
269
}
270
271   /**
272    * Locks the root table(s) of this table so that it is impossible to
273    * overwrite the underlying rows that may appear in this table.
274    * This is used when cells in the table need to be accessed 'outside' the
275    * lock. So we may have late access to cells in the table.
276    * 'lock_key' is a given key that will also unlock the root table(s).
277    * NOTE: This is nothing to do with the 'LockingMechanism' object.
278    */

279   public void lockRoot(int lock_key) {
280     // We don't need to do anything for temporary tables, because they have
281
// no root to lock.
282
}
283
284   /**
285    * Unlocks the root tables so that the underlying rows may
286    * once again be used if they are not locked and have been removed. This
287    * should be called some time after the rows have been locked.
288    */

289   public void unlockRoot(int lock_key) {
290     // We don't need to do anything for temporary tables, because they have
291
// no root to unlock.
292
}
293
294   /**
295    * Returns true if the table has its row roots locked (via the lockRoot(int)
296    * method.
297    */

298   public boolean hasRootsLocked() {
299     // A temporary table _always_ has its roots locked.
300
return true;
301   }
302
303
304   // ---------- Static convenience methods ----------
305

306   /**
307    * Creates a table with a single column with the given name and type.
308    */

309   static final TemporaryTable singleColumnTable(Database database,
310                                                 String JavaDoc col_name, Class JavaDoc c) {
311     TType ttype = TType.fromClass(c);
312     DataTableColumnDef col_def = new DataTableColumnDef();
313     col_def.setName(col_name);
314     col_def.setFromTType(ttype);
315     TemporaryTable table = new TemporaryTable(database, "single",
316                                         new DataTableColumnDef[] { col_def });
317       
318 // int type = TypeUtil.toDBType(c);
319
// TableField[] fields =
320
// { new TableField(col_name, type, Integer.MAX_VALUE, false) };
321
// TemporaryTable table = new TemporaryTable(database, "single", fields);
322
return table;
323   }
324
325 }
326
Popular Tags