1 45 46 package org.exolab.jms.tools.db; 47 48 import java.sql.Connection ; 49 import java.sql.ResultSet ; 50 import java.sql.SQLException ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.Iterator ; 54 55 import org.apache.commons.logging.Log; 56 import org.apache.commons.logging.LogFactory; 57 58 import org.exolab.jms.persistence.PersistenceException; 59 import org.exolab.jms.persistence.SQLHelper; 60 61 62 68 class TypeSet { 69 70 73 private HashMap _types = new HashMap (); 74 75 78 private static final Log _log = LogFactory.getLog(TypeSet.class); 79 80 81 87 public TypeSet(Connection connection) throws PersistenceException { 88 ResultSet set = null; 89 try { 90 set = connection.getMetaData().getTypeInfo(); 91 while (set.next()) { 92 int type = set.getInt("DATA_TYPE"); 93 String name = set.getString("TYPE_NAME"); 94 long precision = set.getLong("PRECISION"); 95 String createParams = set.getString("CREATE_PARAMS"); 96 97 Descriptor descriptor = Descriptor.getDescriptor(type); 98 if (descriptor != null) { 99 addType(type, name, precision, createParams); 100 } else { 101 _log.debug( 102 "TypeSet: skipping unknown type, type id=" + type 103 + ", name=" + name + ", precision=" + precision 104 + ", create params=" + createParams); 105 } 106 } 107 } catch (SQLException exception) { 108 throw new PersistenceException( 109 "Failed to get type meta-data", exception); 110 } finally { 111 SQLHelper.close(set); 112 } 113 } 114 115 122 public Type getType(int type, long precision) { 123 Type result = null; 124 ArrayList types = (ArrayList ) _types.get(new Integer (type)); 125 if (types != null) { 126 Iterator iter = types.iterator(); 127 while (iter.hasNext()) { 128 Type option = (Type) iter.next(); 129 if (precision == -1 && (option.getPrecision() != -1 && 130 option.getParameters())) { 131 result = new Type(type, option.getName(), 134 option.getPrecision(), 135 option.getParameters()); 136 break; 137 } else if (precision <= option.getPrecision()) { 138 result = new Type(type, option.getName(), precision, 140 option.getParameters()); 141 break; 142 } else { 143 _log.debug("TypeSet: requested type=" + type 144 + " exceeds precision for supported " + option); 145 } 146 } 147 } else { 148 _log.debug("TypeSet: no types matching type id=" + type 149 + ", type=" + Descriptor.getDescriptor(type).getName()); 150 } 151 return result; 152 } 153 154 162 public Type getNearestType(int type, long precision) { 163 Type result = null; 164 ArrayList types = (ArrayList ) _types.get(new Integer (type)); 165 if (types != null) { 166 Iterator iter = types.iterator(); 167 Type nearest = null; 168 while (iter.hasNext()) { 169 Type option = (Type) iter.next(); 170 if (precision <= option.getPrecision()) { 171 result = new Type(type, option.getName(), precision, 173 option.getParameters()); 174 break; 175 } else { 176 nearest = option; 177 } 178 } 179 if (result == null && nearest != null) { 180 result = new Type(type, nearest.getName(), 182 nearest.getPrecision(), 183 nearest.getParameters()); 184 _log.warn( 185 "TypeSet: requested type=" + type + ", precision=" 186 + precision + " exceeds precision supported by database. " 187 + "Falling back to " + nearest); 188 } 189 } else { 190 _log.debug("TypeSet: no types matching type id=" + type 191 + ", type=" + Descriptor.getDescriptor(type).getName()); 192 } 193 return result; 194 } 195 196 202 public boolean exists(int type) { 203 return _types.containsKey(new Integer (type)); 204 } 205 206 207 private void addType(int type, String name, long precision, 208 String createParams) { 209 Descriptor descriptor = Descriptor.getDescriptor(type); 210 boolean parameters = false; 211 if (createParams != null && createParams.trim().length() != 0) { 212 parameters = true; 213 } 214 215 Integer key = new Integer (type); 216 ArrayList types = (ArrayList ) _types.get(key); 217 if (types == null) { 218 types = new ArrayList (); 219 _types.put(key, types); 220 } 221 222 _log.debug("TypeSet: type id=" + type 223 + ", type=" + descriptor.getName() 224 + ", name=" + name 225 + ", precision=" + precision 226 + ", createParams=" + createParams); 227 types.add(new Type(type, name, precision, parameters)); 228 } 229 230 } | Popular Tags |