KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > protocol > TypeTag


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2005 Emic Networks
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Marc Herbert
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.common.protocol;
23
24 import java.io.IOException JavaDoc;
25 import java.sql.Types JavaDoc;
26
27 import org.continuent.sequoia.common.stream.DriverBufferedInputStream;
28 import org.continuent.sequoia.common.stream.DriverBufferedOutputStream;
29
30 /**
31  * This class implements protocol type tags with an internal String, but offers
32  * an abstract interface on top of it in order to be transparently substituted
33  * some day (with enums for instance).
34  * <p>
35  * Advantages of using string types is human-readability (debugging, trace
36  * analysis, etc.) and earlier detection in case of protocol corruption.
37  * Drawback maybe a small performance cost.
38  * <p>
39  * Check "the importance of being textual" - by Eric S. Raymond.
40  * http://www.faqs.org/docs/artu/ch05s01.html
41  *
42  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert </a>
43  * @version 1.0
44  */

45 public final class TypeTag
46 {
47   private int typeNumber;
48   private String JavaDoc typeName;
49
50   /** ** Actual Types *** */
51   private static final String JavaDoc TPREFIX = "T-";
52   private static int commandNumber = -1;
53
54   /* SQL "objects" */
55   /*
56    * The reference is table 47.9.3 "JDBC Types Mapped to Java Object Types" in
57    * 2nd edition (JDBC 2.1) of the book "JDBC API Tutorial and reference", or
58    * table 50.3 in the 3rd edition (JDBC 3.0). Also available online in Sun's
59    * "JDBC Technology Guide: Getting Started", section "Mapping SQL and Java
60    * Types".
61    */

62   /* unsupported are: DISTINCT, Array, Struct/SQLData, Ref, JAVA_OBJECT */
63   /* JDBC 3.0 added the type: java.net.URL, also currently unsupported */
64
65   // WARNING: ORDER MATTERS HERE since these statics are internally numbered
66
// in initialization order.
67

68   /** Constant for a SQL/Java type */
69   public static final TypeTag TYPE_ERROR = new TypeTag(
70                                                     TPREFIX
71                                                         + "Type not found or unsupported");
72   /** Constant for a SQL/Java type */
73   public static final TypeTag STRING = new TypeTag(TPREFIX
74                                                     + "String");
75   /** Constant for a SQL/Java type */
76   public static final TypeTag BIGDECIMAL = new TypeTag(TPREFIX
77                                                     + "BigDecimal");
78   /** Constant for a SQL/Java type */
79   public static final TypeTag BOOLEAN = new TypeTag(TPREFIX
80                                                     + "Boolean");
81   /** Constant for a SQL/Java type */
82   public static final TypeTag INTEGER = new TypeTag(TPREFIX
83                                                     + "Integer");
84   /** Constant for a SQL/Java type */
85   public static final TypeTag LONG = new TypeTag(TPREFIX + "Long");
86   /** Constant for a SQL/Java type */
87   public static final TypeTag FLOAT = new TypeTag(TPREFIX + "Float");
88   /** Constant for a SQL/Java type */
89   public static final TypeTag DOUBLE = new TypeTag(TPREFIX
90                                                     + "Double");
91   /** Constant for a SQL/Java type */
92   public static final TypeTag BYTE_ARRAY = new TypeTag(TPREFIX
93                                                     + "Byte[]");
94   /** Constant for a SQL/Java type */
95   public static final TypeTag SQL_DATE = new TypeTag(TPREFIX
96                                                     + "SqlDate");
97   /** Constant for a SQL/Java type */
98   public static final TypeTag SQL_TIME = new TypeTag(TPREFIX
99                                                     + "SqlTime");
100   /** Constant for a SQL/Java type */
101   public static final TypeTag SQL_TIMESTAMP = new TypeTag(TPREFIX
102                                                     + "SqlTimestamp");
103   /** Constant for a SQL/Java type */
104   public static final TypeTag CLOB = new TypeTag(TPREFIX + "Clob");
105   /** Constant for a SQL/Java type */
106   public static final TypeTag BLOB = new TypeTag(TPREFIX + "Blob");
107
108   /** Constant for a Java Serializable object */
109   public static final TypeTag JAVA_SERIALIZABLE = new TypeTag(TPREFIX
110                                                     + "Serializable");
111
112   /** Constant for a null "object" */
113   public static final TypeTag JAVA_NULL = new TypeTag(TPREFIX + "Null");
114
115   /* Structs */
116
117   /** Constant for a SQL structure */
118   public static final TypeTag RESULTSET = new TypeTag(TPREFIX
119                                                     + "ResultSet");
120   /** Null ResultSet used as a special flag */
121   public static final TypeTag NULL_RESULTSET = new TypeTag("null ResultSet");
122
123   /** Constant for a SQL structure */
124   public static final TypeTag FIELD = new TypeTag(TPREFIX + "Field");
125   /** Constant for a SQL structure */
126   public static final TypeTag COL_TYPES = new TypeTag(TPREFIX
127                                                     + "Column types");
128   /** Constant for a SQL structure */
129   public static final TypeTag ROW = new TypeTag(TPREFIX + "Row");
130
131   /** used when there is no type ambiguity; no need to type */
132   public static final TypeTag NOT_EXCEPTION = new TypeTag("OK");
133
134   /** Constant for an exception */
135   public static final TypeTag EXCEPTION = new TypeTag(TPREFIX
136                                                     + "Exception");
137   /** Constant for an exception */
138   public static final TypeTag BACKEND_EXCEPTION = new TypeTag(TPREFIX
139                                                     + "BackendException");
140   /** Constant for an exception */
141   public static final TypeTag CORE_EXCEPTION = new TypeTag(TPREFIX
142                                                     + "CoreException");
143
144   /** Constant for internal protocol data */
145   public static final TypeTag CONTROLLER_READY = new TypeTag("Ready");
146
147   /** Used when the whole column is null AND we could not guess in any other way */
148   public static final TypeTag UNDEFINED = new TypeTag("Undef");
149
150   private TypeTag(String JavaDoc typeName)
151   {
152     this.typeNumber = commandNumber++;
153     this.typeName = typeName;
154   }
155
156   private TypeTag(int typeNumber)
157   {
158     this.typeNumber = typeNumber;
159   }
160
161   /**
162    * Read/deserialize/construct a TypeTag from a stream.
163    *
164    * @param in input stream
165    * @throws IOException stream error
166    */

167   public TypeTag(DriverBufferedInputStream in) throws IOException JavaDoc
168   {
169     this(in.readInt());
170   }
171
172   /**
173    * Serialize "this" tag on the stream.
174    *
175    * @param out output stream
176    * @throws IOException stream error
177    */

178   public void sendToStream(DriverBufferedOutputStream out) throws IOException JavaDoc
179   {
180     out.writeInt(this.typeNumber);
181   }
182
183   /**
184    * Calling this method is a bug, check the type of your argument.
185    *
186    * @param o compared object (which should be a TypeTag!)
187    * @return a buggy result
188    * @see java.lang.Object#equals(java.lang.Object)
189    * @deprecated
190    */

191   public boolean equals(Object JavaDoc o)
192   {
193     System.err
194         .println("internal bug: TypeTag was compared with something else at:");
195     (new Throwable JavaDoc()).printStackTrace();
196     return false;
197   }
198
199   /**
200    * Compares two TypeTags.
201    *
202    * @param b compared TypeTag
203    * @return true if same value
204    */

205   public boolean equals(TypeTag b)
206   {
207     /**
208      * Even if the constants above are static, we cannot use "==" since we also
209      * have to consider objects built from the stream
210      * {@link #TypeTag(DriverBufferedInputStream)}
211      */

212     /*
213      * We could reimplement String.equals here without the instanceof in order
214      * to get back a couple of CPU cycles.
215      */

216     return this.typeNumber == b.typeNumber;
217   }
218
219   /**
220    * Returns a string representation, useful for logging and debugging.
221    *
222    * @return string representation of the tag
223    */

224   public String JavaDoc toString()
225   {
226     if (typeName == null)
227     {
228       if (typeNumber == TYPE_ERROR.typeNumber)
229         return TYPE_ERROR.typeName;
230       if (typeNumber == STRING.typeNumber)
231         return STRING.typeName;
232       if (typeNumber == BIGDECIMAL.typeNumber)
233         return BIGDECIMAL.typeName;
234       if (typeNumber == BOOLEAN.typeNumber)
235         return BOOLEAN.typeName;
236       if (typeNumber == STRING.typeNumber)
237         return INTEGER.typeName;
238       if (typeNumber == INTEGER.typeNumber)
239         return LONG.typeName;
240       if (typeNumber == LONG.typeNumber)
241         return FLOAT.typeName;
242       if (typeNumber == FLOAT.typeNumber)
243         return DOUBLE.typeName;
244       if (typeNumber == DOUBLE.typeNumber)
245         return BYTE_ARRAY.typeName;
246       if (typeNumber == BYTE_ARRAY.typeNumber)
247         return SQL_DATE.typeName;
248       if (typeNumber == SQL_DATE.typeNumber)
249         return SQL_TIME.typeName;
250       if (typeNumber == SQL_TIME.typeNumber)
251         return SQL_TIME.typeName;
252       if (typeNumber == SQL_TIMESTAMP.typeNumber)
253         return SQL_TIMESTAMP.typeName;
254       if (typeNumber == CLOB.typeNumber)
255         return CLOB.typeName;
256       if (typeNumber == BLOB.typeNumber)
257         return BLOB.typeName;
258       if (typeNumber == JAVA_SERIALIZABLE.typeNumber)
259         return JAVA_SERIALIZABLE.typeName;
260       if (typeNumber == JAVA_NULL.typeNumber)
261         return JAVA_NULL.typeName;
262       if (typeNumber == RESULTSET.typeNumber)
263         return RESULTSET.typeName;
264       if (typeNumber == NULL_RESULTSET.typeNumber)
265         return NULL_RESULTSET.typeName;
266       if (typeNumber == FIELD.typeNumber)
267         return FIELD.typeName;
268       if (typeNumber == COL_TYPES.typeNumber)
269         return COL_TYPES.typeName;
270       if (typeNumber == ROW.typeNumber)
271         return ROW.typeName;
272       if (typeNumber == NOT_EXCEPTION.typeNumber)
273         return NOT_EXCEPTION.typeName;
274       if (typeNumber == EXCEPTION.typeNumber)
275         return EXCEPTION.typeName;
276       if (typeNumber == BACKEND_EXCEPTION.typeNumber)
277         return BACKEND_EXCEPTION.typeName;
278       if (typeNumber == CORE_EXCEPTION.typeNumber)
279         return CORE_EXCEPTION.typeName;
280       if (typeNumber == CONTROLLER_READY.typeNumber)
281         return CONTROLLER_READY.typeName;
282       return "Unknown type number: " + typeNumber;
283     }
284     return typeName;
285   }
286
287   /**
288    * Gives the standard JDBC type to Java Object type conversion according to
289    * table "JDBC type to Java Object Type" of the JDBC reference book. (Table
290    * 47.9.3 in 2nd Edition, table 50.3 in 3rd edition). This is the conversion
291    * that the getObject() method of every JDBC driver should perform by default.
292    *
293    * @param jdbcType the JDBC type to convert
294    * @return the Java Object type resulting from the standard type conversion.
295    * @see java.sql.Types
296    */

297
298   public static TypeTag jdbcToJavaObjectType(int jdbcType)
299   {
300     switch (jdbcType)
301     {
302
303       case Types.CHAR :
304       case Types.VARCHAR :
305       case Types.LONGVARCHAR :
306         return TypeTag.STRING;
307
308       case Types.NUMERIC :
309       case Types.DECIMAL :
310         return TypeTag.BIGDECIMAL;
311
312       case Types.BIT :
313         return TypeTag.BOOLEAN;
314
315       case Types.TINYINT :
316       case Types.SMALLINT :
317       case Types.INTEGER :
318         return TypeTag.INTEGER;
319
320       case Types.BIGINT :
321         return TypeTag.LONG;
322
323       case Types.REAL :
324         return TypeTag.FLOAT;
325
326       case Types.FLOAT :
327       case Types.DOUBLE :
328         return TypeTag.DOUBLE;
329
330       case Types.BINARY :
331       case Types.VARBINARY :
332       case Types.LONGVARBINARY :
333         return TypeTag.BYTE_ARRAY;
334
335       case Types.DATE :
336         return TypeTag.SQL_DATE;
337
338       case Types.TIME :
339         return TypeTag.SQL_TIME;
340
341       case Types.TIMESTAMP :
342         return TypeTag.SQL_TIMESTAMP;
343
344       // DISTINCT unsupported
345

346       case Types.CLOB :
347         return TypeTag.CLOB;
348
349       case Types.BLOB :
350         return TypeTag.BLOB;
351
352       case Types.JAVA_OBJECT :
353         return TypeTag.JAVA_SERIALIZABLE;
354
355       // ARRAY, STRUCT/SQLData, REF, JAVA_OBJECT unsupported
356

357       // JDBC 3.0 java.net.URL unsupported
358

359       default :
360         return TypeTag.TYPE_ERROR;
361     }
362   }
363 }
364
Popular Tags