1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.sql.GenericColumnDescriptor |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.sql; |
22 | |
23 | import org.apache.derby.iapi.sql.ResultColumnDescriptor; |
24 | import org.apache.derby.iapi.types.DataTypeDescriptor; |
25 | |
26 | import org.apache.derby.iapi.services.sanity.SanityManager; |
27 | |
28 | import org.apache.derby.iapi.services.io.StoredFormatIds; |
29 | import org.apache.derby.iapi.services.io.FormatIdUtil; |
30 | import org.apache.derby.iapi.services.io.Formatable; |
31 | |
32 | import org.apache.derby.iapi.services.io.FormatableHashtable; |
33 | import org.apache.derby.iapi.services.io.FormatableIntHolder; |
34 | |
35 | import java.io.ObjectOutput; |
36 | import java.io.ObjectInput; |
37 | import java.io.IOException; |
38 | /** |
39 | * This is a stripped down implementation of a column |
40 | * descriptor that is intended for generic use. It |
41 | * can be seralized and attached to plans. |
42 | * |
43 | * @author jamie |
44 | */ |
45 | public final class GenericColumnDescriptor |
46 | implements ResultColumnDescriptor, Formatable |
47 | { |
48 | |
49 | /******************************************************** |
50 | ** |
51 | ** This class implements Formatable. That means that it |
52 | ** can write itself to and from a formatted stream. If |
53 | ** you add more fields to this class, make sure that you |
54 | ** also write/read them with the writeExternal()/readExternal() |
55 | ** methods. |
56 | ** |
57 | ** If, inbetween releases, you add more fields to this class, |
58 | ** then you should bump the version number emitted by the getTypeFormatId() |
59 | ** method. |
60 | ** |
61 | ********************************************************/ |
62 | |
63 | private String name; |
64 | private String schemaName; |
65 | private String tableName; |
66 | private int columnPos; |
67 | private DataTypeDescriptor type; |
68 | private boolean isAutoincrement; |
69 | private boolean updatableByCursor; |
70 | |
71 | /** |
72 | * Niladic constructor for Formatable |
73 | */ |
74 | public GenericColumnDescriptor() |
75 | { |
76 | } |
77 | |
78 | public GenericColumnDescriptor(String name, DataTypeDescriptor type) { |
79 | this.name = name; |
80 | this.type = type; |
81 | } |
82 | |
83 | /** |
84 | * This constructor is used to build a generic (and |
85 | * formatable) ColumnDescriptor. The idea is that |
86 | * it can be passed a ColumnDescriptor from a query |
87 | * tree and convert it to something that can be used |
88 | * anywhere. |
89 | * |
90 | * @param rcd the ResultColumnDescriptor |
91 | */ |
92 | public GenericColumnDescriptor(ResultColumnDescriptor rcd) |
93 | { |
94 | name = rcd.getName(); |
95 | tableName = rcd.getSourceTableName(); |
96 | schemaName = rcd.getSourceSchemaName(); |
97 | columnPos = rcd.getColumnPosition(); |
98 | type = rcd.getType(); |
99 | isAutoincrement = rcd.isAutoincrement(); |
100 | updatableByCursor = rcd.updatableByCursor(); |
101 | } |
102 | |
103 | /** |
104 | * Returns a DataTypeDescriptor for the column. This DataTypeDescriptor |
105 | * will not represent an actual value, it will only represent the type |
106 | * that all values in the column will have. |
107 | * |
108 | * @return A DataTypeDescriptor describing the type of the column. |
109 | */ |
110 | public DataTypeDescriptor getType() |
111 | { |
112 | return type; |
113 | } |
114 | |
115 | /** |
116 | * Returns the name of the Column. |
117 | * |
118 | * @return A String containing the name of the column. |
119 | */ |
120 | public String getName() |
121 | { |
122 | return name; |
123 | } |
124 | |
125 | /** |
126 | * Get the name of the schema for the Column's base table, if any. |
127 | * Following example queries will all return APP (assuming user is in schema APP) |
128 | * select t.a from t |
129 | * select b.a from t as b |
130 | * select app.t.a from t |
131 | * |
132 | * @return A String containing the name of the schema of the Column's table. |
133 | * If the column is not in a schema (i.e. is a derived column), it returns NULL. |
134 | */ |
135 | public String getSourceSchemaName() |
136 | { |
137 | return schemaName; |
138 | } |
139 | |
140 | /** |
141 | * Get the name of the underlying(base) table this column comes from, if any. |
142 | * Following example queries will all return T |
143 | * select a from t |
144 | * select b.a from t as b |
145 | * select t.a from t |
146 | * |
147 | * @return A String containing the name of the Column's base table. |
148 | * If the column is not in a table (i.e. is a derived column), it returns NULL. |
149 | */ |
150 | public String getSourceTableName() |
151 | { |
152 | return tableName; |
153 | } |
154 | |
155 | /** |
156 | * Get the position of the Column. |
157 | * NOTE - position is 1-based. |
158 | * |
159 | * @return An int containing the position of the Column |
160 | * within the table. |
161 | */ |
162 | public int getColumnPosition() |
163 | { |
164 | return columnPos; |
165 | } |
166 | |
167 | public boolean isAutoincrement() |
168 | { |
169 | return isAutoincrement; |
170 | } |
171 | |
172 | public boolean updatableByCursor() |
173 | { |
174 | return updatableByCursor; |
175 | } |
176 | |
177 | ////////////////////////////////////////////// |
178 | // |
179 | // FORMATABLE |
180 | // |
181 | ////////////////////////////////////////////// |
182 | /** |
183 | * Write this object out |
184 | * |
185 | * @param out write bytes here |
186 | * |
187 | * @exception IOException thrown on error |
188 | */ |
189 | public void writeExternal(ObjectOutput out) throws IOException |
190 | { |
191 | FormatableHashtable fh = new FormatableHashtable(); |
192 | fh.put("name", name); |
193 | fh.put("tableName", tableName); |
194 | fh.put("schemaName", schemaName); |
195 | fh.putInt("columnPos", columnPos); |
196 | fh.put("type", type); |
197 | fh.putBoolean("isAutoincrement", isAutoincrement); |
198 | fh.putBoolean("updatableByCursor", updatableByCursor); |
199 | out.writeObject(fh); |
200 | return; |
201 | } |
202 | |
203 | public void djdrcd() {} |
204 | /** |
205 | * Read this object from a stream of stored objects. |
206 | * |
207 | * @param in read this. |
208 | * |
209 | * @exception IOException thrown on error |
210 | * @exception ClassNotFoundException thrown on error |
211 | */ |
212 | public void readExternal(ObjectInput in) |
213 | throws IOException, ClassNotFoundException |
214 | { |
215 | FormatableHashtable fh = (FormatableHashtable)in.readObject(); |
216 | name = (String)fh.get("name"); |
217 | tableName = (String)fh.get("tableName"); |
218 | schemaName = (String)fh.get("schemaName"); |
219 | columnPos = fh.getInt("columnPos"); |
220 | type = (DataTypeDescriptor)fh.get("type"); |
221 | isAutoincrement = fh.getBoolean("isAutoincrement"); |
222 | updatableByCursor = fh.getBoolean("updatableByCursor"); |
223 | } |
224 | |
225 | /** |
226 | * Get the formatID which corresponds to this class. |
227 | * |
228 | * @return the formatID of this class |
229 | */ |
230 | public int getTypeFormatId() { return StoredFormatIds.GENERIC_COLUMN_DESCRIPTOR_V02_ID; } |
231 | |
232 | public String toString() |
233 | { |
234 | if (SanityManager.DEBUG) |
235 | { |
236 | return "GenericColumnDescriptor\n\tname: "+name+ |
237 | "\n\tTable: "+schemaName+"."+tableName+ |
238 | "\n\tcolumnPos: "+columnPos+ |
239 | "\n\tType: "+type; |
240 | } |
241 | else |
242 | { |
243 | return ""; |
244 | } |
245 | } |
246 | } |