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 SQLInteger 71 extends NumberDataType 72 { 73 77 78 79 public int getInt() 83 { 84 85 return value; 86 } 87 88 91 public byte getByte() throws StandardException 92 { 93 if (value > Byte.MAX_VALUE || value < Byte.MIN_VALUE) 94 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "TINYINT"); 95 return (byte) value; 96 } 97 98 101 public short getShort() throws StandardException 102 { 103 if (value > Short.MAX_VALUE || value < Short.MIN_VALUE) 104 throw StandardException.newException(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, "SMALLINT"); 105 return (short) value; 106 } 107 108 public long getLong() 109 { 110 return (long) value; 111 } 112 113 public float getFloat() 114 { 115 return (float) value; 116 } 117 118 public double getDouble() 119 { 120 return (double) value; 121 } 122 123 public boolean getBoolean() 126 { 127 return (value != 0); 128 } 129 130 public String getString() 131 { 132 if (isNull()) 133 return null; 134 else 135 return Integer.toString(value); 136 } 137 138 public Object getObject() 139 { 140 if (isNull()) 141 return null; 142 else 143 return new Integer (value); 144 } 145 146 public int getLength() 147 { 148 return INTEGER_LENGTH; 149 } 150 151 public String getTypeName() 153 { 154 return TypeId.INTEGER_NAME; 155 } 156 157 160 161 162 167 public int getTypeFormatId() { 168 return StoredFormatIds.SQL_INTEGER_ID; 169 } 170 171 174 175 public boolean isNull() 176 { 177 return isnull; 178 } 179 180 public void writeExternal(ObjectOutput out) throws IOException { 181 182 if (SanityManager.DEBUG) 184 SanityManager.ASSERT(! isNull()); 185 186 out.writeInt(value); 187 } 188 189 190 public final void readExternal(ObjectInput in) 191 throws IOException { 192 193 value = in.readInt(); 194 isnull = false; 195 } 196 public final void readExternalFromArray(ArrayInputStream in) 197 throws IOException { 198 199 value = in.readInt(); 200 isnull = false; 201 } 202 203 207 208 public void restoreToNull() 209 { 210 value = 0; 211 isnull = true; 212 } 213 214 215 216 protected int typeCompare(DataValueDescriptor arg) throws StandardException 217 { 218 219 220 int thisValue = this.getInt(); 221 222 int otherValue = arg.getInt(); 223 224 if (thisValue == otherValue) 225 return 0; 226 else if (thisValue > otherValue) 227 return 1; 228 else 229 return -1; 230 } 231 232 233 236 237 238 public DataValueDescriptor getClone() 239 { 240 SQLInteger nsi = new SQLInteger(value); 241 242 nsi.isnull = isnull; 243 return nsi; 244 } 245 246 249 public DataValueDescriptor getNewNull() 250 { 251 return new SQLInteger(); 252 } 253 254 259 public void setValueFromResultSet(ResultSet resultSet, int colNumber, 260 boolean isNullable) 261 throws SQLException 262 { 263 if ((value = resultSet.getInt(colNumber)) == 0) 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.INTEGER); 277 return; 278 } 279 280 ps.setInt(position, value); 281 } 282 288 public final void setInto(ResultSet rs, int position) throws SQLException { 289 rs.updateInt(position, value); 290 } 291 292 295 296 299 300 301 public SQLInteger() 304 { 305 isnull = true; 306 } 307 308 public SQLInteger(int val) 309 { 310 value = val; 311 } 312 313 public SQLInteger(char val) 314 { 315 value = val; 316 } 317 318 public SQLInteger(Integer obj) { 319 if (isnull = (obj == null)) 320 ; 321 else 322 value = obj.intValue(); 323 } 324 325 328 public void setValue(String theValue) 329 throws StandardException 330 { 331 if (theValue == null) 332 { 333 value = 0; 334 isnull = true; 335 } 336 else 337 { 338 try { 339 value = Integer.valueOf(theValue.trim()).intValue(); 340 } catch (NumberFormatException nfe) { 341 throw invalidFormat(); 342 } 343 isnull = false; 344 } 345 } 346 347 public void setValue(int theValue) 348 { 349 value = theValue; 350 isnull = false; 351 } 352 353 356 public void setValue(long theValue) throws StandardException 357 { 358 if (theValue > Integer.MAX_VALUE || theValue < Integer.MIN_VALUE) { 359 throw outOfRange(); 360 } 361 362 value = (int)theValue; 363 isnull = false; 364 } 365 366 371 public void setValue(float theValue) throws StandardException 372 { 373 theValue = NumberDataType.normalizeREAL(theValue); 374 375 if (theValue > Integer.MAX_VALUE || theValue < Integer.MIN_VALUE) 376 throw outOfRange(); 377 378 float floorValue = (float)Math.floor(theValue); 379 380 value = (int)floorValue; 381 isnull = false; 382 } 383 384 389 public void setValue(double theValue) throws StandardException 390 { 391 theValue = NumberDataType.normalizeDOUBLE(theValue); 392 393 if (theValue > Integer.MAX_VALUE || theValue < Integer.MIN_VALUE) 394 throw outOfRange(); 395 396 double floorValue = Math.floor(theValue); 397 398 value = (int)floorValue; 399 isnull = false; 400 } 401 402 public void setValue(boolean theValue) 403 { 404 value = theValue?1:0; 405 isnull = false; 406 } 407 408 protected void setFrom(DataValueDescriptor theValue) throws StandardException { 409 410 setValue(theValue.getInt()); 411 } 412 413 416 417 418 public int typePrecedence() 419 { 420 return TypeId.INT_PRECEDENCE; 421 } 422 423 426 427 438 439 public BooleanDataValue equals(DataValueDescriptor left, 440 DataValueDescriptor right) 441 throws StandardException 442 { 443 return SQLBoolean.truthValue(left, 444 right, 445 left.getInt() == right.getInt()); 446 } 447 448 460 461 public BooleanDataValue notEquals(DataValueDescriptor left, 462 DataValueDescriptor right) 463 throws StandardException 464 { 465 return SQLBoolean.truthValue(left, 466 right, 467 left.getInt() != right.getInt()); 468 } 469 470 482 483 public BooleanDataValue lessThan(DataValueDescriptor left, 484 DataValueDescriptor right) 485 throws StandardException 486 { 487 return SQLBoolean.truthValue(left, 488 right, 489 left.getInt() < right.getInt()); 490 } 491 492 504 505 public BooleanDataValue greaterThan(DataValueDescriptor left, 506 DataValueDescriptor right) 507 throws StandardException 508 { 509 return SQLBoolean.truthValue(left, 510 right, 511 left.getInt() > right.getInt()); 512 } 513 514 526 527 public BooleanDataValue lessOrEquals(DataValueDescriptor left, 528 DataValueDescriptor right) 529 throws StandardException 530 { 531 return SQLBoolean.truthValue(left, 532 right, 533 left.getInt() <= right.getInt()); 534 } 535 536 548 549 public BooleanDataValue greaterOrEquals(DataValueDescriptor left, 550 DataValueDescriptor right) 551 throws StandardException 552 { 553 return SQLBoolean.truthValue(left, 554 right, 555 left.getInt() >= right.getInt()); 556 } 557 558 570 571 public NumberDataValue times(NumberDataValue left, 572 NumberDataValue right, 573 NumberDataValue result) 574 throws StandardException 575 { 576 if (result == null) 577 { 578 result = new SQLInteger(); 579 } 580 581 if (left.isNull() || right.isNull()) 582 { 583 result.setToNull(); 584 return result; 585 } 586 587 596 long tempResult = left.getLong() * right.getLong(); 597 598 result.setValue(tempResult); 599 return result; 600 } 601 602 603 606 public NumberDataValue mod(NumberDataValue dividend, 607 NumberDataValue divisor, 608 NumberDataValue result) 609 throws StandardException { 610 if (result == null) 611 { 612 result = new SQLInteger(); 613 } 614 615 if (dividend.isNull() || divisor.isNull()) 616 { 617 result.setToNull(); 618 return result; 619 } 620 621 622 int intDivisor = divisor.getInt(); 623 if (intDivisor == 0) 624 { 625 throw StandardException.newException(SQLState.LANG_DIVIDE_BY_ZERO); 626 } 627 628 result.setValue(dividend.getInt() % intDivisor); 629 return result; 630 } 631 641 642 public NumberDataValue minus(NumberDataValue result) 643 throws StandardException 644 { 645 int operandValue; 646 647 if (result == null) 648 { 649 result = new SQLInteger(); 650 } 651 652 if (this.isNull()) 653 { 654 result.setToNull(); 655 return result; 656 } 657 658 operandValue = this.getInt(); 659 660 666 if (operandValue == Integer.MIN_VALUE) 667 { 668 throw outOfRange(); 669 } 670 671 result.setValue(-operandValue); 672 return result; 673 } 674 675 681 682 protected boolean isNegative() 683 { 684 return !isNull() && value < 0; 685 } 686 687 690 691 public String toString() 692 { 693 if (isNull()) 694 return "NULL"; 695 else 696 return Integer.toString(value); 697 } 698 699 702 public int hashCode() 703 { 704 return value; 705 } 706 707 710 static final int INTEGER_LENGTH = 4; 712 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLInteger.class); 713 714 public int estimateMemoryUsage() 715 { 716 return BASE_MEMORY_USAGE; 717 } 718 719 722 private int value; 723 private boolean isnull; 724 } 725 | Popular Tags |