KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > FromTableDirectSource


1 /**
2  * com.mckoi.database.sql.FromTableDirectSource 20 Jul 2001
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.interpret;
26
27 import com.mckoi.database.*;
28 import java.util.List JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 /**
32  * An implementation of FromTableInterface that wraps around an
33  * TableName/AbstractDataTable object. The handles case insensitive
34  * resolution.
35  *
36  * @author Tobias Downer
37  */

38
39 public class FromTableDirectSource implements FromTableInterface {
40
41   /**
42    * The TableQueryDef object that links to the underlying table.
43    */

44   private TableQueryDef table_query;
45   
46   /**
47    * The DataTableDef object that describes the table.
48    */

49   private DataTableDef data_table_def;
50
51   /**
52    * The unique name given to this source.
53    */

54   private String JavaDoc unique_name;
55
56   /**
57    * The given TableName of this table.
58    */

59   private TableName table_name;
60
61   /**
62    * The root name of the table. For example, if this table is 'Part P' the
63    * root name is 'Part' and 'P' is the aliased name.
64    */

65   private TableName root_name;
66
67   /**
68    * Set to true if this should do case insensitive resolutions.
69    */

70   private boolean case_insensitive = false;
71
72   /**
73    * Constructs the source.
74    */

75   public FromTableDirectSource(DatabaseConnection connection,
76                                TableQueryDef table_query, String JavaDoc unique_name,
77                                TableName given_name, TableName root_name) {
78     this.unique_name = unique_name;
79     this.data_table_def = table_query.getDataTableDef();
80     this.root_name = root_name;
81     if (given_name != null) {
82       this.table_name = given_name;
83     }
84     else {
85       this.table_name = root_name;
86     }
87     // Is the database case insensitive?
88
this.case_insensitive = connection.isInCaseInsensitiveMode();
89     this.table_query = table_query;
90   }
91
92   /**
93    * Returns the given name of the table. For example, if the Part table is
94    * aliased as P this returns P. If there is no given name, returns the
95    * root table name.
96    */

97   public TableName getGivenTableName() {
98     return table_name;
99   }
100
101   /**
102    * Returns the root name of the table. This TableName can always be used as
103    * a direct reference to a table in the database.
104    */

105   public TableName getRootTableName() {
106     return root_name;
107   }
108
109   /**
110    * Creates a QueryPlanNode to be added into a query tree that fetches the
111    * table source.
112    */

113   public QueryPlanNode createFetchQueryPlanNode() {
114     return table_query.getQueryPlanNode();
115   }
116   
117   /**
118    * Toggle the case sensitivity flag.
119    */

120   public void setCaseInsensitive(boolean status) {
121     case_insensitive = status;
122   }
123
124   private boolean stringCompare(String JavaDoc str1, String JavaDoc str2) {
125     if (!case_insensitive) {
126       return str1.equals(str2);
127     }
128     return str1.equalsIgnoreCase(str2);
129   }
130
131
132   // ---------- Implemented from FromTableInterface ----------
133

134   public String JavaDoc getUniqueName() {
135     return unique_name;
136   }
137
138   public boolean matchesReference(String JavaDoc catalog,
139                                   String JavaDoc schema, String JavaDoc table) {
140 // System.out.println("Matches reference: " + schema + " " + table);
141
// System.out.println(table_name.getName());
142

143     // Does this table name represent the correct schema?
144
if (schema != null &&
145         !stringCompare(schema, table_name.getSchema())) {
146       // If schema is present and we can't resolve to this schema then false
147
return false;
148     }
149     if (table != null &&
150         !stringCompare(table, table_name.getName())) {
151       // If table name is present and we can't resolve to this table name
152
// then return false
153
return false;
154     }
155 // System.out.println("MATCHED!");
156
// Match was successful,
157
return true;
158   }
159
160   public int resolveColumnCount(String JavaDoc catalog, String JavaDoc schema,
161                                 String JavaDoc table, String JavaDoc column) {
162     // NOTE: With this type, we can only ever return either 1 or 0 because
163
// it's impossible to have an ambiguous reference
164

165     // NOTE: Currently 'catalog' is ignored.
166

167     // Does this table name represent the correct schema?
168
if (schema != null &&
169         !stringCompare(schema, table_name.getSchema())) {
170       // If schema is present and we can't resolve to this schema then return 0
171
return 0;
172     }
173     if (table != null &&
174         !stringCompare(table, table_name.getName())) {
175       // If table name is present and we can't resolve to this table name then
176
// return 0
177
return 0;
178     }
179
180     if (column != null) {
181       if (!case_insensitive) {
182         // Can we resolve the column in this table?
183
int i = data_table_def.fastFindColumnName(column);
184         // If i doesn't equal -1 then we've found our column
185
return i == -1 ? 0 : 1;
186       }
187       else {
188         // Case insensitive search (this is slower than case sensitive).
189
int resolve_count = 0;
190         int col_count = data_table_def.columnCount();
191         for (int i = 0; i < col_count; ++i) {
192           if (data_table_def.columnAt(i).getName().equalsIgnoreCase(column)) {
193             ++resolve_count;
194           }
195         }
196         return resolve_count;
197       }
198     }
199     else { // if (column == null)
200
// Return the column count
201
return data_table_def.columnCount();
202     }
203   }
204
205   public Variable resolveColumn(String JavaDoc catalog, String JavaDoc schema,
206                                 String JavaDoc table, String JavaDoc column) {
207
208     // Does this table name represent the correct schema?
209
if (schema != null &&
210         !stringCompare(schema, table_name.getSchema())) {
211       // If schema is present and we can't resolve to this schema
212
throw new Error JavaDoc("Incorrect schema.");
213     }
214     if (table != null &&
215         !stringCompare(table, table_name.getName())) {
216       // If table name is present and we can't resolve to this table name
217
throw new Error JavaDoc("Incorrect table.");
218     }
219
220     if (column != null) {
221       if (!case_insensitive) {
222         // Can we resolve the column in this table?
223
int i = data_table_def.fastFindColumnName(column);
224         if (i == -1) {
225           throw new Error JavaDoc("Could not resolve '" + column + "'");
226         }
227         return new Variable(table_name, column);
228       }
229       else {
230         // Case insensitive search (this is slower than case sensitive).
231
int col_count = data_table_def.columnCount();
232         for (int i = 0; i < col_count; ++i) {
233           String JavaDoc col_name = data_table_def.columnAt(i).getName();
234           if (col_name.equalsIgnoreCase(column)) {
235             return new Variable(table_name, col_name);
236           }
237         }
238         throw new Error JavaDoc("Could not resolve '" + column + "'");
239       }
240     }
241     else { // if (column == null)
242
// Return the first column in the table
243
return new Variable(table_name, data_table_def.columnAt(0).getName());
244     }
245
246   }
247
248   public Variable[] allColumns() {
249     int col_count = data_table_def.columnCount();
250     Variable[] vars = new Variable[col_count];
251     for (int i = 0; i < col_count; ++i) {
252       vars[i] = new Variable(table_name, data_table_def.columnAt(i).getName());
253     }
254     return vars;
255   }
256
257 }
258
Popular Tags