001: /*
002:
003: Derby - Class org.apache.derby.iapi.sql.dictionary.ColumnDescriptor
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.iapi.sql.dictionary;
023:
024: import org.apache.derby.iapi.types.DataTypeDescriptor;
025: import org.apache.derby.iapi.types.DataValueDescriptor;
026:
027: import org.apache.derby.iapi.reference.SQLState;
028: import org.apache.derby.iapi.services.sanity.SanityManager;
029: import org.apache.derby.iapi.sql.StatementType;
030:
031: import org.apache.derby.catalog.DefaultInfo;
032: import org.apache.derby.catalog.UUID;
033:
034: import org.apache.derby.impl.sql.compile.ColumnDefinitionNode;
035:
036: /**
037: * This class represents a column descriptor.
038: *
039: * public methods in this class are:
040: * <ol>
041: * <li>long getAutoincStart()</li>
042: * <li>java.lang.String getColumnName()</li>
043: * <li>DefaultDescriptor getDefaultDescriptor(DataDictionary dd)</li>
044: * <li>DefaultInfo getDefaultInfo</li>
045: * <li>UUID getDefaultUUID</li>
046: * <li>DataValueDescriptor getDefaultValue</li>
047: * <li>int getPosition()</li>
048: * <li>UUID getReferencingUUID()</li>
049: * <li>TableDescriptor getTableDescriptor</li>
050: * <li>DTD getType()</li>
051: * <li>hasNonNullDefault</li>
052: * <li>isAutoincrement</li>
053: * <li>setColumnName</li>
054: * <li>setPosition</li>
055: *</ol>
056: * @author Jeff Lichtman
057: */
058:
059: public final class ColumnDescriptor extends TupleDescriptor {
060:
061: // implementation
062: private DefaultInfo columnDefaultInfo;
063: private TableDescriptor table;
064: private String columnName;
065: private int columnPosition;
066: private DataTypeDescriptor columnType;
067: private DataValueDescriptor columnDefault;
068: private UUID uuid;
069: private UUID defaultUUID;
070: private long autoincStart;
071: private long autoincInc;
072: //Following variable is used to see if the user is adding an autoincrement
073: //column, or if user is altering the existing autoincrement column to change
074: //the increment value or to change the start value. If none of the above,
075: //then it will be set to -1
076: long autoinc_create_or_modify_Start_Increment = -1;
077:
078: /**
079: * Constructor for a ColumnDescriptor when the column involved
080: * is an autoincrement column. The last parameter to this method
081: * indicates if an autoincrement column is getting added or if
082: * the autoincrement column is being modified to change the
083: * increment value or to change the start value
084: *
085: * @param columnName The name of the column
086: * @param columnPosition The ordinal position of the column
087: * @param columnType A DataTypeDescriptor for the type of
088: * the column
089: * @param columnDefault A DataValueDescriptor representing the
090: * default value of the column, if any
091: * (null if no default)
092: * @param columnDefaultInfo The default info for the column.
093: * @param table A TableDescriptor for the table the
094: * column is in
095: * @param defaultUUID The UUID for the default, if any.
096: * @param autoincStart Start value for an autoincrement column.
097: * @param autoincInc Increment for autoincrement column
098: * @param userChangedWhat Adding an autoincrement column OR
099: * changing increment value or start value of
100: * the autoincrement column.
101: */
102:
103: public ColumnDescriptor(String columnName, int columnPosition,
104: DataTypeDescriptor columnType,
105: DataValueDescriptor columnDefault,
106: DefaultInfo columnDefaultInfo, TableDescriptor table,
107: UUID defaultUUID, long autoincStart, long autoincInc,
108: long userChangedWhat) {
109: this (columnName, columnPosition, columnType, columnDefault,
110: columnDefaultInfo, table, defaultUUID, autoincStart,
111: autoincInc);
112: autoinc_create_or_modify_Start_Increment = userChangedWhat;
113: }
114:
115: /**
116: * Constructor for a ColumnDescriptor
117: *
118: * @param columnName The name of the column
119: * @param columnPosition The ordinal position of the column
120: * @param columnType A DataTypeDescriptor for the type of
121: * the column
122: * @param columnDefault A DataValueDescriptor representing the
123: * default value of the column, if any
124: * (null if no default)
125: * @param columnDefaultInfo The default info for the column.
126: * @param table A TableDescriptor for the table the
127: * column is in
128: * @param defaultUUID The UUID for the default, if any.
129: * @param autoincStart Start value for an autoincrement column.
130: * @param autoincInc Increment for autoincrement column
131: */
132:
133: public ColumnDescriptor(String columnName, int columnPosition,
134: DataTypeDescriptor columnType,
135: DataValueDescriptor columnDefault,
136: DefaultInfo columnDefaultInfo, TableDescriptor table,
137: UUID defaultUUID, long autoincStart, long autoincInc) {
138: this .columnName = columnName;
139: this .columnPosition = columnPosition;
140: this .columnType = columnType;
141: this .columnDefault = columnDefault;
142: this .columnDefaultInfo = columnDefaultInfo;
143: this .defaultUUID = defaultUUID;
144: if (table != null) {
145: this .table = table;
146: this .uuid = table.getUUID();
147: }
148:
149: assertAutoinc(autoincInc != 0, autoincInc, columnDefaultInfo);
150:
151: this .autoincStart = autoincStart;
152: this .autoincInc = autoincInc;
153:
154: }
155:
156: /**
157: * Constructor for a ColumnDescriptor. Used when
158: * columnDescriptor doesn't know/care about a table
159: * descriptor.
160: *
161: * @param columnName The name of the column
162: * @param columnPosition The ordinal position of the column
163: * @param columnType A DataTypeDescriptor for the type of
164: * the column
165: * @param columnDefault A DataValueDescriptor representing the
166: * default value of the column, if any
167: * (null if no default)
168: * @param columnDefaultInfo The default info for the column.
169: * @param uuid A uuid for the object that this column
170: * is in.
171: * @param defaultUUID The UUID for the default, if any.
172: * @param autoincStart Start value for an autoincrement column.
173: * @param autoincInc Increment for autoincrement column
174: */
175: public ColumnDescriptor(String columnName, int columnPosition,
176: DataTypeDescriptor columnType,
177: DataValueDescriptor columnDefault,
178: DefaultInfo columnDefaultInfo, UUID uuid, UUID defaultUUID,
179: long autoincStart, long autoincInc)
180:
181: {
182: this .columnName = columnName;
183: this .columnPosition = columnPosition;
184: this .columnType = columnType;
185: this .columnDefault = columnDefault;
186: this .columnDefaultInfo = columnDefaultInfo;
187: this .uuid = uuid;
188: this .defaultUUID = defaultUUID;
189:
190: assertAutoinc(autoincInc != 0, autoincInc, columnDefaultInfo);
191:
192: this .autoincStart = autoincStart;
193: this .autoincInc = autoincInc;
194: }
195:
196: /**
197: * Get the UUID of the object the column is a part of.
198: *
199: * @return The UUID of the table the column is a part of.
200: */
201: public UUID getReferencingUUID() {
202: return uuid;
203: }
204:
205: /**
206: * Get the TableDescriptor of the column's table.
207: *
208: * @return The TableDescriptor of the column's table.
209: */
210: public TableDescriptor getTableDescriptor() {
211: return table;
212: }
213:
214: /**
215: * Get the name of the column.
216: *
217: * @return A String containing the name of the column.
218: */
219: public String getColumnName() {
220: return columnName;
221: }
222:
223: /**
224: * Sets the column name in case of rename column.
225: *
226: * @param newColumnName The new column name.
227: */
228: public void setColumnName(String newColumnName) {
229: this .columnName = newColumnName;
230: }
231:
232: /**
233: * Sets the table descriptor for the column.
234: *
235: * @param tableDescriptor The table descriptor for this column
236: */
237: public void setTableDescriptor(TableDescriptor tableDescriptor) {
238: this .table = tableDescriptor;
239: }
240:
241: /**
242: * Get the ordinal position of the column (1 based)
243: *
244: * @return The ordinal position of the column.
245: */
246: public int getPosition() {
247: return columnPosition;
248: }
249:
250: /**
251: * Get the TypeDescriptor of the column's datatype.
252: *
253: * @return The TypeDescriptor of the column's datatype.
254: */
255: public DataTypeDescriptor getType() {
256: return columnType;
257: }
258:
259: /**
260: * Return whether or not there is a non-null default on this column.
261: *
262: * @return Whether or not there is a non-null default on this column.
263: */
264: public boolean hasNonNullDefault() {
265: if (columnDefault != null && !columnDefault.isNull()) {
266: return true;
267: }
268:
269: return columnDefaultInfo != null;
270: }
271:
272: /**
273: * Get the default value for the column. For columns with primitive
274: * types, the object returned will be of the corresponding object type.
275: * For example, for a float column, getDefaultValue() will return
276: * a Float.
277: *
278: * @return An object with the value and type of the default value
279: * for the column. Returns NULL if there is no default.
280: */
281: public DataValueDescriptor getDefaultValue() {
282: return columnDefault;
283: }
284:
285: /**
286: * Get the DefaultInfo for this ColumnDescriptor.
287: *
288: * @return The DefaultInfo for this ColumnDescriptor.
289: */
290: public DefaultInfo getDefaultInfo() {
291: return columnDefaultInfo;
292: }
293:
294: /**
295: * Get the UUID for the column default, if any.
296: *
297: * @return The UUID for the column default, if any.
298: */
299: public UUID getDefaultUUID() {
300: return defaultUUID;
301: }
302:
303: /**
304: * Get a DefaultDescriptor for the default, if any, associated with this column.
305: *
306: * @param dd The DataDictionary.
307: *
308: * @return A DefaultDescriptor if this column has a column default.
309: */
310: public DefaultDescriptor getDefaultDescriptor(DataDictionary dd) {
311: DefaultDescriptor defaultDescriptor = null;
312:
313: if (defaultUUID != null) {
314: defaultDescriptor = new DefaultDescriptor(dd, defaultUUID,
315: uuid, columnPosition);
316: }
317:
318: return defaultDescriptor;
319: }
320:
321: /**
322: * Is this column an autoincrement column?
323: *
324: * @return Whether or not this is an autoincrement column
325: */
326: public boolean isAutoincrement() {
327: return (autoincInc != 0);
328: }
329:
330: public boolean updatableByCursor() {
331: return false;
332: }
333:
334: /**
335: * Is this column to have autoincremented value always ?
336: */
337: public boolean isAutoincAlways() {
338: return (columnDefaultInfo == null) && isAutoincrement();
339: }
340:
341: /**
342: * Get the start value of an autoincrement column
343: *
344: * @return Get the start value of an autoincrement column
345: */
346: public long getAutoincStart() {
347: return autoincStart;
348: }
349:
350: /**
351: * Get the Increment value given by the user for an autoincrement column
352: *
353: * @return the Increment value for an autoincrement column
354: */
355: public long getAutoincInc() {
356: return autoincInc;
357: }
358:
359: public long getAutoinc_create_or_modify_Start_Increment() {
360: return autoinc_create_or_modify_Start_Increment;
361: }
362:
363: /**
364: * Set the ordinal position of the column.
365: */
366: public void setPosition(int columnPosition) {
367: this .columnPosition = columnPosition;
368: }
369:
370: /**
371: * Convert the ColumnDescriptor to a String.
372: *
373: * @return A String representation of this ColumnDescriptor
374: */
375:
376: public String toString() {
377: if (SanityManager.DEBUG) {
378: /*
379: ** NOTE: This does not format table, because table.toString()
380: ** formats columns, leading to infinite recursion.
381: */
382: return "columnName: " + columnName + "\n"
383: + "columnPosition: " + columnPosition + "\n"
384: + "columnType: " + columnType + "\n"
385: + "columnDefault: " + columnDefault + "\n"
386: + "uuid: " + uuid + "\n" + "defaultUUID: "
387: + defaultUUID + "\n";
388: } else {
389: return "";
390: }
391: }
392:
393: /** @see TupleDescriptor#getDescriptorName */
394: public String getDescriptorName() {
395: // try and get rid of getColumnName!
396: return columnName;
397: }
398:
399: /** @see TupleDescriptor#getDescriptorType */
400: public String getDescriptorType() {
401: return "Column";
402: }
403:
404: private static void assertAutoinc(boolean autoinc, long autoincInc,
405: DefaultInfo defaultInfo) {
406:
407: if (SanityManager.DEBUG) {
408: if (autoinc) {
409: SanityManager.ASSERT((autoincInc != 0),
410: "increment is zero for autoincrement column");
411: SanityManager
412: .ASSERT(
413: (defaultInfo == null || defaultInfo
414: .isDefaultValueAutoinc()),
415: "If column is autoinc and have defaultInfo, "
416: + "isDefaultValueAutoinc must be true.");
417: } else {
418: SanityManager
419: .ASSERT((autoincInc == 0),
420: "increment is non-zero for non-autoincrement column");
421: SanityManager
422: .ASSERT(
423: (defaultInfo == null || !defaultInfo
424: .isDefaultValueAutoinc()),
425: "If column is not autoinc and have defaultInfo, "
426: + "isDefaultValueAutoinc can not be true");
427: }
428: }
429: }
430:
431: }
|