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.types.DataValueDescriptor; 29 import org.apache.derby.iapi.types.TypeId; 30 import org.apache.derby.iapi.types.NumberDataValue; 31 import org.apache.derby.iapi.types.BooleanDataValue; 32 33 import org.apache.derby.iapi.services.io.StoredFormatIds; 34 import org.apache.derby.iapi.services.io.Storable; 35 36 import org.apache.derby.iapi.error.StandardException; 37 import org.apache.derby.iapi.services.sanity.SanityManager; 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 71 public final class SQLSmallint 72 extends NumberDataType 73 { 74 78 79 80 83 86 public int getInt() 87 { 88 return (int) value; 89 } 90 91 95 public byte getByte() throws StandardException 96 { 97 if (value > Byte.MAX_VALUE || value < Byte.MIN_VALUE) 98 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT"); 99 return (byte) value; 100 } 101 102 105 public short getShort() 106 { 107 return value; 108 } 109 110 113 public long getLong() 114 { 115 return (long) value; 116 } 117 118 121 public float getFloat() 122 { 123 return (float) value; 124 } 125 126 129 public double getDouble() 130 { 131 return (double) value; 132 } 133 134 139 public boolean getBoolean() 140 { 141 return (value != 0); 142 } 143 144 147 public String getString() 148 { 149 if (isNull()) 150 return null; 151 else 152 return Short.toString(value); 153 } 154 155 158 public int getLength() 159 { 160 return SMALLINT_LENGTH; 161 } 162 163 166 public Object getObject() 167 { 168 if (isNull()) 169 return null; 170 else 171 return new Integer (value); 172 } 173 174 public String getTypeName() 176 { 177 return TypeId.SMALLINT_NAME; 178 } 179 180 183 184 185 190 public int getTypeFormatId() { 191 return StoredFormatIds.SQL_SMALLINT_ID; 192 } 193 194 198 public boolean isNull() 199 { 200 return isnull; 201 } 202 203 public void writeExternal(ObjectOutput out) throws IOException { 204 205 if (SanityManager.DEBUG) 207 SanityManager.ASSERT(! isNull()); 208 209 out.writeShort(value); 210 } 211 212 213 public void readExternalFromArray(ArrayInputStream in) throws IOException { 214 215 value = in.readShort(); 216 isnull = false; 217 } 218 public void readExternal(ObjectInput in) throws IOException { 219 220 value = in.readShort(); 221 isnull = false; 222 } 223 224 228 public void restoreToNull() 229 { 230 value = 0; 231 isnull = true; 232 } 233 234 235 236 protected int typeCompare(DataValueDescriptor arg) throws StandardException 237 { 238 239 240 241 int thisValue = this.getInt(); 242 int otherValue = arg.getInt(); 243 if (thisValue == otherValue) 244 return 0; 245 else if (thisValue > otherValue) 246 return 1; 247 else 248 return -1; 249 } 250 251 254 255 256 public DataValueDescriptor getClone() 257 { 258 return new SQLSmallint(value, isnull); 259 } 260 261 264 public DataValueDescriptor getNewNull() 265 { 266 return new SQLSmallint(); 267 } 268 269 274 public void setValueFromResultSet(ResultSet resultSet, int colNumber, 275 boolean isNullable) 276 throws SQLException 277 { 278 try { 279 value = resultSet.getShort(colNumber); 280 isnull = (isNullable && resultSet.wasNull()); 281 } catch (SQLException selq) { 282 int i = resultSet.getInt(colNumber); 283 value = (short) i; 284 isnull = false; 285 286 } 287 } 288 293 public final void setInto(PreparedStatement ps, int position) throws SQLException { 294 295 if (isNull()) { 296 ps.setNull(position, java.sql.Types.SMALLINT); 297 return; 298 } 299 300 ps.setShort(position, value); 301 } 302 309 public final void setInto(ResultSet rs, int position) throws SQLException , StandardException { 310 rs.updateShort(position, value); 311 } 312 313 314 317 318 321 322 327 public SQLSmallint() 328 { 329 isnull = true; 330 } 331 332 public SQLSmallint(short val) 333 { 334 value = val; 335 } 336 337 338 public SQLSmallint(short val, boolean isnull) 339 { 340 value = val; 341 this.isnull = isnull; 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 = Short.valueOf(theValue.trim()).shortValue(); 359 } catch (NumberFormatException nfe) { 360 throw invalidFormat(); 361 } 362 isnull = false; 363 } 364 365 } 366 367 public void setValue(short theValue) 368 { 369 value = theValue; 370 isnull = false; 371 } 372 373 public void setValue(byte theValue) 374 { 375 value = theValue; 376 isnull = false; 377 } 378 379 382 public void setValue(int theValue) throws StandardException 383 { 384 if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE) 385 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT"); 386 value = (short)theValue; 387 isnull = false; 388 } 389 390 393 public void setValue(long theValue) throws StandardException 394 { 395 if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE) 396 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT"); 397 value = (short)theValue; 398 isnull = false; 399 } 400 401 406 public void setValue(float theValue) throws StandardException 407 { 408 theValue = NumberDataType.normalizeREAL(theValue); 409 410 if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE) 411 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT"); 412 413 float floorValue = (float)Math.floor(theValue); 414 415 value = (short)floorValue; 416 isnull = false; 417 } 418 419 424 public void setValue(double theValue) throws StandardException 425 { 426 theValue = NumberDataType.normalizeDOUBLE(theValue); 427 428 if (theValue > Short.MAX_VALUE || theValue < Short.MIN_VALUE) 429 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT"); 430 431 double floorValue = Math.floor(theValue); 432 433 value = (short)floorValue; 434 isnull = false; 435 } 436 437 441 public void setValue(boolean theValue) 442 { 443 value = theValue?(short)1:(short)0; 444 isnull = false; 445 } 446 447 protected void setFrom(DataValueDescriptor theValue) throws StandardException { 448 449 setValue(theValue.getShort()); 450 } 451 452 455 456 457 public int typePrecedence() 458 { 459 return TypeId.SMALLINT_PRECEDENCE; 460 } 461 462 463 466 467 478 479 public BooleanDataValue equals(DataValueDescriptor left, 480 DataValueDescriptor right) 481 throws StandardException 482 { 483 return SQLBoolean.truthValue(left, 484 right, 485 left.getShort() == right.getShort()); 486 } 487 488 500 501 public BooleanDataValue notEquals(DataValueDescriptor left, 502 DataValueDescriptor right) 503 throws StandardException 504 { 505 return SQLBoolean.truthValue(left, 506 right, 507 left.getShort() != right.getShort()); 508 } 509 510 523 524 public BooleanDataValue lessThan(DataValueDescriptor left, 525 DataValueDescriptor right) 526 throws StandardException 527 { 528 return SQLBoolean.truthValue(left, 529 right, 530 left.getShort() < right.getShort()); 531 } 532 533 545 546 public BooleanDataValue greaterThan(DataValueDescriptor left, 547 DataValueDescriptor right) 548 throws StandardException 549 { 550 return SQLBoolean.truthValue(left, 551 right, 552 left.getShort() > right.getShort()); 553 } 554 555 567 568 public BooleanDataValue lessOrEquals(DataValueDescriptor left, 569 DataValueDescriptor right) 570 throws StandardException 571 { 572 return SQLBoolean.truthValue(left, 573 right, 574 left.getShort() <= right.getShort()); 575 } 576 577 589 590 public BooleanDataValue greaterOrEquals(DataValueDescriptor left, 591 DataValueDescriptor right) 592 throws StandardException 593 { 594 return SQLBoolean.truthValue(left, 595 right, 596 left.getShort() >= right.getShort()); 597 } 598 599 600 601 602 614 615 public NumberDataValue times(NumberDataValue left, 616 NumberDataValue right, 617 NumberDataValue result) 618 throws StandardException 619 { 620 if (result == null) 621 { 622 result = new SQLSmallint(); 623 } 624 625 if (left.isNull() || right.isNull()) 626 { 627 result.setToNull(); 628 return result; 629 } 630 631 637 int product = left.getShort() * right.getShort(); 638 result.setValue(product); 639 return result; 640 } 641 642 643 646 public NumberDataValue mod(NumberDataValue dividend, 647 NumberDataValue divisor, 648 NumberDataValue result) 649 throws StandardException 650 { 651 if (result == null) 652 { 653 result = new SQLSmallint(); 654 } 655 656 if (dividend.isNull() || divisor.isNull()) 657 { 658 result.setToNull(); 659 return result; 660 } 661 662 663 short shortDivisor = divisor.getShort(); 664 if (shortDivisor == 0) 665 { 666 throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO); 667 } 668 669 result.setValue(dividend.getShort() % shortDivisor); 670 return result; 671 } 672 682 683 public NumberDataValue minus(NumberDataValue result) 684 throws StandardException 685 { 686 if (result == null) 687 { 688 result = new SQLSmallint(); 689 } 690 691 if (this.isNull()) 692 { 693 result.setToNull(); 694 return result; 695 } 696 697 int operandValue = this.getShort(); 698 699 result.setValue(-operandValue); 700 return result; 701 } 702 703 709 710 protected boolean isNegative() 711 { 712 return !isNull() && value < 0; 713 } 714 715 718 719 public String toString() 720 { 721 if (isNull()) 722 return "NULL"; 723 else 724 return Short.toString(value); 725 } 726 727 730 public int hashCode() 731 { 732 return (int) value; 733 } 734 735 738 static final int SMALLINT_LENGTH = 2; 739 740 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLSmallint.class); 741 742 public int estimateMemoryUsage() 743 { 744 return BASE_MEMORY_USAGE; 745 } 746 747 750 private short value; 751 private boolean isnull; 752 } 753 | Popular Tags |