KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > constraints > JdbcTypeHelper


1 package xdoclet.modules.ojb.constraints;
2
3 import java.util.HashMap JavaDoc;
4
5 /* Copyright 2004-2005 The Apache Software Foundation
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 /**
21  * Helper class for jdbc-type related things.
22  *
23  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
24  */

25 public class JdbcTypeHelper
26 {
27     public final static String JavaDoc JDBC_DEFAULT_TYPE = "LONGVARBINARY";
28     public final static String JavaDoc JDBC_DEFAULT_TYPE_FOR_ARRAY = "LONGVARBINARY";
29     public final static String JavaDoc JDBC_DEFAULT_CONVERSION = "org.apache.ojb.broker.accesslayer.conversions.Object2ByteArrFieldConversion";
30
31     /**
32      * Contains the default java->jdbc mappings
33      */

34     private static HashMap JavaDoc _jdbcMappings = new HashMap JavaDoc();
35     /**
36      * Contains conversions for the default java->jdbc mappings
37      */

38     private static HashMap JavaDoc _jdbcConversions = new HashMap JavaDoc();
39     /**
40      * Contains default lengths for jdbc types
41      */

42     private static HashMap JavaDoc _jdbcLengths = new HashMap JavaDoc();
43     /**
44      * Contains default precisions for jdbc types
45      */

46     private static HashMap JavaDoc _jdbcPrecisions = new HashMap JavaDoc();
47     /**
48      * Contains default scales for jdbc types
49      */

50     private static HashMap JavaDoc _jdbcScales = new HashMap JavaDoc();
51
52     static
53     {
54         // mappings
55
_jdbcMappings.put("boolean", "BIT");
56         _jdbcMappings.put("byte", "TINYINT");
57         _jdbcMappings.put("short", "SMALLINT");
58         _jdbcMappings.put("int", "INTEGER");
59         _jdbcMappings.put("long", "BIGINT");
60         _jdbcMappings.put("char", "CHAR");
61         _jdbcMappings.put("float", "REAL");
62         _jdbcMappings.put("double", "FLOAT");
63         _jdbcMappings.put("java.lang.Boolean", "BIT");
64         _jdbcMappings.put("java.lang.Byte", "TINYINT");
65         _jdbcMappings.put("java.lang.Short", "SMALLINT");
66         _jdbcMappings.put("java.lang.Integer", "INTEGER");
67         _jdbcMappings.put("java.lang.Long", "BIGINT");
68         _jdbcMappings.put("java.lang.Character", "CHAR");
69         _jdbcMappings.put("java.lang.Float", "REAL");
70         _jdbcMappings.put("java.lang.Double", "FLOAT");
71         _jdbcMappings.put("java.lang.String", "VARCHAR");
72         _jdbcMappings.put("java.util.Date", "DATE");
73         _jdbcMappings.put("java.sql.Blob", "BLOB");
74         _jdbcMappings.put("java.sql.Clob", "CLOB");
75         _jdbcMappings.put("java.sql.Date", "DATE");
76         _jdbcMappings.put("java.sql.Time", "TIME");
77         _jdbcMappings.put("java.sql.Timestamp", "TIMESTAMP");
78         _jdbcMappings.put("java.math.BigDecimal", "DECIMAL");
79         _jdbcMappings.put("org.apache.ojb.broker.util.GUID", "VARCHAR");
80
81         // conversions
82
_jdbcConversions.put("org.apache.ojb.broker.util.GUID",
83                              "org.apache.ojb.broker.accesslayer.conversions.GUID2StringFieldConversion");
84
85         // lengths
86
_jdbcLengths.put("CHAR", "1");
87         _jdbcLengths.put("VARCHAR", "254");
88
89         // precisions
90
_jdbcPrecisions.put("DECIMAL", "20");
91         _jdbcPrecisions.put("NUMERIC", "20");
92
93         // scales
94
_jdbcScales.put("DECIMAL", "0");
95         _jdbcScales.put("NUMERIC", "0");
96     }
97
98     /**
99      * Returns the default jdbc type for the given java type.
100      *
101      * @param javaType The qualified java type
102      * @return The default jdbc type
103      */

104     public static String JavaDoc getDefaultJdbcTypeFor(String JavaDoc javaType)
105     {
106         return _jdbcMappings.containsKey(javaType) ? (String JavaDoc)_jdbcMappings.get(javaType) : JDBC_DEFAULT_TYPE;
107     }
108
109     /**
110      * Returns the default conversion for the given java type.
111      *
112      * @param javaType The qualified java type
113      * @return The default conversion or <code>null</code> if there is no default conversion for the type
114      */

115     public static String JavaDoc getDefaultConversionFor(String JavaDoc javaType)
116     {
117         return _jdbcConversions.containsKey(javaType) ? (String JavaDoc)_jdbcConversions.get(javaType) : null;
118     }
119
120     /**
121      * Returns the default length for the given jdbc type.
122      *
123      * @param jdbcType The jdbc type
124      * @return The default length or <code>null</code> if there is none defined for this jdbc type
125      */

126     public static String JavaDoc getDefaultLengthFor(String JavaDoc jdbcType)
127     {
128         return (String JavaDoc)_jdbcLengths.get(jdbcType);
129     }
130
131     /**
132      * Returns the default precision for the given jdbc type.
133      *
134      * @param jdbcType The jdbc type
135      * @return The default precision or <code>null</code> if there is none defined for this jdbc type
136      */

137     public static String JavaDoc getDefaultPrecisionFor(String JavaDoc jdbcType)
138     {
139         return (String JavaDoc)_jdbcPrecisions.get(jdbcType);
140     }
141
142     /**
143      * Returns the default scale for the given jdbc type.
144      *
145      * @param jdbcType The jdbc type
146      * @return The default scale or <code>null</code> if there is none defined for this jdbc type
147      */

148     public static String JavaDoc getDefaultScaleFor(String JavaDoc jdbcType)
149     {
150         return (String JavaDoc)_jdbcScales.get(jdbcType);
151     }
152 }
153
Popular Tags