KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > sql > dictionary > ColumnDescriptor


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.dictionary.ColumnDescriptor
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.iapi.sql.dictionary;
23
24 import org.apache.derby.iapi.types.DataTypeDescriptor;
25 import org.apache.derby.iapi.types.DataValueDescriptor;
26
27 import org.apache.derby.iapi.reference.SQLState;
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29 import org.apache.derby.iapi.sql.StatementType;
30
31 import org.apache.derby.catalog.DefaultInfo;
32 import org.apache.derby.catalog.UUID;
33
34 import org.apache.derby.impl.sql.compile.ColumnDefinitionNode;
35
36 /**
37  * This class represents a column descriptor.
38  *
39  * public methods in this class are:
40  * <ol>
41  * <li>long getAutoincStart()</li>
42  * <li>java.lang.String getColumnName()</li>
43  * <li>DefaultDescriptor getDefaultDescriptor(DataDictionary dd)</li>
44  * <li>DefaultInfo getDefaultInfo</li>
45  * <li>UUID getDefaultUUID</li>
46  * <li>DataValueDescriptor getDefaultValue</li>
47  * <li>int getPosition()</li>
48  * <li>UUID getReferencingUUID()</li>
49  * <li>TableDescriptor getTableDescriptor</li>
50  * <li>DTD getType()</li>
51  * <li>hasNonNullDefault</li>
52  * <li>isAutoincrement</li>
53  * <li>setColumnName</li>
54  * <li>setPosition</li>
55  *</ol>
56  * @author Jeff Lichtman
57  */

58
59 public final class ColumnDescriptor extends TupleDescriptor
60 {
61
62     // implementation
63
private DefaultInfo columnDefaultInfo;
64     private TableDescriptor table;
65     private String JavaDoc columnName;
66     private int columnPosition;
67     private DataTypeDescriptor columnType;
68     private DataValueDescriptor columnDefault;
69     private UUID uuid;
70     private UUID defaultUUID;
71     private long autoincStart;
72     private long autoincInc;
73     //Following variable is used to see if the user is adding an autoincrement
74
//column, or if user is altering the existing autoincrement column to change
75
//the increment value or to change the start value. If none of the above,
76
//then it will be set to -1
77
long autoinc_create_or_modify_Start_Increment = -1;
78
79     /**
80      * Constructor for a ColumnDescriptor when the column involved
81      * is an autoincrement column. The last parameter to this method
82      * indicates if an autoincrement column is getting added or if
83      * the autoincrement column is being modified to change the
84      * increment value or to change the start value
85      *
86      * @param columnName The name of the column
87      * @param columnPosition The ordinal position of the column
88      * @param columnType A DataTypeDescriptor for the type of
89      * the column
90      * @param columnDefault A DataValueDescriptor representing the
91      * default value of the column, if any
92      * (null if no default)
93      * @param columnDefaultInfo The default info for the column.
94      * @param table A TableDescriptor for the table the
95      * column is in
96      * @param defaultUUID The UUID for the default, if any.
97      * @param autoincStart Start value for an autoincrement column.
98      * @param autoincInc Increment for autoincrement column
99      * @param userChangedWhat Adding an autoincrement column OR
100      * changing increment value or start value of
101      * the autoincrement column.
102      */

103
104     public ColumnDescriptor(String JavaDoc columnName, int columnPosition,
105                      DataTypeDescriptor columnType, DataValueDescriptor columnDefault,
106                      DefaultInfo columnDefaultInfo,
107                      TableDescriptor table,
108                      UUID defaultUUID, long autoincStart, long autoincInc,
109                      long userChangedWhat)
110     {
111         this(columnName, columnPosition, columnType, columnDefault,
112                 columnDefaultInfo, table, defaultUUID, autoincStart,
113                 autoincInc);
114         autoinc_create_or_modify_Start_Increment = userChangedWhat;
115     }
116
117         /**
118          * Constructor for a ColumnDescriptor
119          *
120          * @param columnName The name of the column
121          * @param columnPosition The ordinal position of the column
122          * @param columnType A DataTypeDescriptor for the type of
123          * the column
124          * @param columnDefault A DataValueDescriptor representing the
125          * default value of the column, if any
126          * (null if no default)
127          * @param columnDefaultInfo The default info for the column.
128          * @param table A TableDescriptor for the table the
129          * column is in
130          * @param defaultUUID The UUID for the default, if any.
131          * @param autoincStart Start value for an autoincrement column.
132          * @param autoincInc Increment for autoincrement column
133          */

134
135         public ColumnDescriptor(String JavaDoc columnName, int columnPosition,
136                          DataTypeDescriptor columnType, DataValueDescriptor columnDefault,
137                          DefaultInfo columnDefaultInfo,
138                          TableDescriptor table,
139                          UUID defaultUUID, long autoincStart, long autoincInc)
140         {
141         this.columnName = columnName;
142         this.columnPosition = columnPosition;
143         this.columnType = columnType;
144         this.columnDefault = columnDefault;
145         this.columnDefaultInfo = columnDefaultInfo;
146         this.defaultUUID = defaultUUID;
147         if (table != null)
148         {
149             this.table = table;
150             this.uuid = table.getUUID();
151         }
152
153         assertAutoinc(autoincInc != 0,
154                   autoincInc,
155                   columnDefaultInfo);
156
157         this.autoincStart = autoincStart;
158         this.autoincInc = autoincInc;
159
160     }
161
162     /**
163      * Constructor for a ColumnDescriptor. Used when
164      * columnDescriptor doesn't know/care about a table
165      * descriptor.
166      *
167      * @param columnName The name of the column
168      * @param columnPosition The ordinal position of the column
169      * @param columnType A DataTypeDescriptor for the type of
170      * the column
171      * @param columnDefault A DataValueDescriptor representing the
172      * default value of the column, if any
173      * (null if no default)
174      * @param columnDefaultInfo The default info for the column.
175      * @param uuid A uuid for the object that this column
176      * is in.
177      * @param defaultUUID The UUID for the default, if any.
178      * @param autoincStart Start value for an autoincrement column.
179      * @param autoincInc Increment for autoincrement column
180      */

181     public ColumnDescriptor(String JavaDoc columnName, int columnPosition,
182         DataTypeDescriptor columnType, DataValueDescriptor columnDefault,
183         DefaultInfo columnDefaultInfo,
184         UUID uuid,
185         UUID defaultUUID,
186         long autoincStart, long autoincInc)
187
188     {
189         this.columnName = columnName;
190         this.columnPosition = columnPosition;
191         this.columnType = columnType;
192         this.columnDefault = columnDefault;
193         this.columnDefaultInfo = columnDefaultInfo;
194         this.uuid = uuid;
195         this.defaultUUID = defaultUUID;
196
197         assertAutoinc(autoincInc!=0,
198                   autoincInc,
199                   columnDefaultInfo);
200         
201         this.autoincStart = autoincStart;
202         this.autoincInc = autoincInc;
203     }
204
205     /**
206      * Get the UUID of the object the column is a part of.
207      *
208      * @return The UUID of the table the column is a part of.
209      */

210     public UUID getReferencingUUID()
211     {
212         return uuid;
213     }
214
215     /**
216      * Get the TableDescriptor of the column's table.
217      *
218      * @return The TableDescriptor of the column's table.
219      */

220     public TableDescriptor getTableDescriptor()
221     {
222         return table;
223     }
224
225     /**
226      * Get the name of the column.
227      *
228      * @return A String containing the name of the column.
229      */

230     public String JavaDoc getColumnName()
231     {
232         return columnName;
233     }
234
235     /**
236      * Sets the column name in case of rename column.
237      *
238      * @param newColumnName The new column name.
239      */

240     public void setColumnName(String JavaDoc newColumnName)
241     {
242         this.columnName = newColumnName;
243     }
244
245     /**
246      * Sets the table descriptor for the column.
247      *
248      * @param tableDescriptor The table descriptor for this column
249      */

250     public void setTableDescriptor(TableDescriptor tableDescriptor)
251     {
252         this.table = tableDescriptor;
253     }
254
255     /**
256      * Get the ordinal position of the column (1 based)
257      *
258      * @return The ordinal position of the column.
259      */

260     public int getPosition()
261     {
262         return columnPosition;
263     }
264
265     /**
266      * Get the TypeDescriptor of the column's datatype.
267      *
268      * @return The TypeDescriptor of the column's datatype.
269      */

270     public DataTypeDescriptor getType()
271     {
272         return columnType;
273     }
274
275     /**
276      * Return whether or not there is a non-null default on this column.
277      *
278      * @return Whether or not there is a non-null default on this column.
279      */

280     public boolean hasNonNullDefault()
281     {
282         if (columnDefault != null && ! columnDefault.isNull())
283         {
284             return true;
285         }
286
287         return columnDefaultInfo != null;
288     }
289
290     /**
291      * Get the default value for the column. For columns with primitive
292      * types, the object returned will be of the corresponding object type.
293      * For example, for a float column, getDefaultValue() will return
294      * a Float.
295      *
296      * @return An object with the value and type of the default value
297      * for the column. Returns NULL if there is no default.
298      */

299     public DataValueDescriptor getDefaultValue()
300     {
301         return columnDefault;
302     }
303
304     /**
305      * Get the DefaultInfo for this ColumnDescriptor.
306      *
307      * @return The DefaultInfo for this ColumnDescriptor.
308      */

309     public DefaultInfo getDefaultInfo()
310     {
311         return columnDefaultInfo;
312     }
313
314     /**
315      * Get the UUID for the column default, if any.
316      *
317      * @return The UUID for the column default, if any.
318      */

319     public UUID getDefaultUUID()
320     {
321         return defaultUUID;
322     }
323
324     /**
325      * Get a DefaultDescriptor for the default, if any, associated with this column.
326      *
327      * @param dd The DataDictionary.
328      *
329      * @return A DefaultDescriptor if this column has a column default.
330      */

331     public DefaultDescriptor getDefaultDescriptor(DataDictionary dd)
332     {
333         DefaultDescriptor defaultDescriptor = null;
334
335         if (defaultUUID != null)
336         {
337             defaultDescriptor = new DefaultDescriptor(dd, defaultUUID, uuid, columnPosition);
338         }
339
340         return defaultDescriptor;
341     }
342
343     /**
344      * Is this column an autoincrement column?
345      *
346      * @return Whether or not this is an autoincrement column
347      */

348     public boolean isAutoincrement()
349     {
350         return (autoincInc != 0);
351     }
352     public boolean updatableByCursor()
353     {
354         return false;
355     }
356
357     /**
358      * Is this column to have autoincremented value always ?
359      */

360     public boolean isAutoincAlways(){
361         return (columnDefaultInfo == null) && isAutoincrement();
362     }
363
364     /**
365      * Get the start value of an autoincrement column
366      *
367      * @return Get the start value of an autoincrement column
368      */

369     public long getAutoincStart()
370     {
371         return autoincStart;
372     }
373     
374     /**
375      * Get the Increment value given by the user for an autoincrement column
376      *
377      * @return the Increment value for an autoincrement column
378      */

379     public long getAutoincInc()
380     {
381         return autoincInc;
382     }
383
384     public long getAutoinc_create_or_modify_Start_Increment()
385     {
386         return autoinc_create_or_modify_Start_Increment;
387     }
388
389     /**
390      * Set the ordinal position of the column.
391      */

392     public void setPosition(int columnPosition)
393     {
394         this.columnPosition = columnPosition;
395     }
396
397     /**
398      * Convert the ColumnDescriptor to a String.
399      *
400      * @return A String representation of this ColumnDescriptor
401      */

402
403     public String JavaDoc toString()
404     {
405         if (SanityManager.DEBUG)
406         {
407             /*
408             ** NOTE: This does not format table, because table.toString()
409             ** formats columns, leading to infinite recursion.
410             */

411             return "columnName: " + columnName + "\n" +
412                 "columnPosition: " + columnPosition + "\n" +
413                 "columnType: " + columnType + "\n" +
414                 "columnDefault: " + columnDefault + "\n" +
415                 "uuid: " + uuid + "\n" +
416                 "defaultUUID: " + defaultUUID + "\n";
417         }
418         else
419         {
420             return "";
421         }
422     }
423     
424     /** @see TupleDescriptor#getDescriptorName */
425     public String JavaDoc getDescriptorName()
426     {
427         // try and get rid of getColumnName!
428
return columnName;
429     }
430
431     /** @see TupleDescriptor#getDescriptorType */
432     public String JavaDoc getDescriptorType()
433     {
434         return "Column";
435     }
436
437     
438     private static void assertAutoinc(boolean autoinc,
439                       long autoincInc,
440                       DefaultInfo defaultInfo){
441
442         if (SanityManager.DEBUG) {
443             if (autoinc){
444                 SanityManager.ASSERT((autoincInc != 0),
445                     "increment is zero for autoincrement column");
446                 SanityManager.ASSERT((defaultInfo == null ||
447                           defaultInfo.isDefaultValueAutoinc()),
448                          "If column is autoinc and have defaultInfo, " +
449                          "isDefaultValueAutoinc must be true.");
450             }
451             else{
452                 SanityManager.ASSERT((autoincInc == 0),
453                     "increment is non-zero for non-autoincrement column");
454                 SanityManager.ASSERT((defaultInfo == null ||
455                           ! defaultInfo.isDefaultValueAutoinc()),
456                          "If column is not autoinc and have defaultInfo, " +
457                          "isDefaultValueAutoinc can not be true");
458             }
459         }
460     }
461
462 }
463
Popular Tags