KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > typing > TypeMap


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.jdbc.typing;
25
26 import java.sql.*;
27 import java.util.*;
28
29 /**
30  * Class containing information on JDBC to Native SQL types mapping.
31  *
32  */

33 public class TypeMap extends HashMap
34 {
35     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37     
38     private SQLType OTHER_Type = new SQLType();
39     
40     /**
41      * Map between JDBC types and natives type.
42      */

43     private Map canonicalTypesMap = new HashMap();
44     
45     public TypeMap() {
46         OTHER_Type.addJDBCType(Types.OTHER, null, true, false);
47     }
48
49     public void load(Connection connection) throws SQLException
50     {
51         ResultSet rs = connection.getMetaData().getTypeInfo();
52         while (rs.next())
53             addType(new SQLType(rs));
54         rs.close();
55     }
56     
57     public SQLType createType() {
58         return new SQLType();
59     }
60     
61     public void addType(SQLType nativeType)
62     {
63         String JavaDoc key = nativeType.getNativeTypeName().toLowerCase();
64         if (get(key) != null)
65             throw new RuntimeException JavaDoc("Internal Error: duplicate entry in type XML data file.");
66         put(key, nativeType);
67     }
68     
69     public SQLType getType(String JavaDoc nativeType)
70     {
71         SQLType ret = (SQLType)get(nativeType.toLowerCase());
72         if (ret == null)
73             ret = OTHER_Type; // No fatal error : caller may need to ignore.
74
return ret;
75     }
76     
77     public SQLType getType(int JDBCType)
78     {
79         SQLType targetType = (SQLType)canonicalTypesMap.get(new Integer JavaDoc(JDBCType));
80         if (targetType == null)
81             throw new RuntimeException JavaDoc("The JDBC type " + JDBCType + " is not supported by the database JDBC driver.");
82         return targetType;
83     }
84     
85     public String JavaDoc getNativeTypeName(int JDBCType)
86     {
87         return getType(JDBCType).getNativeTypeName();
88     }
89     
90     public long getSize(int JDBCType)
91     {
92         return getType(JDBCType).getPrecision();
93     }
94     
95     public boolean isUnsigned(int JDBCType)
96     {
97         return getType(JDBCType).isUnsigned();
98     }
99     
100     public boolean useGetString(int JDBCType)
101     {
102         return getType(JDBCType).useGetString();
103     }
104     
105     public String JavaDoc getTypeParam(int JDBCType)
106     {
107         return getType(JDBCType).getTypeParam();
108     }
109     
110     ///////////////////////////////////////////////////////////////////////////
111
// INNER CLASSES
112
///////////////////////////////////////////////////////////////////////////
113
private class JDBCType implements Cloneable JavaDoc
114     {
115         private int JDBCType;
116         private String JavaDoc createParam; // String to concatenate to nativeTypeName when creating table
117
private boolean useGetString;
118                                       // (containing precision for instance)
119
JDBCType(int type, String JavaDoc createParam, boolean useGetString) {
120             JDBCType = type;
121             this.createParam = createParam;
122             this.useGetString = useGetString;
123         }
124         /**
125          * @return Returns the createParam.
126          */

127         String JavaDoc getCreateParam() {
128             return createParam;
129         }
130
131         /**
132          * @return Returns the jDBCType.
133          */

134         int getJDBCType() {
135             return JDBCType;
136         }
137
138         public boolean useGetString() {
139             return useGetString;
140         }
141         public void setUseGetString(boolean useGetString) {
142             this.useGetString = useGetString;
143         }
144
145     }
146     public class SQLType implements Cloneable JavaDoc
147     {
148         private String JavaDoc nativeTypeName;
149         private List JDBCTypes = new ArrayList(); // note : int in java.sql.Types but short in rs
150
private long precision; // note : int in java.sql.Types but short in rs
151
private boolean unsigned;
152         private boolean indexable = true;
153         private boolean extraction = true; // supports extraction
154
private boolean storage = true; // supports insertion
155
private boolean autoFill = false;
156         /**
157          * Default constructor for TargetNativeType.
158          */

159         public SQLType() {};
160
161         SQLType(ResultSet rs) throws SQLException
162         {
163             nativeTypeName = rs.getString(1);
164             addJDBCType(rs.getShort(2), rs.getString(6), false, false);
165             precision = rs.getLong(3); // getInt -> getLong because of Oracle JDBC 8.1.7
166
unsigned = rs.getBoolean(10);
167         }
168         
169         String JavaDoc getNativeTypeName()
170         {
171             return nativeTypeName;
172         }
173         String JavaDoc getTypeParam()
174         {
175             return ((JDBCType)JDBCTypes.get(0)).getCreateParam();
176         }
177         int getJDBCType()
178         {
179             return ((JDBCType)JDBCTypes.get(0)).getJDBCType();
180         }
181         boolean useGetString()
182         {
183             return ((JDBCType)JDBCTypes.get(0)).useGetString();
184         }
185         long getPrecision()
186         {
187             return precision;
188         }
189         boolean isUnsigned()
190         {
191             return unsigned;
192         }
193         public void setNativeTypeName(String JavaDoc typeName)
194         {
195             nativeTypeName = typeName;
196         }
197         public void setPrecision(long prec)
198         {
199             precision = prec;
200         }
201         public void setUnsigned(boolean unsigned)
202         {
203             this.unsigned = unsigned;
204         }
205         public String JavaDoc toString()
206         {
207             return "[jdbc=" + JDBCTypes + ", sql=" + nativeTypeName + ", prec=" + precision
208             + ", unsigned=" + unsigned + "]";
209         }
210         public Object JavaDoc clone()
211         {
212             try {
213                 return super.clone();
214             }
215             catch (CloneNotSupportedException JavaDoc e) {
216                 return null;
217             }
218         }
219         /**
220          * Sets the jDBCType.
221          * @param jDBCType The jDBCType to set
222          */

223         public void addJDBCType(int jDBCType, String JavaDoc createParams, boolean principal, boolean useGetString)
224         {
225             Integer JavaDoc key = new Integer JavaDoc(jDBCType);
226             JDBCType typeInfo = new JDBCType(jDBCType, createParams, useGetString);
227             if (principal) {
228                 JDBCTypes.add(0, typeInfo);
229                 canonicalTypesMap.put(key, this);
230             }
231             else {
232                 JDBCTypes.add(typeInfo);
233                 if (canonicalTypesMap.get(key) == null)
234                     canonicalTypesMap.put(key, this);
235             }
236         }
237         
238         public void addJDBCType(String JavaDoc jDBCType, String JavaDoc createParams, boolean principal, boolean useGetString)
239         {
240             addJDBCType(parseJDBCType(jDBCType), createParams, principal, useGetString);
241         }
242
243         public boolean isAutoFill() { return autoFill;}
244
245         public void setAutoFill(boolean autoFill) { this.autoFill = autoFill;}
246
247         /**
248          * @return Returns the extraction.
249          */

250         public boolean isExtraction() {
251             return extraction;
252         }
253         /**
254          * @param extraction The extraction to set.
255          */

256         public void setExtraction(boolean extraction) {
257             this.extraction = extraction;
258         }
259         /**
260          * @return Returns the indexable.
261          */

262         public boolean isIndexable() {
263             return indexable;
264         }
265         /**
266          * @param indexable The indexable to set.
267          */

268         public void setIndexable(boolean indexable) {
269             this.indexable = indexable;
270         }
271         /**
272          * @return Returns the storage.
273          */

274         public boolean isStorage() {
275             return storage;
276         }
277         /**
278          * @param storage The storage to set.
279          */

280         public void setStorage(boolean storage) {
281             this.storage = storage;
282         }
283     }
284     
285     public static String JavaDoc getJDBCTypeName(int type)
286     {
287         switch (type)
288         {
289             case Types.ARRAY :
290                 return "ARRAY";
291             case Types.BIGINT :
292                 return "BIGINT";
293             case Types.BINARY :
294                 return "BINARY";
295             case Types.BIT :
296                 return "BIT";
297             case Types.BLOB :
298                 return "BLOB";
299             case DbType.BOOLEAN :
300                 return "BOOLEAN";
301             case Types.CHAR :
302                 return "CHAR";
303             case Types.CLOB :
304                 return "CLOB";
305             case Types.DATE :
306                 return "DATE";
307             case Types.DECIMAL :
308                 return "DECIMAL";
309             case Types.DISTINCT :
310                 return "DISTINCT";
311             case Types.DOUBLE :
312                 return "DOUBLE";
313             case Types.FLOAT :
314                 return "FLOAT";
315             case Types.INTEGER :
316                 return "INTEGER";
317             case Types.JAVA_OBJECT :
318                 return "JAVA_OBJECT";
319             case Types.LONGVARBINARY :
320                 return "LONGVARBINARY";
321             case Types.LONGVARCHAR :
322                 return "LONGVARCHAR";
323             case Types.NULL :
324                 return "NULL";
325             case Types.NUMERIC :
326                 return "NUMERIC";
327             case Types.OTHER :
328                 return "OTHER";
329             case Types.REAL :
330                 return "REAL";
331             case Types.REF :
332                 return "REF";
333             case Types.SMALLINT :
334                 return "SMALLINT";
335             case Types.STRUCT :
336                 return "STRUCT";
337             case Types.TIME :
338                 return "TIME";
339             case Types.TIMESTAMP :
340                 return "TIMESTAMP";
341             case Types.TINYINT :
342                 return "TINYINT";
343             case Types.VARBINARY :
344                 return "VARBINARY";
345             case Types.VARCHAR :
346                 return "VARCHAR";
347             case DbType.ORACLE_ROWID :
348                 return "ROWID";
349             default :
350                 return "";
351         }
352
353     }
354     private static final int[] dataTypes = {
355             Types.ARRAY,
356             Types.BIGINT,
357             Types.BINARY,
358             Types.BIT,
359             Types.BLOB,
360             DbType.BOOLEAN,
361             Types.CHAR,
362             Types.CLOB,
363             Types.DATE,
364             Types.DECIMAL,
365             Types.DISTINCT,
366             Types.DOUBLE,
367             Types.FLOAT,
368             Types.INTEGER,
369             Types.JAVA_OBJECT,
370             Types.LONGVARBINARY,
371             Types.LONGVARCHAR,
372             Types.NULL,
373             Types.NUMERIC,
374             Types.OTHER,
375             Types.REAL,
376             Types.REF,
377             DbType.ORACLE_ROWID,
378             Types.SMALLINT,
379             Types.STRUCT,
380             Types.TIME,
381             Types.TIMESTAMP,
382             Types.TINYINT,
383             Types.VARBINARY,
384             Types.VARCHAR
385     };
386     
387     /* Alphabetically sorted */
388     private static final String JavaDoc[] dataTypeStrings =
389         {
390             "ARRAY",
391             "BIGINT",
392             "BINARY",
393             "BIT",
394             "BLOB",
395             "BOOLEAN",
396             "CHAR",
397             "CLOB",
398             "DATE",
399             "DECIMAL",
400             "DISTINCT",
401             "DOUBLE",
402             "FLOAT",
403             "INTEGER",
404             "JAVA_OBJECT",
405             "LONGVARBINARY",
406             "LONGVARCHAR",
407             "NULL",
408             "NUMERIC",
409             "OTHER",
410             "REAL",
411             "REF",
412             "ROWID",
413             "SMALLINT",
414             "STRUCT",
415             "TIME",
416             "TIMESTAMP",
417             "TINYINT",
418             "VARBINARY",
419             "VARCHAR"
420     };
421
422     public static int parseJDBCType(String JavaDoc type)
423     {
424         int index = Arrays.binarySearch(dataTypeStrings, type);
425         if (index < 0)
426             throw new RuntimeException JavaDoc("The " + type + " JDBC type used in tableSpec.xml is unknown.");
427         return dataTypes[index];
428     }
429 }
430
Popular Tags