KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > GenericColumnDescriptor


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.GenericColumnDescriptor
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql;
23
24 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
25 import org.apache.derby.iapi.types.DataTypeDescriptor;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import org.apache.derby.iapi.services.io.StoredFormatIds;
30 import org.apache.derby.iapi.services.io.FormatIdUtil;
31 import org.apache.derby.iapi.services.io.Formatable;
32
33 import org.apache.derby.iapi.services.io.FormatableHashtable;
34 import org.apache.derby.iapi.services.io.FormatableIntHolder;
35
36 import java.io.ObjectOutput JavaDoc;
37 import java.io.ObjectInput JavaDoc;
38 import java.io.IOException JavaDoc;
39 /**
40  * This is a stripped down implementation of a column
41  * descriptor that is intended for generic use. It
42  * can be seralized and attached to plans.
43  *
44  * @author jamie
45  */

46 public final class GenericColumnDescriptor
47     implements ResultColumnDescriptor, Formatable
48 {
49
50     /********************************************************
51     **
52     ** This class implements Formatable. That means that it
53     ** can write itself to and from a formatted stream. If
54     ** you add more fields to this class, make sure that you
55     ** also write/read them with the writeExternal()/readExternal()
56     ** methods.
57     **
58     ** If, inbetween releases, you add more fields to this class,
59     ** then you should bump the version number emitted by the getTypeFormatId()
60     ** method.
61     **
62     ********************************************************/

63
64     private String JavaDoc name;
65     private String JavaDoc schemaName;
66     private String JavaDoc tableName;
67     private int columnPos;
68     private DataTypeDescriptor type;
69     private boolean isAutoincrement;
70     private boolean updatableByCursor;
71
72     /**
73      * Niladic constructor for Formatable
74      */

75     public GenericColumnDescriptor()
76     {
77     }
78
79     public GenericColumnDescriptor(String JavaDoc name, DataTypeDescriptor type) {
80         this.name = name;
81         this.type = type;
82     }
83
84     /**
85      * This constructor is used to build a generic (and
86      * formatable) ColumnDescriptor. The idea is that
87      * it can be passed a ColumnDescriptor from a query
88      * tree and convert it to something that can be used
89      * anywhere.
90      *
91      * @param rcd the ResultColumnDescriptor
92      */

93     public GenericColumnDescriptor(ResultColumnDescriptor rcd)
94     {
95         name = rcd.getName();
96         tableName = rcd.getSourceTableName();
97         schemaName = rcd.getSourceSchemaName();
98         columnPos = rcd.getColumnPosition();
99         type = rcd.getType();
100         isAutoincrement = rcd.isAutoincrement();
101         updatableByCursor = rcd.updatableByCursor();
102     }
103
104     /**
105      * Returns a DataTypeDescriptor for the column. This DataTypeDescriptor
106      * will not represent an actual value, it will only represent the type
107      * that all values in the column will have.
108      *
109      * @return A DataTypeDescriptor describing the type of the column.
110      */

111     public DataTypeDescriptor getType()
112     {
113         return type;
114     }
115
116     /**
117      * Returns the name of the Column.
118      *
119      * @return A String containing the name of the column.
120      */

121     public String JavaDoc getName()
122     {
123         return name;
124     }
125
126     /**
127      * Get the name of the schema for the Column's base table, if any.
128      * Following example queries will all return APP (assuming user is in schema APP)
129      * select t.a from t
130      * select b.a from t as b
131      * select app.t.a from t
132      *
133      * @return A String containing the name of the schema of the Column's table.
134      * If the column is not in a schema (i.e. is a derived column), it returns NULL.
135      */

136     public String JavaDoc getSourceSchemaName()
137     {
138         return schemaName;
139     }
140
141     /**
142      * Get the name of the underlying(base) table this column comes from, if any.
143      * Following example queries will all return T
144      * select a from t
145      * select b.a from t as b
146      * select t.a from t
147      *
148      * @return A String containing the name of the Column's base table.
149      * If the column is not in a table (i.e. is a derived column), it returns NULL.
150      */

151     public String JavaDoc getSourceTableName()
152     {
153         return tableName;
154     }
155
156     /**
157      * Get the position of the Column.
158      * NOTE - position is 1-based.
159      *
160      * @return An int containing the position of the Column
161      * within the table.
162      */

163     public int getColumnPosition()
164     {
165         return columnPos;
166     }
167
168     public boolean isAutoincrement()
169     {
170         return isAutoincrement;
171     }
172
173     public boolean updatableByCursor()
174     {
175         return updatableByCursor;
176     }
177
178     //////////////////////////////////////////////
179
//
180
// FORMATABLE
181
//
182
//////////////////////////////////////////////
183
/**
184      * Write this object out
185      *
186      * @param out write bytes here
187      *
188      * @exception IOException thrown on error
189      */

190     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
191     {
192         FormatableHashtable fh = new FormatableHashtable();
193         fh.put("name", name);
194         fh.put("tableName", tableName);
195         fh.put("schemaName", schemaName);
196         fh.putInt("columnPos", columnPos);
197         fh.put("type", type);
198         fh.putBoolean("isAutoincrement", isAutoincrement);
199         fh.putBoolean("updatableByCursor", updatableByCursor);
200         out.writeObject(fh);
201         return;
202     }
203
204     public void djdrcd() {}
205     /**
206      * Read this object from a stream of stored objects.
207      *
208      * @param in read this.
209      *
210      * @exception IOException thrown on error
211      * @exception ClassNotFoundException thrown on error
212      */

213     public void readExternal(ObjectInput JavaDoc in)
214         throws IOException JavaDoc, ClassNotFoundException JavaDoc
215     {
216         FormatableHashtable fh = (FormatableHashtable)in.readObject();
217         name = (String JavaDoc)fh.get("name");
218         tableName = (String JavaDoc)fh.get("tableName");
219         schemaName = (String JavaDoc)fh.get("schemaName");
220         columnPos = fh.getInt("columnPos");
221         type = (DataTypeDescriptor)fh.get("type");
222         isAutoincrement = fh.getBoolean("isAutoincrement");
223         updatableByCursor = fh.getBoolean("updatableByCursor");
224     }
225     
226     /**
227      * Get the formatID which corresponds to this class.
228      *
229      * @return the formatID of this class
230      */

231     public int getTypeFormatId() { return StoredFormatIds.GENERIC_COLUMN_DESCRIPTOR_V02_ID; }
232
233     public String JavaDoc toString()
234     {
235         if (SanityManager.DEBUG)
236         {
237             return "GenericColumnDescriptor\n\tname: "+name+
238                 "\n\tTable: "+schemaName+"."+tableName+
239                 "\n\tcolumnPos: "+columnPos+
240                 "\n\tType: "+type;
241         }
242         else
243         {
244             return "";
245         }
246     }
247 }
248
Popular Tags