1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 26 import org.apache.derby.iapi.services.io.ArrayInputStream; 27 28 import org.apache.derby.iapi.services.sanity.SanityManager; 29 import org.apache.derby.iapi.services.io.StoredFormatIds; 30 import org.apache.derby.iapi.services.io.Storable; 31 32 import org.apache.derby.iapi.error.StandardException; 33 34 import org.apache.derby.iapi.types.BooleanDataValue; 35 import org.apache.derby.iapi.types.DataValueDescriptor; 36 import org.apache.derby.iapi.types.NumberDataValue; 37 import org.apache.derby.iapi.types.TypeId; 38 39 import org.apache.derby.iapi.services.cache.ClassSize; 40 41 import org.apache.derby.iapi.types.NumberDataType; 42 import org.apache.derby.iapi.types.SQLBoolean; 43 44 import java.io.ObjectOutput ; 45 import java.io.ObjectInput ; 46 import java.io.IOException ; 47 48 import java.sql.ResultSet ; 49 import java.sql.PreparedStatement ; 50 import java.sql.SQLException ; 51 52 67 public final class SQLTinyint 68 extends NumberDataType 69 { 70 71 74 static final int TINYINT_LENGTH = 1; 75 76 79 private byte value; 80 private boolean isnull; 81 82 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLTinyint.class); 83 84 public int estimateMemoryUsage() 85 { 86 return BASE_MEMORY_USAGE; 87 } 88 89 92 93 96 97 102 public SQLTinyint() 103 { 104 isnull = true; 105 } 106 107 public SQLTinyint(byte val) 108 { 109 value = val; 110 } 111 112 113 public SQLTinyint(byte val, boolean isnull) 114 { 115 value = val; 116 this.isnull = isnull; 117 } 118 119 126 129 public int getInt() 130 { 131 return (int) value; 132 } 133 134 137 public byte getByte() 138 { 139 return value; 140 } 141 142 145 public short getShort() 146 { 147 return (short) value; 148 } 149 150 153 public long getLong() 154 { 155 return (long) value; 156 } 157 158 161 public float getFloat() 162 { 163 return (float) value; 164 } 165 166 169 public double getDouble() 170 { 171 return (double) value; 172 } 173 174 177 public boolean getBoolean() 178 { 179 return (value != 0); 180 } 181 182 185 public String getString() 186 { 187 return (isNull()) ? 188 null: 189 Byte.toString(value); 190 } 191 192 195 public int getLength() 196 { 197 return TINYINT_LENGTH; 198 } 199 200 203 public Object getObject() 204 { 205 return (isNull()) ? 206 null: 207 new Integer (value); 208 } 209 210 public String getTypeName() 212 { 213 return TypeId.TINYINT_NAME; 214 } 215 216 219 220 221 226 public int getTypeFormatId() 227 { 228 return StoredFormatIds.SQL_TINYINT_ID; 229 } 230 231 234 public boolean isNull() 235 { 236 return isnull; 237 } 238 239 public void writeExternal(ObjectOutput out) throws IOException { 240 241 if (SanityManager.DEBUG) 243 SanityManager.ASSERT(! isNull()); 244 245 out.writeByte(value); 246 } 247 248 249 public void readExternal(ObjectInput in) throws IOException { 250 251 value = in.readByte(); 252 isnull = false; 253 } 254 public void readExternalFromArray(ArrayInputStream in) throws IOException { 255 256 value = in.readByte(); 257 isnull = false; 258 } 259 260 261 265 public void restoreToNull() 266 { 267 value = 0; 268 isnull = true; 269 } 270 271 272 protected int typeCompare(DataValueDescriptor arg) throws StandardException 273 { 274 275 int thisValue, otherValue; 276 277 278 thisValue = this.getInt(); 279 otherValue = arg.getInt(); 280 if (thisValue == otherValue) 281 return 0; 282 else if (thisValue > otherValue) 283 return 1; 284 else 285 return -1; 286 } 287 288 291 292 293 public DataValueDescriptor getClone() 294 { 295 return new SQLTinyint(value, isnull); 296 } 297 298 301 public DataValueDescriptor getNewNull() 302 { 303 return new SQLTinyint(); 304 } 305 306 311 public void setValueFromResultSet(ResultSet resultSet, int colNumber, 312 boolean isNullable) 313 throws SQLException 314 { 315 value = resultSet.getByte(colNumber); 316 isnull = (isNullable && resultSet.wasNull()); 317 } 318 323 public final void setInto(PreparedStatement ps, int position) throws SQLException { 324 325 if (isNull()) { 326 ps.setNull(position, java.sql.Types.TINYINT); 327 return; 328 } 329 330 ps.setByte(position, value); 331 } 332 339 public final void setInto(ResultSet rs, int position) throws SQLException , StandardException { 340 rs.updateByte(position, value); 341 } 342 343 344 347 public void setValue(String theValue) 348 throws StandardException 349 { 350 if (theValue == null) 351 { 352 value = 0; 353 isnull = true; 354 } 355 else 356 { 357 try { 358 value = Byte.valueOf(theValue.trim()).byteValue(); 359 } catch (NumberFormatException nfe) { 360 throw invalidFormat(); 361 } 362 isnull = false; 363 } 364 } 365 366 367 public void setValue(byte theValue) 368 { 369 value = theValue; 370 isnull = false; 371 } 372 373 376 public void setValue(short theValue) throws StandardException 377 { 378 if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE) 379 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT"); 380 value = (byte)theValue; 381 isnull = false; 382 } 383 384 387 public void setValue(int theValue) throws StandardException 388 { 389 if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE) 390 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT"); 391 value = (byte)theValue; 392 isnull = false; 393 } 394 395 398 public void setValue(long theValue) throws StandardException 399 { 400 if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE) 401 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT"); 402 value = (byte)theValue; 403 isnull = false; 404 } 405 406 411 public void setValue(float theValue) throws StandardException 412 { 413 theValue = NumberDataType.normalizeREAL(theValue); 414 415 if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE) 416 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT"); 417 418 float floorValue = (float)Math.floor(theValue); 419 420 value = (byte)floorValue; 421 isnull = false; 422 423 } 424 425 430 public void setValue(double theValue) throws StandardException 431 { 432 theValue = NumberDataType.normalizeDOUBLE(theValue); 433 434 if (theValue > Byte.MAX_VALUE || theValue < Byte.MIN_VALUE) 435 throw outOfRange(); 436 437 double floorValue = Math.floor(theValue); 438 439 value = (byte)floorValue; 440 isnull = false; 441 } 442 443 447 public void setValue(boolean theValue) 448 { 449 value = theValue?(byte)1:(byte)0; 450 isnull = false; 451 } 452 453 454 protected void setFrom(DataValueDescriptor theValue) throws StandardException { 455 456 setValue(theValue.getByte()); 457 } 458 459 460 463 464 465 public int typePrecedence() 466 { 467 return TypeId.TINYINT_PRECEDENCE; 468 } 469 470 473 474 485 486 public BooleanDataValue equals(DataValueDescriptor left, 487 DataValueDescriptor right) 488 throws StandardException 489 { 490 return SQLBoolean.truthValue(left, 491 right, 492 left.getByte() == right.getByte()); 493 } 494 495 507 508 public BooleanDataValue notEquals(DataValueDescriptor left, 509 DataValueDescriptor right) 510 throws StandardException 511 { 512 return SQLBoolean.truthValue(left, 513 right, 514 left.getByte() != right.getByte()); 515 } 516 517 529 530 public BooleanDataValue lessThan(DataValueDescriptor left, 531 DataValueDescriptor right) 532 throws StandardException 533 { 534 return SQLBoolean.truthValue(left, 535 right, 536 left.getByte() < right.getByte()); 537 } 538 539 551 552 public BooleanDataValue greaterThan(DataValueDescriptor left, 553 DataValueDescriptor right) 554 throws StandardException 555 { 556 return SQLBoolean.truthValue(left, 557 right, 558 left.getByte() > right.getByte()); 559 } 560 561 573 574 public BooleanDataValue lessOrEquals(DataValueDescriptor left, 575 DataValueDescriptor right) 576 throws StandardException 577 { 578 return SQLBoolean.truthValue(left, 579 right, 580 left.getByte() <= right.getByte()); 581 } 582 583 595 596 public BooleanDataValue greaterOrEquals(DataValueDescriptor left, 597 DataValueDescriptor right) 598 throws StandardException 599 { 600 return SQLBoolean.truthValue(left, 601 right, 602 left.getByte() >= right.getByte()); 603 } 604 605 606 607 608 609 621 622 public NumberDataValue times(NumberDataValue left, 623 NumberDataValue right, 624 NumberDataValue result) 625 throws StandardException 626 { 627 if (result == null) 628 { 629 result = new SQLTinyint(); 630 } 631 632 if (left.isNull() || right.isNull()) 633 { 634 result.setToNull(); 635 return result; 636 } 637 638 645 int product = left.getByte() * right.getByte(); 646 result.setValue(product); 647 return result; 648 } 649 650 651 654 655 public NumberDataValue mod(NumberDataValue dividend, 656 NumberDataValue divisor, 657 NumberDataValue result) 658 throws StandardException 659 { 660 if (result == null) 661 { 662 result = new SQLTinyint(); 663 } 664 665 if (dividend.isNull() || divisor.isNull()) 666 { 667 result.setToNull(); 668 return result; 669 } 670 671 672 byte byteDivisor = divisor.getByte(); 673 if (byteDivisor == 0) 674 { 675 throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO); 676 } 677 678 result.setValue(dividend.getByte() % byteDivisor); 679 return result; 680 } 681 691 692 public NumberDataValue minus(NumberDataValue result) 693 throws StandardException 694 { 695 if (result == null) 696 { 697 result = new SQLTinyint(); 698 } 699 700 if (this.isNull()) 701 { 702 result.setToNull(); 703 return result; 704 } 705 706 int operandValue = this.getByte(); 707 708 result.setValue(-operandValue); 709 return result; 710 } 711 712 718 719 protected boolean isNegative() 720 { 721 return !isNull() && value < 0; 722 } 723 724 727 728 public String toString() 729 { 730 if (isNull()) 731 return "NULL"; 732 else 733 return Byte.toString(value); 734 } 735 736 737 740 public int hashCode() 741 { 742 return (int) value; 743 } 744 } 745 | Popular Tags |