1 2 12 package com.versant.core.jdbc.metadata; 13 14 import com.versant.core.util.StringListParser; 15 import com.versant.core.jdbc.JdbcConverterFactory; 16 import com.versant.core.jdbc.JdbcConverterFactoryRegistry; 17 import com.versant.core.jdbc.JdbcConverter; 18 import com.versant.core.jdbc.JdbcTypeRegistry; 19 20 import com.versant.core.common.BindingSupportImpl; 21 22 30 public class JdbcTypeMapping implements Cloneable { 31 32 public static final int NOT_SET = 0; 33 public static final int FALSE = 1; 34 public static final int TRUE = 2; 35 36 private String database; 37 private int jdbcType; 38 private String sqlType; 39 private int length = -1; 40 private int scale = -1; 41 private int nulls = NOT_SET; 42 private int equalityTest = NOT_SET; 43 private JdbcConverterFactory converterFactory; 44 45 public JdbcTypeMapping() { 46 } 47 48 public JdbcTypeMapping(String sqlType, int length, int scale, 49 int nulls, int equalityTest, JdbcConverterFactory converter) { 50 this.sqlType = sqlType; 51 this.length = length; 52 this.scale = scale; 53 this.nulls = nulls; 54 this.equalityTest = equalityTest; 55 this.converterFactory = converter; 56 } 57 58 public String getDatabase() { 59 return database; 60 } 61 62 public void setDatabase(String database) { 63 this.database = database; 64 } 65 66 public int getJdbcType() { 67 return jdbcType; 68 } 69 70 public void setJdbcType(int jdbcType) { 71 this.jdbcType = jdbcType; 72 } 73 74 public String getSqlType() { 75 return sqlType; 76 } 77 78 public void setSqlType(String sqlType) { 79 this.sqlType = sqlType; 80 } 81 82 public int getLength() { 83 return length; 84 } 85 86 public void setLength(int length) { 87 this.length = length; 88 } 89 90 public int getScale() { 91 return scale; 92 } 93 94 public void setScale(int scale) { 95 this.scale = scale; 96 } 97 98 public int getNulls() { 99 return nulls; 100 } 101 102 public void setNulls(int nulls) { 103 this.nulls = nulls; 104 } 105 106 public int getEqualityTest() { 107 return equalityTest; 108 } 109 110 public void setEqualityTest(int equalityTest) { 111 this.equalityTest = equalityTest; 112 } 113 114 public JdbcConverterFactory getConverterFactory() { 115 return converterFactory; 116 } 117 118 public void setConverterFactory(JdbcConverterFactory converterFactory) { 119 this.converterFactory = converterFactory; 120 } 121 122 public String toString() { 123 StringBuffer s = new StringBuffer (); 124 if (database != null) s.append("database: " + database + " "); 125 if (jdbcType != 0) { 126 s.append("jdbcType: " + JdbcTypes.toString(jdbcType) + " "); 127 } 128 if (sqlType != null) s.append("sqlType: " + sqlType + " "); 129 if (length >= 0) s.append("length: " + length + " "); 130 if (scale >= 0) s.append("scale: " + scale + " "); 131 if (nulls != NOT_SET) s.append("nulls: " + (nulls == TRUE) + " "); 132 if (equalityTest != NOT_SET) { 133 s.append("equalityTest: " + (equalityTest == TRUE) + " "); 134 } 135 if (converterFactory != null) { 136 s.append("converterFactory: " + converterFactory); 137 } 138 return s.toString(); 139 } 140 141 private int nextTriState(StringListParser p, String name) { 142 String s = p.nextQuotedString(); 143 if (s == null) { 144 return NOT_SET; 145 } else if (s.equals("true")) { 146 return TRUE; 147 } else if (s.equals("false")) { 148 return FALSE; 149 } else { 150 throw BindingSupportImpl.getInstance().runtime( 151 "Invalid " + name + " setting: '" + s + "'"); 152 } 153 } 154 155 158 public void parse(StringListParser p, JdbcConverterFactoryRegistry jcfreg) { 159 jdbcType = JdbcTypes.parse(p.nextString()); 160 database = p.nextQuotedString(); 161 sqlType = p.nextQuotedString(); 162 length = getInt(p); 163 scale = getInt(p); 164 nulls = nextTriState(p, "nulls"); 165 equalityTest = nextTriState(p, "equalityTest"); 166 String s = p.nextQuotedString(); 167 if (s != null) converterFactory = jcfreg.getFactory(s); 168 } 169 170 private int getInt(StringListParser p) { 171 String s = p.nextQuotedString(); 172 if (s == null) return -1; 173 return Integer.parseInt(s); 174 } 175 176 182 public boolean match(int jdbcType, String database) { 183 return this.jdbcType == jdbcType 184 && (database == null 185 || this.database == null 186 || this.database.equals(database)); 187 } 188 189 193 public void copyFrom(JdbcTypeMapping r) { 194 if (database == null) database = r.database; 195 if (jdbcType == 0) jdbcType = r.jdbcType; 196 if (sqlType == null) sqlType = r.sqlType; 197 if (length == -1) length = r.length; 198 if (scale == -1) scale = r.scale; 199 if (nulls == NOT_SET) nulls = r.nulls; 200 if (equalityTest == NOT_SET) equalityTest = r.equalityTest; 201 if (converterFactory == null) converterFactory = r.converterFactory; 202 } 203 204 public Object clone() { 205 try { 206 return super.clone(); 207 } catch (CloneNotSupportedException e) { 208 throw new IllegalStateException ("clone() failed?"); 209 } 210 } 211 212 } 213 | Popular Tags |