001: /**
002: * com.mckoi.database.VirtualTable 08 Mar 1998
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: import com.mckoi.util.IntegerVector;
026: import com.mckoi.util.BlockIntegerList;
027:
028: /**
029: * A VirtualTable is a representation of a table whose rows are actually
030: * physically stored in another table. In other words, this table just
031: * stores pointers to rows in other tables.
032: * <p>
033: * We use the VirtualTable to represent temporary tables created from select,
034: * join, etc operations.
035: * <p>
036: * An important note about VirtualTables: When we perform a 'select' operation
037: * on a virtual table, unlike a DataTable that permanently stores information
038: * about column cell relations, we must resolve column relations between the
039: * sub-set at select time. This involves asking the tables parent(s) for a
040: * scheme to describe relations in a sub-set.
041: *
042: * @author Tobias Downer
043: */
044:
045: public class VirtualTable extends JoinedTable {
046:
047: /**
048: * Array of IntegerVectors that represent the rows taken from the given
049: * parents.
050: */
051: protected IntegerVector[] row_list;
052:
053: /**
054: * The number of rows in the table.
055: */
056: private int row_count;
057:
058: /**
059: * Helper function for the constructor.
060: */
061: protected void init(Table[] tables) {
062: super .init(tables);
063:
064: int table_count = tables.length;
065: row_list = new IntegerVector[table_count];
066: for (int i = 0; i < table_count; ++i) {
067: row_list[i] = new IntegerVector();
068: }
069: }
070:
071: /**
072: * The Constructor. It is constructed with a list of tables that this
073: * virtual table is a sub-set or join of.
074: */
075: VirtualTable(Table[] tables) {
076: super (tables);
077: }
078:
079: VirtualTable(Table table) {
080: super (table);
081: }
082:
083: protected VirtualTable() {
084: super ();
085: }
086:
087: /**
088: * Returns the list of IntegerVector that represents the rows that this
089: * VirtualTable references.
090: */
091: protected IntegerVector[] getReferenceRows() {
092: return row_list;
093: }
094:
095: /**
096: * Returns the number of rows stored in the table.
097: */
098: public int getRowCount() {
099: return row_count;
100: }
101:
102: /**
103: * Sets the rows in this table. We should search for the
104: * 'table' in the 'reference_list' however we don't for efficiency.
105: */
106: void set(Table table, IntegerVector rows) {
107: row_list[0] = new IntegerVector(rows);
108: row_count = rows.size();
109: }
110:
111: /**
112: * This is used in a join to set a list or joined rows and tables. The
113: * 'tables' array should be an exact mirror of the 'reference_list'. The
114: * IntegerVector[] array contains the rows to add for each respective table.
115: * The given IntegerVector objects should have identical lengths.
116: */
117: void set(Table[] tables, IntegerVector[] rows) {
118: for (int i = 0; i < tables.length; ++i) {
119: row_list[i] = new IntegerVector(rows[i]);
120: }
121: if (rows.length > 0) {
122: row_count = rows[0].size();
123: }
124: }
125:
126: /**
127: * Sets the rows in this table as above, but uses a BlockIntegerList as an
128: * argument instead.
129: */
130: void set(Table table, BlockIntegerList rows) {
131: row_list[0] = new IntegerVector(rows);
132: row_count = rows.size();
133: }
134:
135: /**
136: * Sets the rows in this table as above, but uses a BlockIntegerList array
137: * as an argument instead.
138: */
139: void set(Table[] tables, BlockIntegerList[] rows) {
140: for (int i = 0; i < tables.length; ++i) {
141: row_list[i] = new IntegerVector(rows[i]);
142: }
143: if (rows.length > 0) {
144: row_count = rows[0].size();
145: }
146: }
147:
148: // ---------- Implemented from JoinedTable ----------
149:
150: protected int resolveRowForTableAt(int row_number, int table_num) {
151: return row_list[table_num].intAt(row_number);
152: }
153:
154: protected void resolveAllRowsForTableAt(IntegerVector row_set,
155: int table_num) {
156: IntegerVector cur_row_list = row_list[table_num];
157: for (int n = row_set.size() - 1; n >= 0; --n) {
158: int aa = row_set.intAt(n);
159: int bb = cur_row_list.intAt(aa);
160: row_set.setIntAt(bb, n);
161: }
162: }
163:
164: }
|