001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.util.Vector;
047:
048: import java.sql.SQLException;
049:
050: import com.quadcap.util.Debug;
051:
052: /**
053: * TableExpression permitting individual tables in the <code>SELECT</code>
054: * statement to be "renamed" via the <code>AS</code> clause.
055: * This may be necessary
056: * for example, in cases where a table is joined to itself. The optional
057: * column names in the FROM clause can be used to rename columns; this
058: * renaming doesn't affect the names of the columns in the final result
059: * set, but rather may be used to disambiguate column names in the
060: * <code>WHERE</code> clause.</p>
061: *
062: * @author Stan Bailes
063: */
064: public class SelectFromItem extends TableExpression implements
065: Externalizable {
066: TableExpression e;
067: String asName = null;
068: Vector columns = null;
069:
070: public SelectFromItem() {
071: }
072:
073: public SelectFromItem(TableExpression e, String asName,
074: Vector columns) {
075: this .e = e;
076: this .asName = asName;
077: this .columns = columns;
078: }
079:
080: public void readExternal(ObjectInput in) throws IOException,
081: ClassNotFoundException {
082: this .e = (TableExpression) in.readObject();
083: this .asName = (String) in.readObject();
084: this .columns = (Vector) in.readObject();
085: }
086:
087: public void writeExternal(ObjectOutput out) throws IOException {
088: out.writeObject(e);
089: out.writeObject(asName);
090: out.writeObject(columns);
091: }
092:
093: public int rank() {
094: return e.rank();
095: }
096:
097: public boolean isUpdatable() {
098: return e.isUpdatable();
099: }
100:
101: public void getBaseTables(Vector v) {
102: e.getBaseTables(v);
103: }
104:
105: public void setWhere(Expression where) {
106: this .where = where;
107: e.setWhere(where);
108: }
109:
110: public Cursor getCursor(Session session, Cursor outer)
111: throws SQLException {
112: Cursor c = e.getCursor(session, outer);
113: return new RenameCursor(session, c, asName, columns);
114: }
115:
116: public String toString() {
117: StringBuffer sb = new StringBuffer(e.toString());
118: if (asName != null && asName.length() > 0) {
119: sb.append(" AS ");
120: sb.append(asName);
121: if (columns != null) {
122: sb.append('(');
123: for (int i = 0; i < columns.size(); i++) {
124: if (i > 0)
125: sb.append(',');
126: sb.append(columns.get(i));
127: }
128: sb.append(')');
129: }
130: }
131: return sb.toString();
132: }
133:
134: //#ifdef DEBUG
135: public String name() {
136: StringBuffer sb = new StringBuffer(e.name());
137: if (asName != null) {
138: sb.append(" AS ");
139: sb.append(asName);
140: }
141: return sb.toString();
142: }
143: //#endif
144: }
|