KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > db > TypeMapper


1 /* ====================================================================
2 * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3 *
4 * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. The end-user documentation included with the redistribution,
19 * if any, must include the following acknowledgment:
20 * "This product includes software developed by Jcorporate Ltd.
21 * (http://www.jcorporate.com/)."
22 * Alternately, this acknowledgment may appear in the software itself,
23 * if and wherever such third-party acknowledgments normally appear.
24 *
25 * 4. "Jcorporate" and product names such as "Expresso" must
26 * not be used to endorse or promote products derived from this
27 * software without prior written permission. For written permission,
28 * please contact info@jcorporate.com.
29 *
30 * 5. Products derived from this software may not be called "Expresso",
31 * or other Jcorporate product names; nor may "Expresso" or other
32 * Jcorporate product names appear in their name, without prior
33 * written permission of Jcorporate Ltd.
34 *
35 * 6. No product derived from this software may compete in the same
36 * market space, i.e. framework, without prior written permission
37 * of Jcorporate Ltd. For written permission, please contact
38 * partners@jcorporate.com.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Jcorporate Ltd. Contributions back
56 * to the project(s) are encouraged when you make modifications.
57 * Please send them to support@jcorporate.com. For more information
58 * on Jcorporate Ltd. and its products, please see
59 * <http://www.jcorporate.com/>.
60 *
61 * Portions of this software are based upon other open source
62 * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.core.db;
65
66 import com.jcorporate.expresso.core.db.config.TypeMappingConfig;
67 import com.jcorporate.expresso.core.dbobj.DBField;
68 import com.jcorporate.expresso.core.misc.ConfigContext;
69 import com.jcorporate.expresso.core.misc.ConfigManager;
70 import com.jcorporate.expresso.core.misc.StringUtil;
71 import com.jcorporate.expresso.kernel.ComponentLifecycle;
72 import com.jcorporate.expresso.kernel.Configuration;
73 import com.jcorporate.expresso.kernel.exception.ConfigurationException;
74 import com.jcorporate.expresso.kernel.util.LocatorUtils;
75 import org.apache.log4j.Logger;
76
77 import java.util.Enumeration JavaDoc;
78 import java.util.HashMap JavaDoc;
79 import java.util.Hashtable JavaDoc;
80 import java.util.Iterator JavaDoc;
81 import java.util.Map JavaDoc;
82
83
84 /**
85  * Type Mapper takes care of the job of mapping various Java types to SQL
86  * types. This, in turn, is used by items like DBCreate, Database Verifier,
87  * etc. The type mapper is embedded within the PersistenceManager.
88  *
89  * @author Michael Rimov
90  * @see com.jcorporate.expresso.core.dataobjects.PersistenceManager
91  */

92 public class TypeMapper extends com.jcorporate.expresso.kernel.EmbeddedComponent
93         implements ComponentLifecycle {
94     /**
95      * Logger instance
96      */

97     private static Logger log = Logger.getLogger(TypeMapper.class);
98
99     /**
100      * Data context to TypeMapper map... will be removed once Componentization
101      * is truly complete
102      */

103     private static Map JavaDoc mapInstances = new HashMap JavaDoc();
104
105     /**
106      * SQL to Expresso Type Map
107      */

108     private Map JavaDoc expressoFromSQL = new HashMap JavaDoc(30);
109
110     /**
111      * Expresso type to Java type map
112      */

113     private Map JavaDoc expressoToJava = new HashMap JavaDoc(30);
114
115     /**
116      * Expresso to SQL type map
117      */

118     private Map JavaDoc expressoToSQL = new HashMap JavaDoc(30);
119
120     /**
121      * SQL to DB Type Map
122      */

123     private Map JavaDoc sqlToDB = new HashMap JavaDoc(30);
124
125     /**
126      * Mapping of SQL types to sql type names
127      */

128     private Map JavaDoc sqlTypeNames = new HashMap JavaDoc(30);
129
130     /**
131      * Type to String Map
132      */

133     private Map JavaDoc typeToString = new HashMap JavaDoc(30);
134
135     /**
136      * Name for the current data context
137      */

138     private String JavaDoc dataContext = null;
139
140     /**
141      * Set to true if we're in the component configuration system.
142      */

143     private boolean componentConfiguration = false;
144
145     /**
146      * Default constructor. Required for componentization.
147      */

148     public TypeMapper() {
149     }
150
151     /**
152      * Constructor that initializes the type mapper based upon the data context
153      *
154      * @param thedataContext the dataContext to load the Type Mapping for
155      */

156     protected TypeMapper(String JavaDoc thedataContext) {
157         this.dataContext = thedataContext;
158     }
159
160     /**
161      * Retrieve a DBField to java.sql.Types mapping value.
162      *
163      * @param dbFieldType the DBField data type
164      * @return an integer corresponding to the java.sql.Types or -1 if no
165      * mapping is found
166      */

167     public Integer JavaDoc getExpressoToJava(String JavaDoc dbFieldType) {
168         return (Integer JavaDoc) expressoToJava.get(dbFieldType);
169     }
170
171     /**
172      * Get the ExpressoToSQL Map
173      *
174      * @return A Map getting this context's ExpressoToSQL maps.
175      */

176     public Map JavaDoc getExpressoToSQLMap() {
177         return expressoToSQL;
178     }
179     /* getExpressoToSQLMap(String) */
180
181     /**
182      * Retrieves an instance of the type mapper.
183      * <p/>
184      * To be deprecated soon. use the Component System with
185      * PersistenceManager to retrieve an instance of the type
186      * mapper instead.
187      *
188      * @param dataContext the data context to retrieve it for.
189      * @return a fully initialized TypeMapper instance
190      * @throws DBException upon error getting or initializing the TypeMapper
191      * for that context
192      */

193     public static synchronized TypeMapper getInstance(String JavaDoc dataContext)
194             throws DBException {
195         if (dataContext == null) {
196             throw new IllegalArgumentException JavaDoc("Parameter data context cannot be null");
197         }
198
199         TypeMapper theInstance = (TypeMapper) mapInstances.get(dataContext);
200
201         synchronized (TypeMapper.class) {
202             if (theInstance == null) {
203                 theInstance = new TypeMapper();
204                 theInstance.dataContext = dataContext;
205                 theInstance.initializeTypes(dataContext);
206                 mapInstances.put(dataContext, theInstance);
207             }
208         }
209
210         return theInstance;
211     }
212
213     /**
214      * Return a java.sql.Types integer that maps to the field type listed
215      *
216      * @param fieldType the type of the field to map.
217      * @return a DoubleOrderedMap containing all the type mappings for this
218      * context
219      * @throws DBException if there was an error initializing the ExpressoToSQL
220      * maps.
221      */

222     public int getJavaSQLType(String JavaDoc fieldType) throws DBException {
223         Integer JavaDoc i = (Integer JavaDoc) expressoToJava.get(fieldType);
224
225         if (i == null) {
226             throw new DBException("Unable to locate java.sql.Types mapping for field type: " +
227                     fieldType);
228         }
229
230         return i.intValue();
231     }
232
233     /**
234      * Because we look up our metadata in the parent component, we have to
235      * somehow know WHAT component's metadata is ours. Sub classes need to
236      * override this one-liner and return the name of the metadata we're
237      * looking for.
238      *
239      * @return java.lang.String
240      */

241     public String JavaDoc getMetadataName() {
242         return "TypeMapper";
243     }
244
245     /**
246      * Get the actual java.sql.Type in a friendly String name
247      *
248      * @param sqlType java.sql.Type integer
249      * @return the corresponding string
250      */

251     public String JavaDoc getNameForSQL(int sqlType) {
252         return (String JavaDoc) sqlTypeNames.get(new Integer JavaDoc(sqlType));
253     }
254
255     /**
256      * Get the SQL to Database type Map
257      *
258      * @return A hashtable getting this context's SqlToDB map.
259      */

260     public Map JavaDoc getSQLToDBMap() {
261         return sqlToDB;
262     }
263     /* getSQLToDBMap(String) */
264
265     /**
266      * Map a type used in Expresso to an appropriate database-specific type,
267      * utilizing the default type mapping followed by the custom type
268      * mapping(s) for this context (if any)
269      *
270      * @param expressoType The java.sql datatype to check against.
271      * @return The appropriate datatype for this database context.
272      * @throws DBException If there's an getting the datatype.
273      */

274     public String JavaDoc getTypeForDB(String JavaDoc expressoType) throws DBException {
275         /* First map the expresso type to the java.sql.Types type */
276         String JavaDoc sqlType = (String JavaDoc) expressoToSQL.get(expressoType);
277
278         if (sqlType == null) {
279             throw new DBException("There was no mapping of types from the" +
280                     " expresso type '" + expressoType +
281                     "' to a java.sql.Types String " +
282                     "type - perhaps there should be a type mapping " +
283                     "entry in expresso-config.xml?");
284         }
285
286         /* Now map the java.sql.Type to a database type */
287         String JavaDoc dbType = (String JavaDoc) sqlToDB.get(sqlType);
288
289         if (dbType == null) {
290             Integer JavaDoc javaIntType = (Integer JavaDoc) expressoToJava.get(sqlType.toLowerCase());
291
292             if (javaIntType == null) {
293                 throw new DBException("There was no mapping of types from the" +
294                         " expresso type '" + expressoType +
295                         "' to a java.sql.Types " +
296                         "type integer - perhaps there should be a type mapping " +
297                         "entry in expresso-config.xml?");
298             }
299
300             dbType = (String JavaDoc) sqlToDB.get(javaIntType);
301
302             if (dbType == null) {
303                 throw new DBException("There was no mapping of the java.sql.Type." + sqlType +
304                         " type to a specific database type. Either this " +
305                         "type is not supported by the specified JDBC driver, or " +
306                         "there should be an type mapping entry in " +
307                         "expresso-config.xml");
308             }
309         }
310
311         return dbType;
312     }
313     /* typeForDB(String, String) */
314
315     /**
316      * Map a type used in SQL to an appropriate expresso-specific type
317      *
318      * @param sqlType The java.sql datatype to check against.
319      * @return The appropriate datatype from this database context.
320      * @throws DBException If there's an getting the datatype.
321      */

322     public String JavaDoc getTypeForExpresso(int sqlType) throws DBException {
323         /* First get the string name from the type */
324         Integer JavaDoc sqlInt = new Integer JavaDoc(sqlType);
325         String JavaDoc sqlStr = (String JavaDoc) typeToString.get(sqlInt);
326
327         if (sqlStr == null) {
328             throw new DBException("There was no mapping for type " + sqlType +
329                     " to a string type ");
330         }
331
332         String JavaDoc expressoType = (String JavaDoc) expressoFromSQL.get(sqlStr.toLowerCase());
333
334         if (expressoType == null) {
335             throw new DBException("There was no mapping of types from the" +
336                     " sql type '" + sqlStr + "' to a expresso Type ");
337         }
338
339         return expressoType;
340     }
341     /* typeForExpresso(String, int) */
342
343     /**
344      * Retrieve a 'friendly' name for a java.sql.Type integer
345      *
346      * @param sqlType the java.sql.Types integer value
347      * @return a String corresponding to that integer value.
348      */

349     public String JavaDoc getTypeForSQL(int sqlType) {
350         return (String JavaDoc) typeToString.get(new Integer JavaDoc(sqlType));
351     }
352
353     /**
354      * Sets up all the types now that we know the parent's data context.
355      *
356      * @param newConfig Configuration settings.
357      * @throws ConfigurationException upon error
358      */

359     public synchronized void configure(Configuration newConfig)
360             throws ConfigurationException {
361         synchronized (TypeMapper.class) {
362             LocatorUtils lc = new LocatorUtils(this);
363             dataContext = lc.getDataContextName(this);
364
365             try {
366                 if (dataContext != null) {
367                     this.initializeTypes(dataContext);
368                     mapInstances.put(dataContext, this);
369                 } else {
370                     throw new ConfigurationException("Unable to find parent data context!");
371                 }
372             } catch (DBException ex) {
373                 log.error("Error configuring TypeMapper", ex);
374                 throw new ConfigurationException("Error Configuring TypeMapper",
375                         ex);
376             } catch (Throwable JavaDoc t) {
377                 log.error("Error configuring TypeMapper", t);
378                 throw new ConfigurationException("Error Configuring TypeMapper",
379                         t);
380             }
381         }
382     }
383
384     /**
385      * Destroys the component. Removes the mapinstances and deallocates all
386      * the HashMaps that were allocated
387      */

388     public void destroy() {
389         synchronized (TypeMapper.class) {
390             if (mapInstances != null) {
391                 mapInstances.remove(dataContext);
392                 expressoToSQL = null;
393             }
394
395             expressoToSQL = null;
396             expressoFromSQL = null;
397             sqlToDB = null;
398             typeToString = null;
399             expressoToJava = null;
400             sqlTypeNames = null;
401         }
402     }
403
404     /**
405      * Any quick initializations here.
406      */

407     public void initialize() {
408         componentConfiguration = true;
409     }
410
411     /**
412      * Reconfigures the TypeMapper. Resets all the HashMaps and reinitializes
413      * all the type mappings.
414      *
415      * @param newConfig the new Configuration bean.
416      * @throws ConfigurationException upon reconfiguration error.
417      */

418     public synchronized void reconfigure(Configuration newConfig)
419             throws ConfigurationException {
420         synchronized (TypeMapper.class) {
421             LocatorUtils lc = new LocatorUtils(this);
422             dataContext = lc.getDataContextName(this);
423             mapInstances.remove(dataContext);
424
425             try {
426                 expressoToSQL = new HashMap JavaDoc(30);
427                 expressoFromSQL = new HashMap JavaDoc(30);
428                 sqlToDB = new HashMap JavaDoc(30);
429                 typeToString = new HashMap JavaDoc(30);
430                 expressoToJava = new HashMap JavaDoc(30);
431                 sqlTypeNames = new HashMap JavaDoc(30);
432                 this.initializeTypes(dataContext);
433             } catch (DBException ex) {
434                 throw new ConfigurationException("Error Configuring TypeMapper",
435                         ex);
436             }
437
438             mapInstances.put(dataContext, this);
439         }
440     }
441
442     /**
443      * Initialize the mappings between java types, expresso types, and SQL
444      * types.
445      *
446      * @param dataContext the data context to map for
447      * @throws DBException upon error
448      */

449     protected void initializeTypes(String JavaDoc dataContext)
450             throws DBException {
451         if (log.isInfoEnabled()) {
452             log.info("Initializing Type Mapping for context: " + dataContext);
453         }
454
455
456         buildTypeToString();
457         buildSqlTypeNames();
458         buildExpressoToSQL();
459
460         /* get the expresso type from an SQL type
461            we use the reverse mapping of the
462            expressoToSQL table */

463         for (Iterator JavaDoc i = expressoToSQL.keySet().iterator(); i.hasNext();) {
464             String JavaDoc oneKey = (String JavaDoc) i.next();
465             String JavaDoc oneValue = (String JavaDoc) expressoToSQL.get(oneKey);
466             expressoFromSQL.put(oneValue.toLowerCase(), oneKey);
467         }
468
469         /**
470          * Now initialize the java.sql.Type to database-specific type mappings
471          */

472         DBConnection myConnection = null;
473         DBConnectionPool myPool = null;
474
475         buildSqlToDB();
476
477         //
478
//We fill the Expresso to java.sql.Types map with the reverse
479
//so we can peform reverse lookups
480
//
481
for (Iterator JavaDoc i = typeToString.keySet().iterator(); i.hasNext();) {
482             Integer JavaDoc aKey = (Integer JavaDoc) i.next();
483             String JavaDoc aValue = (String JavaDoc) typeToString.get(aKey);
484             expressoToJava.put(aValue.toLowerCase(), aKey);
485         }
486
487         //Add aliases
488
expressoToJava.put(DBField.INTEGER_TYPE,
489                 new Integer JavaDoc(java.sql.Types.INTEGER));
490         expressoToJava.put(DBField.BOOLEAN_TYPE, new Integer JavaDoc(java.sql.Types.BIT));
491         expressoToJava.put(DBField.LONG_TYPE, new Integer JavaDoc(java.sql.Types.BIGINT));
492         expressoToJava.put(DBField.DATETIME_TYPE,
493                 new Integer JavaDoc(java.sql.Types.TIMESTAMP));
494         expressoToJava.put(DBField.TEXT_TYPE,
495                 new Integer JavaDoc(java.sql.Types.LONGVARCHAR));
496         expressoToJava.put(DBField.AUTOINC_TYPE,
497                 new Integer JavaDoc(java.sql.Types.INTEGER));
498
499         try {
500             myPool = DBConnectionPool.getInstance(dataContext);
501             myConnection = myPool.getConnection("Schema");
502
503             /* Add any mappings specified by the SQL connection itself, */
504             /* overridding the default if appropriate */
505             Hashtable JavaDoc dbMap = myConnection.getTypeMap();
506             myPool.release(myConnection);
507
508             Integer JavaDoc oneIntValue = null;
509
510             for (Enumeration JavaDoc ex = dbMap.keys(); ex.hasMoreElements();) {
511                 String JavaDoc oneKey = (String JavaDoc) ex.nextElement();
512                 oneIntValue = (Integer JavaDoc) dbMap.get(oneKey);
513
514                 String JavaDoc oneValue = (String JavaDoc) typeToString.get(oneIntValue);
515
516                 if (oneValue == null) {
517                     log.warn("Unable to map java.sql.Types value " +
518                             oneIntValue.toString() + " to a string. The database " +
519                             " may have returned an invalid type map. New type being added...");
520                     typeToString.put(oneIntValue, oneKey);
521                     oneValue = (String JavaDoc) typeToString.get(oneIntValue);
522                 } else {
523                     sqlToDB.put(oneIntValue,
524                             StringUtil.notNull(oneKey).toLowerCase());
525                     expressoToJava.put(StringUtil.notNull(oneKey).toLowerCase(),
526                             oneIntValue);
527                 }
528             }
529         } catch (DBException de) {
530             log.error("Error initializing types", de);
531             throw new DBException(de);
532         } catch (Throwable JavaDoc t) {
533             log.error("Error initializing types", t);
534             throw new DBException(t);
535         } finally {
536             if (myPool != null) {
537                 myPool.release(myConnection);
538             }
539         }
540
541         mergeTypeMappings();
542     }
543
544     private void buildExpressoToSQL() throws DBException {
545         /* First initialize the expresso to java.sql.Type mappings */
546         expressoToSQL.put(DBField.ARRAY_TYPE, "ARRAY");
547         expressoToSQL.put(DBField.BIGINT_TYPE, "BIGINT");
548
549         // long is a synonym for bigint
550
expressoToSQL.put(DBField.LONG_TYPE, "BIGINT");
551         expressoToSQL.put(DBField.BINARY_TYPE, "BINARY");
552         expressoToSQL.put(DBField.BIT_TYPE, "BIT");
553
554         // boolean is synonym
555
expressoToSQL.put(DBField.BOOLEAN_TYPE, "BIT");
556         expressoToSQL.put(DBField.BLOB_TYPE, "BLOB");
557         expressoToSQL.put(DBField.CHAR_TYPE, "CHAR");
558         expressoToSQL.put(DBField.CLOB_TYPE, "CLOB");
559         expressoToSQL.put(DBField.DATE_TYPE, "DATE");
560         expressoToSQL.put(DBField.DECIMAL_TYPE, "DECIMAL");
561         expressoToSQL.put("distinct", "DISTINCT");
562         expressoToSQL.put(DBField.DOUBLE_TYPE, "DOUBLE");
563         expressoToSQL.put(DBField.FLOAT_TYPE, "FLOAT");
564         expressoToSQL.put(DBField.INTEGER_TYPE, "INTEGER");
565
566         // int is a synonym
567
expressoToSQL.put(DBField.INT_TYPE, "INTEGER");
568         expressoToSQL.put(DBField.JAVA_OBJECT, "JAVA_OBJECT");
569         expressoToSQL.put(DBField.LONGVARBINARY, "LONGVARBINARY");
570         expressoToSQL.put(DBField.LONGVARCHAR_TYPE, "LONGVARCHAR");
571         expressoToSQL.put(DBField.NUMERIC_TYPE, "NUMERIC");
572         expressoToSQL.put(DBField.OTHER_TYPE, "OTHER");
573         expressoToSQL.put(DBField.REAL_TYPE, "REAL");
574         expressoToSQL.put(DBField.REF_TYPE, "REF");
575         expressoToSQL.put(DBField.SMALLINT_TYPE, "SMALLINT");
576         expressoToSQL.put(DBField.STRUCT_TYPE, "STRUCT");
577         expressoToSQL.put(DBField.TIME_TYPE, "TIME");
578         expressoToSQL.put(DBField.TIMESTAMP_TYPE, "TIMESTAMP");
579
580         // datetime is synonym
581
expressoToSQL.put(DBField.DATETIME_TYPE, "TIMESTAMP");
582         expressoToSQL.put(DBField.TINYINT_TYPE, "TINYINT");
583         expressoToSQL.put(DBField.VARBINARY_TYPE, "VARBINARY");
584         expressoToSQL.put(DBField.VARCHAR_TYPE, "VARCHAR");
585
586         // TEXT_TYPE synonym for longvarchar
587
expressoToSQL.put(DBField.TEXT_TYPE, "LONGVARCHAR");
588
589         // auto-inc is unique to expresso
590
expressoToSQL.put(DBField.AUTOINC_TYPE, "INTEGER");
591
592         /* The user can specify in the properties file for each db/context mappings to
593         * specify new expresso types and which java.sql.Type they correspond to.
594         * These mappings take the form:
595         * <type-mapping>
596         * <expresso-type>foo</expresso-type>
597         * <java-type>CHAR</java-type>
598         * </type-mapping>
599         * This would allow use of the type "foo" in an "addField" statement for a
600         * db object, and would map it to the java.sql.Type.CHAR data type
601          */

602         TypeMappingConfig oneMap = null;
603
604         for (Enumeration JavaDoc e = this.getTypeMappings().elements();
605              e.hasMoreElements();) {
606             oneMap = (TypeMappingConfig) e.nextElement();
607
608             if ((oneMap.getExpressoType() != null) &&
609                     (oneMap.getJavaType() != null)) {
610                 expressoToSQL.put(oneMap.getExpressoType(), oneMap.getJavaType());
611             }
612         }
613     }
614
615     /**
616      * Builds the SQL to DB Map. The SQL to DB map is keyed by the
617      * java.sql.Types
618      */

619     private void buildSqlToDB() {
620         /**
621          * default mapping of java.sql.Types to actual database types
622          */

623         sqlToDB.put(new Integer JavaDoc(java.sql.Types.INTEGER), DBField.INT_TYPE);
624         sqlToDB.put(new Integer JavaDoc(java.sql.Types.VARCHAR), DBField.VARCHAR_TYPE);
625         sqlToDB.put(new Integer JavaDoc(java.sql.Types.CHAR), DBField.CHAR_TYPE);
626         sqlToDB.put(new Integer JavaDoc(java.sql.Types.DATE), DBField.DATE_TYPE);
627         sqlToDB.put(new Integer JavaDoc(java.sql.Types.TIMESTAMP), DBField.DATETIME_TYPE);
628         sqlToDB.put(new Integer JavaDoc(java.sql.Types.TIME), DBField.TIME_TYPE);
629         sqlToDB.put(new Integer JavaDoc(java.sql.Types.FLOAT), DBField.FLOAT_TYPE);
630         sqlToDB.put(new Integer JavaDoc(java.sql.Types.DOUBLE), DBField.DOUBLE_TYPE);
631         sqlToDB.put(new Integer JavaDoc(java.sql.Types.LONGVARCHAR),
632                 DBField.LONGVARCHAR_TYPE);
633         sqlToDB.put(new Integer JavaDoc(java.sql.Types.REAL), DBField.REAL_TYPE);
634         sqlToDB.put(new Integer JavaDoc(java.sql.Types.SMALLINT), DBField.SMALLINT_TYPE);
635         sqlToDB.put(new Integer JavaDoc(java.sql.Types.TINYINT), DBField.TINYINT_TYPE);
636         sqlToDB.put(new Integer JavaDoc(java.sql.Types.NUMERIC), DBField.NUMERIC_TYPE);
637         sqlToDB.put(new Integer JavaDoc(java.sql.Types.DECIMAL), DBField.DECIMAL_TYPE);
638         sqlToDB.put(new Integer JavaDoc(java.sql.Types.BIGINT), DBField.LONG_TYPE);
639         sqlToDB.put(new Integer JavaDoc(java.sql.Types.ARRAY), DBField.ARRAY_TYPE);
640         sqlToDB.put(new Integer JavaDoc(java.sql.Types.BINARY), DBField.BINARY_TYPE);
641         sqlToDB.put(new Integer JavaDoc(java.sql.Types.BIT), DBField.BIT_TYPE);
642         sqlToDB.put(new Integer JavaDoc(java.sql.Types.BLOB), DBField.BLOB_TYPE);
643         sqlToDB.put(new Integer JavaDoc(java.sql.Types.CLOB), DBField.CLOB_TYPE);
644         sqlToDB.put(new Integer JavaDoc(java.sql.Types.DISTINCT), "distinct");
645         sqlToDB.put(new Integer JavaDoc(java.sql.Types.JAVA_OBJECT), DBField.JAVA_OBJECT);
646         sqlToDB.put(new Integer JavaDoc(java.sql.Types.LONGVARBINARY),
647                 DBField.LONGVARBINARY);
648         sqlToDB.put(new Integer JavaDoc(java.sql.Types.NULL), DBField.NULL_TYPE);
649         sqlToDB.put(new Integer JavaDoc(java.sql.Types.OTHER), DBField.OTHER_TYPE);
650         sqlToDB.put(new Integer JavaDoc(java.sql.Types.REF), DBField.REF_TYPE);
651         sqlToDB.put(new Integer JavaDoc(java.sql.Types.STRUCT), "struct");
652         sqlToDB.put(new Integer JavaDoc(java.sql.Types.VARBINARY),
653                 DBField.VARBINARY_TYPE);
654     }
655
656     /**
657      * Builds the sqlTypeNames
658      */

659     private void buildSqlTypeNames() {
660         //
661
//Java integer to a friendly string
662
//
663
sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.LONGVARCHAR),
664                 "java.sql.Types.LONGVARCHAR");
665         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.ARRAY),
666                 "java.sql.Types.ARRAY");
667         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.BIGINT),
668                 "java.sql.Types.BIGINT");
669         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.BINARY),
670                 "java.sql.Types.BINARY");
671         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.BIT), "java.sql.Types.BIT");
672         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.BLOB), "java.sql.Types.BLOB");
673         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.CHAR), "java.sql.Types.CHAR");
674         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.CLOB), "java.sql.Types.CLOB");
675         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.DATE), "java.sql.Types.DATE");
676         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.DECIMAL),
677                 "java.sql.Types.DECIMAL");
678         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.DISTINCT),
679                 "java.sql.Types.DISTINCT");
680         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.DOUBLE),
681                 "java.sql.Types.DOUBLE");
682         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.FLOAT),
683                 "java.sql.Types.FLOAT");
684         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.INTEGER),
685                 "java.sql.Types.INTEGER");
686         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.JAVA_OBJECT),
687                 "java.sql.Types.JAVA_OBJECT");
688         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.LONGVARBINARY),
689                 "java.sql.Types.LONGVARBINARY");
690         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.LONGVARCHAR),
691                 "java.sql.Types.LONGVARCHAR");
692         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.NUMERIC),
693                 "java.sql.Types.NUMERIC");
694         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.OTHER),
695                 "java.sql.Types.OTHER");
696         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.REAL), "java.sql.Types.REAL");
697         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.REF), "java.sql.Types.REF");
698         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.SMALLINT),
699                 "java.sql.Types.SMALLINT");
700         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.STRUCT),
701                 "java.sql.Types.STRUCT");
702         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.TIME), "java.sql.Types.TIME");
703         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.TIMESTAMP),
704                 "java.sql.Types.TIMESTAMP");
705         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.TINYINT),
706                 "java.sql.Types.TINYINT");
707         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.VARBINARY),
708                 "java.sql.Types.VARBINARY");
709         sqlTypeNames.put(new Integer JavaDoc(java.sql.Types.VARCHAR),
710                 "java.sql.Types.VARCHAR");
711     }
712
713     /**
714      * Builds the type to string HashMap.
715      */

716     private void buildTypeToString() {
717         typeToString.put(new Integer JavaDoc(java.sql.Types.LONGVARCHAR), "LONGVARCHAR");
718         typeToString.put(new Integer JavaDoc(java.sql.Types.ARRAY), "ARRAY");
719         typeToString.put(new Integer JavaDoc(java.sql.Types.BIGINT), "BIGINT");
720         typeToString.put(new Integer JavaDoc(java.sql.Types.BINARY), "BINARY");
721         typeToString.put(new Integer JavaDoc(java.sql.Types.BIT), "BIT");
722         typeToString.put(new Integer JavaDoc(java.sql.Types.BLOB), "BLOB");
723         typeToString.put(new Integer JavaDoc(java.sql.Types.CHAR), "CHAR");
724         typeToString.put(new Integer JavaDoc(java.sql.Types.CLOB), "CLOB");
725         typeToString.put(new Integer JavaDoc(java.sql.Types.DATE), "DATE");
726         typeToString.put(new Integer JavaDoc(java.sql.Types.DECIMAL), "DECIMAL");
727         typeToString.put(new Integer JavaDoc(java.sql.Types.DISTINCT), "DISTINCT");
728         typeToString.put(new Integer JavaDoc(java.sql.Types.DOUBLE), "DOUBLE");
729         typeToString.put(new Integer JavaDoc(java.sql.Types.FLOAT), "FLOAT");
730         typeToString.put(new Integer JavaDoc(java.sql.Types.INTEGER), "INTEGER");
731         typeToString.put(new Integer JavaDoc(java.sql.Types.JAVA_OBJECT), "JAVA_OBJECT");
732         typeToString.put(new Integer JavaDoc(java.sql.Types.LONGVARBINARY),
733                 "LONGVARBINARY");
734         typeToString.put(new Integer JavaDoc(java.sql.Types.LONGVARCHAR), "LONGVARCHAR");
735         typeToString.put(new Integer JavaDoc(java.sql.Types.NUMERIC), "NUMERIC");
736         typeToString.put(new Integer JavaDoc(java.sql.Types.OTHER), "OTHER");
737         typeToString.put(new Integer JavaDoc(java.sql.Types.REAL), "REAL");
738         typeToString.put(new Integer JavaDoc(java.sql.Types.REF), "REF");
739         typeToString.put(new Integer JavaDoc(java.sql.Types.SMALLINT), "SMALLINT");
740         typeToString.put(new Integer JavaDoc(java.sql.Types.STRUCT), "STRUCT");
741         typeToString.put(new Integer JavaDoc(java.sql.Types.TIME), "TIME");
742         typeToString.put(new Integer JavaDoc(java.sql.Types.TIMESTAMP), "TIMESTAMP");
743         typeToString.put(new Integer JavaDoc(java.sql.Types.TINYINT), "TINYINT");
744         typeToString.put(new Integer JavaDoc(java.sql.Types.VARBINARY), "VARBINARY");
745         typeToString.put(new Integer JavaDoc(java.sql.Types.VARCHAR), "VARCHAR");
746     }
747
748     private void mergeTypeMappings() throws DBException {
749         /* The user can specify in their properties file mappings to force a
750         * specific java.sql.Type to be mapped to a particular database type.
751         * These mappings take the form:
752                     <type-mapping>
753                     <java-type>INTEGER</java-type>
754                     <db-type>smallint</db-type>
755                   </type-mapping>
756         * This would force the database to use the "smallint" string when creating
757         * fields in a table that use the java.sql.Type.INTEGER data type
758            */

759         TypeMappingConfig oneMapping = null;
760
761
762         for (Enumeration JavaDoc e = this.getTypeMappings().elements();
763              e.hasMoreElements();) {
764             oneMapping = (TypeMappingConfig) e.nextElement();
765
766             if ((oneMapping.getDBType() != null) &&
767                     (oneMapping.getJavaType() != null)) {
768                 String JavaDoc value = oneMapping.getJavaType().toLowerCase();
769                 Integer JavaDoc key = (Integer JavaDoc) expressoToJava.get(value);
770
771                 if (key == null) {
772                     log.warn("Unable to map " + oneMapping.getJavaType() +
773                             " to java.SQL.Types data type");
774                 } else {
775                     sqlToDB.put(key,
776                             ((String JavaDoc) oneMapping.getDBType().toLowerCase()));
777                     expressoToJava.put(value, key);
778                 }
779             }
780         }
781     }
782
783     /**
784      * Private method that uses either the old ConfigManager way of initialization
785      * or the new Configuration Methods.
786      *
787      * @return Vector of TypeMapping objects
788      * @throws DBException upon error getting the configuration mappings [Old way
789      * only]
790      * @todo Change return value to ArrayList
791      */

792     private java.util.Vector JavaDoc getTypeMappings() throws DBException {
793         if (componentConfiguration) {
794             //Get the Type Mappings from the Persistence Manager
795
DBConfig config = (DBConfig) this.getParent().locateComponent("DBConfig");
796             return new java.util.Vector JavaDoc(config.getCurrentConfig().getTypeMappings());
797         } else {
798             /* Mapping from the java.sql.Types to equivilant strings */
799             ConfigContext myContext = null;
800
801             try {
802                 myContext = ConfigManager.getContext(dataContext);
803                 return myContext.getTypeMappings();
804             } catch (com.jcorporate.expresso.core.misc.ConfigurationException ce) {
805                 throw new DBException(ce);
806             }
807         }
808
809     }
810 }
811
Popular Tags