1 23 24 package com.sun.jdo.spi.persistence.generator.database; 25 26 27 import com.sun.jdo.spi.persistence.utility.StringHelper; 28 29 import com.sun.jdo.spi.persistence.utility.logging.Logger; 30 31 35 class JDBCInfo { 36 37 43 44 private static final String INDICATOR_TYPE = 45 DatabaseGenerationConstants.INDICATOR_JDBC_TYPE; 46 47 48 private static final String INDICATOR_NULLABLE = 49 DatabaseGenerationConstants.INDICATOR_JDBC_NULLABLE; 50 51 52 private static final String INDICATOR_PRECISION = 53 DatabaseGenerationConstants.INDICATOR_JDBC_PRECISION; 54 55 56 private static final String INDICATOR_SCALE = 57 DatabaseGenerationConstants.INDICATOR_JDBC_SCALE; 58 59 60 private static final String INDICATOR_LENGTH = 61 DatabaseGenerationConstants.INDICATOR_JDBC_LENGTH; 62 63 64 private static final String NO_LENGTH_INDICATOR = "null"; 65 66 67 private static final Integer NO_LENGTH = new Integer (-1); 68 69 70 private static final Logger logger = 71 LogHelperDatabaseGenerator.getLogger(); 72 73 74 private int jdbcType; 75 76 77 private boolean nullable = false; 78 79 80 private Integer precision = null; 81 82 83 private Integer scale = null; 84 85 86 private Integer length = null; 87 88 89 93 94 private byte fieldsWithValues = 0; 95 96 97 private static final byte MASK_JDBC_TYPE = 1 << 0; 98 99 100 private static final byte MASK_NULLABLE = 1 << 1; 101 102 103 private static final byte MASK_PRECISION = 1 << 2; 104 105 106 private static final byte MASK_SCALE = 1 << 3; 107 108 109 private static final byte MASK_LENGTH = 1 << 4; 110 111 112 private static final byte MASK_ALL = MASK_JDBC_TYPE | MASK_NULLABLE 113 | MASK_PRECISION | MASK_SCALE | MASK_LENGTH; 114 115 116 124 JDBCInfo(int jdbcType, Integer precision, Integer scale, 125 Integer length, boolean nullable) { 126 127 this.jdbcType = jdbcType; 128 this.precision = precision; 129 this.scale = scale; 130 this.length = length; 131 this.nullable = nullable; 132 133 fieldsWithValues = MASK_ALL; 134 } 135 136 140 JDBCInfo() { } 141 142 143 154 void setValue(String value, String indicator) 155 throws IllegalJDBCTypeException { 156 157 if (indicator.equals(INDICATOR_TYPE)) { 158 if (!StringHelper.isEmpty(value)) { 159 Integer type = MappingPolicy.getJdbcType(value); 160 if (null == type) { 161 throw new IllegalJDBCTypeException(); 162 } 163 this.jdbcType = type.intValue(); 164 this.fieldsWithValues |= MASK_JDBC_TYPE; 165 } 166 167 } else if (indicator.equals(INDICATOR_NULLABLE)) { 168 if (StringHelper.isEmpty(value)) { 169 this.nullable = false; } else { 171 this.nullable = Boolean.valueOf(value).booleanValue(); 172 } 173 this.fieldsWithValues |= MASK_NULLABLE; 174 175 } else if (indicator.equals(INDICATOR_PRECISION)) { 176 this.precision = getIntegerValue(value); 177 this.fieldsWithValues |= MASK_PRECISION; 178 179 } else if (indicator.equals(INDICATOR_SCALE)) { 180 this.scale = getIntegerValue(value); 181 this.fieldsWithValues |= MASK_SCALE; 182 183 } else if (indicator.equals(INDICATOR_LENGTH)) { 184 if (value.trim().equals(NO_LENGTH_INDICATOR)) { 185 this.length = NO_LENGTH; 186 } else { 187 this.length = getIntegerValue(value); 188 } 189 this.fieldsWithValues |= MASK_LENGTH; 190 } 191 } 192 193 197 private Integer getIntegerValue(String s) { 198 Integer rc = null; 199 if (!StringHelper.isEmpty(s)) { 200 rc = new Integer (s); 201 } 202 return rc; 203 } 204 205 223 224 231 void complete(JDBCInfo other) { 240 if (logger.isLoggable(Logger.FINEST)) { 241 logger.finest("Entering JDBCInfo.complete: " + "\nthis: " + this + "\nother: " + other); } 245 if (MASK_ALL != fieldsWithValues) { 246 if ((fieldsWithValues & MASK_JDBC_TYPE) == 0) { 247 this.jdbcType = other.jdbcType; 248 } 249 250 if ((fieldsWithValues & MASK_NULLABLE) == 0) { 251 this.nullable = other.nullable; 252 } 253 254 if ((fieldsWithValues & MASK_PRECISION) == 0) { 255 this.precision = other.precision; 256 } 257 258 if ((fieldsWithValues & MASK_SCALE) == 0) { 259 this.scale = other.scale; 260 } 261 262 if ((fieldsWithValues & MASK_LENGTH) == 0 263 || (other.length == NO_LENGTH) 264 || (other.length.intValue() < this.length.intValue())) { 265 this.length = other.length; 266 } 267 268 fieldsWithValues = MASK_ALL; 269 } 270 if (logger.isLoggable(Logger.FINEST)) { 271 logger.finest("Leaving JDBCInfo.complete: " + "\nthis: " + this); } 274 } 275 276 280 boolean isComplete() { 281 return fieldsWithValues == MASK_ALL; 282 } 283 284 289 void override(JDBCInfo other) { 290 if (null != other) { 291 this.jdbcType = other.jdbcType; 292 } 293 } 294 295 298 public boolean hasJdbcType() { 299 return (fieldsWithValues & MASK_JDBC_TYPE) == 1; 300 } 301 302 306 public int getJdbcType() { 307 return jdbcType; 308 } 309 310 314 public boolean getNullable() { 315 return nullable; 316 } 317 318 321 public Integer getPrecision() { 322 return precision; 323 } 324 325 328 public Integer getScale() { 329 return scale; 330 } 331 332 336 public Integer getLength() { 337 return NO_LENGTH.equals(length) ? null : length; 338 } 339 340 344 public String toString() { 345 return "JDBCInfo:" + " jdbcType=" + jdbcType + " nullable=" + nullable + " precision=" + precision + " scale=" + scale + " length=" + length + " fieldsWithValues=0x" + Integer.toHexString(fieldsWithValues); } 353 354 357 static class IllegalJDBCTypeException extends Exception { 358 IllegalJDBCTypeException() { } 359 } 360 } 361 | Popular Tags |