KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > TypesMapping


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. 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,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba;
21
22 import java.io.Serializable JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.sql.DatabaseMetaData JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Types JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import org.apache.cayenne.util.Util;
37
38 /**
39  * A utility class that handles mappings of JDBC data types to the database types and Java
40  * types. Also contains methods that provide information about JDBC types.
41  *
42  * @author Michael Shengaout
43  * @author Andrus Adamchik
44  */

45 public class TypesMapping {
46
47     // Never use "-1" or any other normal integer, since there
48
// is a big chance it is being reserved in java.sql.Types
49
public static final int NOT_DEFINED = Integer.MAX_VALUE;
50
51     // char constants for the sql data types
52
public static final String JavaDoc SQL_ARRAY = "ARRAY";
53     public static final String JavaDoc SQL_BIGINT = "BIGINT";
54     public static final String JavaDoc SQL_BINARY = "BINARY";
55     public static final String JavaDoc SQL_BIT = "BIT";
56     public static final String JavaDoc SQL_BLOB = "BLOB";
57
58     /**
59      * @since 1.2
60      */

61     public static final String JavaDoc SQL_BOOLEAN = "BOOLEAN";
62
63     public static final String JavaDoc SQL_CLOB = "CLOB";
64     public static final String JavaDoc SQL_CHAR = "CHAR";
65     public static final String JavaDoc SQL_DATE = "DATE";
66     public static final String JavaDoc SQL_DECIMAL = "DECIMAL";
67     public static final String JavaDoc SQL_DOUBLE = "DOUBLE";
68     public static final String JavaDoc SQL_FLOAT = "FLOAT";
69     public static final String JavaDoc SQL_INTEGER = "INTEGER";
70     public static final String JavaDoc SQL_LONGVARCHAR = "LONGVARCHAR";
71     public static final String JavaDoc SQL_LONGVARBINARY = "LONGVARBINARY";
72     public static final String JavaDoc SQL_NUMERIC = "NUMERIC";
73     public static final String JavaDoc SQL_REAL = "REAL";
74     public static final String JavaDoc SQL_SMALLINT = "SMALLINT";
75     public static final String JavaDoc SQL_TINYINT = "TINYINT";
76     public static final String JavaDoc SQL_TIME = "TIME";
77     public static final String JavaDoc SQL_TIMESTAMP = "TIMESTAMP";
78     public static final String JavaDoc SQL_VARBINARY = "VARBINARY";
79     public static final String JavaDoc SQL_VARCHAR = "VARCHAR";
80     public static final String JavaDoc SQL_OTHER = "OTHER";
81
82     // char constants for Java data types
83
public static final String JavaDoc JAVA_LONG = "java.lang.Long";
84     public static final String JavaDoc JAVA_BYTES = "byte[]";
85     public static final String JavaDoc JAVA_BOOLEAN = "java.lang.Boolean";
86     public static final String JavaDoc JAVA_STRING = "java.lang.String";
87     public static final String JavaDoc JAVA_SQLDATE = "java.sql.Date";
88     public static final String JavaDoc JAVA_UTILDATE = "java.util.Date";
89     public static final String JavaDoc JAVA_BIGDECIMAL = "java.math.BigDecimal";
90     public static final String JavaDoc JAVA_DOUBLE = "java.lang.Double";
91     public static final String JavaDoc JAVA_FLOAT = "java.lang.Float";
92     public static final String JavaDoc JAVA_INTEGER = "java.lang.Integer";
93     public static final String JavaDoc JAVA_SHORT = "java.lang.Short";
94     public static final String JavaDoc JAVA_BYTE = "java.lang.Byte";
95     public static final String JavaDoc JAVA_TIME = "java.sql.Time";
96     public static final String JavaDoc JAVA_TIMESTAMP = "java.sql.Timestamp";
97     public static final String JavaDoc JAVA_BLOB = "java.sql.Blob";
98
99     /**
100      * Keys: SQL string type names, Values: SQL int type definitions from java.sql.Types
101      */

102     private static final Map JavaDoc sqlStringType = new HashMap JavaDoc();
103
104     /**
105      * Keys: SQL int type definitions from java.sql.Types, Values: SQL string type names
106      */

107     private static final Map JavaDoc sqlEnumType = new HashMap JavaDoc();
108
109     /**
110      * Keys: SQL int type definitions from java.sql.Types, Values: java class names
111      */

112     private static final Map JavaDoc sqlEnumJava = new HashMap JavaDoc();
113
114     /**
115      * Keys: java class names, Values: SQL int type definitions from java.sql.Types
116      */

117     private static final Map JavaDoc javaSqlEnum = new HashMap JavaDoc();
118
119     static {
120         sqlStringType.put(SQL_ARRAY, new Integer JavaDoc(Types.ARRAY));
121         sqlStringType.put(SQL_BIGINT, new Integer JavaDoc(Types.BIGINT));
122         sqlStringType.put(SQL_BINARY, new Integer JavaDoc(Types.BINARY));
123         sqlStringType.put(SQL_BIT, new Integer JavaDoc(Types.BIT));
124         sqlStringType.put(SQL_BLOB, new Integer JavaDoc(Types.BLOB));
125         sqlStringType.put(SQL_BOOLEAN, new Integer JavaDoc(Types.BOOLEAN));
126         sqlStringType.put(SQL_CLOB, new Integer JavaDoc(Types.CLOB));
127         sqlStringType.put(SQL_CHAR, new Integer JavaDoc(Types.CHAR));
128         sqlStringType.put(SQL_DATE, new Integer JavaDoc(Types.DATE));
129         sqlStringType.put(SQL_DECIMAL, new Integer JavaDoc(Types.DECIMAL));
130         sqlStringType.put(SQL_DOUBLE, new Integer JavaDoc(Types.DOUBLE));
131         sqlStringType.put(SQL_FLOAT, new Integer JavaDoc(Types.FLOAT));
132         sqlStringType.put(SQL_INTEGER, new Integer JavaDoc(Types.INTEGER));
133         sqlStringType.put(SQL_LONGVARCHAR, new Integer JavaDoc(Types.LONGVARCHAR));
134         sqlStringType.put(SQL_LONGVARBINARY, new Integer JavaDoc(Types.LONGVARBINARY));
135         sqlStringType.put(SQL_NUMERIC, new Integer JavaDoc(Types.NUMERIC));
136         sqlStringType.put(SQL_REAL, new Integer JavaDoc(Types.REAL));
137         sqlStringType.put(SQL_SMALLINT, new Integer JavaDoc(Types.SMALLINT));
138         sqlStringType.put(SQL_TINYINT, new Integer JavaDoc(Types.TINYINT));
139         sqlStringType.put(SQL_TIME, new Integer JavaDoc(Types.TIME));
140         sqlStringType.put(SQL_TIMESTAMP, new Integer JavaDoc(Types.TIMESTAMP));
141         sqlStringType.put(SQL_VARBINARY, new Integer JavaDoc(Types.VARBINARY));
142         sqlStringType.put(SQL_VARCHAR, new Integer JavaDoc(Types.VARCHAR));
143         sqlStringType.put(SQL_OTHER, new Integer JavaDoc(Types.OTHER));
144
145         sqlEnumType.put(new Integer JavaDoc(Types.ARRAY), SQL_ARRAY);
146         sqlEnumType.put(new Integer JavaDoc(Types.BIGINT), SQL_BIGINT);
147         sqlEnumType.put(new Integer JavaDoc(Types.BINARY), SQL_BINARY);
148         sqlEnumType.put(new Integer JavaDoc(Types.BIT), SQL_BIT);
149         sqlEnumType.put(new Integer JavaDoc(Types.BOOLEAN), SQL_BOOLEAN);
150         sqlEnumType.put(new Integer JavaDoc(Types.BLOB), SQL_BLOB);
151         sqlEnumType.put(new Integer JavaDoc(Types.CLOB), SQL_CLOB);
152         sqlEnumType.put(new Integer JavaDoc(Types.CHAR), SQL_CHAR);
153         sqlEnumType.put(new Integer JavaDoc(Types.DATE), SQL_DATE);
154         sqlEnumType.put(new Integer JavaDoc(Types.DECIMAL), SQL_DECIMAL);
155         sqlEnumType.put(new Integer JavaDoc(Types.DOUBLE), SQL_DOUBLE);
156         sqlEnumType.put(new Integer JavaDoc(Types.FLOAT), SQL_FLOAT);
157         sqlEnumType.put(new Integer JavaDoc(Types.INTEGER), SQL_INTEGER);
158         sqlEnumType.put(new Integer JavaDoc(Types.LONGVARCHAR), SQL_LONGVARCHAR);
159         sqlEnumType.put(new Integer JavaDoc(Types.LONGVARBINARY), SQL_LONGVARBINARY);
160         sqlEnumType.put(new Integer JavaDoc(Types.NUMERIC), SQL_NUMERIC);
161         sqlEnumType.put(new Integer JavaDoc(Types.REAL), SQL_REAL);
162         sqlEnumType.put(new Integer JavaDoc(Types.SMALLINT), SQL_SMALLINT);
163         sqlEnumType.put(new Integer JavaDoc(Types.TINYINT), SQL_TINYINT);
164         sqlEnumType.put(new Integer JavaDoc(Types.TIME), SQL_TIME);
165         sqlEnumType.put(new Integer JavaDoc(Types.TIMESTAMP), SQL_TIMESTAMP);
166         sqlEnumType.put(new Integer JavaDoc(Types.VARBINARY), SQL_VARBINARY);
167         sqlEnumType.put(new Integer JavaDoc(Types.VARCHAR), SQL_VARCHAR);
168         sqlEnumType.put(new Integer JavaDoc(Types.OTHER), SQL_OTHER);
169
170         sqlEnumJava.put(new Integer JavaDoc(Types.BIGINT), JAVA_LONG);
171         sqlEnumJava.put(new Integer JavaDoc(Types.BINARY), JAVA_BYTES);
172         sqlEnumJava.put(new Integer JavaDoc(Types.BIT), JAVA_BOOLEAN);
173         sqlEnumJava.put(new Integer JavaDoc(Types.BOOLEAN), JAVA_BOOLEAN);
174         sqlEnumJava.put(new Integer JavaDoc(Types.BLOB), JAVA_BYTES);
175         sqlEnumJava.put(new Integer JavaDoc(Types.CLOB), JAVA_STRING);
176         sqlEnumJava.put(new Integer JavaDoc(Types.CHAR), JAVA_STRING);
177         sqlEnumJava.put(new Integer JavaDoc(Types.DATE), JAVA_UTILDATE);
178         sqlEnumJava.put(new Integer JavaDoc(Types.DECIMAL), JAVA_BIGDECIMAL);
179         sqlEnumJava.put(new Integer JavaDoc(Types.DOUBLE), JAVA_DOUBLE);
180         sqlEnumJava.put(new Integer JavaDoc(Types.FLOAT), JAVA_FLOAT);
181         sqlEnumJava.put(new Integer JavaDoc(Types.INTEGER), JAVA_INTEGER);
182         sqlEnumJava.put(new Integer JavaDoc(Types.LONGVARCHAR), JAVA_STRING);
183         sqlEnumJava.put(new Integer JavaDoc(Types.LONGVARBINARY), JAVA_BYTES);
184         sqlEnumJava.put(new Integer JavaDoc(Types.NUMERIC), JAVA_BIGDECIMAL);
185         sqlEnumJava.put(new Integer JavaDoc(Types.REAL), JAVA_FLOAT);
186         sqlEnumJava.put(new Integer JavaDoc(Types.SMALLINT), JAVA_SHORT);
187         sqlEnumJava.put(new Integer JavaDoc(Types.TINYINT), JAVA_BYTE);
188         sqlEnumJava.put(new Integer JavaDoc(Types.TIME), JAVA_UTILDATE);
189         sqlEnumJava.put(new Integer JavaDoc(Types.TIMESTAMP), JAVA_UTILDATE);
190         sqlEnumJava.put(new Integer JavaDoc(Types.VARBINARY), JAVA_BYTES);
191         sqlEnumJava.put(new Integer JavaDoc(Types.VARCHAR), JAVA_STRING);
192
193         javaSqlEnum.put(JAVA_LONG, new Integer JavaDoc(Types.BIGINT));
194         javaSqlEnum.put(JAVA_BYTES, new Integer JavaDoc(Types.BINARY));
195         javaSqlEnum.put(JAVA_BOOLEAN, new Integer JavaDoc(Types.BIT));
196         javaSqlEnum.put(JAVA_STRING, new Integer JavaDoc(Types.VARCHAR));
197         javaSqlEnum.put(JAVA_SQLDATE, new Integer JavaDoc(Types.DATE));
198         javaSqlEnum.put(JAVA_TIMESTAMP, new Integer JavaDoc(Types.TIMESTAMP));
199         javaSqlEnum.put(JAVA_BIGDECIMAL, new Integer JavaDoc(Types.DECIMAL));
200         javaSqlEnum.put(JAVA_DOUBLE, new Integer JavaDoc(Types.DOUBLE));
201         javaSqlEnum.put(JAVA_FLOAT, new Integer JavaDoc(Types.FLOAT));
202         javaSqlEnum.put(JAVA_INTEGER, new Integer JavaDoc(Types.INTEGER));
203         javaSqlEnum.put(JAVA_SHORT, new Integer JavaDoc(Types.SMALLINT));
204         javaSqlEnum.put(JAVA_BYTE, new Integer JavaDoc(Types.TINYINT));
205         javaSqlEnum.put(JAVA_TIME, new Integer JavaDoc(Types.TIME));
206         javaSqlEnum.put(JAVA_TIMESTAMP, new Integer JavaDoc(Types.TIMESTAMP));
207
208         // add primitives
209
javaSqlEnum.put("byte", new Integer JavaDoc(Types.TINYINT));
210         javaSqlEnum.put("int", new Integer JavaDoc(Types.INTEGER));
211         javaSqlEnum.put("short", new Integer JavaDoc(Types.SMALLINT));
212         javaSqlEnum.put("char", new Integer JavaDoc(Types.CHAR));
213         javaSqlEnum.put("double", new Integer JavaDoc(Types.DOUBLE));
214         javaSqlEnum.put("long", new Integer JavaDoc(Types.BIGINT));
215         javaSqlEnum.put("float", new Integer JavaDoc(Types.FLOAT));
216         javaSqlEnum.put("boolean", new Integer JavaDoc(Types.BIT));
217     }
218
219     /**
220      * Returns true if supplied type can have a length attribute as a part of column
221      * definition.
222      */

223     public static boolean supportsLength(int type) {
224         return type == Types.BINARY
225                 || type == Types.CHAR
226                 || type == Types.DECIMAL
227                 || type == Types.DOUBLE
228                 || type == Types.FLOAT
229                 || type == Types.NUMERIC
230                 || type == Types.REAL
231                 || type == Types.VARBINARY
232                 || type == Types.VARCHAR;
233     }
234
235     /**
236      * Returns true if supplied type is a numeric type.
237      */

238     public static boolean isNumeric(int type) {
239         return type == Types.BIGINT
240                 || type == Types.BIT
241                 || type == Types.DECIMAL
242                 || type == Types.DOUBLE
243                 || type == Types.FLOAT
244                 || type == Types.INTEGER
245                 || type == Types.NUMERIC
246                 || type == Types.REAL
247                 || type == Types.SMALLINT
248                 || type == Types.TINYINT;
249     }
250
251     /**
252      * Returns true if supplied type is a decimal type.
253      */

254     public static boolean isDecimal(int type) {
255         return type == Types.DECIMAL
256                 || type == Types.DOUBLE
257                 || type == Types.FLOAT
258                 || type == Types.REAL
259                 || type == Types.NUMERIC;
260     }
261
262     /** Returns an array of string names of the default JDBC data types. */
263     public static String JavaDoc[] getDatabaseTypes() {
264         Set JavaDoc keys = sqlStringType.keySet();
265         int len = keys.size();
266         String JavaDoc[] types = new String JavaDoc[len];
267
268         Iterator JavaDoc it = keys.iterator();
269         for (int i = 0; i < len; i++) {
270             types[i] = (String JavaDoc) it.next();
271         }
272
273         return types;
274     }
275
276     /**
277      * Method implements an algorithm to pick a data type from a list of alternatives that
278      * most closely matches JDBC data type.
279      */

280     protected static String JavaDoc pickDataType(int jdbcType, TypeInfo[] alts) {
281         int len = alts.length;
282
283         if (len == 0)
284             return null;
285
286         if (len == 1)
287             return alts[0].name;
288
289         // now the fun starts.. try to guess the right type
290

291         String JavaDoc jdbcName = getSqlNameByType(jdbcType).toUpperCase();
292
293         // 1. exact match
294
for (int i = 0; i < len; i++) {
295             if (jdbcName.equalsIgnoreCase(alts[i].name))
296                 return alts[i].name;
297         }
298
299         // 2. filter those with biggest precision
300
long maxPrec = 0;
301         for (int i = 0; i < len; i++) {
302             if (maxPrec < alts[i].precision) {
303                 maxPrec = alts[i].precision;
304             }
305         }
306
307         List JavaDoc list = new ArrayList JavaDoc();
308         for (int i = 0; i < len; i++) {
309             if (maxPrec == alts[i].precision) {
310                 list.add(alts[i]);
311             }
312         }
313
314         // work with smaller list now.....
315
int slen = list.size();
316         if (slen == 1)
317             return ((TypeInfo) list.get(0)).name;
318
319         // start/end match
320
for (int i = 0; i < slen; i++) {
321             String JavaDoc uppercase = ((TypeInfo) list.get(i)).name.toUpperCase();
322             if (uppercase.startsWith(jdbcName) || uppercase.endsWith(jdbcName))
323                 return ((TypeInfo) list.get(i)).name;
324         }
325
326         // in the middle match
327
for (int i = 0; i < slen; i++) {
328             String JavaDoc uppercase = ((TypeInfo) list.get(i)).name.toUpperCase();
329
330             if (uppercase.indexOf(jdbcName) >= 0)
331                 return ((TypeInfo) list.get(i)).name;
332         }
333
334         // out of ideas... return the first one
335
return ((TypeInfo) list.get(0)).name;
336     }
337
338     /**
339      * Returns a JDBC int type for SQL typem name.
340      */

341     public static int getSqlTypeByName(String JavaDoc typeName) {
342         Integer JavaDoc tmp = (Integer JavaDoc) sqlStringType.get(typeName);
343         return (null == tmp) ? NOT_DEFINED : tmp.intValue();
344     }
345
346     /**
347      * Returns a String representation of the SQL type from its JDBC code.
348      */

349     public static String JavaDoc getSqlNameByType(int type) {
350         return (String JavaDoc) sqlEnumType.get(new Integer JavaDoc(type));
351     }
352
353     /**
354      * Returns default java.sql.Types type by the Java type name.
355      *
356      * @param className Fully qualified Java Class name.
357      * @return The SQL type or NOT_DEFINED if no type found.
358      */

359     public static int getSqlTypeByJava(String JavaDoc className) {
360         if (className == null) {
361             return NOT_DEFINED;
362         }
363
364         Integer JavaDoc type = (Integer JavaDoc) javaSqlEnum.get(className);
365         if (type != null) {
366             return type.intValue();
367         }
368
369         // try to load a Java class - some nonstandard mappings may work
370

371         Class JavaDoc aClass;
372         try {
373             aClass = Util.getJavaClass(className);
374         }
375         catch (Throwable JavaDoc th) {
376             return NOT_DEFINED;
377         }
378
379         return getSqlTypeByJava(aClass);
380     }
381
382     /**
383      * Guesses a default JDBC type for the Java class.
384      *
385      * @since 1.1
386      */

387     public static int getSqlTypeByJava(Class JavaDoc javaClass) {
388         if (javaClass == null) {
389             return NOT_DEFINED;
390         }
391
392         // check standard mapping of class and superclasses
393
Class JavaDoc aClass = javaClass;
394         while (aClass != null) {
395
396             String JavaDoc name;
397
398             if (aClass.isArray()) {
399                 name = aClass.getComponentType().getName() + "[]";
400             }
401             else {
402                 name = aClass.getName();
403             }
404
405             Object JavaDoc type = javaSqlEnum.get(name);
406             if (type != null) {
407                 return ((Number JavaDoc) type).intValue();
408             }
409
410             aClass = aClass.getSuperclass();
411         }
412
413         // check non-standard JDBC types that are still supported by JPA
414
if (javaClass.isArray()) {
415
416             Class JavaDoc elementType = javaClass.getComponentType();
417             if (Character JavaDoc.class.isAssignableFrom(elementType)
418                     || Character.TYPE.isAssignableFrom(elementType)) {
419                 return Types.VARCHAR;
420             }
421             else if (Byte JavaDoc.class.isAssignableFrom(elementType)
422                     || Byte.TYPE.isAssignableFrom(elementType)) {
423                 return Types.VARBINARY;
424             }
425         }
426         
427         if (Calendar JavaDoc.class.isAssignableFrom(javaClass)) {
428             return Types.TIMESTAMP;
429         }
430         else if (BigInteger JavaDoc.class.isAssignableFrom(javaClass)) {
431             return Types.BIGINT;
432         }
433         // serializable check should be the last one when all other mapping attempts
434
// failed
435
else if (Serializable JavaDoc.class.isAssignableFrom(javaClass)) {
436             return Types.VARBINARY;
437         }
438
439         return NOT_DEFINED;
440     }
441
442     /**
443      * Get the corresponding Java type by its java.sql.Types counterpart.
444      *
445      * @return Fully qualified Java type name or null if not found.
446      */

447     public static String JavaDoc getJavaBySqlType(int type) {
448         return (String JavaDoc) sqlEnumJava.get(new Integer JavaDoc(type));
449     }
450
451     /**
452      * Get the corresponding Java type by its java.sql.Types counterpart.
453      *
454      * @return Fully qualified Java type name or null if not found.
455      */

456     public static String JavaDoc getJavaBySqlType(int type, int length, int precision) {
457
458         if (type == Types.NUMERIC && precision == 0) {
459             type = Types.INTEGER;
460         }
461         return (String JavaDoc) sqlEnumJava.get(new Integer JavaDoc(type));
462     }
463
464     // *************************************************************
465
// non-static code
466
// *************************************************************
467

468     protected Map JavaDoc databaseTypes = new HashMap JavaDoc();
469
470     public TypesMapping(DatabaseMetaData JavaDoc metaData) throws SQLException JavaDoc {
471         // map database types to standard JDBC types
472
ResultSet JavaDoc rs = metaData.getTypeInfo();
473
474         try {
475             while (rs.next()) {
476                 TypeInfo info = new TypeInfo();
477                 info.name = rs.getString("TYPE_NAME");
478                 info.jdbcType = rs.getInt("DATA_TYPE");
479                 info.precision = rs.getLong("PRECISION");
480
481                 Integer JavaDoc key = new Integer JavaDoc(info.jdbcType);
482                 List JavaDoc infos = (List JavaDoc) databaseTypes.get(key);
483
484                 if (infos == null) {
485                     infos = new ArrayList JavaDoc();
486                     databaseTypes.put(key, infos);
487                 }
488
489                 infos.add(info);
490             }
491         }
492         finally {
493             rs.close();
494         }
495
496         // do some tricks to substitute for missing datatypes
497

498         // 1. swap TIMESTAMP - DATE
499
Integer JavaDoc ts = new Integer JavaDoc(Types.TIMESTAMP);
500         Integer JavaDoc dt = new Integer JavaDoc(Types.DATE);
501         List JavaDoc tsInfo = (List JavaDoc) databaseTypes.get(ts);
502         List JavaDoc dtInfo = (List JavaDoc) databaseTypes.get(dt);
503
504         if (tsInfo != null && dtInfo == null)
505             databaseTypes.put(dt, tsInfo);
506
507         if (dtInfo != null && tsInfo == null)
508             databaseTypes.put(ts, dtInfo);
509
510         // 2. Swap CLOB - LONGVARCHAR
511
Integer JavaDoc clob = new Integer JavaDoc(Types.CLOB);
512         Integer JavaDoc lvc = new Integer JavaDoc(Types.LONGVARCHAR);
513         List JavaDoc clobInfo = (List JavaDoc) databaseTypes.get(clob);
514         List JavaDoc lvcInfo = (List JavaDoc) databaseTypes.get(lvc);
515
516         if (clobInfo != null && lvcInfo == null)
517             databaseTypes.put(lvc, clobInfo);
518
519         if (lvcInfo != null && clobInfo == null)
520             databaseTypes.put(clob, lvcInfo);
521
522         // 2. Swap BLOB - LONGVARBINARY
523
Integer JavaDoc blob = new Integer JavaDoc(Types.BLOB);
524         Integer JavaDoc lvb = new Integer JavaDoc(Types.LONGVARBINARY);
525         List JavaDoc blobInfo = (List JavaDoc) databaseTypes.get(blob);
526         List JavaDoc lvbInfo = (List JavaDoc) databaseTypes.get(lvb);
527
528         if (blobInfo != null && lvbInfo == null)
529             databaseTypes.put(lvb, blobInfo);
530
531         if (lvbInfo != null && blobInfo == null)
532             databaseTypes.put(blob, lvbInfo);
533     }
534
535     /** Stores (incomplete) information about database data type */
536     static class TypeInfo {
537
538         String JavaDoc name;
539         int jdbcType;
540         long precision;
541
542         public String JavaDoc toString() {
543             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
544             buf.append("[ TypeInfo: ").append(name);
545             buf.append("\n JDBC Type: ").append(
546                     TypesMapping.getSqlNameByType(jdbcType));
547             buf.append("\n Precision: ").append(precision);
548             buf.append("\n]");
549             return buf.toString();
550         }
551     }
552
553 }
554
Popular Tags