KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > catalog > types > BaseTypeIdImpl


1 /*
2
3    Derby - Class org.apache.derby.catalog.types.BaseTypeIdImpl
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.catalog.types;
23
24 import org.apache.derby.catalog.TypeDescriptor;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27 import org.apache.derby.iapi.services.io.Formatable;
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29
30 import org.apache.derby.iapi.types.DataValueDescriptor;
31 import org.apache.derby.iapi.types.TypeId;
32
33 import org.apache.derby.iapi.services.io.StreamStorable;
34
35 import org.apache.derby.iapi.services.i18n.MessageService;
36
37 import org.apache.derby.iapi.services.i18n.MessageService;
38 import org.apache.derby.iapi.reference.SQLState;
39
40 import org.apache.derby.iapi.services.info.JVMInfo;
41
42 import java.sql.Types JavaDoc;
43 import org.apache.derby.iapi.reference.JDBC20Translation; // needed for BLOB/CLOB types
44

45 import java.io.InputStream JavaDoc;
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ObjectOutput JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.EOFException JavaDoc;
51
52 /**
53  * This class is the base class for all type ids that are written to the
54  * system tables.
55  */

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

72
73     String JavaDoc SQLTypeName;
74     int JDBCTypeId;
75     int formatId;
76     int wrapperTypeFormatId;
77
78     /**
79      * niladic constructor. Needed for Formatable interface to work.
80      *
81      */

82
83     public BaseTypeIdImpl() {}
84
85     /**
86      * 1 argument constructor. Needed for Formatable interface to work.
87      *
88      * @param formatId Format id of specific type id.
89      */

90     public BaseTypeIdImpl(int formatId)
91     {
92         this.formatId = formatId;
93         setTypeIdSpecificInstanceVariables();
94     }
95
96     /**
97      * Constructor for an BaseTypeIdImpl
98      *
99      * @param SQLTypeName The SQL name of the type
100      */

101
102     protected BaseTypeIdImpl(String JavaDoc SQLTypeName)
103     {
104         this.SQLTypeName = SQLTypeName;
105     }
106
107     /**
108      * Returns the SQL name of the datatype. If it is a user-defined type,
109      * it returns the full Java path name for the datatype, meaning the
110      * dot-separated path including the package names.
111      *
112      * @return A String containing the SQL name of this type.
113      */

114     public String JavaDoc getSQLTypeName()
115     {
116         return SQLTypeName;
117     }
118
119     /**
120      * Get the jdbc type id for this type. JDBC type can be
121      * found in java.sql.Types.
122      *
123      * @return a jdbc type, e.g. java.sql.Types.DECIMAL
124      *
125      * @see Types
126      */

127     public int getJDBCTypeId()
128     {
129         return JDBCTypeId;
130     }
131
132     /**
133      * Converts this TypeId, given a data type descriptor
134      * (including length/precision), to a string. E.g.
135      *
136      * VARCHAR(30)
137      *
138      *
139      * For most data types, we just return the SQL type name.
140      *
141      * @param td Data type descriptor that holds the
142      * length/precision etc. as necessary
143      *
144      * @return String version of datatype, suitable for running
145      * through the Parser.
146      */

147     public String JavaDoc toParsableString(TypeDescriptor td)
148     {
149         String JavaDoc retval = getSQLTypeName();
150
151         switch (formatId)
152         {
153           case StoredFormatIds.BIT_TYPE_ID_IMPL:
154           case StoredFormatIds.VARBIT_TYPE_ID_IMPL:
155               int rparen = retval.indexOf(')');
156               String JavaDoc lead = retval.substring(0, rparen);
157               retval = lead + td.getMaximumWidth() + retval.substring(rparen);
158               break;
159
160           case StoredFormatIds.CHAR_TYPE_ID_IMPL:
161           case StoredFormatIds.VARCHAR_TYPE_ID_IMPL:
162           case StoredFormatIds.NATIONAL_CHAR_TYPE_ID_IMPL:
163           case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID_IMPL:
164           case StoredFormatIds.BLOB_TYPE_ID_IMPL:
165           case StoredFormatIds.CLOB_TYPE_ID_IMPL:
166           case StoredFormatIds.NCLOB_TYPE_ID_IMPL:
167                 retval += "(" + td.getMaximumWidth() + ")";
168                 break;
169
170           case StoredFormatIds.DECIMAL_TYPE_ID_IMPL:
171                 retval += "(" + td.getPrecision() + "," + td.getScale() + ")";
172                 break;
173         }
174
175         return retval;
176     }
177
178     /** Does this type id represent a user type? */
179     public boolean userType()
180     {
181         return false;
182     }
183
184     /**
185      * Format this BaseTypeIdImpl as a String
186      *
187      * @return This BaseTypeIdImpl formatted as a String
188      */

189
190     public String JavaDoc toString()
191     {
192         return MessageService.getTextMessage(SQLState.TI_SQL_TYPE_NAME) +
193                 ": " + SQLTypeName;
194     }
195
196     /**
197      * we want equals to say if these are the same type id or not.
198      */

199     public boolean equals(Object JavaDoc that)
200     {
201         if (that instanceof BaseTypeIdImpl)
202         {
203             return this.SQLTypeName.equals(((BaseTypeIdImpl)that).getSQLTypeName());
204         }
205         else
206         {
207             return false;
208         }
209     }
210
211     /**
212       Hashcode which works with equals.
213       */

214     public int hashCode()
215     {
216         return this.SQLTypeName.hashCode();
217     }
218
219     /**
220      * Get the format id for the wrapper type id that corresponds to
221      * this type id.
222      */

223     public int wrapperTypeFormatId()
224     {
225         return wrapperTypeFormatId;
226     }
227
228
229     /**
230      * Get the formatID which corresponds to this class.
231      *
232      * @return the formatID of this class
233      */

234     public int getTypeFormatId()
235     {
236         return formatId;
237     }
238
239     /**
240      * Read this object from a stream of stored objects.
241      *
242      * @param in read this.
243      *
244      * @exception IOException thrown on error
245      * @exception ClassNotFoundException thrown on error
246      */

247     public void readExternal( ObjectInput JavaDoc in )
248              throws IOException JavaDoc, ClassNotFoundException JavaDoc
249     {
250         SQLTypeName = in.readUTF();
251     }
252
253     /**
254      * Write this object to a stream of stored objects.
255      *
256      * @param out write bytes here.
257      *
258      * @exception IOException thrown on error
259      */

260     public void writeExternal( ObjectOutput JavaDoc out )
261              throws IOException JavaDoc
262     {
263         out.writeUTF( SQLTypeName );
264     }
265
266     private void setTypeIdSpecificInstanceVariables()
267     {
268         switch (formatId)
269         {
270           case StoredFormatIds.BOOLEAN_TYPE_ID_IMPL:
271                 SQLTypeName = TypeId.BOOLEAN_NAME;
272                 JDBCTypeId = JVMInfo.JAVA_SQL_TYPES_BOOLEAN;
273                 wrapperTypeFormatId = StoredFormatIds.BOOLEAN_TYPE_ID;
274                 break;
275
276           case StoredFormatIds.INT_TYPE_ID_IMPL:
277                 SQLTypeName = TypeId.INTEGER_NAME;
278                 JDBCTypeId = Types.INTEGER;
279                 wrapperTypeFormatId = StoredFormatIds.INT_TYPE_ID;
280                 break;
281
282           case StoredFormatIds.SMALLINT_TYPE_ID_IMPL:
283                 SQLTypeName = TypeId.SMALLINT_NAME;
284                 JDBCTypeId = Types.SMALLINT;
285                 wrapperTypeFormatId = StoredFormatIds.SMALLINT_TYPE_ID;
286                 break;
287
288           case StoredFormatIds.TINYINT_TYPE_ID_IMPL:
289                 SQLTypeName = TypeId.TINYINT_NAME;
290                 JDBCTypeId = Types.TINYINT;
291                 wrapperTypeFormatId = StoredFormatIds.TINYINT_TYPE_ID;
292                 break;
293
294           case StoredFormatIds.LONGINT_TYPE_ID_IMPL:
295                 SQLTypeName = TypeId.LONGINT_NAME;
296                 JDBCTypeId = Types.BIGINT;
297                 wrapperTypeFormatId = StoredFormatIds.LONGINT_TYPE_ID;
298                 break;
299
300           case StoredFormatIds.DECIMAL_TYPE_ID_IMPL:
301                 SQLTypeName = TypeId.DECIMAL_NAME;
302                 JDBCTypeId = Types.DECIMAL;
303                 wrapperTypeFormatId = StoredFormatIds.DECIMAL_TYPE_ID;
304                 break;
305
306           case StoredFormatIds.DOUBLE_TYPE_ID_IMPL:
307                 SQLTypeName = TypeId.DOUBLE_NAME;
308                 JDBCTypeId = Types.DOUBLE;
309                 wrapperTypeFormatId = StoredFormatIds.DOUBLE_TYPE_ID;
310                 break;
311
312           case StoredFormatIds.REAL_TYPE_ID_IMPL:
313                 SQLTypeName = TypeId.REAL_NAME;
314                 JDBCTypeId = Types.REAL;
315                 wrapperTypeFormatId = StoredFormatIds.REAL_TYPE_ID;
316                 break;
317                 
318           case StoredFormatIds.REF_TYPE_ID_IMPL:
319                 SQLTypeName = TypeId.REF_NAME;
320                 JDBCTypeId = Types.OTHER;
321                 wrapperTypeFormatId = StoredFormatIds.REF_TYPE_ID;
322                 break;
323
324           case StoredFormatIds.CHAR_TYPE_ID_IMPL:
325                 SQLTypeName = TypeId.CHAR_NAME;
326                 JDBCTypeId = Types.CHAR;
327                 wrapperTypeFormatId = StoredFormatIds.CHAR_TYPE_ID;
328                 break;
329
330           case StoredFormatIds.VARCHAR_TYPE_ID_IMPL:
331                 SQLTypeName = TypeId.VARCHAR_NAME;
332                 JDBCTypeId = Types.VARCHAR;
333                 wrapperTypeFormatId = StoredFormatIds.VARCHAR_TYPE_ID;
334                 break;
335
336           case StoredFormatIds.LONGVARCHAR_TYPE_ID_IMPL:
337                 SQLTypeName = TypeId.LONGVARCHAR_NAME;
338                 JDBCTypeId = Types.LONGVARCHAR;
339                 wrapperTypeFormatId = StoredFormatIds.LONGVARCHAR_TYPE_ID;
340                 break;
341
342           case StoredFormatIds.CLOB_TYPE_ID_IMPL:
343                 SQLTypeName = TypeId.CLOB_NAME;
344                 JDBCTypeId = JDBC20Translation.SQL_TYPES_CLOB;
345                 wrapperTypeFormatId = StoredFormatIds.CLOB_TYPE_ID;
346                 break;
347
348           case StoredFormatIds.NATIONAL_CHAR_TYPE_ID_IMPL:
349                 SQLTypeName = TypeId.NATIONAL_CHAR_NAME;
350                 JDBCTypeId = Types.CHAR;
351                 wrapperTypeFormatId = StoredFormatIds.NATIONAL_CHAR_TYPE_ID;
352                 break;
353
354           case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID_IMPL:
355                 SQLTypeName = TypeId.NATIONAL_VARCHAR_NAME;
356                 JDBCTypeId = Types.VARCHAR;
357                 wrapperTypeFormatId = StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID;
358                 break;
359
360           case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID_IMPL:
361                 SQLTypeName = TypeId.NATIONAL_LONGVARCHAR_NAME;
362                 JDBCTypeId = Types.LONGVARCHAR;
363                 wrapperTypeFormatId = StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID;
364                 break;
365
366          case StoredFormatIds.NCLOB_TYPE_ID_IMPL:
367                 SQLTypeName = TypeId.NCLOB_NAME;
368                 JDBCTypeId = JDBC20Translation.SQL_TYPES_CLOB;
369                 wrapperTypeFormatId = StoredFormatIds.NCLOB_TYPE_ID;
370                 break;
371
372           case StoredFormatIds.BIT_TYPE_ID_IMPL:
373                 SQLTypeName = TypeId.BIT_NAME;
374                 JDBCTypeId = Types.BINARY;
375                 wrapperTypeFormatId = StoredFormatIds.BIT_TYPE_ID;
376                 break;
377
378           case StoredFormatIds.VARBIT_TYPE_ID_IMPL:
379                 SQLTypeName = TypeId.VARBIT_NAME;
380                 JDBCTypeId = Types.VARBINARY;
381                 wrapperTypeFormatId = StoredFormatIds.VARBIT_TYPE_ID;
382                 break;
383
384           case StoredFormatIds.LONGVARBIT_TYPE_ID_IMPL:
385                 SQLTypeName = TypeId.LONGVARBIT_NAME;
386                 JDBCTypeId = Types.LONGVARBINARY;
387                 wrapperTypeFormatId = StoredFormatIds.LONGVARBIT_TYPE_ID;
388                 break;
389
390           case StoredFormatIds.BLOB_TYPE_ID_IMPL:
391                 SQLTypeName = TypeId.BLOB_NAME;
392                 JDBCTypeId = JDBC20Translation.SQL_TYPES_BLOB;
393                 wrapperTypeFormatId = StoredFormatIds.BLOB_TYPE_ID;
394                 break;
395
396           case StoredFormatIds.DATE_TYPE_ID_IMPL:
397                 SQLTypeName = TypeId.DATE_NAME;
398                 JDBCTypeId = Types.DATE;
399                 wrapperTypeFormatId = StoredFormatIds.DATE_TYPE_ID;
400                 break;
401
402           case StoredFormatIds.TIME_TYPE_ID_IMPL:
403                 SQLTypeName = TypeId.TIME_NAME;
404                 JDBCTypeId = Types.TIME;
405                 wrapperTypeFormatId = StoredFormatIds.TIME_TYPE_ID;
406                 break;
407
408           case StoredFormatIds.TIMESTAMP_TYPE_ID_IMPL:
409                 SQLTypeName = TypeId.TIMESTAMP_NAME;
410                 JDBCTypeId = Types.TIMESTAMP;
411                 wrapperTypeFormatId = StoredFormatIds.TIMESTAMP_TYPE_ID;
412                 break;
413
414           case StoredFormatIds.XML_TYPE_ID_IMPL:
415                 SQLTypeName = TypeId.XML_NAME;
416                 // RESOLVE: There isn't a JDBC type for XML, so we
417
// just use our internal type. Is it okay to do this,
418
// or should "JDBCTypeId" be renamed since it no longer
419
// just holds JDBC types?
420
JDBCTypeId = StoredFormatIds.XML_TYPE_ID;
421                 wrapperTypeFormatId = StoredFormatIds.XML_TYPE_ID;
422                 break;
423
424           default:
425                 if (SanityManager.DEBUG)
426                 {
427                         SanityManager.THROWASSERT("Unexpected formatId " + formatId);
428                 }
429                 break;
430         }
431     }
432 }
433
Popular Tags