View Javadoc

1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
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  
19  import java.sql.Types;
20  import java.util.Hashtable;
21  import java.util.Iterator;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  // I don't know if the peer system deals
27  // with the recommended mappings.
28  //
29  //import java.sql.Date;
30  //import java.sql.Time;
31  //import java.sql.Timestamp;
32  
33  /***
34   * A class that maps JDBC types to their corresponding
35   * Java object types, and Java native types. Used
36   * by Column.java to perform object/native mappings.
37   *
38   * These are the official SQL type to Java type mappings.
39   * These don't quite correspond to the way the peer
40   * system works so we'll have to make some adjustments.
41   * <pre>
42   * -------------------------------------------------------
43   * SQL Type      | Java Type            | Peer Type
44   * -------------------------------------------------------
45   * CHAR          | String               | String
46   * VARCHAR       | String               | String
47   * LONGVARCHAR   | String               | String
48   * NUMERIC       | java.math.BigDecimal | java.math.BigDecimal
49   * DECIMAL       | java.math.BigDecimal | java.math.BigDecimal
50   * BIT           | boolean OR Boolean   | Boolean
51   * TINYINT       | byte OR Byte         | Byte
52   * SMALLINT      | short OR Short       | Short
53   * INTEGER       | int OR Integer       | Integer
54   * BIGINT        | long OR Long         | Long
55   * REAL          | float OR Float       | Float
56   * FLOAT         | double OR Double     | Double
57   * DOUBLE        | double OR Double     | Double
58   * BINARY        | byte[]               | ?
59   * VARBINARY     | byte[]               | ?
60   * LONGVARBINARY | byte[]               | ?
61   * DATE          | java.sql.Date        | java.util.Date
62   * TIME          | java.sql.Time        | java.util.Date
63   * TIMESTAMP     | java.sql.Timestamp   | java.util.Date
64   *
65   * -------------------------------------------------------
66   * A couple variations have been introduced to cover cases
67   * that may arise, but are not covered above
68   * BOOLEANCHAR   | boolean OR Boolean   | String
69   * BOOLEANINT    | boolean OR Boolean   | Integer
70   * </pre>
71   *
72   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
73   * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
74   * @version $Id: TypeMap.java 239626 2005-08-24 12:19:51Z henning $
75   */
76  public class TypeMap
77  {
78      /*** Logging class from commons.logging */
79      private static Log log = LogFactory.getLog(Column.class);
80  
81      private static final SchemaType[] TEXT_TYPES =
82      {
83          SchemaType.CHAR, SchemaType.VARCHAR, SchemaType.LONGVARCHAR,
84          SchemaType.CLOB, SchemaType.DATE, SchemaType.TIME,
85          SchemaType.TIMESTAMP, SchemaType.BOOLEANCHAR
86      };
87  
88      public static final String CHAR_OBJECT_TYPE = "\"\"";
89      public static final String VARCHAR_OBJECT_TYPE = "\"\"";
90      public static final String LONGVARCHAR_OBJECT_TYPE = "\"\"";
91      public static final String CLOB_OBJECT_TYPE = "\"\"";
92      public static final String NUMERIC_OBJECT_TYPE = "new BigDecimal(0)";
93      public static final String DECIMAL_OBJECT_TYPE = "new BigDecimal(0)";
94      public static final String BIT_OBJECT_TYPE = "new Boolean(true)";
95      public static final String TINYINT_OBJECT_TYPE = "new Byte((byte)0)";
96      public static final String SMALLINT_OBJECT_TYPE = "new Short((short)0)";
97      public static final String INTEGER_OBJECT_TYPE = "new Integer(0)";
98      public static final String BIGINT_OBJECT_TYPE = "new Long(0)";
99      public static final String REAL_OBJECT_TYPE = "new Float(0)";
100     public static final String FLOAT_OBJECT_TYPE = "new Double(0)";
101     public static final String DOUBLE_OBJECT_TYPE = "new Double(0)";
102     public static final String BINARY_OBJECT_TYPE = "new Object()"; //?
103     public static final String VARBINARY_OBJECT_TYPE = "new Object()"; //?
104     public static final String LONGVARBINARY_OBJECT_TYPE = "new Object()"; //?
105     public static final String BLOB_OBJECT_TYPE = "new Object()"; //?
106     public static final String DATE_OBJECT_TYPE = "new Date()";
107     public static final String TIME_OBJECT_TYPE = "new Date()";
108     public static final String TIMESTAMP_OBJECT_TYPE = "new Date()";
109     public static final String BOOLEANCHAR_OBJECT_TYPE = "\"\"";
110     public static final String BOOLEANINT_OBJECT_TYPE = "new Integer(0)";
111 
112     public static final String CHAR_NATIVE_TYPE = "String";
113     public static final String VARCHAR_NATIVE_TYPE = "String";
114     public static final String LONGVARCHAR_NATIVE_TYPE = "String";
115     public static final String CLOB_NATIVE_TYPE = "String";
116     public static final String NUMERIC_NATIVE_TYPE = "BigDecimal";
117     public static final String DECIMAL_NATIVE_TYPE = "BigDecimal";
118     public static final String BIT_NATIVE_TYPE = "boolean";
119     public static final String TINYINT_NATIVE_TYPE = "byte";
120     public static final String SMALLINT_NATIVE_TYPE = "short";
121     public static final String INTEGER_NATIVE_TYPE = "int";
122     public static final String BIGINT_NATIVE_TYPE = "long";
123     public static final String REAL_NATIVE_TYPE = "float";
124     public static final String FLOAT_NATIVE_TYPE = "double";
125     public static final String DOUBLE_NATIVE_TYPE = "double";
126     public static final String BINARY_NATIVE_TYPE = "byte[]";
127     public static final String VARBINARY_NATIVE_TYPE = "byte[]";
128     public static final String LONGVARBINARY_NATIVE_TYPE = "byte[]";
129     public static final String BLOB_NATIVE_TYPE = "byte[]";
130     public static final String DATE_NATIVE_TYPE = "Date";
131     public static final String TIME_NATIVE_TYPE = "Date";
132     public static final String TIMESTAMP_NATIVE_TYPE = "Date";
133     public static final String BOOLEANCHAR_NATIVE_TYPE = "boolean";
134     public static final String BOOLEANINT_NATIVE_TYPE = "boolean";
135 
136     public static final String BIT_NATIVE_OBJECT_TYPE = "Boolean";
137     public static final String TINYINT_NATIVE_OBJECT_TYPE = "Byte";
138     public static final String SMALLINT_NATIVE_OBJECT_TYPE = "Short";
139     public static final String INTEGER_NATIVE_OBJECT_TYPE = "Integer";
140     public static final String BIGINT_NATIVE_OBJECT_TYPE = "Long";
141     public static final String REAL_NATIVE_OBJECT_TYPE = "Float";
142     public static final String FLOAT_NATIVE_OBJECT_TYPE = "Double";
143     public static final String DOUBLE_NATIVE_OBJECT_TYPE = "Double";
144     public static final String BOOLEANCHAR_NATIVE_OBJECT_TYPE = "Boolean";
145     public static final String BOOLEANINT_NATIVE_OBJECT_TYPE = "Boolean";
146 
147     public static final String CHAR_VILLAGE_METHOD = "asString()";
148     public static final String VARCHAR_VILLAGE_METHOD = "asString()";
149     public static final String LONGVARCHAR_VILLAGE_METHOD = "asString()";
150     public static final String CLOB_VILLAGE_METHOD = "asString()";
151     public static final String NUMERIC_VILLAGE_METHOD = "asBigDecimal()";
152     public static final String DECIMAL_VILLAGE_METHOD = "asBigDecimal()";
153     public static final String BIT_VILLAGE_METHOD = "asBoolean()";
154     public static final String TINYINT_VILLAGE_METHOD = "asByte()";
155     public static final String SMALLINT_VILLAGE_METHOD = "asShort()";
156     public static final String INTEGER_VILLAGE_METHOD = "asInt()";
157     public static final String BIGINT_VILLAGE_METHOD = "asLong()";
158     public static final String REAL_VILLAGE_METHOD = "asFloat()";
159     public static final String FLOAT_VILLAGE_METHOD = "asDouble()";
160     public static final String DOUBLE_VILLAGE_METHOD = "asDouble()";
161     public static final String BINARY_VILLAGE_METHOD = "asBytes()";
162     public static final String VARBINARY_VILLAGE_METHOD = "asBytes()";
163     public static final String LONGVARBINARY_VILLAGE_METHOD = "asBytes()";
164     public static final String BLOB_VILLAGE_METHOD = "asBytes()";
165     public static final String DATE_VILLAGE_METHOD = "asUtilDate()";
166     public static final String TIME_VILLAGE_METHOD = "asUtilDate()";
167     public static final String TIMESTAMP_VILLAGE_METHOD = "asUtilDate()";
168     public static final String BOOLEANCHAR_VILLAGE_METHOD = "asBoolean()";
169     public static final String BOOLEANINT_VILLAGE_METHOD = "asBoolean()";
170 
171     public static final String BIT_VILLAGE_OBJECT_METHOD = "asBooleanObj()";
172     public static final String TINYINT_VILLAGE_OBJECT_METHOD = "asByteObj()";
173     public static final String SMALLINT_VILLAGE_OBJECT_METHOD = "asShortObj()";
174     public static final String INTEGER_VILLAGE_OBJECT_METHOD = "asIntegerObj()";
175     public static final String BIGINT_VILLAGE_OBJECT_METHOD = "asLongObj()";
176     public static final String REAL_VILLAGE_OBJECT_METHOD = "asFloatObj()";
177     public static final String FLOAT_VILLAGE_OBJECT_METHOD = "asDoubleObj()";
178     public static final String DOUBLE_VILLAGE_OBJECT_METHOD = "asDoubleObj()";
179     public static final String BOOLEANCHAR_VILLAGE_OBJECT_METHOD = "asBooleanObj()";
180     public static final String BOOLEANINT_VILLAGE_OBJECT_METHOD = "asBooleanObj()";
181 
182     public static final String CHAR_PP_METHOD = "getString(ppKey)";
183     public static final String VARCHAR_PP_METHOD = "getString(ppKey)";
184     public static final String LONGVARCHAR_PP_METHOD = "getString(ppKey)";
185     public static final String NUMERIC_PP_METHOD = "getBigDecimal(ppKey)";
186     public static final String DECIMAL_PP_METHOD = "getBigDecimal(ppKey)";
187     public static final String BIT_PP_METHOD = "getBoolean(ppKey)";
188     public static final String TINYINT_PP_METHOD = "getByte(ppKey)";
189     public static final String SMALLINT_PP_METHOD = "getShort(ppKey)";
190     public static final String INTEGER_PP_METHOD = "getInt(ppKey)";
191     public static final String BIGINT_PP_METHOD = "getLong(ppKey)";
192     public static final String REAL_PP_METHOD = "getFloat(ppKey)";
193     public static final String FLOAT_PP_METHOD = "getDouble(ppKey)";
194     public static final String DOUBLE_PP_METHOD = "getDouble(ppKey)";
195     public static final String BINARY_PP_METHOD = "getBytes(ppKey)";
196     public static final String VARBINARY_PP_METHOD = "getBytes(ppKey)";
197     public static final String LONGVARBINARY_PP_METHOD = "getBytes(ppKey)";
198     public static final String DATE_PP_METHOD = "getDate(ppKey)";
199     public static final String TIME_PP_METHOD = "getDate(ppKey)";
200     public static final String TIMESTAMP_PP_METHOD = "getDate(ppKey)";
201     public static final String BOOLEANCHAR_PP_METHOD = "getBoolean(ppKey)";
202     public static final String BOOLEANINT_PP_METHOD = "getBoolean(ppKey)";
203 
204     private static Hashtable jdbcToJavaObjectMap = null;
205     private static Hashtable jdbcToJavaNativeMap = null;
206     private static Hashtable jdbcToJavaNativeObjectMap = null;
207     private static Hashtable jdbcToVillageMethodMap = null;
208     private static Hashtable jdbcToVillageObjectMethodMap = null;
209     private static Hashtable jdbcToPPMethodMap = null;
210     private static Hashtable torqueTypeToJdbcTypeMap = null;
211     private static Hashtable jdbcToTorqueTypeMap = null;
212     private static boolean isInitialized = false;
213 
214     /***
215      * Initializes the SQL to Java map so that it
216      * can be used by client code.
217      */
218     public synchronized static void initialize()
219     {
220         if (!isInitialized)
221         {
222             // Create JDBC -> Java object mappings.
223             jdbcToJavaObjectMap = new Hashtable();
224 
225             jdbcToJavaObjectMap.put(SchemaType.CHAR, CHAR_OBJECT_TYPE);
226             jdbcToJavaObjectMap.put(SchemaType.VARCHAR, VARCHAR_OBJECT_TYPE);
227             jdbcToJavaObjectMap.put(SchemaType.LONGVARCHAR, LONGVARCHAR_OBJECT_TYPE);
228             jdbcToJavaObjectMap.put(SchemaType.CLOB, CLOB_OBJECT_TYPE);
229             jdbcToJavaObjectMap.put(SchemaType.NUMERIC, NUMERIC_OBJECT_TYPE);
230             jdbcToJavaObjectMap.put(SchemaType.DECIMAL, DECIMAL_OBJECT_TYPE);
231             jdbcToJavaObjectMap.put(SchemaType.BIT, BIT_OBJECT_TYPE);
232             jdbcToJavaObjectMap.put(SchemaType.TINYINT, TINYINT_OBJECT_TYPE);
233             jdbcToJavaObjectMap.put(SchemaType.SMALLINT, SMALLINT_OBJECT_TYPE);
234             jdbcToJavaObjectMap.put(SchemaType.INTEGER, INTEGER_OBJECT_TYPE);
235             jdbcToJavaObjectMap.put(SchemaType.BIGINT, BIGINT_OBJECT_TYPE);
236             jdbcToJavaObjectMap.put(SchemaType.REAL, REAL_OBJECT_TYPE);
237             jdbcToJavaObjectMap.put(SchemaType.FLOAT, FLOAT_OBJECT_TYPE);
238             jdbcToJavaObjectMap.put(SchemaType.DOUBLE, DOUBLE_OBJECT_TYPE);
239             jdbcToJavaObjectMap.put(SchemaType.BINARY, BINARY_OBJECT_TYPE);
240             jdbcToJavaObjectMap.put(SchemaType.VARBINARY, VARBINARY_OBJECT_TYPE);
241             jdbcToJavaObjectMap.put(SchemaType.LONGVARBINARY, LONGVARBINARY_OBJECT_TYPE);
242             jdbcToJavaObjectMap.put(SchemaType.BLOB, BLOB_OBJECT_TYPE);
243             jdbcToJavaObjectMap.put(SchemaType.DATE, DATE_OBJECT_TYPE);
244             jdbcToJavaObjectMap.put(SchemaType.TIME, TIME_OBJECT_TYPE);
245             jdbcToJavaObjectMap.put(SchemaType.TIMESTAMP, TIMESTAMP_OBJECT_TYPE);
246             jdbcToJavaObjectMap.put(SchemaType.BOOLEANCHAR, BOOLEANCHAR_OBJECT_TYPE);
247             jdbcToJavaObjectMap.put(SchemaType.BOOLEANINT, BOOLEANINT_OBJECT_TYPE);
248 
249             // Create JDBC -> native Java type mappings.
250             jdbcToJavaNativeMap = new Hashtable();
251 
252             jdbcToJavaNativeMap.put(SchemaType.CHAR, CHAR_NATIVE_TYPE);
253             jdbcToJavaNativeMap.put(SchemaType.VARCHAR, VARCHAR_NATIVE_TYPE);
254             jdbcToJavaNativeMap.put(SchemaType.LONGVARCHAR, LONGVARCHAR_NATIVE_TYPE);
255             jdbcToJavaNativeMap.put(SchemaType.CLOB, CLOB_NATIVE_TYPE);
256             jdbcToJavaNativeMap.put(SchemaType.NUMERIC, NUMERIC_NATIVE_TYPE);
257             jdbcToJavaNativeMap.put(SchemaType.DECIMAL, DECIMAL_NATIVE_TYPE);
258             jdbcToJavaNativeMap.put(SchemaType.BIT, BIT_NATIVE_TYPE);
259             jdbcToJavaNativeMap.put(SchemaType.TINYINT, TINYINT_NATIVE_TYPE);
260             jdbcToJavaNativeMap.put(SchemaType.SMALLINT, SMALLINT_NATIVE_TYPE);
261             jdbcToJavaNativeMap.put(SchemaType.INTEGER, INTEGER_NATIVE_TYPE);
262             jdbcToJavaNativeMap.put(SchemaType.BIGINT, BIGINT_NATIVE_TYPE);
263             jdbcToJavaNativeMap.put(SchemaType.REAL, REAL_NATIVE_TYPE);
264             jdbcToJavaNativeMap.put(SchemaType.FLOAT, FLOAT_NATIVE_TYPE);
265             jdbcToJavaNativeMap.put(SchemaType.DOUBLE, DOUBLE_NATIVE_TYPE);
266             jdbcToJavaNativeMap.put(SchemaType.BINARY, BINARY_NATIVE_TYPE);
267             jdbcToJavaNativeMap.put(SchemaType.VARBINARY, VARBINARY_NATIVE_TYPE);
268             jdbcToJavaNativeMap.put(SchemaType.LONGVARBINARY, LONGVARBINARY_NATIVE_TYPE);
269             jdbcToJavaNativeMap.put(SchemaType.BLOB, BLOB_NATIVE_TYPE);
270             jdbcToJavaNativeMap.put(SchemaType.DATE, DATE_NATIVE_TYPE);
271             jdbcToJavaNativeMap.put(SchemaType.TIME, TIME_NATIVE_TYPE);
272             jdbcToJavaNativeMap.put(SchemaType.TIMESTAMP, TIMESTAMP_NATIVE_TYPE);
273             jdbcToJavaNativeMap.put(SchemaType.BOOLEANCHAR, BOOLEANCHAR_NATIVE_TYPE);
274             jdbcToJavaNativeMap.put(SchemaType.BOOLEANINT, BOOLEANINT_NATIVE_TYPE);
275 
276             jdbcToJavaNativeObjectMap = new Hashtable();
277             jdbcToJavaNativeObjectMap.put(SchemaType.BIT, BIT_NATIVE_OBJECT_TYPE);
278             jdbcToJavaNativeObjectMap.put(SchemaType.TINYINT, TINYINT_NATIVE_OBJECT_TYPE);
279             jdbcToJavaNativeObjectMap.put(SchemaType.SMALLINT, SMALLINT_NATIVE_OBJECT_TYPE);
280             jdbcToJavaNativeObjectMap.put(SchemaType.INTEGER, INTEGER_NATIVE_OBJECT_TYPE);
281             jdbcToJavaNativeObjectMap.put(SchemaType.BIGINT, BIGINT_NATIVE_OBJECT_TYPE);
282             jdbcToJavaNativeObjectMap.put(SchemaType.REAL, REAL_NATIVE_OBJECT_TYPE);
283             jdbcToJavaNativeObjectMap.put(SchemaType.FLOAT, FLOAT_NATIVE_OBJECT_TYPE);
284             jdbcToJavaNativeObjectMap.put(SchemaType.DOUBLE, DOUBLE_NATIVE_OBJECT_TYPE);
285             jdbcToJavaNativeObjectMap.put(SchemaType.BOOLEANCHAR,
286                                           BOOLEANCHAR_NATIVE_OBJECT_TYPE);
287             jdbcToJavaNativeObjectMap.put(SchemaType.BOOLEANINT,
288                                           BOOLEANINT_NATIVE_OBJECT_TYPE);
289 
290             // Create JDBC -> Village asX() mappings.
291             jdbcToVillageMethodMap = new Hashtable();
292 
293             jdbcToVillageMethodMap.put(SchemaType.CHAR, CHAR_VILLAGE_METHOD);
294             jdbcToVillageMethodMap.put(SchemaType.VARCHAR, VARCHAR_VILLAGE_METHOD);
295             jdbcToVillageMethodMap.put(SchemaType.LONGVARCHAR, LONGVARCHAR_VILLAGE_METHOD);
296             jdbcToVillageMethodMap.put(SchemaType.CLOB, CLOB_VILLAGE_METHOD);
297             jdbcToVillageMethodMap.put(SchemaType.NUMERIC, NUMERIC_VILLAGE_METHOD);
298             jdbcToVillageMethodMap.put(SchemaType.DECIMAL, DECIMAL_VILLAGE_METHOD);
299             jdbcToVillageMethodMap.put(SchemaType.BIT, BIT_VILLAGE_METHOD);
300             jdbcToVillageMethodMap.put(SchemaType.TINYINT, TINYINT_VILLAGE_METHOD);
301             jdbcToVillageMethodMap.put(SchemaType.SMALLINT, SMALLINT_VILLAGE_METHOD);
302             jdbcToVillageMethodMap.put(SchemaType.INTEGER, INTEGER_VILLAGE_METHOD);
303             jdbcToVillageMethodMap.put(SchemaType.BIGINT, BIGINT_VILLAGE_METHOD);
304             jdbcToVillageMethodMap.put(SchemaType.REAL, REAL_VILLAGE_METHOD);
305             jdbcToVillageMethodMap.put(SchemaType.FLOAT, FLOAT_VILLAGE_METHOD);
306             jdbcToVillageMethodMap.put(SchemaType.DOUBLE, DOUBLE_VILLAGE_METHOD);
307             jdbcToVillageMethodMap.put(SchemaType.BINARY, BINARY_VILLAGE_METHOD);
308             jdbcToVillageMethodMap.put(SchemaType.VARBINARY, VARBINARY_VILLAGE_METHOD);
309             jdbcToVillageMethodMap.put(SchemaType.LONGVARBINARY, LONGVARBINARY_VILLAGE_METHOD);
310             jdbcToVillageMethodMap.put(SchemaType.BLOB, BLOB_VILLAGE_METHOD);
311             jdbcToVillageMethodMap.put(SchemaType.DATE, DATE_VILLAGE_METHOD);
312             jdbcToVillageMethodMap.put(SchemaType.TIME, TIME_VILLAGE_METHOD);
313             jdbcToVillageMethodMap.put(SchemaType.TIMESTAMP, TIMESTAMP_VILLAGE_METHOD);
314             jdbcToVillageMethodMap.put(SchemaType.BOOLEANCHAR, BOOLEANCHAR_VILLAGE_METHOD);
315             jdbcToVillageMethodMap.put(SchemaType.BOOLEANINT, BOOLEANINT_VILLAGE_METHOD);
316 
317 
318             jdbcToVillageObjectMethodMap = new Hashtable();
319             jdbcToVillageObjectMethodMap.put(SchemaType.BIT, BIT_VILLAGE_OBJECT_METHOD);
320             jdbcToVillageObjectMethodMap.put(SchemaType.TINYINT,
321                                              TINYINT_VILLAGE_OBJECT_METHOD);
322             jdbcToVillageObjectMethodMap.put(SchemaType.SMALLINT,
323                                              SMALLINT_VILLAGE_OBJECT_METHOD);
324             jdbcToVillageObjectMethodMap.put(SchemaType.INTEGER,
325                                              INTEGER_VILLAGE_OBJECT_METHOD);
326             jdbcToVillageObjectMethodMap.put(SchemaType.BIGINT,
327                                              BIGINT_VILLAGE_OBJECT_METHOD);
328             jdbcToVillageObjectMethodMap.put(SchemaType.REAL, REAL_VILLAGE_OBJECT_METHOD);
329             jdbcToVillageObjectMethodMap.put(SchemaType.FLOAT, FLOAT_VILLAGE_OBJECT_METHOD);
330             jdbcToVillageObjectMethodMap.put(SchemaType.DOUBLE,
331                                              DOUBLE_VILLAGE_OBJECT_METHOD);
332             jdbcToVillageObjectMethodMap.put(SchemaType.BOOLEANCHAR,
333                                              BOOLEANCHAR_VILLAGE_OBJECT_METHOD);
334             jdbcToVillageObjectMethodMap.put(SchemaType.BOOLEANINT,
335                                              BOOLEANINT_VILLAGE_OBJECT_METHOD);
336 
337             // Create JDBC -> ParameterParser getX() mappings.
338             jdbcToPPMethodMap = new Hashtable();
339 
340             jdbcToPPMethodMap.put(SchemaType.CHAR, CHAR_PP_METHOD);
341             jdbcToPPMethodMap.put(SchemaType.VARCHAR, VARCHAR_PP_METHOD);
342             jdbcToPPMethodMap.put(SchemaType.LONGVARCHAR, LONGVARCHAR_PP_METHOD);
343             jdbcToPPMethodMap.put(SchemaType.NUMERIC, NUMERIC_PP_METHOD);
344             jdbcToPPMethodMap.put(SchemaType.DECIMAL, DECIMAL_PP_METHOD);
345             jdbcToPPMethodMap.put(SchemaType.BIT, BIT_PP_METHOD);
346             jdbcToPPMethodMap.put(SchemaType.TINYINT, TINYINT_PP_METHOD);
347             jdbcToPPMethodMap.put(SchemaType.SMALLINT, SMALLINT_PP_METHOD);
348             jdbcToPPMethodMap.put(SchemaType.INTEGER, INTEGER_PP_METHOD);
349             jdbcToPPMethodMap.put(SchemaType.BIGINT, BIGINT_PP_METHOD);
350             jdbcToPPMethodMap.put(SchemaType.REAL, REAL_PP_METHOD);
351             jdbcToPPMethodMap.put(SchemaType.FLOAT, FLOAT_PP_METHOD);
352             jdbcToPPMethodMap.put(SchemaType.DOUBLE, DOUBLE_PP_METHOD);
353             jdbcToPPMethodMap.put(SchemaType.BINARY, BINARY_PP_METHOD);
354             jdbcToPPMethodMap.put(SchemaType.VARBINARY, VARBINARY_PP_METHOD);
355             jdbcToPPMethodMap.put(SchemaType.LONGVARBINARY, LONGVARBINARY_PP_METHOD);
356             jdbcToPPMethodMap.put(SchemaType.DATE, DATE_PP_METHOD);
357             jdbcToPPMethodMap.put(SchemaType.TIME, TIME_PP_METHOD);
358             jdbcToPPMethodMap.put(SchemaType.TIMESTAMP, TIMESTAMP_PP_METHOD);
359             jdbcToPPMethodMap.put(SchemaType.BOOLEANCHAR, BOOLEANCHAR_PP_METHOD);
360             jdbcToPPMethodMap.put(SchemaType.BOOLEANINT, BOOLEANINT_PP_METHOD);
361 
362             // Create JDBC -> Java object mappings.
363             torqueTypeToJdbcTypeMap = new Hashtable();
364 
365             Iterator iter = SchemaType.iterator();
366             while (iter.hasNext())
367             {
368                 SchemaType type = (SchemaType) iter.next();
369                 torqueTypeToJdbcTypeMap.put(type, type);
370             }
371             torqueTypeToJdbcTypeMap.put(SchemaType.BOOLEANCHAR, SchemaType.CHAR);
372             torqueTypeToJdbcTypeMap.put(SchemaType.BOOLEANINT, SchemaType.INTEGER);
373 
374             // Create JDBC type code to torque type map.
375             jdbcToTorqueTypeMap = new Hashtable();
376 
377             jdbcToTorqueTypeMap.put(new Integer(Types.CHAR), SchemaType.CHAR);
378             jdbcToTorqueTypeMap.put(new Integer(Types.VARCHAR), SchemaType.VARCHAR);
379             jdbcToTorqueTypeMap.put(new Integer(Types.LONGVARCHAR), SchemaType.LONGVARCHAR);
380             jdbcToTorqueTypeMap.put(new Integer(Types.CLOB), SchemaType.CLOB);
381             jdbcToTorqueTypeMap.put(new Integer(Types.NUMERIC), SchemaType.NUMERIC);
382             jdbcToTorqueTypeMap.put(new Integer(Types.DECIMAL), SchemaType.DECIMAL);
383             jdbcToTorqueTypeMap.put(new Integer(Types.BIT), SchemaType.BIT);
384             jdbcToTorqueTypeMap.put(new Integer(Types.TINYINT), SchemaType.TINYINT);
385             jdbcToTorqueTypeMap.put(new Integer(Types.SMALLINT), SchemaType.SMALLINT);
386             jdbcToTorqueTypeMap.put(new Integer(Types.INTEGER), SchemaType.INTEGER);
387             jdbcToTorqueTypeMap.put(new Integer(Types.BIGINT), SchemaType.BIGINT);
388             jdbcToTorqueTypeMap.put(new Integer(Types.REAL), SchemaType.REAL);
389             jdbcToTorqueTypeMap.put(new Integer(Types.FLOAT), SchemaType.FLOAT);
390             jdbcToTorqueTypeMap.put(new Integer(Types.DOUBLE), SchemaType.DOUBLE);
391             jdbcToTorqueTypeMap.put(new Integer(Types.BINARY), SchemaType.BINARY);
392             jdbcToTorqueTypeMap.put(new Integer(Types.VARBINARY), SchemaType.VARBINARY);
393             jdbcToTorqueTypeMap.put(new Integer(Types.LONGVARBINARY), SchemaType.LONGVARBINARY);
394             jdbcToTorqueTypeMap.put(new Integer(Types.BLOB), SchemaType.BLOB);
395             jdbcToTorqueTypeMap.put(new Integer(Types.DATE), SchemaType.DATE);
396             jdbcToTorqueTypeMap.put(new Integer(Types.TIME), SchemaType.TIME);
397             jdbcToTorqueTypeMap.put(new Integer(Types.TIMESTAMP), SchemaType.TIMESTAMP);
398 
399             isInitialized = true;
400         }
401     }
402 
403     /***
404      * Report whether this object has been initialized.
405      *
406      * @return true if this object has been initialized
407      */
408     public static boolean isInitialized()
409     {
410         return isInitialized;
411     }
412 
413     /***
414      * Return a Java object which corresponds to the
415      * JDBC type provided. Use in MapBuilder generation.
416      *
417      * @param jdbcType the JDBC type
418      * @return name of the Object
419      */
420     public static String getJavaObject(SchemaType jdbcType)
421     {
422         // Make sure the we are initialized.
423         if (!isInitialized)
424         {
425             initialize();
426         }
427         return (String) jdbcToJavaObjectMap.get(jdbcType);
428     }
429 
430     /***
431      * Return native java type which corresponds to the
432      * JDBC type provided. Use in the base object class generation.
433      *
434      * @param jdbcType the JDBC type
435      * @return name of the native java type
436      */
437     public static String getJavaNative(SchemaType jdbcType)
438     {
439         // Make sure the we are initialized.
440         if (!isInitialized)
441         {
442             initialize();
443         }
444         return (String) jdbcToJavaNativeMap.get(jdbcType);
445     }
446 
447     /***
448      * Return native java type which corresponds to the
449      * JDBC type provided. Use in the base object class generation.
450      *
451      * @param jdbcType the JDBC type
452      * @return name of the Object
453      */
454     public static String getJavaNativeObject(SchemaType jdbcType)
455     {
456         // Make sure the we are initialized.
457         if (!isInitialized)
458         {
459             initialize();
460         }
461         String s = (String) jdbcToJavaNativeObjectMap.get(jdbcType);
462         if (s == null)
463         {
464             s = (String) jdbcToJavaNativeMap.get(jdbcType);
465         }
466         return s;
467     }
468 
469     /***
470      * Return Village asX() method which corresponds to the
471      * JDBC type provided. Use in the Peer class generation.
472      *
473      * @param jdbcType the JDBC type
474      * @return name of the Village asX() method
475      */
476     public static String getVillageMethod(SchemaType jdbcType)
477     {
478         // Make sure the we are initialized.
479         if (!isInitialized)
480         {
481             initialize();
482         }
483         return (String) jdbcToVillageMethodMap.get(jdbcType);
484     }
485 
486     /***
487      * Return Village asX() method which corresponds to the
488      * JDBC type provided. Use in the Peer class generation.
489      *
490      * @param jdbcType the JDBC type
491      * @return name of the Village asX() method
492      */
493     public static String getVillageObjectMethod(SchemaType jdbcType)
494     {
495         // Make sure the we are initialized.
496         if (!isInitialized)
497         {
498             initialize();
499         }
500         String s = (String) jdbcToVillageObjectMethodMap.get(jdbcType);
501         if (s == null)
502         {
503             s = (String) jdbcToVillageMethodMap.get(jdbcType);
504         }
505         return s;
506     }
507 
508     /***
509      * Return ParameterParser getX() method which corresponds to the
510      * JDBC type provided. Use in the Object class generation.
511      *
512      * @param jdbcType the JDBC type
513      * @return name of the ParameterParser getX() method
514      */
515     public static String getPPMethod(SchemaType jdbcType)
516     {
517         // Make sure the we are initialized.
518         if (!isInitialized)
519         {
520             initialize();
521         }
522         return (String) jdbcToPPMethodMap.get(jdbcType);
523     }
524 
525     /***
526      * Returns the correct jdbc type for torque added types
527      *
528      * @param type the torque added type
529      * @return name of the the correct jdbc type
530      * @deprecated the type conversion is handled by the platform package
531      *             (since torque 3.2)
532      */
533     public static SchemaType getJdbcType(SchemaType type)
534     {
535         // Make sure the we are initialized.
536         if (!isInitialized)
537         {
538             initialize();
539         }
540         return (SchemaType) torqueTypeToJdbcTypeMap.get(type);
541     }
542 
543     /***
544      * Returns Torque type constant corresponding to JDBC type code.
545      * Used by the Torque JDBC task.
546      *
547      * @param sqlType the SQL type
548      * @return Torque type constant
549      */
550     public static SchemaType getTorqueType(Integer sqlType)
551     {
552         // Make sure the we are initialized.
553         if (!isInitialized)
554         {
555             initialize();
556         }
557         SchemaType st = (SchemaType) jdbcToTorqueTypeMap.get(sqlType);
558         if (st == null)
559         {
560             st = SchemaType.VARCHAR;
561             log.warn("SchemaType for JdbcType '" + sqlType +
562                      "' is not defined: Defaulting to '" + st + '\'');
563         }
564         return st;
565     }
566 
567     /***
568      * Returns true if the type is boolean in the java
569      * object and a numeric (1 or 0) in the db.
570      *
571      * @param type The type to check.
572      * @return true if the type is BOOLEANINT
573      */
574     public static boolean isBooleanInt(SchemaType type)
575     {
576         return SchemaType.BOOLEANINT.equals(type);
577     }
578 
579     /***
580      * Returns true if the type is boolean in the
581      * java object and a String "Y" or "N" in the db.
582      *
583      * @param type The type to check.
584      * @return true if the type is BOOLEANCHAR
585      */
586     public static boolean isBooleanChar(SchemaType type)
587     {
588         return SchemaType.BOOLEANCHAR.equals(type);
589     }
590 
591     /***
592      * Returns true if the type is boolean in the
593      * java object and a Bit "1" or "0" in the db.
594      *
595      * @param type The type to check.
596      * @return true if the type is BIT
597      */
598     public static boolean isBit(SchemaType type)
599     {
600         return SchemaType.BIT.equals(type);
601     }
602 
603     /***
604      * Returns true if values for the type need to be quoted.
605      *
606      * @param type The type to check.
607      * @return true if values for the type need to be quoted.
608      */
609     public static final boolean isTextType(SchemaType type)
610     {
611         for (int i = 0; i < TEXT_TYPES.length; i++)
612         {
613             if (type.equals(TEXT_TYPES[i]))
614             {
615                 return true;
616             }
617         }
618 
619         // If we get this far, there were no matches.
620         return false;
621     }
622 }