KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.ReferenceTable 03 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 is an implementation of a Table that references a DataTable as its
31  * parent. This is a one-to-one relationship unlike the VirtualTable class
32  * which is a one-to-many relationship.
33  * <p>
34  * The entire purpose of this class is as a filter. We can use it to rename
35  * a DataTable class to any domain we feel like. This allows us to generate
36  * unique column names.
37  * <p>
38  * For example, say we need to join the same table. We can use this method
39  * to ensure that the newly joined table won't have duplicate column names.
40  * <p>
41  * This object implements RootTable.
42  *
43  * @author Tobias Downer
44  */

45
46 public final class ReferenceTable extends FilterTable implements RootTable {
47
48   /**
49    * This represents the new name of the table.
50    */

51   private TableName table_name;
52
53   /**
54    * The modified DataTableDef object for this reference.
55    */

56   private DataTableDef modified_table_def;
57
58   
59   /**
60    * The Constructor.
61    */

62   ReferenceTable(Table table, TableName tname) {
63     super(table);
64     table_name = tname;
65
66     // Create a modified table def based on the parent def.
67
modified_table_def = new DataTableDef(table.getDataTableDef());
68     modified_table_def.setTableName(tname);
69     modified_table_def.setImmutable();
70   }
71
72   /**
73    * Constructs the ReferenceTable given the parent table, and a new
74    * DataTableDef that describes the columns in this table. This is used if
75    * we want to redefine the column names.
76    * <p>
77    * Note that the given DataTableDef must contain the same number of columns as
78    * the parent table, and the columns must be the same type.
79    */

80   ReferenceTable(Table table, DataTableDef def) {
81     super(table);
82     table_name = def.getTableName();
83     
84     modified_table_def = def;
85   }
86   
87   /**
88    * Filters the name of the table. This returns the declared name of the
89    * table.
90    */

91   public TableName getTableName() {
92     return table_name;
93   }
94
95   /**
96    * Returns the 'modified' DataTableDef object for this reference.
97    */

98   public DataTableDef getDataTableDef() {
99     return modified_table_def;
100   }
101
102   /**
103    * Given a fully qualified variable field name, ie. 'APP.CUSTOMER.CUSTOMERID'
104    * this will return the column number the field is at. Returns -1 if the
105    * field does not exist in the table.
106    */

107   public int findFieldName(Variable v) {
108     TableName table_name = v.getTableName();
109     if (table_name != null && table_name.equals(getTableName())) {
110       return getDataTableDef().fastFindColumnName(v.getName());
111     }
112     return -1;
113   }
114
115   /**
116    * Returns a fully qualified Variable object that represents the name of
117    * the column at the given index. For example,
118    * new Variable(new TableName("APP", "CUSTOMER"), "ID")
119    */

120   public Variable getResolvedVariable(int column) {
121     return new Variable(getTableName(),
122                         getDataTableDef().columnAt(column).getName());
123   }
124
125   public boolean typeEquals(RootTable table) {
126     return (this == table);
127   }
128
129 }
130
Popular Tags