KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.interpret.FromClause 09 Sep 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.Set JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32
33 /**
34  * A container for the From clause of a select statement. This handles
35  * the different types of joins.
36  *
37  * @author Tobias Downer
38  */

39
40 public final class FromClause
41            implements java.io.Serializable JavaDoc, StatementTreeObject, Cloneable JavaDoc {
42
43   static final long serialVersionUID = 565726601314503609L;
44
45   /**
46    * The JoiningSet object that we have created to represent the joins in this
47    * FROM clause.
48    */

49   private JoiningSet join_set = new JoiningSet();
50
51   /**
52    * A list of all FromTableDef objects in this clause in order of when they
53    * were specified.
54    */

55   private ArrayList JavaDoc def_list = new ArrayList JavaDoc();
56
57   /**
58    * A list of all table names in this from clause.
59    */

60   private ArrayList JavaDoc all_table_names = new ArrayList JavaDoc();
61
62   /**
63    * An id used for making unique names for anonymous inner selects.
64    */

65   private int table_key = 0;
66
67
68   /**
69    * Creates a new unique key string.
70    */

71   private String JavaDoc createNewKey() {
72     ++table_key;
73     return Integer.toString(table_key);
74   }
75
76
77   private void addTableDef(String JavaDoc table_name, FromTableDef def) {
78     if (table_name != null) {
79       if (all_table_names.contains(table_name)) {
80         throw new Error JavaDoc("Duplicate table name in FROM clause: " + table_name);
81       }
82       all_table_names.add(table_name);
83     }
84     // Create a new unique key for this table
85
String JavaDoc key = createNewKey();
86     def.setUniqueKey(key);
87     // Add the table key to the join set
88
join_set.addTable(new TableName(key));
89     // Add to the alias def map
90
def_list.add(def);
91   }
92
93   /**
94    * Adds a table name to this FROM clause. Note that the given name
95    * may be a dot deliminated ref such as (schema.table_name).
96    */

97   public void addTable(String JavaDoc table_name) {
98     addTableDef(table_name, new FromTableDef(table_name));
99   }
100
101   /**
102    * Adds a table name + alias to this FROM clause.
103    */

104   public void addTable(String JavaDoc table_name, String JavaDoc table_alias) {
105     addTableDef(table_alias, new FromTableDef(table_name, table_alias));
106   }
107
108   /**
109    * A generic form of a table declaration. If any parameters are 'null' it
110    * means the information is not available.
111    */

112   public void addTableDeclaration(String JavaDoc table_name,
113                                   TableSelectExpression select,
114                                   String JavaDoc table_alias) {
115     // This is an inner select in the FROM clause
116
if (table_name == null && select != null) {
117       if (table_alias == null) {
118         addTableDef(null, new FromTableDef(select));
119       }
120       else {
121         addTableDef(table_alias, new FromTableDef(select, table_alias));
122       }
123     }
124     // This is a standard table reference in the FROM clause
125
else if (table_name != null && select == null) {
126       if (table_alias == null) {
127         addTable(table_name);
128       }
129       else {
130         addTable(table_name, table_alias);
131       }
132     }
133     // Error
134
else {
135       throw new Error JavaDoc("Unvalid declaration parameters.");
136     }
137
138   }
139
140   /**
141    * Adds a Join to the from clause. 'type' must be a join type as defined
142    * in JoiningSet.
143    */

144   public void addJoin(int type) {
145 // System.out.println("Add Join: " + type);
146
join_set.addJoin(type);
147   }
148
149   /**
150    * Hack, add a joining type to the previous entry from the end. This is
151    * an artifact of how joins are parsed.
152    */

153   public void addPreviousJoin(int type, Expression on_expression) {
154     join_set.addPreviousJoin(type, on_expression);
155   }
156
157   /**
158    * Adds a Join to the from clause. 'type' must be a join type as defined
159    * in JoiningSet, and expression represents the ON condition.
160    */

161   public void addJoin(int type, Expression on_expression) {
162     join_set.addJoin(type, on_expression);
163   }
164
165   /**
166    * Returns the JoiningSet object for the FROM clause.
167    */

168   public JoiningSet getJoinSet() {
169     return join_set;
170   }
171
172   /**
173    * Returns the type of join after table 'n' in the set of tables in the
174    * from clause. Returns, JoiningSet.INNER_JOIN, JoiningSet.FULL_OUTER_JOIN,
175    * etc.
176    */

177   public int getJoinType(int n) {
178     return getJoinSet().getJoinType(n);
179   }
180
181   /**
182    * Returns the ON Expression for the type of join after table 'n' in the
183    * set.
184    */

185   public Expression getOnExpression(int n) {
186     return getJoinSet().getOnExpression(n);
187   }
188
189   /**
190    * Returns a Set of FromTableDef objects that represent all the tables
191    * that are in this from clause.
192    */

193   public Collection JavaDoc allTables() {
194     return def_list;
195   }
196
197   // Implemented from StatementTreeObject
198
public void prepareExpressions(ExpressionPreparer preparer)
199                                                   throws DatabaseException {
200     // Prepare expressions in the JoiningSet first
201
int size = join_set.getTableCount() - 1;
202     for (int i = 0; i < size; ++i) {
203       Expression exp = join_set.getOnExpression(i);
204       if (exp != null) {
205         exp.prepare(preparer);
206       }
207     }
208     // Prepare the StatementTree sub-queries in the from tables
209
for (int i = 0; i < def_list.size(); ++i) {
210       FromTableDef table_def = (FromTableDef) def_list.get(i);
211       table_def.prepareExpressions(preparer);
212     }
213
214   }
215
216   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
217     FromClause v = (FromClause) super.clone();
218     v.join_set = (JoiningSet) join_set.clone();
219     ArrayList JavaDoc cloned_def_list = new ArrayList JavaDoc(def_list.size());
220     v.def_list = cloned_def_list;
221     v.all_table_names = (ArrayList JavaDoc) all_table_names.clone();
222
223     for (int i = 0; i < def_list.size(); ++i) {
224       FromTableDef table_def = (FromTableDef) def_list.get(i);
225       cloned_def_list.add(table_def.clone());
226     }
227
228     return v;
229   }
230
231 }
232
Popular Tags