KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > ColumnInfo


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.ColumnInfo
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.impl.sql.execute;
23
24 import org.apache.derby.iapi.services.io.Formatable;
25 import org.apache.derby.iapi.services.io.StoredFormatIds;
26 import org.apache.derby.iapi.services.io.FormatIdUtil;
27
28 import org.apache.derby.iapi.error.StandardException;
29
30 import org.apache.derby.iapi.types.DataTypeDescriptor;
31 import org.apache.derby.iapi.types.DataValueDescriptor;
32
33 import org.apache.derby.iapi.services.sanity.SanityManager;
34
35 import org.apache.derby.catalog.DefaultInfo;
36 import org.apache.derby.catalog.UUID;
37
38 import org.apache.derby.iapi.services.io.FormatableHashtable;
39 import org.apache.derby.iapi.services.io.FormatableIntHolder;
40 import org.apache.derby.iapi.services.io.FormatableLongHolder;
41
42 import java.io.ObjectOutput JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * This is the Column descriptor that is passed from Compilation to Execution
48  * for CREATE TABLE statements.
49  *
50  * @version 0.1
51  * @author Rick Hillegas
52  */

53
54 public class ColumnInfo implements Formatable
55 {
56     /********************************************************
57     **
58     ** This class implements Formatable. That means that it
59     ** can write itself to and from a formatted stream. If
60     ** you add more fields to this class, make sure that you
61     ** also write/read them with the writeExternal()/readExternal()
62     ** methods.
63     **
64     ** If, inbetween releases, you add more fields to this class,
65     ** then you should bump the version number emitted by the getTypeFormatId()
66     ** method.
67     **
68     ********************************************************/

69
70     public int action;
71     public String JavaDoc name;
72     public DataTypeDescriptor dataType;
73     public DefaultInfo defaultInfo;
74     public DataValueDescriptor defaultValue;
75     public UUID newDefaultUUID;
76     public UUID oldDefaultUUID;
77     // autoinc columns.
78
public long autoincStart;
79     public long autoincInc;
80     //if this is an autoincrement column, then following variable will have CREATE or
81
//MODIFY_COLUMN_DEFAULT_RESTART or MODIFY_COLUMN_DEFAULT_INCREMENT. Otherwise,
82
//this variable will be set to -1.
83
public long autoinc_create_or_modify_Start_Increment = -1;
84
85     //This indicates column is for CREATE TABLE
86
public static final int CREATE = 0;
87     public static final int DROP = 1;
88     public static final int MODIFY_COLUMN_TYPE = 2;
89     public static final int MODIFY_COLUMN_CONSTRAINT = 3;
90     public static final int MODIFY_COLUMN_CONSTRAINT_NOT_NULL = 4;
91     //This indicates column is for ALTER TABLE to change the start value of autoinc column
92
public static final int MODIFY_COLUMN_DEFAULT_RESTART = 5;
93     //This indicates column is for ALTER TABLE to change the increment value of autoinc column
94
public static final int MODIFY_COLUMN_DEFAULT_INCREMENT = 6;
95     // CONSTRUCTORS
96

97     /**
98      * Public niladic constructor. Needed for Formatable interface to work.
99      *
100      */

101     public ColumnInfo() {}
102
103     /**
104      * Make one of these puppies.
105      *
106      * @param name Column name.
107      * @param dataType Column type.
108      * @param defaultValue Column default value.
109      * @param defaultInfo Column default info.
110      * @param newDefaultUUID New UUID for default.
111      * @param oldDefaultUUID Old UUID for default.
112      * @param action Action (create, modify default, etc.)
113      * @param autoincStart Start of autoincrement values.
114      * @param autoincInc Increment of autoincrement values-- if parameter
115      * is 0, it implies that this is not an autoincrement
116      * value.
117      */

118     public ColumnInfo(
119                        String JavaDoc name,
120                        DataTypeDescriptor dataType,
121                        DataValueDescriptor defaultValue,
122                        DefaultInfo defaultInfo,
123                        UUID newDefaultUUID,
124                        UUID oldDefaultUUID,
125                        int action,
126                        long autoincStart,
127                        long autoincInc,
128                        long autoinc_create_or_modify_Start_Increment)
129     {
130         this.name = name;
131         this.dataType = dataType;
132         this.defaultValue = defaultValue;
133         this.defaultInfo = defaultInfo;
134         this.newDefaultUUID = newDefaultUUID;
135         this.oldDefaultUUID = oldDefaultUUID;
136         this.action = action;
137         this.autoincStart = autoincStart;
138         this.autoincInc = autoincInc;
139         this.autoinc_create_or_modify_Start_Increment = autoinc_create_or_modify_Start_Increment;
140     }
141
142     // Formatable methods
143

144     /**
145      * Read this object from a stream of stored objects.
146      *
147      * @param in read this.
148      *
149      * @exception IOException thrown on error
150      * @exception ClassNotFoundException thrown on error
151      */

152     public void readExternal( ObjectInput JavaDoc in )
153          throws IOException JavaDoc, ClassNotFoundException JavaDoc
154     {
155
156         FormatableLongHolder flh;
157
158         FormatableHashtable fh = (FormatableHashtable)in.readObject();
159         name = (String JavaDoc)fh.get("name");
160         dataType = (DataTypeDescriptor) fh.get("dataType");
161         defaultValue = (DataValueDescriptor)fh.get("defaultValue");
162         defaultInfo = (DefaultInfo)fh.get("defaultInfo");
163         newDefaultUUID = (UUID)fh.get("newDefaultUUID");
164         oldDefaultUUID = (UUID)fh.get("oldDefaultUUID");
165         action = fh.getInt("action");
166         
167         if (fh.get("autoincStart") != null)
168         {
169             autoincStart = fh.getLong("autoincStart");
170             autoincInc = fh.getLong("autoincInc");
171         }
172         else
173         {
174             autoincInc = autoincStart = 0;
175         }
176     }
177
178     /**
179      * Write this object to a stream of stored objects.
180      *
181      * @param out write bytes here.
182      *
183      * @exception IOException thrown on error
184      */

185     public void writeExternal( ObjectOutput JavaDoc out )
186          throws IOException JavaDoc
187     {
188         FormatableHashtable fh = new FormatableHashtable();
189         fh.put("name", name);
190         fh.put("dataType", dataType);
191         fh.put("defaultValue", defaultValue);
192         fh.put("defaultInfo", defaultInfo);
193         fh.put("newDefaultUUID", newDefaultUUID);
194         fh.put("oldDefaultUUID", oldDefaultUUID );
195         fh.putInt("action", action);
196         
197         if (autoincInc != 0)
198         {
199             // only write out autoinc values if its an autoinc column.
200
fh.putLong("autoincStart", autoincStart);
201             fh.putLong("autoincInc", autoincInc);
202         }
203         out.writeObject(fh);
204     }
205  
206     /**
207      * Get the formatID which corresponds to this class.
208      *
209      * @return the formatID of this class
210      */

211     public int getTypeFormatId() { return StoredFormatIds.COLUMN_INFO_V02_ID; }
212
213     /*
214       Object methods.
215       */

216     public String JavaDoc toString()
217     {
218         if (SanityManager.DEBUG)
219         {
220             String JavaDoc traceName;
221             String JavaDoc traceDataType;
222             String JavaDoc traceDefaultValue;
223             String JavaDoc traceDefaultInfo;
224             String JavaDoc traceNewDefaultUUID;
225             String JavaDoc traceOldDefaultUUID;
226             String JavaDoc traceAction;
227             String JavaDoc traceAI;
228             if (name == null)
229             {
230                 traceName = "name: null ";
231             }
232             else
233             {
234                 traceName = "name: "+name+" ";
235             }
236
237             if (dataType == null)
238             {
239                 traceDataType = "dataType: null ";
240             }
241             else
242             {
243                 traceDataType = "dataType: "+dataType+" ";
244             }
245
246             if (defaultValue == null)
247             {
248                 traceDefaultValue = "defaultValue: null ";
249             }
250             else
251             {
252                 traceDefaultValue = "defaultValue: "+defaultValue+" ";
253             }
254
255             if (defaultInfo == null)
256             {
257                 traceDefaultInfo = "defaultInfo: null ";
258             }
259             else
260             {
261                 traceDefaultInfo = "defaultInfo: "+defaultInfo+" ";
262             }
263
264             if (newDefaultUUID == null)
265             {
266                 traceNewDefaultUUID = "newDefaultUUID: null ";
267             }
268             else
269             {
270                 traceNewDefaultUUID = "newDefaultUUID: "+newDefaultUUID+" ";
271             }
272
273             if (oldDefaultUUID == null)
274             {
275                 traceOldDefaultUUID = "oldDefaultUUID: null ";
276             }
277             else
278             {
279                 traceOldDefaultUUID = "oldDefaultUUID: "+oldDefaultUUID+" ";
280             }
281
282             traceAction = "action: "+action+" ";
283
284             if (autoincInc != 0)
285             {
286                 traceAI = "autoincrement, start: " + autoincStart +
287                     " increment:" + autoincInc;
288             }
289             else
290             {
291                 traceAI = "NOT autoincrement";
292             }
293             return "ColumnInfo: ("+traceName+traceDataType+traceDefaultValue+
294                                traceDefaultInfo+traceNewDefaultUUID+traceOldDefaultUUID+traceAction+traceAI+")";
295         }
296         else
297         {
298             return "";
299         }
300     }
301 }
302
Popular Tags