1 2 12 package com.versant.core.jdbc.metadata; 13 14 import com.versant.core.jdbc.sql.SqlDriver; 15 import com.versant.core.jdbc.JdbcTypeRegistry; 16 import com.versant.core.jdbc.JdbcConverterFactory; 17 import com.versant.core.metadata.MetaDataUtils; 18 19 import java.sql.Types ; 20 import java.util.*; 21 22 import com.versant.core.common.BindingSupportImpl; 23 24 33 public final class JdbcMappingResolver implements JdbcTypeRegistry { 34 35 private String database; 36 private HashMap javaTypeMappings; private JdbcTypeMapping[] typeMappings; 39 private static final int FIRST_TYPE = Types.BIT; 40 private static final int LAST_TYPE = Types.REF; 41 42 public JdbcMappingResolver() { 43 } 44 45 50 public void init(SqlDriver sqlDriver, List dsTypeMappings, 51 List dsJavaTypeMappings) { 52 database = sqlDriver.getName(); 53 54 JdbcTypeMapping[] sqlDriverTypeMappings = sqlDriver.getTypeMappings(); 55 int n = sqlDriverTypeMappings.length; 56 int q = dsTypeMappings.size(); 57 typeMappings = new JdbcTypeMapping[LAST_TYPE - FIRST_TYPE]; 58 for (int i = 0; i < n; i++) { 59 JdbcTypeMapping m = null; 60 JdbcTypeMapping dm = sqlDriverTypeMappings[i]; 61 int jdbcType = dm.getJdbcType(); 62 for (int j = 0; j < q; j++) { 63 JdbcTypeMapping dsm = (JdbcTypeMapping)dsTypeMappings.get(j); 64 if (dsm.match(jdbcType, database)) { 65 dsm.copyFrom(dm); 66 m = dsm; 67 break; 68 } 69 } 70 if (m == null) m = (JdbcTypeMapping)dm.clone(); 71 typeMappings[m.getJdbcType() - FIRST_TYPE] = m; 72 } 73 74 javaTypeMappings = sqlDriver.getJavaTypeMappings(); 75 n = dsJavaTypeMappings.size(); 76 for (int i = 0; i < n; i++) { 77 JdbcJavaTypeMapping m = (JdbcJavaTypeMapping)dsJavaTypeMappings.get(i); 78 String mdb = m.getDatabase(); 79 if (mdb != null && !mdb.equals(database)) continue; 80 JdbcJavaTypeMapping sm = (JdbcJavaTypeMapping)javaTypeMappings.get(m.getJavaType()); 81 if (sm != null) m.copyFrom(sm); 82 javaTypeMappings.put(m.getJavaType(), m); 83 } 84 } 85 86 public String getDatabase() { 87 return database; 88 } 89 90 public JdbcTypeMapping[] getTypeMappings() { 91 return typeMappings; 92 } 93 94 public HashMap getJavaTypeMappings() { 95 return javaTypeMappings; 96 } 97 98 102 public JdbcJavaTypeMapping resolveMapping(Class javaType) { 103 return resolveMapping(null, null, javaType); 104 } 105 106 111 public JdbcJavaTypeMapping resolveMapping(JdbcJavaTypeMapping fieldMapping, 112 String fieldName, Class javaType) { 113 JdbcJavaTypeMapping ans = fieldMapping; 114 if (ans == null) ans = new JdbcJavaTypeMapping(); 115 JdbcJavaTypeMapping m = (JdbcJavaTypeMapping)javaTypeMappings.get(javaType); 116 if (m != null) ans.copyFrom(m); 117 JdbcTypeMapping tm = typeMappings[ans.getJdbcType() - FIRST_TYPE]; 118 if (tm == null) { 119 throw BindingSupportImpl.getInstance().runtime("No JDBC type mapping found for: " + 120 JdbcTypes.toString(ans.getJdbcType()) + " (" + javaType + " " + 121 fieldName + ")"); 122 } 123 ans.copyFrom(tm); 124 return ans; 125 } 126 127 133 public void fillMappingForJdbcType(JdbcJavaTypeMapping fm) { 134 if (fm.getJdbcType() == 0) return; 135 JdbcTypeMapping tm = typeMappings[fm.getJdbcType() - FIRST_TYPE]; 136 if (tm == null) { 137 throw BindingSupportImpl.getInstance().runtime("No JDBC type mapping found for: " + 138 JdbcTypes.toString(fm.getJdbcType())); 139 } 140 fm.copyFrom(tm); 141 } 142 143 146 public JdbcTypeMapping getTypeMapping(int jdbcType) { 147 return typeMappings[jdbcType - FIRST_TYPE]; 148 } 149 150 156 public JdbcConverterFactory getJdbcConverterFactory(int jdbcType) { 157 JdbcTypes.toString(jdbcType); 158 JdbcTypeMapping tm = getTypeMapping(jdbcType); 159 if (tm == null) { 160 throw BindingSupportImpl.getInstance().illegalArgument( 161 "No JDBC type mapping found for: " + JdbcTypes.toString(jdbcType)); 162 } 163 JdbcConverterFactory ans = tm.getConverterFactory(); 164 if (ans == null) { 165 throw BindingSupportImpl.getInstance().illegalArgument( 166 "No JdbcConverter found for JDBC type: " + 167 JdbcTypes.toString(jdbcType)); 168 } 169 return ans; 170 } 171 172 176 public void registerStoreTypes(MetaDataUtils mdutils) { 177 for (Iterator i = javaTypeMappings.entrySet().iterator(); i.hasNext(); ) { 178 Map.Entry e = (Map.Entry)i.next(); 179 Class type = (Class )e.getKey(); 180 JdbcJavaTypeMapping mapping = (JdbcJavaTypeMapping)e.getValue(); 181 if (mapping.getEnabled() != JdbcJavaTypeMapping.FALSE 182 && !mdutils.isPersistentType(type, 183 184 Collections.EMPTY_MAP 185 186 187 )) 188 { 189 mdutils.registerStoreType(type); 190 } 191 } 192 } 193 194 } 195 | Popular Tags |