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 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: TypeMapper.java,v 1.4 2003/08/07 13:33:11 tanderson Exp $ 44 */ 45 46 package org.exolab.jms.tools.db; 47 48 import java.sql.Types; 49 import java.util.HashMap; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 54 55 /*** 56 * A helper class for mapping between types.</br> 57 * NOTE: this mapping is not complete, only reflecting those types required 58 * by OpenJMS 59 * 60 * @version $Revision: 1.4 $ $Date: 2003/08/07 13:33:11 $ 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 TypeSet _set = null; 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 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 type 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, binaries); 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.DECIMAL type 107 Type[] decimals = {new Type(Types.NUMERIC, 0, false)}; 108 add(Types.DECIMAL, decimals); 109 110 // alternatives for the Types.DATE type 111 Type[] dates = {new Type(Types.TIMESTAMP, 0, false)}; 112 add(Types.DATE, dates); 113 114 // alternatives for the Types.BIGINT type 115 long precision = Long.toString(Long.MAX_VALUE).length(); 116 Type[] bigints = {new Type(Types.NUMERIC, precision, true)}; 117 add(Types.BIGINT, bigints); 118 } 119 120 public Type getType(int type, long precision) { 121 // check if type set supports the requested type directly 122 Type result = _set.getType(type, precision); 123 if (result == null) { 124 // type not supported so return the mapping, if one is available 125 result = (Type) _mappings.get(new Integer(type)); 126 } 127 return result; 128 } 129 130 private void add(int type, Type[] mappings) { 131 for (int i = 0; i < mappings.length; ++i) { 132 Type requested = mappings[i]; 133 Type supported = _set.getType(requested.getType(), 134 requested.getPrecision()); 135 if (supported == null) { 136 _log.debug( 137 "TypeMapper: alternative mapping for type=" + 138 Descriptor.getDescriptor(type).getName() + 139 " is not supported by the database"); 140 } else { 141 long precision = requested.getPrecision(); 142 long maxPrecision = supported.getPrecision(); 143 _log.debug( 144 "TypeMapper: alternative mapping for type=" + 145 Descriptor.getDescriptor(type).getName() + 146 ", precision=" + precision + 147 ", is supported by the database as " + supported); 148 if (type == supported.getType() && 149 ((precision > 0) && (precision < maxPrecision))) { 150 // if the requested type is supported by the database, and 151 // has a smaller precision, override the maximum precision 152 // defined by the supported type 153 supported = new Type( 154 supported.getType(), supported.getName(), 155 precision, supported.getParameters()); 156 } 157 _mappings.put(new Integer(type), supported); 158 break; 159 } 160 } 161 } 162 163 } //-- TypeMapper

This page was automatically generated by Maven