1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.services.io.ArrayInputStream; 25 26 import org.apache.derby.iapi.types.DataValueDescriptor; 27 import org.apache.derby.iapi.types.TypeId; 28 import org.apache.derby.iapi.types.NumberDataValue; 29 import org.apache.derby.iapi.types.BooleanDataValue; 30 import org.apache.derby.iapi.reference.SQLState; 31 32 import org.apache.derby.iapi.services.io.StoredFormatIds; 33 import org.apache.derby.iapi.services.io.Storable; 34 35 import org.apache.derby.iapi.error.StandardException; 36 import org.apache.derby.iapi.services.sanity.SanityManager; 37 38 import org.apache.derby.iapi.services.cache.ClassSize; 39 40 import org.apache.derby.iapi.types.NumberDataType; 41 import org.apache.derby.iapi.types.SQLBoolean; 42 43 import java.io.ObjectOutput ; 44 import java.io.ObjectInput ; 45 import java.io.IOException ; 46 47 import java.sql.ResultSet ; 48 import java.sql.PreparedStatement ; 49 import java.sql.SQLException ; 50 51 70 public final class SQLLongint 71 extends NumberDataType 72 { 73 77 78 79 85 public int getInt() throws StandardException 86 { 87 88 89 if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) 90 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "INTEGER"); 91 return (int) value; 92 } 93 94 97 public byte getByte() throws StandardException 98 { 99 if (value > Byte.MAX_VALUE || value < Byte.MIN_VALUE) 100 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT"); 101 return (byte) value; 102 } 103 104 107 public short getShort() throws StandardException 108 { 109 if (value > Short.MAX_VALUE || value < Short.MIN_VALUE) 110 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT"); 111 return (short) value; 112 } 113 114 public long getLong() 115 { 116 return value; 117 } 118 119 public float getFloat() 120 { 121 return (float) value; 122 } 123 124 public double getDouble() 125 { 126 return (double) value; 127 } 128 129 public boolean getBoolean() 132 { 133 return (value != 0); 134 } 135 136 public String getString() 137 { 138 if (isNull()) 139 return null; 140 else 141 return Long.toString(value); 142 } 143 144 public Object getObject() 145 { 146 if (isNull()) 147 return null; 148 else 149 return new Long (value); 150 } 151 152 public int getLength() 153 { 154 return TypeId.LONGINT_MAXWIDTH; 155 } 156 157 public String getTypeName() 159 { 160 return TypeId.LONGINT_NAME; 161 } 162 163 166 167 168 173 public int getTypeFormatId() { 174 return StoredFormatIds.SQL_LONGINT_ID; 175 } 176 177 180 181 public boolean isNull() 182 { 183 return isnull; 184 } 185 186 public void writeExternal(ObjectOutput out) throws IOException { 187 188 if (SanityManager.DEBUG) 190 SanityManager.ASSERT(! isNull()); 191 192 out.writeLong(value); 193 } 194 195 196 public void readExternal(ObjectInput in) throws IOException { 197 198 value = in.readLong(); 199 isnull = false; 200 } 201 public void readExternalFromArray(ArrayInputStream in) throws IOException { 202 203 value = in.readLong(); 204 isnull = false; 205 } 206 207 211 212 public void restoreToNull() 213 { 214 value = 0; 215 isnull = true; 216 } 217 218 219 protected int typeCompare(DataValueDescriptor arg) throws StandardException 220 { 221 222 223 224 long thisValue = this.getLong(); 225 226 long otherValue = arg.getLong(); 227 228 if (thisValue == otherValue) 229 return 0; 230 else if (thisValue > otherValue) 231 return 1; 232 else 233 return -1; 234 } 235 236 239 240 241 public DataValueDescriptor getClone() 242 { 243 return new SQLLongint(value, isnull); 244 } 245 246 249 public DataValueDescriptor getNewNull() 250 { 251 return new SQLLongint(); 252 } 253 254 259 public void setValueFromResultSet(ResultSet resultSet, int colNumber, 260 boolean isNullable) 261 throws SQLException 262 { 263 if ((value = resultSet.getLong(colNumber)) == 0L) 264 isnull = (isNullable && resultSet.wasNull()); 265 else 266 isnull = false; 267 } 268 273 public final void setInto(PreparedStatement ps, int position) throws SQLException { 274 275 if (isNull()) { 276 ps.setNull(position, java.sql.Types.BIGINT); 277 return; 278 } 279 280 ps.setLong(position, value); 281 } 282 288 public final void setInto(ResultSet rs, int position) throws SQLException { 289 rs.updateLong(position, value); 290 } 291 292 295 296 299 300 301 public SQLLongint() 304 { 305 isnull = true; 306 } 307 308 public SQLLongint(long val) 309 { 310 value = val; 311 } 312 313 314 private SQLLongint(long val, boolean isnull) 315 { 316 value = val; 317 this.isnull = isnull; 318 } 319 public SQLLongint(Long obj) { 320 if (isnull = (obj == null)) 321 ; 322 else 323 value = obj.longValue(); 324 } 325 326 329 public void setValue(String theValue) 330 throws StandardException 331 { 332 if (theValue == null) 333 { 334 value = 0; 335 isnull = true; 336 } 337 else 338 { 339 try { 340 value = Long.valueOf(theValue.trim()).longValue(); 341 } catch (NumberFormatException nfe) { 342 throw invalidFormat(); 343 } 344 isnull = false; 345 } 346 } 347 348 353 public final void setValue(Number theValue) 354 { 355 if (objectNull(theValue)) 356 return; 357 358 if (SanityManager.ASSERT) 359 { 360 if (!(theValue instanceof java.lang.Long )) 361 SanityManager.THROWASSERT("SQLLongint.setValue(Number) passed a " + theValue.getClass()); 362 } 363 364 setValue(theValue.longValue()); 365 } 366 367 public void setValue(long theValue) 368 { 369 value = theValue; 370 isnull = false; 371 } 372 373 public void setValue(int theValue) 374 { 375 value = theValue; 376 isnull = false; 377 } 378 379 384 public void setValue(float theValue) throws StandardException 385 { 386 theValue = NumberDataType.normalizeREAL(theValue); 387 388 if (theValue > Long.MAX_VALUE 389 || theValue < Long.MIN_VALUE) 390 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT"); 391 392 float floorValue = (float)Math.floor(theValue); 393 394 value = (long)floorValue; 395 isnull = false; 396 } 397 398 403 public void setValue(double theValue) throws StandardException 404 { 405 theValue = NumberDataType.normalizeDOUBLE(theValue); 406 407 if (theValue > Long.MAX_VALUE 408 || theValue < Long.MIN_VALUE) 409 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT"); 410 411 double floorValue = Math.floor(theValue); 412 413 value = (long)floorValue; 414 isnull = false; 415 416 } 417 418 422 public void setValue(boolean theValue) 423 { 424 value = theValue?1:0; 425 isnull = false; 426 427 } 428 429 433 void setObject(Object theValue) 434 { 435 setValue(((Long ) theValue).longValue()); 436 } 437 438 protected void setFrom(DataValueDescriptor theValue) throws StandardException { 439 440 setValue(theValue.getLong()); 441 } 442 443 446 447 448 public int typePrecedence() 449 { 450 return TypeId.LONGINT_PRECEDENCE; 451 } 452 453 456 457 468 469 public BooleanDataValue equals(DataValueDescriptor left, 470 DataValueDescriptor right) 471 throws StandardException 472 { 473 return SQLBoolean.truthValue(left, 474 right, 475 left.getLong() == right.getLong()); 476 } 477 478 490 491 public BooleanDataValue notEquals(DataValueDescriptor left, 492 DataValueDescriptor right) 493 throws StandardException 494 { 495 return SQLBoolean.truthValue(left, 496 right, 497 left.getLong() != right.getLong()); 498 } 499 500 512 513 public BooleanDataValue lessThan(DataValueDescriptor left, 514 DataValueDescriptor right) 515 throws StandardException 516 { 517 return SQLBoolean.truthValue(left, 518 right, 519 left.getLong() < right.getLong()); 520 } 521 522 534 535 public BooleanDataValue greaterThan(DataValueDescriptor left, 536 DataValueDescriptor right) 537 throws StandardException 538 { 539 return SQLBoolean.truthValue(left, 540 right, 541 left.getLong() > right.getLong()); 542 } 543 544 556 557 public BooleanDataValue lessOrEquals(DataValueDescriptor left, 558 DataValueDescriptor right) 559 throws StandardException 560 { 561 return SQLBoolean.truthValue(left, 562 right, 563 left.getLong() <= right.getLong()); 564 } 565 566 578 579 public BooleanDataValue greaterOrEquals(DataValueDescriptor left, 580 DataValueDescriptor right) 581 throws StandardException 582 { 583 return SQLBoolean.truthValue(left, 584 right, 585 left.getLong() >= right.getLong()); 586 } 587 588 600 601 public NumberDataValue plus(NumberDataValue addend1, 602 NumberDataValue addend2, 603 NumberDataValue result) 604 throws StandardException 605 { 606 if (result == null) 607 { 608 result = new SQLLongint(); 609 } 610 611 if (addend1.isNull() || addend2.isNull()) 612 { 613 result.setToNull(); 614 return result; 615 } 616 long addend1Long = addend1.getLong(); 617 long addend2Long = addend2.getLong(); 618 619 long resultValue = addend1Long + addend2Long; 620 621 629 if ((addend1Long < 0) == (addend2Long < 0)) 630 { 631 635 if ((addend1Long < 0) != (resultValue < 0)) 636 { 637 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT"); 638 } 639 } 640 result.setValue(resultValue); 641 642 return result; 643 } 644 645 657 658 public NumberDataValue minus(NumberDataValue left, 659 NumberDataValue right, 660 NumberDataValue result) 661 throws StandardException 662 { 663 if (result == null) 664 { 665 result = new SQLLongint(); 666 } 667 668 if (left.isNull() || right.isNull()) 669 { 670 result.setToNull(); 671 return result; 672 } 673 674 long diff = left.getLong() - right.getLong(); 675 676 684 if ((left.getLong() < 0) != (right.getLong() < 0)) 685 { 686 690 if ((left.getLong() < 0) != (diff < 0)) 691 { 692 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT"); 693 } 694 } 695 696 result.setValue(diff); 697 return result; 698 } 699 700 712 713 public NumberDataValue times(NumberDataValue left, 714 NumberDataValue right, 715 NumberDataValue result) 716 throws StandardException 717 { 718 long tempResult; 719 720 if (result == null) 721 { 722 result = new SQLLongint(); 723 } 724 725 if (left.isNull() || right.isNull()) 726 { 727 result.setToNull(); 728 return result; 729 } 730 731 740 tempResult = left.getLong() * right.getLong(); 741 if ((right.getLong() != 0) && (left.getLong() != tempResult / right.getLong())) 742 { 743 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT"); 744 } 745 746 result.setValue(tempResult); 747 return result; 748 } 749 750 762 763 public NumberDataValue divide(NumberDataValue dividend, 764 NumberDataValue divisor, 765 NumberDataValue result) 766 throws StandardException 767 { 768 long longDivisor; 769 770 if (result == null) 771 { 772 result = new SQLLongint(); 773 } 774 775 if (dividend.isNull() || divisor.isNull()) 776 { 777 result.setToNull(); 778 return result; 779 } 780 781 782 longDivisor = divisor.getLong(); 783 if (longDivisor == 0) 784 { 785 throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO); 786 } 787 788 result.setValue(dividend.getLong() / longDivisor); 789 return result; 790 } 791 794 public NumberDataValue mod(NumberDataValue dividend, 795 NumberDataValue divisor, 796 NumberDataValue result) 797 throws StandardException 798 { 799 if (result == null) 800 { 801 result = new SQLLongint(); 802 } 803 804 if (dividend.isNull() || divisor.isNull()) 805 { 806 result.setToNull(); 807 return result; 808 } 809 810 811 long longDivisor = divisor.getLong(); 812 if (longDivisor == 0) 813 { 814 throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO); 815 } 816 817 result.setValue(dividend.getLong() % longDivisor); 818 return result; 819 } 820 830 831 public NumberDataValue minus(NumberDataValue result) 832 throws StandardException 833 { 834 long operandValue; 835 836 if (result == null) 837 { 838 result = new SQLLongint(); 839 } 840 841 if (this.isNull()) 842 { 843 result.setToNull(); 844 return result; 845 } 846 847 operandValue = this.getLong(); 848 849 854 if (operandValue == Long.MIN_VALUE) 855 { 856 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "BIGINT"); 857 } 858 859 result.setValue(-operandValue); 860 return result; 861 } 862 863 870 871 protected boolean isNegative() 872 { 873 return !isNull() && value < 0L; 874 } 875 876 879 880 public String toString() 881 { 882 if (isNull()) 883 return "NULL"; 884 else 885 return Long.toString(value); 886 } 887 888 889 892 public int hashCode() 893 { 894 return (int) (value ^ (value >> 32)); 895 } 896 897 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLLongint.class); 898 899 public int estimateMemoryUsage() 900 { 901 return BASE_MEMORY_USAGE; 902 } 903 904 907 private long value; 908 private boolean isnull; 909 } 910 | Popular Tags |