View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2001-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: TypeMapper.java,v 1.2 2005/09/04 07:17:05 tanderson Exp $
44   */
45  package org.exolab.jms.tools.db;
46  
47  import java.sql.Types;
48  import java.util.HashMap;
49  
50  import org.apache.commons.logging.Log;
51  import org.apache.commons.logging.LogFactory;
52  
53  
54  /***
55   * A helper class for mapping between SQL types.
56   * <p/>
57   * NOTE: this mapping is not complete, only reflecting those types required
58   * by OpenJMS
59   *
60   * @version     $Revision: 1.2 $ $Date: 2005/09/04 07:17:05 $
61   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62   */
63  class TypeMapper {
64  
65      /***
66       * The set of types supported by the RDBMS.
67       */
68      private final TypeSet _set;
69  
70      /***
71       * A mapping of SQL type identifiers to their corresponding alternative
72       * types. The alternatives are represented as an array of Type
73       */
74      private final HashMap _mappings = new HashMap();
75  
76      /***
77       * The logger.
78       */
79      private static final Log _log = LogFactory.getLog(TypeMapper.class);
80  
81  
82      /***
83       * Construct the type mapper with the set of types supported by the
84       * RDBMS.
85       */
86      public TypeMapper(TypeSet set) {
87          _set = set;
88  
89          // alternatives for the Types.BINARY type
90          Type[] binaries = {new Type(Types.VARBINARY, 0, true),
91                             new Type(Types.LONGVARBINARY, 0, true)};
92          add(Types.BINARY, binaries);
93  
94          // alternatives for the Types.VARBINARY type
95          Type[] varbinaries = {new Type(Types.LONGVARBINARY, 0, true)};
96          add(Types.VARBINARY, varbinaries);
97  
98          // alternatives for the Types.BIT type
99          Type[] bits = {new Type(Types.CHAR, 0, false),
100                        new Type(Types.TINYINT, 0, false),
101                        new Type(Types.SMALLINT, 0, false),
102                        new Type(Types.INTEGER, 0, false),
103                        new Type(Types.NUMERIC, 1, true)};
104         add(Types.BIT, bits);
105 
106         // alternatives for the Types.TINYINT type
107         Type[] tinyints = {new Type(Types.SMALLINT, 0, false),
108                            new Type(Types.INTEGER, 0, false),
109                            new Type(Types.NUMERIC, 0, true)};
110         add(Types.TINYINT, tinyints);
111 
112         // alternatives for the Types.DECIMAL type
113         Type[] decimals = {new Type(Types.NUMERIC, 0, false)};
114         add(Types.DECIMAL, decimals);
115 
116         // alternatives for the Types.DATE type
117         Type[] dates = {new Type(Types.TIMESTAMP, 0, false)};
118         add(Types.DATE, dates);
119 
120         // alternatives for the Types.BIGINT type
121         long precision = Long.toString(Long.MAX_VALUE).length();
122         Type[] bigints = {new Type(Types.NUMERIC, precision, true)};
123         add(Types.BIGINT, bigints);
124     }
125 
126     public Type getType(int type, long precision) {
127         // check if type set supports the requested type directly
128         Type result = _set.getType(type, precision);
129         if (result == null) {
130             // type not supported so return the mapping, if one is available
131             result = (Type) _mappings.get(new Integer(type));
132         }
133         return result;
134     }
135 
136     private void add(int type, Type[] mappings) {
137         for (int i = 0; i < mappings.length; ++i) {
138             Type requested = mappings[i];
139             Type supported = _set.getType(requested.getType(),
140                 requested.getPrecision());
141             if (supported == null) {
142                 _log.debug(
143                     "TypeMapper: alternative mapping for type=" +
144                     Descriptor.getDescriptor(type).getName() +
145                     " is not supported by the database");
146             } else {
147                 long precision = requested.getPrecision();
148                 long maxPrecision = supported.getPrecision();
149                 _log.debug(
150                     "TypeMapper: alternative mapping for type=" +
151                     Descriptor.getDescriptor(type).getName() +
152                     ", precision=" + precision +
153                     ", is supported by the database as " + supported);
154                 if (type == supported.getType() &&
155                     ((precision > 0) && (precision < maxPrecision))) {
156                     // if the requested type is supported by the database, and
157                     // has a smaller precision, override the maximum precision
158                     // defined by the supported type
159                     supported = new Type(
160                         supported.getType(), supported.getName(),
161                         precision, supported.getParameters());
162                 }
163                 _mappings.put(new Integer(type), supported);
164                 break;
165             }
166         }
167     }
168 
169 }