1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.sql.dictionary.ColumnDescriptor |
4 | |
5 | Copyright 1997, 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.iapi.sql.dictionary; |
22 | |
23 | import org.apache.derby.iapi.types.DataTypeDescriptor; |
24 | import org.apache.derby.iapi.types.DataValueDescriptor; |
25 | |
26 | import org.apache.derby.iapi.reference.SQLState; |
27 | import org.apache.derby.iapi.services.sanity.SanityManager; |
28 | import org.apache.derby.iapi.sql.StatementType; |
29 | |
30 | import org.apache.derby.catalog.DefaultInfo; |
31 | import org.apache.derby.catalog.UUID; |
32 | |
33 | import org.apache.derby.impl.sql.compile.ColumnDefinitionNode; |
34 | |
35 | /** |
36 | * This class represents a column descriptor. |
37 | * |
38 | * public methods in this class are: |
39 | * <ol> |
40 | * <li>long getAutoincStart()</li> |
41 | * <li>java.lang.String getColumnName()</li> |
42 | * <li>DefaultDescriptor getDefaultDescriptor(DataDictionary dd)</li> |
43 | * <li>DefaultInfo getDefaultInfo</li> |
44 | * <li>UUID getDefaultUUID</li> |
45 | * <li>DataValueDescriptor getDefaultValue</li> |
46 | * <li>int getPosition()</li> |
47 | * <li>UUID getReferencingUUID()</li> |
48 | * <li>TableDescriptor getTableDescriptor</li> |
49 | * <li>DTD getType()</li> |
50 | * <li>hasNonNullDefault</li> |
51 | * <li>isAutoincrement</li> |
52 | * <li>setColumnName</li> |
53 | * <li>setPosition</li> |
54 | *</ol> |
55 | * @author Jeff Lichtman |
56 | */ |
57 | |
58 | public class ColumnDescriptor extends TupleDescriptor |
59 | { |
60 | |
61 | // implementation |
62 | DefaultInfo columnDefaultInfo; |
63 | TableDescriptor table; |
64 | String columnName; |
65 | int columnPosition; |
66 | DataTypeDescriptor columnType; |
67 | DataValueDescriptor columnDefault; |
68 | UUID uuid; |
69 | UUID defaultUUID; |
70 | long autoincStart; |
71 | long autoincInc; |
72 | //Following variable is used to see if the user is adding an autoincrement |
73 | //column, or if user is altering the existing autoincrement column to change |
74 | //the increment value or to change the start value. If none of the above, |
75 | //then it will be set to -1 |
76 | long autoinc_create_or_modify_Start_Increment = -1; |
77 | |
78 | /** |
79 | * Constructor for a ColumnDescriptor when the column involved |
80 | * is an autoincrement column. The last parameter to this method |
81 | * indicates if an autoincrement column is getting added or if |
82 | * the autoincrement column is being modified to change the |
83 | * increment value or to change the start value |
84 | * |
85 | * @param columnName The name of the column |
86 | * @param columnPosition The ordinal position of the column |
87 | * @param columnType A DataTypeDescriptor for the type of |
88 | * the column |
89 | * @param columnDefault A DataValueDescriptor representing the |
90 | * default value of the column, if any |
91 | * (null if no default) |
92 | * @param columnDefaultInfo The default info for the column. |
93 | * @param table A TableDescriptor for the table the |
94 | * column is in |
95 | * @param defaultUUID The UUID for the default, if any. |
96 | * @param autoincStart Start value for an autoincrement column. |
97 | * @param autoincInc Increment for autoincrement column |
98 | * @param userChangedWhat Adding an autoincrement column OR |
99 | * changing increment value or start value of |
100 | * the autoincrement column. |
101 | */ |
102 | |
103 | public ColumnDescriptor(String columnName, int columnPosition, |
104 | DataTypeDescriptor columnType, DataValueDescriptor columnDefault, |
105 | DefaultInfo columnDefaultInfo, |
106 | TableDescriptor table, |
107 | UUID defaultUUID, long autoincStart, long autoincInc, |
108 | long userChangedWhat) |
109 | { |
110 | this(columnName, columnPosition, columnType, columnDefault, |
111 | columnDefaultInfo, table, defaultUUID, autoincStart, |
112 | autoincInc); |
113 | autoinc_create_or_modify_Start_Increment = userChangedWhat; |
114 | } |
115 | |
116 | /** |
117 | * Constructor for a ColumnDescriptor |
118 | * |
119 | * @param columnName The name of the column |
120 | * @param columnPosition The ordinal position of the column |
121 | * @param columnType A DataTypeDescriptor for the type of |
122 | * the column |
123 | * @param columnDefault A DataValueDescriptor representing the |
124 | * default value of the column, if any |
125 | * (null if no default) |
126 | * @param columnDefaultInfo The default info for the column. |
127 | * @param table A TableDescriptor for the table the |
128 | * column is in |
129 | * @param defaultUUID The UUID for the default, if any. |
130 | * @param autoincStart Start value for an autoincrement column. |
131 | * @param autoincInc Increment for autoincrement column |
132 | */ |
133 | |
134 | public ColumnDescriptor(String columnName, int columnPosition, |
135 | DataTypeDescriptor columnType, DataValueDescriptor columnDefault, |
136 | DefaultInfo columnDefaultInfo, |
137 | TableDescriptor table, |
138 | UUID defaultUUID, long autoincStart, long autoincInc) |
139 | { |
140 | this.columnName = columnName; |
141 | this.columnPosition = columnPosition; |
142 | this.columnType = columnType; |
143 | this.columnDefault = columnDefault; |
144 | this.columnDefaultInfo = columnDefaultInfo; |
145 | this.defaultUUID = defaultUUID; |
146 | if (table != null) |
147 | { |
148 | this.table = table; |
149 | this.uuid = table.getUUID(); |
150 | } |
151 | |
152 | assertAutoinc(autoincInc != 0, |
153 | autoincInc, |
154 | columnDefaultInfo); |
155 | |
156 | this.autoincStart = autoincStart; |
157 | this.autoincInc = autoincInc; |
158 | |
159 | } |
160 | |
161 | /** |
162 | * Constructor for a ColumnDescriptor. Used when |
163 | * columnDescriptor doesn't know/care about a table |
164 | * descriptor. |
165 | * |
166 | * @param columnName The name of the column |
167 | * @param columnPosition The ordinal position of the column |
168 | * @param columnType A DataTypeDescriptor for the type of |
169 | * the column |
170 | * @param columnDefault A DataValueDescriptor representing the |
171 | * default value of the column, if any |
172 | * (null if no default) |
173 | * @param columnDefaultInfo The default info for the column. |
174 | * @param uuid A uuid for the object that this column |
175 | * is in. |
176 | * @param defaultUUID The UUID for the default, if any. |
177 | * @param autoincStart Start value for an autoincrement column. |
178 | * @param autoincInc Increment for autoincrement column |
179 | */ |
180 | public ColumnDescriptor(String columnName, int columnPosition, |
181 | DataTypeDescriptor columnType, DataValueDescriptor columnDefault, |
182 | DefaultInfo columnDefaultInfo, |
183 | UUID uuid, |
184 | UUID defaultUUID, |
185 | long autoincStart, long autoincInc) |
186 | |
187 | { |
188 | this.columnName = columnName; |
189 | this.columnPosition = columnPosition; |
190 | this.columnType = columnType; |
191 | this.columnDefault = columnDefault; |
192 | this.columnDefaultInfo = columnDefaultInfo; |
193 | this.uuid = uuid; |
194 | this.defaultUUID = defaultUUID; |
195 | |
196 | assertAutoinc(autoincInc!=0, |
197 | autoincInc, |
198 | columnDefaultInfo); |
199 | |
200 | this.autoincStart = autoincStart; |
201 | this.autoincInc = autoincInc; |
202 | } |
203 | |
204 | /** |
205 | * Get the UUID of the object the column is a part of. |
206 | * |
207 | * @return The UUID of the table the column is a part of. |
208 | */ |
209 | public UUID getReferencingUUID() |
210 | { |
211 | return uuid; |
212 | } |
213 | |
214 | /** |
215 | * Get the TableDescriptor of the column's table. |
216 | * |
217 | * @return The TableDescriptor of the column's table. |
218 | */ |
219 | public TableDescriptor getTableDescriptor() |
220 | { |
221 | return table; |
222 | } |
223 | |
224 | /** |
225 | * Get the name of the column. |
226 | * |
227 | * @return A String containing the name of the column. |
228 | */ |
229 | public String getColumnName() |
230 | { |
231 | return columnName; |
232 | } |
233 | |
234 | /** |
235 | * Sets the column name in case of rename column. |
236 | * |
237 | * @param newColumnName The new column name. |
238 | */ |
239 | public void setColumnName(String newColumnName) |
240 | { |
241 | this.columnName = newColumnName; |
242 | } |
243 | |
244 | /** |
245 | * Sets the table descriptor for the column. |
246 | * |
247 | * @param tableDescriptor The table descriptor for this column |
248 | */ |
249 | public void setTableDescriptor(TableDescriptor tableDescriptor) |
250 | { |
251 | this.table = tableDescriptor; |
252 | } |
253 | |
254 | /** |
255 | * Get the ordinal position of the column (1 based) |
256 | * |
257 | * @return The ordinal position of the column. |
258 | */ |
259 | public int getPosition() |
260 | { |
261 | return columnPosition; |
262 | } |
263 | |
264 | /** |
265 | * Get the TypeDescriptor of the column's datatype. |
266 | * |
267 | * @return The TypeDescriptor of the column's datatype. |
268 | */ |
269 | public DataTypeDescriptor getType() |
270 | { |
271 | return columnType; |
272 | } |
273 | |
274 | /** |
275 | * Return whether or not there is a non-null default on this column. |
276 | * |
277 | * @return Whether or not there is a non-null default on this column. |
278 | */ |
279 | public boolean hasNonNullDefault() |
280 | { |
281 | if (columnDefault != null && ! columnDefault.isNull()) |
282 | { |
283 | return true; |
284 | } |
285 | |
286 | return columnDefaultInfo != null; |
287 | } |
288 | |
289 | /** |
290 | * Get the default value for the column. For columns with primitive |
291 | * types, the object returned will be of the corresponding object type. |
292 | * For example, for a float column, getDefaultValue() will return |
293 | * a Float. |
294 | * |
295 | * @return An object with the value and type of the default value |
296 | * for the column. Returns NULL if there is no default. |
297 | */ |
298 | public DataValueDescriptor getDefaultValue() |
299 | { |
300 | return columnDefault; |
301 | } |
302 | |
303 | /** |
304 | * Get the DefaultInfo for this ColumnDescriptor. |
305 | * |
306 | * @return The DefaultInfo for this ColumnDescriptor. |
307 | */ |
308 | public DefaultInfo getDefaultInfo() |
309 | { |
310 | return columnDefaultInfo; |
311 | } |
312 | |
313 | /** |
314 | * Get the UUID for the column default, if any. |
315 | * |
316 | * @return The UUID for the column default, if any. |
317 | */ |
318 | public UUID getDefaultUUID() |
319 | { |
320 | return defaultUUID; |
321 | } |
322 | |
323 | /** |
324 | * Get a DefaultDescriptor for the default, if any, associated with this column. |
325 | * |
326 | * @param dd The DataDictionary. |
327 | * |
328 | * @return A DefaultDescriptor if this column has a column default. |
329 | */ |
330 | public DefaultDescriptor getDefaultDescriptor(DataDictionary dd) |
331 | { |
332 | DefaultDescriptor defaultDescriptor = null; |
333 | |
334 | if (defaultUUID != null) |
335 | { |
336 | defaultDescriptor = new DefaultDescriptor(dd, defaultUUID, uuid, columnPosition); |
337 | } |
338 | |
339 | return defaultDescriptor; |
340 | } |
341 | |
342 | /** |
343 | * Is this column an autoincrement column? |
344 | * |
345 | * @return Whether or not this is an autoincrement column |
346 | */ |
347 | public boolean isAutoincrement() |
348 | { |
349 | return (autoincInc != 0); |
350 | } |
351 | public boolean updatableByCursor() |
352 | { |
353 | return false; |
354 | } |
355 | |
356 | /** |
357 | * Is this column to have autoincremented value always ? |
358 | */ |
359 | public boolean isAutoincAlways(){ |
360 | return (columnDefaultInfo == null) && isAutoincrement(); |
361 | } |
362 | |
363 | /** |
364 | * Get the start value of an autoincrement column |
365 | * |
366 | * @return Get the start value of an autoincrement column |
367 | */ |
368 | public long getAutoincStart() |
369 | { |
370 | return autoincStart; |
371 | } |
372 | |
373 | /** |
374 | * Get the Increment value given by the user for an autoincrement column |
375 | * |
376 | * @return the Increment value for an autoincrement column |
377 | */ |
378 | public long getAutoincInc() |
379 | { |
380 | return autoincInc; |
381 | } |
382 | |
383 | public long getAutoinc_create_or_modify_Start_Increment() |
384 | { |
385 | return autoinc_create_or_modify_Start_Increment; |
386 | } |
387 | |
388 | /** |
389 | * Set the ordinal position of the column. |
390 | */ |
391 | public void setPosition(int columnPosition) |
392 | { |
393 | this.columnPosition = columnPosition; |
394 | } |
395 | |
396 | /** |
397 | * Convert the ColumnDescriptor to a String. |
398 | * |
399 | * @return A String representation of this ColumnDescriptor |
400 | */ |
401 | |
402 | public String toString() |
403 | { |
404 | if (SanityManager.DEBUG) |
405 | { |
406 | /* |
407 | ** NOTE: This does not format table, because table.toString() |
408 | ** formats columns, leading to infinite recursion. |
409 | */ |
410 | return "columnName: " + columnName + "\n" + |
411 | "columnPosition: " + columnPosition + "\n" + |
412 | "columnType: " + columnType + "\n" + |
413 | "columnDefault: " + columnDefault + "\n" + |
414 | "uuid: " + uuid + "\n" + |
415 | "defaultUUID: " + defaultUUID + "\n"; |
416 | } |
417 | else |
418 | { |
419 | return ""; |
420 | } |
421 | } |
422 | |
423 | /** @see TupleDescriptor#getDescriptorName */ |
424 | public String getDescriptorName() |
425 | { |
426 | // try and get rid of getColumnName! |
427 | return columnName; |
428 | } |
429 | |
430 | /** @see TupleDescriptor#getDescriptorType */ |
431 | public String getDescriptorType() |
432 | { |
433 | return "Column"; |
434 | } |
435 | |
436 | |
437 | private static void assertAutoinc(boolean autoinc, |
438 | long autoincInc, |
439 | DefaultInfo defaultInfo){ |
440 | |
441 | if (SanityManager.DEBUG) { |
442 | if (autoinc){ |
443 | SanityManager.ASSERT((autoincInc != 0), |
444 | "increment is zero for autoincrement column"); |
445 | SanityManager.ASSERT((defaultInfo == null || |
446 | defaultInfo.isDefaultValueAutoinc()), |
447 | "If column is autoinc and have defaultInfo, " + |
448 | "isDefaultValueAutoinc must be true."); |
449 | } |
450 | else{ |
451 | SanityManager.ASSERT((autoincInc == 0), |
452 | "increment is non-zero for non-autoincrement column"); |
453 | SanityManager.ASSERT((defaultInfo == null || |
454 | ! defaultInfo.isDefaultValueAutoinc()), |
455 | "If column is not autoinc and have defaultInfo, " + |
456 | "isDefaultValueAutoinc can not be true"); |
457 | } |
458 | } |
459 | } |
460 | |
461 | } |