1 21 22 package org.apache.derby.iapi.types; 23 24 25 import org.apache.derby.iapi.services.io.ArrayInputStream; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 29 import org.apache.derby.iapi.services.io.Storable; 30 import org.apache.derby.iapi.services.io.StoredFormatIds; 31 32 import org.apache.derby.iapi.error.StandardException; 33 34 import org.apache.derby.iapi.types.DataValueDescriptor; 35 import org.apache.derby.iapi.types.TypeId; 36 import org.apache.derby.iapi.types.BooleanDataValue; 37 38 39 import org.apache.derby.iapi.services.cache.ClassSize; 40 import org.apache.derby.iapi.util.StringUtil; 41 42 import java.io.ObjectOutput ; 43 import java.io.ObjectInput ; 44 import java.io.IOException ; 45 46 import java.sql.ResultSet ; 47 import java.sql.PreparedStatement ; 48 import java.sql.SQLException ; 49 50 69 public final class SQLBoolean 70 extends DataType implements BooleanDataValue 71 { 72 76 77 80 public boolean isNull() 81 { 82 return isnull; 83 } 84 85 public boolean getBoolean() 86 { 87 return value; 88 } 89 90 private static int makeInt(boolean b) 91 { 92 return (b?1:0); 93 } 94 95 98 public byte getByte() 99 { 100 return (byte) makeInt(value); 101 } 102 103 106 public short getShort() 107 { 108 return (short) makeInt(value); 109 } 110 111 114 public int getInt() 115 { 116 return makeInt(value); 117 } 118 119 122 public long getLong() 123 { 124 return (long) makeInt(value); 125 } 126 127 130 public float getFloat() 131 { 132 return (float) makeInt(value); 133 } 134 135 138 public double getDouble() 139 { 140 return (double) makeInt(value); 141 } 142 143 146 public int typeToBigDecimal() 147 { 148 return java.sql.Types.BIGINT; 149 } 150 public String getString() 151 { 152 if (isNull()) 153 return null; 154 else if (value == true) 155 return "true"; 156 else 157 return "false"; 158 } 159 160 public Object getObject() 161 { 162 if (isNull()) 163 return null; 164 else 165 return new Boolean (value); 166 } 167 168 public int getLength() 169 { 170 return BOOLEAN_LENGTH; 171 } 172 173 public String getTypeName() 175 { 176 return TypeId.BOOLEAN_NAME; 177 } 178 179 182 183 184 189 public int getTypeFormatId() { 190 return StoredFormatIds.SQL_BOOLEAN_ID; 191 } 192 193 public void writeExternal(ObjectOutput out) throws IOException { 194 195 if (SanityManager.DEBUG) 197 SanityManager.ASSERT(! isNull()); 198 199 out.writeBoolean(value); 200 } 201 202 203 public void readExternal(ObjectInput in) throws IOException { 204 205 if (SanityManager.DEBUG) 206 SanityManager.ASSERT( ! immutable, 207 "Attempt to set the value of an immutable SQLBoolean"); 208 209 value = in.readBoolean(); 210 isnull = false; 211 } 212 public void readExternalFromArray(ArrayInputStream in) throws IOException { 213 214 if (SanityManager.DEBUG) 215 SanityManager.ASSERT( ! immutable, 216 "Attempt to set the value of an immutable SQLBoolean"); 217 218 value = in.readBoolean(); 219 isnull = false; 220 } 221 222 226 public void restoreToNull() 227 { 228 if (SanityManager.DEBUG) 229 SanityManager.ASSERT( ! immutable, 230 "Attempt to set the value of an immutable SQLBoolean"); 231 232 value = false; 233 isnull = true; 234 } 235 236 239 240 243 public int compare(DataValueDescriptor other) throws StandardException 244 { 245 248 if (typePrecedence() < other.typePrecedence()) 249 { 250 return - (other.compare(this)); 251 } 252 253 boolean thisNull, otherNull; 254 thisNull = this.isNull(); 255 otherNull = other.isNull(); 256 257 268 if (thisNull || otherNull) 269 { 270 if (!thisNull) return 1; 272 if (!otherNull) return -1; 274 return 0; 275 } 276 277 278 boolean thisValue; 279 boolean otherValue = false; 280 thisValue = this.getBoolean(); 281 282 otherValue = other.getBoolean(); 283 284 if (thisValue == otherValue) 285 return 0; 286 else if (thisValue && !otherValue) 287 return 1; 288 else 289 return -1; 290 } 291 292 295 public boolean compare(int op, 296 DataValueDescriptor other, 297 boolean orderedNulls, 298 boolean unknownRV) 299 throws StandardException 300 { 301 if (!orderedNulls) { 303 if (this.isNull() || other.isNull()) 304 return unknownRV; 305 } 306 307 return super.compare(op, other, orderedNulls, unknownRV); 308 } 309 310 313 314 315 public DataValueDescriptor getClone() 316 { 317 return new SQLBoolean(value, isnull); 318 } 319 320 323 public DataValueDescriptor getNewNull() 324 { 325 return new SQLBoolean(); 326 } 327 328 333 public void setValueFromResultSet(ResultSet resultSet, int colNumber, 334 boolean isNullable) 335 throws SQLException 336 { 337 value = resultSet.getBoolean(colNumber); 338 isnull = (isNullable && resultSet.wasNull()); 339 } 340 345 public final void setInto(PreparedStatement ps, int position) throws SQLException { 346 347 if (isNull()) { 348 ps.setNull(position, java.sql.Types.BIT); 349 return; 350 } 351 352 ps.setBoolean(position, value); 353 } 354 357 358 361 362 368 369 public SQLBoolean() 370 { 371 isnull = true; 372 } 373 374 public SQLBoolean(boolean val) 375 { 376 value = val; 377 } 378 public SQLBoolean(Boolean obj) { 379 if (isnull = (obj == null)) 380 ; 381 else 382 value = obj.booleanValue(); 383 } 384 385 386 private SQLBoolean(boolean val, boolean isnull) 387 { 388 value = val; 389 this.isnull = isnull; 390 } 391 392 393 public void setValue(boolean theValue) 394 { 395 if (SanityManager.DEBUG) 396 SanityManager.ASSERT( ! immutable, 397 "Attempt to set the value of an immutable SQLBoolean"); 398 value = theValue; 399 isnull = false; 400 401 } 402 403 public void setValue(Boolean theValue) 404 { 405 if (SanityManager.DEBUG) 406 SanityManager.ASSERT( ! immutable, 407 "Attempt to set the value of an immutable SQLBoolean"); 408 if (theValue == null) 409 { 410 value = false; 411 isnull = true; 412 } 413 else 414 { 415 value = theValue.booleanValue(); 416 isnull = false; 417 } 418 419 } 420 421 public void setValue(byte theValue) 423 { 424 if (SanityManager.DEBUG) 425 SanityManager.ASSERT( ! immutable, 426 "Attempt to set the value of an immutable SQLBoolean"); 427 value = theValue != 0; 428 isnull = false; 429 430 } 431 432 433 public void setValue(short theValue) 435 { 436 if (SanityManager.DEBUG) 437 SanityManager.ASSERT( ! immutable, 438 "Attempt to set the value of an immutable SQLBoolean"); 439 value = theValue != 0; 440 isnull = false; 441 442 } 443 444 445 public void setValue(int theValue) 447 { 448 if (SanityManager.DEBUG) 449 SanityManager.ASSERT( ! immutable, 450 "Attempt to set the value of an immutable SQLBoolean"); 451 value = theValue != 0; 452 isnull = false; 453 454 } 455 456 public void setValue(long theValue) 457 { 458 if (SanityManager.DEBUG) 459 SanityManager.ASSERT( ! immutable, 460 "Attempt to set the value of an immutable SQLBoolean"); 461 value = theValue != 0; 462 isnull = false; 463 464 } 465 466 public void setValue(float theValue) 468 { 469 if (SanityManager.DEBUG) 470 SanityManager.ASSERT( ! immutable, 471 "Attempt to set the value of an immutable SQLBoolean"); 472 value = theValue != 0; 473 isnull = false; 474 475 } 476 477 public void setValue(double theValue) 478 { 479 if (SanityManager.DEBUG) 480 SanityManager.ASSERT( ! immutable, 481 "Attempt to set the value of an immutable SQLBoolean"); 482 value = theValue != 0; 483 isnull = false; 484 485 } 486 487 public void setBigDecimal(Number bigDecimal) throws StandardException 488 { 489 if (SanityManager.DEBUG) 490 SanityManager.ASSERT( ! immutable, 491 "Attempt to set the value of an immutable SQLBoolean"); 492 if (bigDecimal == null) 493 { 494 value = false; 495 isnull = true; 496 } 497 else 498 { 499 DataValueDescriptor tempDecimal = NumberDataType.ZERO_DECIMAL.getNewNull(); 500 tempDecimal.setBigDecimal(bigDecimal); 501 value = NumberDataType.ZERO_DECIMAL.compare(tempDecimal) != 0; 502 isnull = false; 503 } 504 505 } 506 507 512 public void setValue(byte[] theValue) 513 { 514 if (SanityManager.DEBUG) 515 SanityManager.ASSERT( ! immutable, 516 "Attempt to set the value of an immutable SQLBoolean"); 517 518 if (theValue != null) 519 { 520 isnull = false; 521 int length = theValue.length; 522 523 528 for (int i = 0; i < length; i++) 529 { 530 if (theValue[i] != 0) 531 { 532 value = true; 533 return; 534 } 535 } 536 } 537 else 538 { 539 isnull = true; 540 } 541 value = false; 542 543 } 544 545 546 555 public void setValue(String theValue) 556 throws StandardException 557 { 558 if (SanityManager.DEBUG) 559 SanityManager.ASSERT( ! immutable, 560 "Attempt to set the value of an immutable SQLBoolean"); 561 if (theValue == null) 562 { 563 value = false; 564 isnull = true; 565 } 566 else 567 { 568 572 String cleanedValue = StringUtil.SQLToUpperCase(theValue.trim()); 573 if (cleanedValue.equals("TRUE")) 574 { 575 value = true; 576 } 577 else if (cleanedValue.equals("FALSE")) 578 { 579 value = false; 580 } 581 else 582 { 583 throw invalidFormat(); 584 } 585 isnull = false; 586 } 587 588 } 589 590 private void setValueCore(Number theValue) 591 { 592 if (SanityManager.DEBUG) 593 SanityManager.ASSERT( ! immutable, 594 "Attempt to set the value of an immutable SQLBoolean"); 595 596 if (theValue == null) 597 { 598 isnull = true; 599 value = false; 600 } 601 else 602 { 603 value = (theValue.intValue() != 0); 604 isnull = false; 605 } 606 } 607 608 611 void setObject(Object theValue) 612 { 613 setValue((Boolean ) theValue); 614 } 615 protected void setFrom(DataValueDescriptor theValue) throws StandardException { 616 617 setValue(theValue.getBoolean()); 618 } 619 620 621 624 625 636 637 public BooleanDataValue equals(DataValueDescriptor left, 638 DataValueDescriptor right) 639 throws StandardException 640 { 641 return truthValue(left, 642 right, 643 left.getBoolean() == right.getBoolean()); 644 } 645 646 658 659 public BooleanDataValue notEquals(DataValueDescriptor left, 660 DataValueDescriptor right) 661 throws StandardException 662 { 663 return truthValue(left, 664 right, 665 left.getBoolean() != right.getBoolean()); 666 } 667 668 680 681 public BooleanDataValue lessThan(DataValueDescriptor left, 682 DataValueDescriptor right) 683 throws StandardException 684 { 685 688 boolean leftBoolean = left.getBoolean(); 689 boolean rightBoolean = right.getBoolean(); 690 691 return truthValue(left, 692 right, 693 leftBoolean == false && rightBoolean == true); 694 } 695 696 708 709 public BooleanDataValue greaterThan(DataValueDescriptor left, 710 DataValueDescriptor right) 711 throws StandardException 712 { 713 716 boolean leftBoolean = left.getBoolean(); 717 boolean rightBoolean = right.getBoolean(); 718 719 return truthValue(left, 720 right, 721 leftBoolean == true && rightBoolean == false); 722 } 723 724 736 737 public BooleanDataValue lessOrEquals(DataValueDescriptor left, 738 DataValueDescriptor right) 739 throws StandardException 740 { 741 744 boolean leftBoolean = left.getBoolean(); 745 boolean rightBoolean = right.getBoolean(); 746 747 return truthValue(left, 748 right, 749 leftBoolean == false || rightBoolean == true); 750 } 751 752 764 765 public BooleanDataValue greaterOrEquals(DataValueDescriptor left, 766 DataValueDescriptor right) 767 throws StandardException 768 { 769 772 boolean leftBoolean = left.getBoolean(); 773 boolean rightBoolean = right.getBoolean(); 774 775 return truthValue(left, 776 right, 777 leftBoolean == true || rightBoolean == false); 778 } 779 780 789 790 public BooleanDataValue and(BooleanDataValue otherValue) 791 { 792 795 if (this.equals(false) || otherValue.equals(false)) 796 { 797 return BOOLEAN_FALSE; 798 } 799 else 800 { 801 return truthValue(this, 802 otherValue, 803 this.getBoolean() && otherValue.getBoolean()); 804 } 805 } 806 807 816 817 public BooleanDataValue or(BooleanDataValue otherValue) 818 { 819 822 if (this.equals(true) || otherValue.equals(true)) 823 { 824 return BOOLEAN_TRUE; 825 } 826 else 827 { 828 return truthValue(this, 829 otherValue, 830 this.getBoolean() || otherValue.getBoolean()); 831 } 832 } 833 834 853 public BooleanDataValue is(BooleanDataValue otherValue) 854 { 855 if ( this.equals(true) && otherValue.equals(true) ) 856 { return BOOLEAN_TRUE; } 857 858 if ( this.equals(false) && otherValue.equals(false) ) 859 { return BOOLEAN_TRUE; } 860 861 if ( this.isNull() && otherValue.isNull() ) 862 { return BOOLEAN_TRUE; } 863 864 return BOOLEAN_FALSE; 865 } 866 867 876 public BooleanDataValue isNot(BooleanDataValue otherValue) 877 { 878 BooleanDataValue isValue = is( otherValue ); 879 880 if ( isValue.equals(true) ) { return BOOLEAN_FALSE; } 881 else { return BOOLEAN_TRUE; } 882 } 883 884 900 public BooleanDataValue throwExceptionIfFalse( 901 String sqlState, 902 String tableName, 903 String constraintName) 904 throws StandardException 905 { 906 if ( ( ! isNull() ) && (value == false) ) 907 { 908 throw StandardException.newException(sqlState, 909 tableName, 910 constraintName); 911 } 912 913 return this; 914 } 915 916 919 920 921 public int typePrecedence() 922 { 923 return TypeId.BOOLEAN_PRECEDENCE; 924 } 925 926 929 930 955 956 public static SQLBoolean truthValue( 957 DataValueDescriptor leftOperand, 958 DataValueDescriptor rightOperand, 959 boolean truth) 960 { 961 962 if (leftOperand.isNull() || rightOperand.isNull()) 963 { 964 return unknownTruthValue(); 965 } 966 967 968 if (truth == true) 969 { 970 return BOOLEAN_TRUE; 971 } 972 else 973 { 974 return BOOLEAN_FALSE; 975 } 976 } 977 978 981 public static SQLBoolean truthValue( 982 DataValueDescriptor leftOperand, 983 DataValueDescriptor rightOperand, 984 Boolean truth) 985 { 986 987 if (leftOperand.isNull() || rightOperand.isNull() || truth==null) 988 { 989 return unknownTruthValue(); 990 } 991 992 993 if (truth == Boolean.TRUE) 994 { 995 return BOOLEAN_TRUE; 996 } 997 else 998 { 999 return BOOLEAN_FALSE; 1000 } 1001 } 1002 1003 1010 public static SQLBoolean truthValue(boolean value) 1011 { 1012 1016 if (value == true) 1017 return BOOLEAN_TRUE; 1018 else 1019 return BOOLEAN_FALSE; 1020 } 1021 1022 1028 public static SQLBoolean unknownTruthValue() 1029 { 1030 return UNKNOWN; 1031 } 1032 1033 1039 public static SQLBoolean falseTruthValue() 1040 { 1041 return BOOLEAN_FALSE; 1042 } 1043 1044 1050 public static SQLBoolean trueTruthValue() 1051 { 1052 return BOOLEAN_TRUE; 1053 } 1054 1055 1066 1067 public boolean equals(boolean val) 1068 { 1069 if (isNull()) 1070 return false; 1071 else 1072 return value == val; 1073 } 1074 1075 1079 public BooleanDataValue getImmutable() 1080 { 1081 if (isNull()) 1082 return SQLBoolean.UNKNOWN; 1083 1084 return value ? SQLBoolean.BOOLEAN_TRUE : SQLBoolean.BOOLEAN_FALSE; 1085 } 1086 1087 1090 1091 public String toString() 1092 { 1093 if (isNull()) 1094 return "NULL"; 1095 else if (value == true) 1096 return "true"; 1097 else 1098 return "false"; 1099 } 1100 1101 1104 public int hashCode() 1105 { 1106 if (isNull()) 1107 { 1108 return -1; 1109 } 1110 1111 return (value) ? 1 : 0; 1112 } 1113 1114 1117 static final int BOOLEAN_LENGTH = 1; 1119 private static final SQLBoolean BOOLEAN_TRUE = new SQLBoolean(true); 1120 private static final SQLBoolean BOOLEAN_FALSE = new SQLBoolean(false); 1121 static final SQLBoolean UNKNOWN = new SQLBoolean(); 1122 1123 1124 static 1125 { 1126 1127 BOOLEAN_TRUE.immutable = true; 1128 BOOLEAN_FALSE.immutable = true; 1129 UNKNOWN.immutable = true; 1130 } 1131 1132 private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLBoolean.class); 1133 1134 public int estimateMemoryUsage() 1135 { 1136 return BASE_MEMORY_USAGE; 1137 } 1138 1139 1142 private boolean value; 1143 private boolean isnull; 1144 private boolean immutable; 1145} 1146 | Popular Tags |