001: /**
002: * com.mckoi.database.interpret.TableSelectExpression 30 Oct 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.interpret;
024:
025: import com.mckoi.database.*;
026: import java.util.*;
027:
028: /**
029: * A container object for the a table select expression, eg.
030: * <p><pre>
031: * SELECT [columns]
032: * FROM [tables]
033: * WHERE [search_clause]
034: * GROUP BY [column]
035: * HAVING [search_clause]
036: * [composite_function] [table_select_expression]
037: * </pre><p>
038: * Note that a TableSelectExpression can be nested in the various clauses of
039: * this object.
040: *
041: * @author Tobias Downer
042: */
043:
044: public final class TableSelectExpression implements
045: java.io.Serializable, StatementTreeObject, Cloneable {
046:
047: static final long serialVersionUID = 6946017316981412561L;
048:
049: /**
050: * True if we only search for distinct elements.
051: */
052: public boolean distinct = false;
053:
054: /**
055: * The list of columns to select from.
056: * (SelectColumn)
057: */
058: public ArrayList columns = new ArrayList();
059:
060: /**
061: * The from clause.
062: */
063: public FromClause from_clause = new FromClause();
064:
065: /**
066: * The where clause.
067: */
068: public SearchExpression where_clause = new SearchExpression();
069:
070: /**
071: * The list of columns to group by.
072: * (ByColumn)
073: */
074: public ArrayList group_by = new ArrayList();
075:
076: /**
077: * The group max variable or null if no group max.
078: */
079: public Variable group_max = null;
080:
081: /**
082: * The having clause.
083: */
084: public SearchExpression having_clause = new SearchExpression();
085:
086: /**
087: * If there is a composite function this is set to the composite enumeration
088: * from CompositeTable.
089: */
090: int composite_function = -1; // (None)
091:
092: /**
093: * If this is an ALL composite (no removal of duplicate rows) it is true.
094: */
095: boolean is_composite_all;
096:
097: /**
098: * The composite table itself.
099: */
100: TableSelectExpression next_composite;
101:
102: /**
103: * Constructor.
104: */
105: public TableSelectExpression() {
106: }
107:
108: /**
109: * Chains a new composite function to this expression. For example, if
110: * this expression is a UNION ALL with another expression it would be
111: * set through this method.
112: */
113: public void chainComposite(TableSelectExpression expression,
114: String composite, boolean is_all) {
115: this .next_composite = expression;
116: composite = composite.toLowerCase();
117: if (composite.equals("union")) {
118: composite_function = CompositeTable.UNION;
119: } else if (composite.equals("intersect")) {
120: composite_function = CompositeTable.INTERSECT;
121: } else if (composite.equals("except")) {
122: composite_function = CompositeTable.EXCEPT;
123: } else {
124: throw new Error("Don't understand composite function '"
125: + composite + "'");
126: }
127: is_composite_all = is_all;
128: }
129:
130: // ---------- Implemented from StatementTreeObject ----------
131:
132: /**
133: * Prepares all the expressions in the list.
134: */
135: private static void prepareAllInList(List list,
136: ExpressionPreparer preparer) throws DatabaseException {
137: for (int n = 0; n < list.size(); ++n) {
138: StatementTreeObject ob = (StatementTreeObject) list.get(n);
139: ob.prepareExpressions(preparer);
140: }
141: }
142:
143: public void prepareExpressions(ExpressionPreparer preparer)
144: throws DatabaseException {
145: prepareAllInList(columns, preparer);
146: from_clause.prepareExpressions(preparer);
147: where_clause.prepareExpressions(preparer);
148: prepareAllInList(group_by, preparer);
149: having_clause.prepareExpressions(preparer);
150:
151: // Go to the next chain
152: if (next_composite != null) {
153: next_composite.prepareExpressions(preparer);
154: }
155: }
156:
157: public Object clone() throws CloneNotSupportedException {
158: TableSelectExpression v = (TableSelectExpression) super .clone();
159: if (columns != null) {
160: v.columns = (ArrayList) StatementTree
161: .cloneSingleObject(columns);
162: }
163: if (from_clause != null) {
164: v.from_clause = (FromClause) from_clause.clone();
165: }
166: if (where_clause != null) {
167: v.where_clause = (SearchExpression) where_clause.clone();
168: }
169: if (group_by != null) {
170: v.group_by = (ArrayList) StatementTree
171: .cloneSingleObject(group_by);
172: }
173: if (group_max != null) {
174: v.group_max = (Variable) group_max.clone();
175: }
176: if (having_clause != null) {
177: v.having_clause = (SearchExpression) having_clause.clone();
178: }
179: if (next_composite != null) {
180: v.next_composite = (TableSelectExpression) next_composite
181: .clone();
182: }
183: return v;
184: }
185:
186: }
|