1 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 63 class TypeMapper { 64 65 68 private TypeSet _set = null; 69 70 74 private HashMap _mappings = new HashMap (); 75 76 79 private static final Log _log = LogFactory.getLog(TypeMapper.class); 80 81 82 86 public TypeMapper(TypeSet set) { 87 _set = set; 88 89 Type[] binaries = {new Type(Types.VARBINARY, 0, true), 91 new Type(Types.LONGVARBINARY, 0, true)}; 92 add(Types.BINARY, binaries); 93 94 Type[] varbinaries = {new Type(Types.LONGVARBINARY, 0, true)}; 96 add(Types.VARBINARY, binaries); 97 98 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 Type[] decimals = {new Type(Types.NUMERIC, 0, false)}; 108 add(Types.DECIMAL, decimals); 109 110 Type[] dates = {new Type(Types.TIMESTAMP, 0, false)}; 112 add(Types.DATE, dates); 113 114 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 Type result = _set.getType(type, precision); 123 if (result == null) { 124 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 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 } | Popular Tags |