KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.sql.FromTableSubQuerySource 21 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
29 /**
30  * An implementation of FromTableInterface that wraps around a
31  * TableSelectExpression object as a sub-query source.
32  *
33  * @author Tobias Downer
34  */

35
36 public class FromTableSubQuerySource implements FromTableInterface {
37
38   /**
39    * The wrapped object.
40    */

41   private TableSelectExpression table_expression;
42
43   /**
44    * The fully prepared TableExpressionFromSet object that is used to
45    * qualify variables in the table.
46    */

47   private TableExpressionFromSet from_set;
48
49   /**
50    * The TableName that this source is generated to (aliased name). If null,
51    * we inherit from the root set.
52    */

53   private TableName end_table_name;
54
55   /**
56    * A unique name given to this source that is used to reference it in a
57    * TableSet.
58    */

59   private String JavaDoc unique_key;
60
61   /**
62    * The list of all variable names in the resultant source.
63    */

64   private Variable[] vars;
65
66   /**
67    * Set to true if this should do case insensitive resolutions.
68    */

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

74   public FromTableSubQuerySource(DatabaseConnection connection,
75                                  String JavaDoc unique_key,
76                                  TableSelectExpression table_expression,
77                                  TableExpressionFromSet from_set,
78                                  TableName aliased_table_name) {
79     this.unique_key = unique_key;
80     this.table_expression = table_expression;
81     this.from_set = from_set;
82     this.end_table_name = aliased_table_name;
83     // Is the database case insensitive?
84
this.case_insensitive = connection.isInCaseInsensitiveMode();
85   }
86
87   /**
88    * Returns the TableSelectExpression for this sub-query.
89    */

90   TableSelectExpression getTableExpression() {
91     return table_expression;
92   }
93
94   /**
95    * Returns the TableExpressionFromSet for this sub-query.
96    */

97   TableExpressionFromSet getFromSet() {
98     return from_set;
99   }
100
101   /**
102    * Returns the aliased table name of this sub-query or null if it is left
103    * as-is.
104    */

105   TableName getAliasedName() {
106     return end_table_name;
107   }
108
109
110   /**
111    * Makes sure the 'vars' list is created correctly.
112    */

113   private void ensureVarList() {
114     if (vars == null) {
115       vars = from_set.generateResolvedVariableList();
116 // for (int i = 0; i < vars.length; ++i) {
117
// System.out.println("+ " + vars[i]);
118
// }
119
// System.out.println("0000");
120
// Are the variables aliased to a table name?
121
if (end_table_name != null) {
122         for (int i = 0; i < vars.length; ++i) {
123           vars[i].setTableName(end_table_name);
124         }
125       }
126     }
127   }
128
129   /**
130    * Returns the unique name of this source.
131    */

132   public String JavaDoc getUniqueKey() {
133     return unique_key;
134   }
135
136   /**
137    * Toggle the case sensitivity flag.
138    */

139   public void setCaseInsensitive(boolean status) {
140     case_insensitive = status;
141   }
142
143   private boolean stringCompare(String JavaDoc str1, String JavaDoc str2) {
144     if (!case_insensitive) {
145       return str1.equals(str2);
146     }
147     return str1.equalsIgnoreCase(str2);
148   }
149
150   /**
151    * If the given Variable matches the reference then this method returns
152    * true.
153    */

154   private boolean matchesVar(Variable v, String JavaDoc catalog, String JavaDoc schema,
155                                          String JavaDoc table, String JavaDoc column) {
156     TableName tn = v.getTableName();
157     String JavaDoc cn = v.getName();
158
159     if (column == null) {
160       return true;
161     }
162     if (!stringCompare(cn, column)) {
163       return false;
164     }
165
166     if (table == null) {
167       return true;
168     }
169     if (tn == null) {
170       return false;
171     }
172     String JavaDoc tname = tn.getName();
173     if (tname != null && !stringCompare(tname, table)) {
174       return false;
175     }
176
177     if (schema == null) {
178       return true;
179     }
180     String JavaDoc sname = tn.getSchema();
181     if (sname != null && !stringCompare(sname, schema)) {
182       return false;
183     }
184
185     // Currently we ignore catalog
186
return true;
187
188   }
189
190   // ---------- Implemented from FromTableInterface ----------
191

192   public String JavaDoc getUniqueName() {
193     return getUniqueKey();
194   }
195
196   public boolean matchesReference(String JavaDoc catalog,
197                                   String JavaDoc schema, String JavaDoc table) {
198     if (schema == null && table == null) {
199       return true;
200     }
201     if (end_table_name != null) {
202       String JavaDoc ts = end_table_name.getSchema();
203       String JavaDoc tt = end_table_name.getName();
204       if (schema == null) {
205         if (stringCompare(tt, table)) {
206           return true;
207         }
208       }
209       else {
210         if (stringCompare(tt, table) && stringCompare(ts, schema)) {
211           return true;
212         }
213       }
214     }
215     // No way to determine if there is a match
216
return false;
217   }
218
219   public int resolveColumnCount(String JavaDoc catalog, String JavaDoc schema,
220                                 String JavaDoc table, String JavaDoc column) {
221     ensureVarList();
222
223     if (catalog == null && schema == null && table == null && column == null) {
224       // Return the column count
225
return vars.length;
226     }
227
228     int matched_count = 0;
229     for (int i = 0; i < vars.length; ++i) {
230       Variable v = vars[i];
231       if (matchesVar(v, catalog, schema, table, column)) {
232         ++matched_count;
233       }
234     }
235
236     return matched_count;
237
238   }
239
240   public Variable resolveColumn(String JavaDoc catalog, String JavaDoc schema,
241                                 String JavaDoc table, String JavaDoc column) {
242     ensureVarList();
243
244 // System.out.println("resolveColumn: " + catalog + ", " + schema + ", " +
245
// table + ", " + column);
246

247     for (int i = 0; i < vars.length; ++i) {
248       Variable v = vars[i];
249       if (matchesVar(v, catalog, schema, table, column)) {
250 // System.out.println("Result: " + v);
251
return v;
252       }
253     }
254
255     throw new Error JavaDoc("Couldn't resolve to a column.");
256   }
257
258   public Variable[] allColumns() {
259     ensureVarList();
260     return vars;
261   }
262
263 }
264
Popular Tags