001: /**
002: * com.mckoi.database.interpret.FromTableDef 31 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.ExpressionPreparer;
026: import com.mckoi.database.DatabaseException;
027: import com.mckoi.database.StatementTree;
028:
029: /**
030: * Describes a single table declaration in the from clause of a table
031: * expression (SELECT).
032: *
033: * @author Tobias Downer
034: */
035:
036: public final class FromTableDef implements java.io.Serializable,
037: Cloneable {
038:
039: static final long serialVersionUID = -606852454508224625L;
040:
041: /**
042: * If this is true, then the table def represents a sub-query table.
043: * The 'getSubSelectStatement' and 'getAlias' method can be used to
044: * get the table information.
045: * <p>
046: * eg. FROM ( SELECT id, number FROM Part ) AS part_info, ....
047: */
048: private boolean subquery_table;
049:
050: /**
051: * The unique key name given to this table definition.
052: */
053: private String unique_key;
054:
055: /**
056: * The name of the table this definition references.
057: */
058: private String table_name;
059:
060: /**
061: * The alias of the table or null if no alias was defined.
062: */
063: private String table_alias;
064:
065: /**
066: * The TableSelectExpression if this is a subquery table.
067: */
068: private TableSelectExpression subselect_table;
069:
070: /**
071: * Constructs the table def. The constructs a table that is aliased under
072: * a different name.
073: */
074: public FromTableDef(String table_name, String table_alias) {
075: this .table_name = table_name;
076: this .table_alias = table_alias;
077: subselect_table = null;
078: subquery_table = false;
079: }
080:
081: /**
082: * A simple table definition (not aliased).
083: */
084: public FromTableDef(String table_name) {
085: this (table_name, null);
086: }
087:
088: /**
089: * A table that is a sub-query and given an aliased name.
090: */
091: public FromTableDef(TableSelectExpression select, String table_alias) {
092: this .subselect_table = select;
093: this .table_name = table_alias;
094: this .table_alias = table_alias;
095: subquery_table = true;
096: }
097:
098: /**
099: * A simple sub-query table definition (not aliased).
100: */
101: public FromTableDef(TableSelectExpression select) {
102: this .subselect_table = select;
103: this .table_name = null;
104: this .table_alias = null;
105: subquery_table = true;
106: }
107:
108: /**
109: * Sets the unique key.
110: */
111: public void setUniqueKey(String unique_key) {
112: this .unique_key = unique_key;
113: }
114:
115: /**
116: * Returns the name of the table.
117: */
118: public String getName() {
119: return table_name;
120: }
121:
122: /**
123: * Returns the alias for this table (or null if no alias given).
124: */
125: public String getAlias() {
126: return table_alias;
127: }
128:
129: /**
130: * Returns the unique key.
131: */
132: public String getUniqueKey() {
133: return unique_key;
134: }
135:
136: /**
137: * Returns true if this item in the FROM clause is a subquery table.
138: */
139: public boolean isSubQueryTable() {
140: return subquery_table;
141: }
142:
143: /**
144: * Returns the TableSelectExpression if this is a subquery table.
145: */
146: public TableSelectExpression getTableSelectExpression() {
147: return subselect_table;
148: }
149:
150: /**
151: * Prepares the expressions in this table def.
152: */
153: public void prepareExpressions(ExpressionPreparer preparer)
154: throws DatabaseException {
155: if (subselect_table != null) {
156: subselect_table.prepareExpressions(preparer);
157: }
158: }
159:
160: /**
161: * Clones the object (deep clone of immutable members).
162: */
163: public Object clone() throws CloneNotSupportedException {
164: FromTableDef v = (FromTableDef) super .clone();
165: if (subselect_table != null) {
166: v.subselect_table = (TableSelectExpression) subselect_table
167: .clone();
168: }
169: return v;
170: }
171:
172: }
|