1 2 12 package com.versant.core.jdbc.metadata; 13 14 import com.versant.core.util.*; 15 import com.versant.core.jdbc.JdbcConverterFactory; 16 import com.versant.core.jdbc.JdbcConverterFactoryRegistry; 17 18 import com.versant.core.common.BindingSupportImpl; 19 20 27 public class JdbcJavaTypeMapping { 28 29 public static final int NOT_SET = 0; 30 public static final int FALSE = 1; 31 public static final int TRUE = 2; 32 33 private Class javaType; 34 private String database; 35 private String columnName; 36 private int jdbcType; 37 private String sqlType; 38 private int length = -1; 39 private int scale = -1; 40 private int nulls = NOT_SET; 41 private int equalityTest = NOT_SET; 42 private JdbcConverterFactory converterFactory; 43 private int enabled = NOT_SET; 44 private int shared = NOT_SET; 45 46 public JdbcJavaTypeMapping() { 47 } 48 49 public JdbcJavaTypeMapping(Class javaType, int jdbcType, int length, 50 int scale, int nulls, JdbcConverterFactory converterFactory) { 51 this.javaType = javaType; 52 this.jdbcType = jdbcType; 53 this.length = length; 54 this.scale = scale; 55 this.nulls = nulls; 56 this.equalityTest = TRUE; 57 this.converterFactory = converterFactory; 58 enabled = TRUE; 59 } 60 61 public JdbcJavaTypeMapping(Class javaType, int jdbcType, int length, 62 int scale, int nulls, JdbcConverterFactory converterFactory, 63 boolean enabled) { 64 this(javaType, jdbcType, length, scale, nulls, converterFactory); 65 this.enabled = enabled ? TRUE : FALSE; 66 } 67 68 public JdbcJavaTypeMapping(Class javaType, int jdbcType, boolean nulls, 69 boolean equalityTest) { 70 this(javaType, jdbcType, nulls); 71 this.equalityTest = equalityTest ? TRUE : FALSE; 72 enabled = TRUE; 73 } 74 75 public JdbcJavaTypeMapping(Class javaType, int jdbcType, boolean nulls) { 76 this.javaType = javaType; 77 this.jdbcType = jdbcType; 78 this.nulls = nulls ? TRUE : FALSE; 79 enabled = TRUE; 80 } 81 82 public JdbcJavaTypeMapping(Class javaType, int jdbcType, 83 JdbcConverterFactory converterFactory) { 84 this.javaType = javaType; 85 this.jdbcType = jdbcType; 86 this.nulls = TRUE; 87 this.equalityTest = FALSE; 88 this.converterFactory = converterFactory; 89 enabled = TRUE; 90 } 91 92 public JdbcJavaTypeMapping(Class javaType, int jdbcType, 93 JdbcConverterFactory converterFactory, boolean enabled) { 94 this(javaType, jdbcType, converterFactory); 95 this.enabled = enabled ? TRUE : FALSE; 96 } 97 98 public Class getJavaType() { 99 return javaType; 100 } 101 102 public void setJavaType(Class javaType) { 103 this.javaType = javaType; 104 } 105 106 public String getDatabase() { 107 return database; 108 } 109 110 public void setDatabase(String database) { 111 this.database = database; 112 } 113 114 public String getColumnName() { 115 return columnName; 116 } 117 118 public void setColumnName(String columnName) { 119 this.columnName = columnName; 120 } 121 122 public int getJdbcType() { 123 return jdbcType; 124 } 125 126 public void setJdbcType(int jdbcType) { 127 this.jdbcType = jdbcType; 128 } 129 130 public String getSqlType() { 131 return sqlType; 132 } 133 134 public void setSqlType(String sqlType) { 135 this.sqlType = sqlType; 136 } 137 138 public int getLength() { 139 return length; 140 } 141 142 public void setLength(int length) { 143 this.length = length; 144 } 145 146 public int getScale() { 147 return scale; 148 } 149 150 public void setScale(int scale) { 151 this.scale = scale; 152 } 153 154 public int getNulls() { 155 return nulls; 156 } 157 158 public void setNulls(int nulls) { 159 this.nulls = nulls; 160 } 161 162 public void setNulls(boolean nulls) { 163 this.nulls = nulls ? TRUE : FALSE; 164 } 165 166 public int getEqualityTest() { 167 return equalityTest; 168 } 169 170 public void setEqualityTest(int equalityTest) { 171 this.equalityTest = equalityTest; 172 } 173 174 public void setEqualityTest(boolean equalityTest) { 175 this.equalityTest = equalityTest ? TRUE : FALSE; 176 } 177 178 public JdbcConverterFactory getConverterFactory() { 179 return converterFactory; 180 } 181 182 public void setConverterFactory(JdbcConverterFactory converterFactory) { 183 this.converterFactory = converterFactory; 184 } 185 186 public int getEnabled() { 187 return enabled; 188 } 189 190 public void setEnabled(boolean enabled) { 191 this.enabled = enabled ? TRUE : FALSE; 192 } 193 194 public int getShared() { 195 return shared; 196 } 197 198 public void setShared(int shared) { 199 this.shared = shared; 200 } 201 202 public void setShared(boolean on) { 203 this.shared = on ? TRUE : FALSE; 204 } 205 206 public String toString() { 207 StringBuffer s = new StringBuffer (); 208 if (javaType != null) s.append("javaType: " + javaType + " "); 209 if (database != null) s.append("database: " + database + " "); 210 if (jdbcType != 0) s.append("jdbcType: " + JdbcTypes.toString(jdbcType) + " "); 211 if (sqlType != null) s.append("sqlType: " + sqlType + " "); 212 if (length >= 0) s.append("length: " + length + " "); 213 if (scale >= 0) s.append("scale: " + scale + " "); 214 if (nulls != NOT_SET) s.append("nulls: " + (nulls == TRUE) + " "); 215 if (equalityTest != NOT_SET) s.append("equalityTest: " + (equalityTest == TRUE) + " "); 216 if (converterFactory != null) s.append("converterFactory: " + converterFactory); 217 if (enabled != NOT_SET) s.append("enabled: " + (enabled == TRUE) + " "); 218 if (shared != NOT_SET) s.append("shared: " + (shared == TRUE) + " "); 219 return s.toString(); 220 } 221 222 228 public boolean match(String database, String fieldName, Class javaType) { 229 return (this.database == null || this.database.equals(database)) 230 && (this.javaType == null || this.javaType == javaType); 231 } 232 233 238 public void copyFrom(JdbcJavaTypeMapping r) { 239 if (javaType == null) javaType = r.javaType; 240 if (database == null) database = r.database; 241 if (columnName == null) columnName = r.columnName; 242 if (jdbcType == 0) jdbcType = r.jdbcType; 243 if (sqlType == null) sqlType = r.sqlType; 244 if (length == -1) length = r.length; 245 if (scale == -1) scale = r.scale; 246 if (nulls == NOT_SET) nulls = r.nulls; 247 if (equalityTest == NOT_SET) equalityTest = r.equalityTest; 248 if (converterFactory == null) converterFactory = r.converterFactory; 249 if (enabled == NOT_SET) enabled = r.enabled; 250 if (shared == NOT_SET) shared = r.shared; 251 } 252 253 257 public void copyFrom(JdbcTypeMapping r) { 258 if (sqlType == null) sqlType = r.getSqlType(); 259 if (length == -1) length = r.getLength(); 260 if (scale == -1) scale = r.getScale(); 261 if (nulls == NOT_SET) nulls = r.getNulls(); 262 if (equalityTest == NOT_SET) equalityTest = r.getEqualityTest(); 263 if (converterFactory == null) converterFactory = r.getConverterFactory(); 264 } 265 266 private int nextTriState(StringListParser p, String name) { 267 String s = p.nextQuotedString(); 268 if (s == null) return NOT_SET; 269 else if (s.equals("true")) return TRUE; 270 else if (s.equals("false")) return FALSE; 271 else throw BindingSupportImpl.getInstance().runtime("Invalid " + name + " setting: '" + s + "'"); 272 } 273 274 277 public void parse(StringListParser p, JdbcConverterFactoryRegistry jcfreg) { 278 try { 279 javaType = p.nextClass(jcfreg.getLoader()); 280 } catch (ClassNotFoundException e) { 281 throw BindingSupportImpl.getInstance().runtime( 282 "Unable to load class: " + e.getMessage() , e); 283 } 284 database = p.nextQuotedString(); 285 String s = p.nextQuotedString(); 286 jdbcType = s == null ? 0 : JdbcTypes.parse(s); 287 sqlType = p.nextQuotedString(); 288 length = getInt(p); 289 scale = getInt(p); 290 nulls = nextTriState(p, "nulls"); 291 equalityTest = nextTriState(p, "equalityTest"); 292 s = p.nextQuotedString(); 293 if (s != null) converterFactory = jcfreg.getFactory(s); 294 if (p.hasNext()) enabled = nextTriState(p, "enabled"); 295 } 296 297 private int getInt(StringListParser p) { 298 String s = p.nextQuotedString(); 299 if (s == null) return -1; 300 return Integer.parseInt(s); 301 } 302 303 } 304 305 | Popular Tags |