KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.interpret.TableSelectExpression 30 Oct 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.*;
29
30 /**
31  * A container object for the a table select expression, eg.
32  * <p><pre>
33  * SELECT [columns]
34  * FROM [tables]
35  * WHERE [search_clause]
36  * GROUP BY [column]
37  * HAVING [search_clause]
38  * [composite_function] [table_select_expression]
39  * </pre><p>
40  * Note that a TableSelectExpression can be nested in the various clauses of
41  * this object.
42  *
43  * @author Tobias Downer
44  */

45
46 public final class TableSelectExpression
47             implements java.io.Serializable JavaDoc, StatementTreeObject, Cloneable JavaDoc {
48
49   static final long serialVersionUID = 6946017316981412561L;
50
51   /**
52    * True if we only search for distinct elements.
53    */

54   public boolean distinct = false;
55
56   /**
57    * The list of columns to select from.
58    * (SelectColumn)
59    */

60   public ArrayList columns = new ArrayList();
61
62   /**
63    * The from clause.
64    */

65   public FromClause from_clause = new FromClause();
66
67   /**
68    * The where clause.
69    */

70   public SearchExpression where_clause = new SearchExpression();
71
72
73
74   /**
75    * The list of columns to group by.
76    * (ByColumn)
77    */

78   public ArrayList group_by = new ArrayList();
79
80   /**
81    * The group max variable or null if no group max.
82    */

83   public Variable group_max = null;
84
85   /**
86    * The having clause.
87    */

88   public SearchExpression having_clause = new SearchExpression();
89
90
91   /**
92    * If there is a composite function this is set to the composite enumeration
93    * from CompositeTable.
94    */

95   int composite_function = -1; // (None)
96

97   /**
98    * If this is an ALL composite (no removal of duplicate rows) it is true.
99    */

100   boolean is_composite_all;
101
102   /**
103    * The composite table itself.
104    */

105   TableSelectExpression next_composite;
106
107   /**
108    * Constructor.
109    */

110   public TableSelectExpression() {
111   }
112
113   /**
114    * Chains a new composite function to this expression. For example, if
115    * this expression is a UNION ALL with another expression it would be
116    * set through this method.
117    */

118   public void chainComposite(TableSelectExpression expression,
119                              String JavaDoc composite, boolean is_all) {
120     this.next_composite = expression;
121     composite = composite.toLowerCase();
122     if (composite.equals("union")) {
123       composite_function = CompositeTable.UNION;
124     }
125     else if (composite.equals("intersect")) {
126       composite_function = CompositeTable.INTERSECT;
127     }
128     else if (composite.equals("except")) {
129       composite_function = CompositeTable.EXCEPT;
130     }
131     else {
132       throw new Error JavaDoc("Don't understand composite function '" +
133                       composite + "'");
134     }
135     is_composite_all = is_all;
136   }
137
138
139
140
141   // ---------- Implemented from StatementTreeObject ----------
142

143   /**
144    * Prepares all the expressions in the list.
145    */

146   private static void prepareAllInList(
147           List list, ExpressionPreparer preparer) throws DatabaseException {
148     for (int n = 0; n < list.size(); ++n) {
149       StatementTreeObject ob = (StatementTreeObject) list.get(n);
150       ob.prepareExpressions(preparer);
151     }
152   }
153
154
155   public void prepareExpressions(ExpressionPreparer preparer)
156                                                throws DatabaseException {
157     prepareAllInList(columns, preparer);
158     from_clause.prepareExpressions(preparer);
159     where_clause.prepareExpressions(preparer);
160     prepareAllInList(group_by, preparer);
161     having_clause.prepareExpressions(preparer);
162
163     // Go to the next chain
164
if (next_composite != null) {
165       next_composite.prepareExpressions(preparer);
166     }
167   }
168
169   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
170     TableSelectExpression v = (TableSelectExpression) super.clone();
171     if (columns != null) {
172       v.columns = (ArrayList) StatementTree.cloneSingleObject(columns);
173     }
174     if (from_clause != null) {
175       v.from_clause = (FromClause) from_clause.clone();
176     }
177     if (where_clause != null) {
178       v.where_clause = (SearchExpression) where_clause.clone();
179     }
180     if (group_by != null) {
181       v.group_by = (ArrayList) StatementTree.cloneSingleObject(group_by);
182     }
183     if (group_max != null) {
184       v.group_max = (Variable) group_max.clone();
185     }
186     if (having_clause != null) {
187       v.having_clause = (SearchExpression) having_clause.clone();
188     }
189     if (next_composite != null) {
190       v.next_composite = (TableSelectExpression) next_composite.clone();
191     }
192     return v;
193   }
194
195 }
196
Popular Tags