View Javadoc

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