001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.GenericColumnDescriptor
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql;
023:
024: import org.apache.derby.iapi.sql.ResultColumnDescriptor;
025: import org.apache.derby.iapi.types.DataTypeDescriptor;
026:
027: import org.apache.derby.iapi.services.sanity.SanityManager;
028:
029: import org.apache.derby.iapi.services.io.StoredFormatIds;
030: import org.apache.derby.iapi.services.io.FormatIdUtil;
031: import org.apache.derby.iapi.services.io.Formatable;
032:
033: import org.apache.derby.iapi.services.io.FormatableHashtable;
034: import org.apache.derby.iapi.services.io.FormatableIntHolder;
035:
036: import java.io.ObjectOutput;
037: import java.io.ObjectInput;
038: import java.io.IOException;
039:
040: /**
041: * This is a stripped down implementation of a column
042: * descriptor that is intended for generic use. It
043: * can be seralized and attached to plans.
044: *
045: * @author jamie
046: */
047: public final class GenericColumnDescriptor implements
048: ResultColumnDescriptor, Formatable {
049:
050: /********************************************************
051: **
052: ** This class implements Formatable. That means that it
053: ** can write itself to and from a formatted stream. If
054: ** you add more fields to this class, make sure that you
055: ** also write/read them with the writeExternal()/readExternal()
056: ** methods.
057: **
058: ** If, inbetween releases, you add more fields to this class,
059: ** then you should bump the version number emitted by the getTypeFormatId()
060: ** method.
061: **
062: ********************************************************/
063:
064: private String name;
065: private String schemaName;
066: private String tableName;
067: private int columnPos;
068: private DataTypeDescriptor type;
069: private boolean isAutoincrement;
070: private boolean updatableByCursor;
071:
072: /**
073: * Niladic constructor for Formatable
074: */
075: public GenericColumnDescriptor() {
076: }
077:
078: public GenericColumnDescriptor(String name, DataTypeDescriptor type) {
079: this .name = name;
080: this .type = type;
081: }
082:
083: /**
084: * This constructor is used to build a generic (and
085: * formatable) ColumnDescriptor. The idea is that
086: * it can be passed a ColumnDescriptor from a query
087: * tree and convert it to something that can be used
088: * anywhere.
089: *
090: * @param rcd the ResultColumnDescriptor
091: */
092: public GenericColumnDescriptor(ResultColumnDescriptor rcd) {
093: name = rcd.getName();
094: tableName = rcd.getSourceTableName();
095: schemaName = rcd.getSourceSchemaName();
096: columnPos = rcd.getColumnPosition();
097: type = rcd.getType();
098: isAutoincrement = rcd.isAutoincrement();
099: updatableByCursor = rcd.updatableByCursor();
100: }
101:
102: /**
103: * Returns a DataTypeDescriptor for the column. This DataTypeDescriptor
104: * will not represent an actual value, it will only represent the type
105: * that all values in the column will have.
106: *
107: * @return A DataTypeDescriptor describing the type of the column.
108: */
109: public DataTypeDescriptor getType() {
110: return type;
111: }
112:
113: /**
114: * Returns the name of the Column.
115: *
116: * @return A String containing the name of the column.
117: */
118: public String getName() {
119: return name;
120: }
121:
122: /**
123: * Get the name of the schema for the Column's base table, if any.
124: * Following example queries will all return APP (assuming user is in schema APP)
125: * select t.a from t
126: * select b.a from t as b
127: * select app.t.a from t
128: *
129: * @return A String containing the name of the schema of the Column's table.
130: * If the column is not in a schema (i.e. is a derived column), it returns NULL.
131: */
132: public String getSourceSchemaName() {
133: return schemaName;
134: }
135:
136: /**
137: * Get the name of the underlying(base) table this column comes from, if any.
138: * Following example queries will all return T
139: * select a from t
140: * select b.a from t as b
141: * select t.a from t
142: *
143: * @return A String containing the name of the Column's base table.
144: * If the column is not in a table (i.e. is a derived column), it returns NULL.
145: */
146: public String getSourceTableName() {
147: return tableName;
148: }
149:
150: /**
151: * Get the position of the Column.
152: * NOTE - position is 1-based.
153: *
154: * @return An int containing the position of the Column
155: * within the table.
156: */
157: public int getColumnPosition() {
158: return columnPos;
159: }
160:
161: public boolean isAutoincrement() {
162: return isAutoincrement;
163: }
164:
165: public boolean updatableByCursor() {
166: return updatableByCursor;
167: }
168:
169: //////////////////////////////////////////////
170: //
171: // FORMATABLE
172: //
173: //////////////////////////////////////////////
174: /**
175: * Write this object out
176: *
177: * @param out write bytes here
178: *
179: * @exception IOException thrown on error
180: */
181: public void writeExternal(ObjectOutput out) throws IOException {
182: FormatableHashtable fh = new FormatableHashtable();
183: fh.put("name", name);
184: fh.put("tableName", tableName);
185: fh.put("schemaName", schemaName);
186: fh.putInt("columnPos", columnPos);
187: fh.put("type", type);
188: fh.putBoolean("isAutoincrement", isAutoincrement);
189: fh.putBoolean("updatableByCursor", updatableByCursor);
190: out.writeObject(fh);
191: return;
192: }
193:
194: public void djdrcd() {
195: }
196:
197: /**
198: * Read this object from a stream of stored objects.
199: *
200: * @param in read this.
201: *
202: * @exception IOException thrown on error
203: * @exception ClassNotFoundException thrown on error
204: */
205: public void readExternal(ObjectInput in) throws IOException,
206: ClassNotFoundException {
207: FormatableHashtable fh = (FormatableHashtable) in.readObject();
208: name = (String) fh.get("name");
209: tableName = (String) fh.get("tableName");
210: schemaName = (String) fh.get("schemaName");
211: columnPos = fh.getInt("columnPos");
212: type = (DataTypeDescriptor) fh.get("type");
213: isAutoincrement = fh.getBoolean("isAutoincrement");
214: updatableByCursor = fh.getBoolean("updatableByCursor");
215: }
216:
217: /**
218: * Get the formatID which corresponds to this class.
219: *
220: * @return the formatID of this class
221: */
222: public int getTypeFormatId() {
223: return StoredFormatIds.GENERIC_COLUMN_DESCRIPTOR_V02_ID;
224: }
225:
226: public String toString() {
227: if (SanityManager.DEBUG) {
228: return "GenericColumnDescriptor\n\tname: " + name
229: + "\n\tTable: " + schemaName + "." + tableName
230: + "\n\tcolumnPos: " + columnPos + "\n\tType: "
231: + type;
232: } else {
233: return "";
234: }
235: }
236: }
|