KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.VirtualTable 08 Mar 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 import com.mckoi.util.BlockIntegerList;
29
30 /**
31  * A VirtualTable is a representation of a table whose rows are actually
32  * physically stored in another table. In other words, this table just
33  * stores pointers to rows in other tables.
34  * <p>
35  * We use the VirtualTable to represent temporary tables created from select,
36  * join, etc operations.
37  * <p>
38  * An important note about VirtualTables: When we perform a 'select' operation
39  * on a virtual table, unlike a DataTable that permanently stores information
40  * about column cell relations, we must resolve column relations between the
41  * sub-set at select time. This involves asking the tables parent(s) for a
42  * scheme to describe relations in a sub-set.
43  *
44  * @author Tobias Downer
45  */

46
47 public class VirtualTable extends JoinedTable {
48
49   /**
50    * Array of IntegerVectors that represent the rows taken from the given
51    * parents.
52    */

53   protected IntegerVector[] row_list;
54
55   /**
56    * The number of rows in the table.
57    */

58   private int row_count;
59
60   /**
61    * Helper function for the constructor.
62    */

63   protected void init(Table[] tables) {
64     super.init(tables);
65
66     int table_count = tables.length;
67     row_list = new IntegerVector[table_count];
68     for (int i = 0; i < table_count; ++i) {
69       row_list[i] = new IntegerVector();
70     }
71   }
72
73   /**
74    * The Constructor. It is constructed with a list of tables that this
75    * virtual table is a sub-set or join of.
76    */

77   VirtualTable(Table[] tables) {
78     super(tables);
79   }
80
81   VirtualTable(Table table) {
82     super(table);
83   }
84
85   protected VirtualTable() {
86     super();
87   }
88
89   /**
90    * Returns the list of IntegerVector that represents the rows that this
91    * VirtualTable references.
92    */

93   protected IntegerVector[] getReferenceRows() {
94     return row_list;
95   }
96   
97   /**
98    * Returns the number of rows stored in the table.
99    */

100   public int getRowCount() {
101     return row_count;
102   }
103
104   /**
105    * Sets the rows in this table. We should search for the
106    * 'table' in the 'reference_list' however we don't for efficiency.
107    */

108   void set(Table table, IntegerVector rows) {
109     row_list[0] = new IntegerVector(rows);
110     row_count = rows.size();
111   }
112
113   /**
114    * This is used in a join to set a list or joined rows and tables. The
115    * 'tables' array should be an exact mirror of the 'reference_list'. The
116    * IntegerVector[] array contains the rows to add for each respective table.
117    * The given IntegerVector objects should have identical lengths.
118    */

119   void set(Table[] tables, IntegerVector[] rows) {
120     for (int i = 0; i < tables.length; ++i) {
121       row_list[i] = new IntegerVector(rows[i]);
122     }
123     if (rows.length > 0) {
124       row_count = rows[0].size();
125     }
126   }
127
128   /**
129    * Sets the rows in this table as above, but uses a BlockIntegerList as an
130    * argument instead.
131    */

132   void set(Table table, BlockIntegerList rows) {
133     row_list[0] = new IntegerVector(rows);
134     row_count = rows.size();
135   }
136
137   /**
138    * Sets the rows in this table as above, but uses a BlockIntegerList array
139    * as an argument instead.
140    */

141   void set(Table[] tables, BlockIntegerList[] rows) {
142     for (int i = 0; i < tables.length; ++i) {
143       row_list[i] = new IntegerVector(rows[i]);
144     }
145     if (rows.length > 0) {
146       row_count = rows[0].size();
147     }
148   }
149
150   // ---------- Implemented from JoinedTable ----------
151

152   protected int resolveRowForTableAt(int row_number, int table_num) {
153     return row_list[table_num].intAt(row_number);
154   }
155
156   protected void resolveAllRowsForTableAt(
157                                       IntegerVector row_set, int table_num) {
158     IntegerVector cur_row_list = row_list[table_num];
159     for (int n = row_set.size() - 1; n >= 0; --n) {
160       int aa = row_set.intAt(n);
161       int bb = cur_row_list.intAt(aa);
162       row_set.setIntAt(bb, n);
163     }
164   }
165
166
167 }
168
Popular Tags