--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.sql.dictionary.ColumnDescriptor\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.sql.dictionary;\r
+\r
+import org.apache.derby.iapi.types.DataTypeDescriptor;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+\r
+import org.apache.derby.iapi.reference.SQLState;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.sql.StatementType;\r
+\r
+import org.apache.derby.catalog.DefaultInfo;\r
+import org.apache.derby.catalog.UUID;\r
+\r
+import org.apache.derby.impl.sql.compile.ColumnDefinitionNode;\r
+\r
+/**\r
+ * This class represents a column descriptor.\r
+ *\r
+ * public methods in this class are:\r
+ * <ol>\r
+ * <li>long getAutoincStart()</li>\r
+ * <li>java.lang.String getColumnName()</li>\r
+ * <li>DefaultDescriptor getDefaultDescriptor(DataDictionary dd)</li>\r
+ * <li>DefaultInfo getDefaultInfo</li>\r
+ * <li>UUID getDefaultUUID</li>\r
+ * <li>DataValueDescriptor getDefaultValue</li>\r
+ * <li>int getPosition()</li>\r
+ * <li>UUID getReferencingUUID()</li>\r
+ * <li>TableDescriptor getTableDescriptor</li>\r
+ * <li>DTD getType()</li>\r
+ * <li>hasNonNullDefault</li>\r
+ * <li>isAutoincrement</li>\r
+ * <li>setColumnName</li>\r
+ * <li>setPosition</li>\r
+ *</ol>\r
+ */\r
+\r
+public final class ColumnDescriptor extends TupleDescriptor\r
+{\r
+\r
+ // implementation\r
+ private DefaultInfo columnDefaultInfo;\r
+ private TableDescriptor table;\r
+ private String columnName;\r
+ private int columnPosition;\r
+ private DataTypeDescriptor columnType;\r
+ private DataValueDescriptor columnDefault;\r
+ private UUID uuid;\r
+ private UUID defaultUUID;\r
+ private long autoincStart;\r
+ private long autoincInc;\r
+ private long autoincValue;\r
+ //Following variable is used to see if the user is adding an autoincrement \r
+ //column, or if user is altering the existing autoincrement column to change \r
+ //the increment value or to change the start value. If none of the above,\r
+ //then it will be set to -1\r
+ long autoinc_create_or_modify_Start_Increment = -1; \r
+\r
+ /**\r
+ * Constructor for a ColumnDescriptor when the column involved\r
+ * is an autoincrement column. The last parameter to this method\r
+ * indicates if an autoincrement column is getting added or if\r
+ * the autoincrement column is being modified to change the\r
+ * increment value or to change the start value\r
+ *\r
+ * @param columnName The name of the column\r
+ * @param columnPosition The ordinal position of the column\r
+ * @param columnType A DataTypeDescriptor for the type of\r
+ * the column\r
+ * @param columnDefault A DataValueDescriptor representing the\r
+ * default value of the column, if any\r
+ * (null if no default)\r
+ * @param columnDefaultInfo The default info for the column.\r
+ * @param table A TableDescriptor for the table the\r
+ * column is in\r
+ * @param defaultUUID The UUID for the default, if any.\r
+ * @param autoincStart Start value for an autoincrement column.\r
+ * @param autoincInc Increment for autoincrement column\r
+ * @param userChangedWhat Adding an autoincrement column OR\r
+ * changing increment value or start value of\r
+ * the autoincrement column.\r
+ */\r
+\r
+ public ColumnDescriptor(String columnName, int columnPosition,\r
+ DataTypeDescriptor columnType, DataValueDescriptor columnDefault,\r
+ DefaultInfo columnDefaultInfo,\r
+ TableDescriptor table,\r
+ UUID defaultUUID, long autoincStart, long autoincInc, \r
+ long userChangedWhat)\r
+ {\r
+ this(columnName, columnPosition, columnType, columnDefault,\r
+ columnDefaultInfo, table, defaultUUID, autoincStart,\r
+ autoincInc); \r
+ autoinc_create_or_modify_Start_Increment = userChangedWhat;\r
+ }\r
+\r
+ /**\r
+ * Constructor for a ColumnDescriptor\r
+ *\r
+ * @param columnName The name of the column\r
+ * @param columnPosition The ordinal position of the column\r
+ * @param columnType A DataTypeDescriptor for the type of\r
+ * the column\r
+ * @param columnDefault A DataValueDescriptor representing the\r
+ * default value of the column, if any\r
+ * (null if no default)\r
+ * @param columnDefaultInfo The default info for the column.\r
+ * @param table A TableDescriptor for the table the\r
+ * column is in\r
+ * @param defaultUUID The UUID for the default, if any.\r
+ * @param autoincStart Start value for an autoincrement column.\r
+ * @param autoincInc Increment for autoincrement column\r
+ */\r
+\r
+ public ColumnDescriptor(String columnName, int columnPosition,\r
+ DataTypeDescriptor columnType, DataValueDescriptor columnDefault,\r
+ DefaultInfo columnDefaultInfo,\r
+ TableDescriptor table,\r
+ UUID defaultUUID, long autoincStart, long autoincInc)\r
+ {\r
+ this.columnName = columnName;\r
+ this.columnPosition = columnPosition;\r
+ this.columnType = columnType;\r
+ this.columnDefault = columnDefault;\r
+ this.columnDefaultInfo = columnDefaultInfo;\r
+ this.defaultUUID = defaultUUID;\r
+ if (table != null)\r
+ {\r
+ this.table = table;\r
+ this.uuid = table.getUUID();\r
+ }\r
+\r
+ assertAutoinc(autoincInc != 0,\r
+ autoincInc,\r
+ columnDefaultInfo);\r
+\r
+ this.autoincStart = autoincStart;\r
+ this.autoincValue = autoincStart;\r
+ this.autoincInc = autoincInc;\r
+\r
+ }\r
+\r
+ /**\r
+ * Constructor for a ColumnDescriptor. Used when\r
+ * columnDescriptor doesn't know/care about a table\r
+ * descriptor.\r
+ *\r
+ * @param columnName The name of the column\r
+ * @param columnPosition The ordinal position of the column\r
+ * @param columnType A DataTypeDescriptor for the type of\r
+ * the column\r
+ * @param columnDefault A DataValueDescriptor representing the\r
+ * default value of the column, if any\r
+ * (null if no default)\r
+ * @param columnDefaultInfo The default info for the column.\r
+ * @param uuid A uuid for the object that this column\r
+ * is in.\r
+ * @param defaultUUID The UUID for the default, if any.\r
+ * @param autoincStart Start value for an autoincrement column.\r
+ * @param autoincInc Increment for autoincrement column\r
+ * @param autoincValue Current value of the autoincrement column\r
+ */\r
+ public ColumnDescriptor(String columnName, int columnPosition,\r
+ DataTypeDescriptor columnType, DataValueDescriptor columnDefault,\r
+ DefaultInfo columnDefaultInfo,\r
+ UUID uuid,\r
+ UUID defaultUUID,\r
+ long autoincStart, long autoincInc, long autoincValue)\r
+\r
+ {\r
+ this.columnName = columnName;\r
+ this.columnPosition = columnPosition;\r
+ this.columnType = columnType;\r
+ this.columnDefault = columnDefault;\r
+ this.columnDefaultInfo = columnDefaultInfo;\r
+ this.uuid = uuid;\r
+ this.defaultUUID = defaultUUID;\r
+\r
+ assertAutoinc(autoincInc!=0,\r
+ autoincInc,\r
+ columnDefaultInfo);\r
+ \r
+ this.autoincStart = autoincStart;\r
+ this.autoincValue = autoincValue;\r
+ this.autoincInc = autoincInc;\r
+ }\r
+\r
+ /**\r
+ * Get the UUID of the object the column is a part of.\r
+ *\r
+ * @return The UUID of the table the column is a part of.\r
+ */\r
+ public UUID getReferencingUUID()\r
+ {\r
+ return uuid;\r
+ }\r
+\r
+ /**\r
+ * Get the TableDescriptor of the column's table.\r
+ *\r
+ * @return The TableDescriptor of the column's table.\r
+ */\r
+ public TableDescriptor getTableDescriptor()\r
+ {\r
+ return table;\r
+ }\r
+\r
+ /**\r
+ * Get the name of the column.\r
+ *\r
+ * @return A String containing the name of the column.\r
+ */\r
+ public String getColumnName()\r
+ {\r
+ return columnName;\r
+ }\r
+\r
+ /**\r
+ * Sets the column name in case of rename column.\r
+ *\r
+ * @param newColumnName The new column name.\r
+ */\r
+ public void setColumnName(String newColumnName)\r
+ {\r
+ this.columnName = newColumnName;\r
+ }\r
+\r
+ /**\r
+ * Sets the table descriptor for the column.\r
+ *\r
+ * @param tableDescriptor The table descriptor for this column\r
+ */\r
+ public void setTableDescriptor(TableDescriptor tableDescriptor)\r
+ {\r
+ this.table = tableDescriptor;\r
+ }\r
+\r
+ /**\r
+ * Get the ordinal position of the column (1 based)\r
+ *\r
+ * @return The ordinal position of the column.\r
+ */\r
+ public int getPosition()\r
+ {\r
+ return columnPosition;\r
+ }\r
+\r
+ /**\r
+ * Get the TypeDescriptor of the column's datatype.\r
+ *\r
+ * @return The TypeDescriptor of the column's datatype.\r
+ */\r
+ public DataTypeDescriptor getType()\r
+ {\r
+ return columnType;\r
+ }\r
+\r
+ /**\r
+ * Return whether or not there is a non-null default on this column.\r
+ *\r
+ * @return Whether or not there is a non-null default on this column.\r
+ */\r
+ public boolean hasNonNullDefault()\r
+ {\r
+ if (columnDefault != null && ! columnDefault.isNull())\r
+ {\r
+ return true;\r
+ }\r
+\r
+ return columnDefaultInfo != null;\r
+ }\r
+\r
+ /**\r
+ * Get the default value for the column. For columns with primitive\r
+ * types, the object returned will be of the corresponding object type.\r
+ * For example, for a float column, getDefaultValue() will return\r
+ * a Float.\r
+ *\r
+ * @return An object with the value and type of the default value\r
+ * for the column. Returns NULL if there is no default.\r
+ */\r
+ public DataValueDescriptor getDefaultValue()\r
+ {\r
+ return columnDefault;\r
+ }\r
+\r
+ /**\r
+ * Get the DefaultInfo for this ColumnDescriptor.\r
+ *\r
+ * @return The DefaultInfo for this ColumnDescriptor.\r
+ */\r
+ public DefaultInfo getDefaultInfo()\r
+ {\r
+ return columnDefaultInfo;\r
+ }\r
+\r
+ /**\r
+ * Get the UUID for the column default, if any.\r
+ *\r
+ * @return The UUID for the column default, if any.\r
+ */\r
+ public UUID getDefaultUUID()\r
+ {\r
+ return defaultUUID;\r
+ }\r
+\r
+ /**\r
+ * Get a DefaultDescriptor for the default, if any, associated with this column.\r
+ *\r
+ * @param dd The DataDictionary.\r
+ *\r
+ * @return A DefaultDescriptor if this column has a column default.\r
+ */\r
+ public DefaultDescriptor getDefaultDescriptor(DataDictionary dd)\r
+ {\r
+ DefaultDescriptor defaultDescriptor = null;\r
+\r
+ if (defaultUUID != null)\r
+ {\r
+ defaultDescriptor = new DefaultDescriptor(dd, defaultUUID, uuid, columnPosition);\r
+ }\r
+\r
+ return defaultDescriptor;\r
+ }\r
+\r
+ /**\r
+ * Is this column an autoincrement column?\r
+ *\r
+ * @return Whether or not this is an autoincrement column\r
+ */\r
+ public boolean isAutoincrement()\r
+ {\r
+ return (autoincInc != 0);\r
+ }\r
+ public boolean updatableByCursor()\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Is this column to have autoincremented value always ?\r
+ */\r
+ public boolean isAutoincAlways(){\r
+ return (columnDefaultInfo == null) && isAutoincrement();\r
+ }\r
+\r
+ /**\r
+ * Get the start value of an autoincrement column\r
+ * \r
+ * @return Get the start value of an autoincrement column\r
+ */\r
+ public long getAutoincStart()\r
+ {\r
+ return autoincStart;\r
+ }\r
+ \r
+ /**\r
+ * Get the Increment value given by the user for an autoincrement column\r
+ *\r
+ * @return the Increment value for an autoincrement column\r
+ */\r
+ public long getAutoincInc()\r
+ {\r
+ return autoincInc;\r
+ }\r
+\r
+ /**\r
+ * Get the current value for an autoincrement column.\r
+ *\r
+ * One case in which this is used involves dropping a column\r
+ * from a table. When ALTER TABLE DROP COLUMN runs, it drops\r
+ * the column from SYSCOLUMNS, and then must adjust the\r
+ * column positions of the other subsequent columns in the table\r
+ * to account for the removal of the dropped columns. This\r
+ * involves deleting and re-adding the column descriptors to\r
+ * SYSCOLUMNS, but during that process we must be careful to\r
+ * preserve the current value of any autoincrement column.\r
+ *\r
+ * @return the current value for an autoincrement column\r
+ */\r
+ public long getAutoincValue()\r
+ {\r
+ return autoincValue;\r
+ }\r
+\r
+ public long getAutoinc_create_or_modify_Start_Increment()\r
+ {\r
+ return autoinc_create_or_modify_Start_Increment;\r
+ }\r
+ public void setAutoinc_create_or_modify_Start_Increment(int c_or_m)\r
+ {\r
+ autoinc_create_or_modify_Start_Increment = c_or_m;\r
+ }\r
+\r
+ /**\r
+ * Set the ordinal position of the column.\r
+ */\r
+ public void setPosition(int columnPosition)\r
+ {\r
+ this.columnPosition = columnPosition;\r
+ }\r
+\r
+ /**\r
+ * Convert the ColumnDescriptor to a String.\r
+ *\r
+ * @return A String representation of this ColumnDescriptor\r
+ */\r
+\r
+ public String toString()\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ /*\r
+ ** NOTE: This does not format table, because table.toString()\r
+ ** formats columns, leading to infinite recursion.\r
+ */\r
+ return "columnName: " + columnName + "\n" +\r
+ "columnPosition: " + columnPosition + "\n" +\r
+ "columnType: " + columnType + "\n" +\r
+ "columnDefault: " + columnDefault + "\n" +\r
+ "uuid: " + uuid + "\n" +\r
+ "defaultUUID: " + defaultUUID + "\n";\r
+ }\r
+ else\r
+ {\r
+ return "";\r
+ }\r
+ }\r
+ \r
+ /** @see TupleDescriptor#getDescriptorName */\r
+ public String getDescriptorName()\r
+ {\r
+ // try and get rid of getColumnName!\r
+ return columnName;\r
+ }\r
+\r
+ /** @see TupleDescriptor#getDescriptorType */\r
+ public String getDescriptorType()\r
+ {\r
+ return "Column";\r
+ }\r
+\r
+ \r
+ private static void assertAutoinc(boolean autoinc,\r
+ long autoincInc,\r
+ DefaultInfo defaultInfo){\r
+\r
+ if (SanityManager.DEBUG) {\r
+ if (autoinc){\r
+ SanityManager.ASSERT((autoincInc != 0),\r
+ "increment is zero for autoincrement column");\r
+ SanityManager.ASSERT((defaultInfo == null ||\r
+ defaultInfo.isDefaultValueAutoinc()),\r
+ "If column is autoinc and have defaultInfo, " + \r
+ "isDefaultValueAutoinc must be true.");\r
+ }\r
+ else{\r
+ SanityManager.ASSERT((autoincInc == 0),\r
+ "increment is non-zero for non-autoincrement column");\r
+ SanityManager.ASSERT((defaultInfo == null ||\r
+ ! defaultInfo.isDefaultValueAutoinc()),\r
+ "If column is not autoinc and have defaultInfo, " + \r
+ "isDefaultValueAutoinc can not be true");\r
+ }\r
+ }\r
+ }\r
+\r
+}\r