1 21 22 package org.apache.derby.catalog.types; 23 24 import org.apache.derby.iapi.services.io.StoredFormatIds; 25 import org.apache.derby.iapi.services.io.Formatable; 26 27 import org.apache.derby.catalog.TypeDescriptor; 28 29 import java.io.ObjectOutput ; 30 import java.io.ObjectInput ; 31 import java.io.IOException ; 32 import java.sql.Types ; 33 34 public class TypeDescriptorImpl implements TypeDescriptor, Formatable 35 { 36 49 50 private BaseTypeIdImpl typeId; 51 private int precision; 52 private int scale; 53 private boolean isNullable; 54 private int maximumWidth; 55 56 60 public TypeDescriptorImpl() {} 61 62 72 public TypeDescriptorImpl( 73 BaseTypeIdImpl typeId, 74 int precision, 75 int scale, 76 boolean isNullable, 77 int maximumWidth) 78 { 79 this.typeId = typeId; 80 this.precision = precision; 81 this.scale = scale; 82 this.isNullable = isNullable; 83 this.maximumWidth = maximumWidth; 84 } 85 86 94 public TypeDescriptorImpl( 95 BaseTypeIdImpl typeId, 96 boolean isNullable, 97 int maximumWidth) 98 { 99 this.typeId = typeId; 100 this.isNullable = isNullable; 101 this.maximumWidth = maximumWidth; 102 } 103 104 116 public TypeDescriptorImpl( 117 TypeDescriptorImpl source, 118 int precision, 119 int scale, 120 boolean isNullable, 121 int maximumWidth) 122 { 123 this.typeId = source.typeId; 124 this.precision = precision; 125 this.scale = scale; 126 this.isNullable = isNullable; 127 this.maximumWidth = maximumWidth; 128 } 129 130 138 public TypeDescriptorImpl( 139 TypeDescriptorImpl source, 140 boolean isNullable, 141 int maximumWidth) 142 { 143 this.typeId = source.typeId; 144 this.precision = source.precision; 145 this.scale = source.scale; 146 this.isNullable = isNullable; 147 this.maximumWidth = maximumWidth; 148 } 149 150 153 public int getMaximumWidth() 154 { 155 return maximumWidth; 156 } 157 158 175 public int getMaximumWidthInBytes() 176 { 177 switch (typeId.getJDBCTypeId()) { 178 179 case Types.BIT: 180 case Types.TINYINT: 181 case Types.SMALLINT: 182 case Types.INTEGER: 183 case Types.REAL: 184 case Types.DOUBLE: 185 case Types.FLOAT: 186 case Types.BINARY: 187 case Types.VARBINARY: 188 case Types.LONGVARBINARY: 189 case Types.BLOB: 190 191 return maximumWidth; 194 195 case Types.BIGINT: 201 return 40; 202 203 case Types.DATE: 209 case Types.TIME: 210 return 6; 211 212 case Types.TIMESTAMP: 213 return 16; 214 215 case Types.NUMERIC: 223 case Types.DECIMAL: 224 return 2 * (precision + 2); 225 226 case Types.CHAR: 234 case Types.VARCHAR: 235 case Types.LONGVARCHAR: 236 case Types.CLOB: 237 if ((maximumWidth > 0) && (2 * maximumWidth < 0)) 238 return Integer.MAX_VALUE; 240 else 241 return 2 * maximumWidth; 242 243 case Types.ARRAY: 244 case Types.DISTINCT: 245 case Types.NULL: 246 case Types.OTHER: 247 case Types.REF: 248 case Types.STRUCT: 249 case Types.JAVA_OBJECT: 250 default: 251 252 return -1; 255 256 } 257 258 } 259 260 268 public int getJDBCTypeId() 269 { 270 return typeId.getJDBCTypeId(); 271 } 272 273 279 public String getTypeName() 280 { 281 return typeId.getSQLTypeName(); 282 } 283 284 290 public int getPrecision() 291 { 292 return precision; 293 } 294 295 302 public int getScale() 303 { 304 return scale; 305 } 306 307 314 public boolean isNullable() 315 { 316 return isNullable; 317 } 318 319 325 public void setNullability(boolean nullable) 326 { 327 isNullable = nullable; 328 } 329 330 345 public String getSQLstring() 346 { 347 return typeId.toParsableString( this ); 348 } 349 350 public String toString() 351 { 352 String s = getSQLstring(); 353 if (!isNullable()) 354 return s + " NOT NULL"; 355 return s; 356 } 357 358 361 public BaseTypeIdImpl getTypeId() 362 { 363 return typeId; 364 } 365 366 370 public boolean equals(Object object) 371 { 372 TypeDescriptor typeDescriptor = (TypeDescriptor)object; 373 374 if(!this.getTypeName().equals(typeDescriptor.getTypeName()) || 375 this.precision != typeDescriptor.getPrecision() || 376 this.scale != typeDescriptor.getScale() || 377 this.isNullable != typeDescriptor.isNullable() || 378 this.maximumWidth != typeDescriptor.getMaximumWidth()) 379 return false; 380 else 381 return true; 382 } 383 384 386 394 public void readExternal( ObjectInput in ) 395 throws IOException , ClassNotFoundException 396 { 397 typeId = (BaseTypeIdImpl) in.readObject(); 398 precision = in.readInt(); 399 scale = in.readInt(); 400 isNullable = in.readBoolean(); 401 maximumWidth = in.readInt(); 402 } 403 404 411 public void writeExternal( ObjectOutput out ) 412 throws IOException 413 { 414 out.writeObject( typeId ); 415 out.writeInt( precision ); 416 out.writeInt( scale ); 417 out.writeBoolean( isNullable ); 418 out.writeInt( maximumWidth ); 419 } 420 421 426 public int getTypeFormatId() { return StoredFormatIds.DATA_TYPE_IMPL_DESCRIPTOR_V01_ID; } 427 } 428 | Popular Tags |