KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > protocol > TypeTag


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 Emic Networks
4  * Contact: c-jdbc@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Initial developer(s): Marc Herbert
21  * Contributor(s): ______________________.
22  */

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

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

63   /* unsupported are: DISTINCT, Array, Struct/SQLData, Ref, JAVA_OBJECT */
64   /* JDBC 3.0 added the type: java.net.URL, also currently unsupported */
65
66   /** Constant for a SQL/Java type */
67   public static final TypeTag TYPE_ERROR = new TypeTag(
68                                                    TPREFIX
69                                                        + "Type not found or unsupported");
70   /** Constant for a SQL/Java type */
71   public static final TypeTag STRING = new TypeTag(TPREFIX + "String");
72   /** Constant for a SQL/Java type */
73   public static final TypeTag BIGDECIMAL = new TypeTag(TPREFIX
74                                                    + "BigDecimal");
75   /** Constant for a SQL/Java type */
76   public static final TypeTag BOOLEAN = new TypeTag(TPREFIX
77                                                    + "Boolean");
78   /** Constant for a SQL/Java type */
79   public static final TypeTag INTEGER = new TypeTag(TPREFIX
80                                                    + "Integer");
81   /** Constant for a SQL/Java type */
82   public static final TypeTag LONG = new TypeTag(TPREFIX + "Long");
83   /** Constant for a SQL/Java type */
84   public static final TypeTag FLOAT = new TypeTag(TPREFIX + "Float");
85   /** Constant for a SQL/Java type */
86   public static final TypeTag DOUBLE = new TypeTag(TPREFIX + "Double");
87   /** Constant for a SQL/Java type */
88   public static final TypeTag BYTE_ARRAY = new TypeTag(TPREFIX + "Byte[]");
89   /** Constant for a SQL/Java type */
90   public static final TypeTag SQL_DATE = new TypeTag(TPREFIX
91                                                    + "SqlDate");
92   /** Constant for a SQL/Java type */
93   public static final TypeTag SQL_TIME = new TypeTag(TPREFIX
94                                                    + "SqlTime");
95   /** Constant for a SQL/Java type */
96   public static final TypeTag SQL_TIMESTAMP = new TypeTag(TPREFIX
97                                                    + "SqlTimestamp");
98   /** Constant for a SQL/Java type */
99   public static final TypeTag CLOB = new TypeTag(TPREFIX + "Clob");
100   /** Constant for a SQL/Java type */
101   public static final TypeTag BLOB = new TypeTag(TPREFIX + "Blob");
102
103   /* Structs */
104   
105   /** Constant for a SQL structure */
106   public static final TypeTag RESULTSET = new TypeTag(TPREFIX
107                                                    + "ResultSet");
108   /** Null ResultSet */
109   public static final TypeTag NULL_RESULTSET = new TypeTag("null ResultSet");
110
111   /** Constant for a SQL structure */
112   public static final TypeTag FIELD = new TypeTag(TPREFIX + "Field");
113   /** Constant for a SQL structure */
114   public static final TypeTag COL_TYPES = new TypeTag(TPREFIX
115                                                    + "Column types");
116   /** Constant for a SQL structure */
117   public static final TypeTag ROW = new TypeTag(TPREFIX + "Row");
118
119   
120   /** used when there is no type ambiguity; no need to type */
121   public static final TypeTag NOT_EXCEPTION = new TypeTag("OK");
122
123   /** Constant for an exception */
124   public static final TypeTag EXCEPTION = new TypeTag(TPREFIX
125                                                    + "Exception");
126   /** Constant for an exception */
127   public static final TypeTag BACKEND_EXCEPTION = new TypeTag(TPREFIX
128                                                    + "BackendException");
129   /** Constant for an exception */
130   public static final TypeTag CORE_EXCEPTION = new TypeTag(TPREFIX
131                                                    + "CoreException");
132
133   /** Constant for internal protocol data */
134   public static final TypeTag CONTROLLER_READY = new TypeTag("Ready");
135
136   private TypeTag(String JavaDoc init)
137   {
138     this.internalString = init;
139   }
140
141   /**
142    * Read/deserialize/construct a TypeTag from a stream.
143    *
144    * @param in input stream
145    * @throws IOException stream error
146    */

147   public TypeTag(CJDBCInputStream in) throws IOException JavaDoc
148   {
149     this(in.readUTF());
150   }
151
152   /**
153    * Serialize "this" tag on the stream.
154    *
155    * @param out output stream
156    * @throws IOException stream error
157    */

158   public void sendToStream(CJDBCOutputStream out) throws IOException JavaDoc
159   {
160     out.writeUTF(this.internalString);
161   }
162
163   /**
164    * Calling this method is a bug, check the type of your argument.
165    *
166    * @param o compared object (which should be a TypeTag!)
167    * @return a buggy result
168    * @see java.lang.Object#equals(java.lang.Object)
169    * @deprecated
170    */

171   public boolean equals(Object JavaDoc o)
172   {
173     System.err
174         .println("internal bug: TypeTag was compared with something else at:");
175     (new Throwable JavaDoc()).printStackTrace();
176     return false;
177   }
178
179   /**
180    * Compares two TypeTags.
181    *
182    * @param b compared TypeTag
183    * @return true if same value
184    */

185   public boolean equals(TypeTag b)
186   {
187     /**
188      * Even if the constants above are static, we cannot use "==" since we also
189      * have to consider objects built from the stream
190      * {@link #TypeTag(CJDBCInputStream)}
191      */

192     /*
193      * We could reimplement String.equals here without the instanceof in order
194      * to get back a couple of CPU cycles.
195      */

196     return this.internalString.equals(b.internalString);
197   }
198
199   /**
200    * Returns a string representation, useful for logging and debugging.
201    *
202    * @return string representation of the tag
203    */

204   public String JavaDoc toSring()
205   {
206     return internalString;
207   }
208
209   /**
210    * Gives the standard JDBC type to Java Object type conversion according to
211    * table "JDBC type to Java Object Type" of the JDBC reference book. (Table
212    * 47.9.3 in 2nd Edition, table 50.3 in 3rd edition). This is the conversion
213    * that the getObject() method of every JDBC driver should perform by default.
214    *
215    * @param jdbcType the JDBC type to convert
216    * @return the Java Object type resulting from the standard type conversion.
217    * @see java.sql.Types
218    */

219
220   public static TypeTag jdbcToJavaObjectType(int jdbcType)
221   {
222     switch (jdbcType)
223     {
224
225       case Types.CHAR :
226       case Types.VARCHAR :
227       case Types.LONGVARCHAR :
228         return TypeTag.STRING;
229
230       case Types.NUMERIC :
231       case Types.DECIMAL :
232         return TypeTag.BIGDECIMAL;
233
234       case Types.BIT :
235         return TypeTag.BOOLEAN;
236
237       case Types.TINYINT :
238       case Types.SMALLINT :
239       case Types.INTEGER :
240         return TypeTag.INTEGER;
241
242       case Types.BIGINT :
243         return TypeTag.LONG;
244
245       case Types.REAL :
246         return TypeTag.FLOAT;
247
248       case Types.FLOAT :
249       case Types.DOUBLE :
250         return TypeTag.DOUBLE;
251
252       case Types.BINARY :
253       case Types.VARBINARY :
254       case Types.LONGVARBINARY :
255         return TypeTag.BYTE_ARRAY;
256
257       case Types.DATE :
258         return TypeTag.SQL_DATE;
259
260       case Types.TIME :
261         return TypeTag.SQL_TIME;
262
263       case Types.TIMESTAMP :
264         return TypeTag.SQL_TIMESTAMP;
265
266       // DISTINCT unsupported
267

268       case Types.CLOB :
269         return TypeTag.CLOB;
270
271       case Types.BLOB :
272         return TypeTag.BLOB;
273
274       // ARRAY, STRUCT/SQLData, REF, JAVA_OBJECT unsupported
275

276       // JDBC 3.0 java.net.URL unsupported
277

278       default :
279         return TypeTag.TYPE_ERROR;
280     }
281   }
282 }
283
Popular Tags