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.sql.meta.MetaCursor;
051:
052: import com.quadcap.util.Debug;
053:
054: /**
055: * Table expression implementation of <b>TABLE</b> <i>table-name</i>.
056: *
057: * @author Stan Bailes
058: */
059: public class SelectFromTable extends TableExpression implements
060: Externalizable {
061: String tableName;
062: Relation table = null;
063:
064: /**
065: * Default constructor (required for Externalizable)
066: */
067: public SelectFromTable() {
068: }
069:
070: /**
071: * Constructor from specified table name
072: */
073: public SelectFromTable(String tableName) {
074: this .tableName = tableName;
075: }
076:
077: /**
078: * We are a table expression;
079: */
080: public int rank() {
081: return 2;
082: }
083:
084: /**
085: * And to prove it, here's our cursor.
086: */
087: public Cursor getCursor(Session session, Cursor cursor)
088: throws SQLException {
089: if (table == null) {
090: try {
091: Database db = session.getDatabase();
092: table = db.getRelation(tableName);
093: } catch (IOException e) {
094: throw DbException.wrapThrowable(e);
095: }
096: if (table == null) {
097: Cursor c = MetaCursor.find(session, tableName);
098: if (c != null)
099: return c;
100:
101: throw new SQLException("No such table: " + tableName,
102: "42000");
103: }
104: }
105: return table.getCursor(session, where, null, cursor);
106: }
107:
108: /**
109: * Our cursor is even updateable; we are so cool
110: */
111: public boolean isUpdatable() {
112: return true;
113: }
114:
115: /**
116: * TableExpression.getBaseTables(): We *are* the base table!
117: */
118: public void getBaseTables(Vector v) {
119: v.addElement(tableName);
120: }
121:
122: /**
123: * Externalizable.readExternal(): Read me from a stream.
124: */
125: public void readExternal(ObjectInput in) throws IOException,
126: ClassNotFoundException {
127: this .tableName = (String) in.readObject();
128: this .table = null;
129: }
130:
131: /**
132: * Externalizable.writeExternal(): Write me to a stream.
133: */
134: public void writeExternal(ObjectOutput out) throws IOException {
135: out.writeObject(tableName);
136: }
137:
138: public String toString() {
139: return tableName;
140: }
141:
142: /**
143: * Public accessor for table name
144: */
145: public String getTableName() {
146: return tableName;
147: }
148:
149: /**
150: * Public 'setter' for table name
151: */
152: public void setTableName(String t) {
153: tableName = t;
154: }
155:
156: //#ifdef DEBUG
157: public String name() {
158: return tableName;
159: }
160: //#endif
161: }
|