1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.types.DataValueDescriptor; 25 import org.apache.derby.iapi.types.DataTypeDescriptor; 26 import org.apache.derby.iapi.types.BooleanDataValue; 27 import org.apache.derby.iapi.types.CloneableObject; 28 import org.apache.derby.iapi.types.Orderable; 29 30 import org.apache.derby.iapi.reference.SQLState; 31 import org.apache.derby.iapi.error.StandardException; 32 import org.apache.derby.iapi.services.i18n.MessageService; 33 import org.apache.derby.iapi.services.sanity.SanityManager; 34 35 import org.apache.derby.iapi.services.i18n.LocaleFinder; 36 37 import java.io.InputStream ; 38 import java.sql.Date ; 39 import java.sql.Time ; 40 import java.sql.Timestamp ; 41 import java.sql.PreparedStatement ; 42 import java.sql.SQLException ; 43 import java.sql.ResultSet ; 44 45 import java.util.Calendar ; 46 47 66 public abstract class DataType 67 implements DataValueDescriptor, CloneableObject 68 { 69 72 73 81 public boolean getBoolean() throws StandardException 82 { 83 throw dataTypeConversion("boolean"); 84 } 85 86 94 public byte getByte() throws StandardException 95 { 96 throw dataTypeConversion("byte"); 97 } 98 99 107 public short getShort() throws StandardException 108 { 109 throw dataTypeConversion("short"); 110 } 111 112 120 public int getInt() throws StandardException 121 { 122 throw dataTypeConversion("int"); 123 } 124 125 133 public long getLong() throws StandardException 134 { 135 throw dataTypeConversion("long"); 136 } 137 138 146 public float getFloat() throws StandardException 147 { 148 throw dataTypeConversion("float"); 149 } 150 151 159 public double getDouble() throws StandardException 160 { 161 throw dataTypeConversion("double"); 162 } 163 164 public int typeToBigDecimal() throws StandardException 165 { 166 throw dataTypeConversion("java.math.BigDecimal"); 167 } 168 176 public byte[] getBytes() throws StandardException 177 { 178 throw dataTypeConversion("byte[]"); 179 } 180 181 189 public Date getDate( Calendar cal) throws StandardException 190 { 191 throw dataTypeConversion("java.sql.Date"); 192 } 193 194 202 public Time getTime( Calendar cal) throws StandardException 203 { 204 throw dataTypeConversion("java.sql.Time"); 205 } 206 207 215 public Timestamp getTimestamp( Calendar cal) throws StandardException 216 { 217 throw dataTypeConversion("java.sql.Timestamp"); 218 } 219 220 228 public InputStream getStream() throws StandardException 229 { 230 throw dataTypeConversion( 231 MessageService.getTextMessage(SQLState.LANG_STREAM)); 232 } 233 234 243 public String getTraceString() throws StandardException { 244 return getString(); 245 } 246 247 250 251 259 260 public final BooleanDataValue isNullOp() 261 { 262 return SQLBoolean.truthValue(isNull()); 263 } 264 265 273 274 public final BooleanDataValue isNotNull() 275 { 276 return SQLBoolean.truthValue(!isNull()); 277 } 278 279 285 public void setValue(Time theValue) throws StandardException 286 { 287 setValue( theValue, (Calendar ) null); 288 } 289 290 297 public void setValue(Time theValue, Calendar cal) throws StandardException 298 { 299 throwLangSetMismatch("java.sql.Time"); 300 } 301 302 308 public void setValue(Timestamp theValue) throws StandardException 309 { 310 setValue( theValue, (Calendar ) null); 311 } 312 313 320 public void setValue(Timestamp theValue, Calendar cal) throws StandardException 321 { 322 throwLangSetMismatch("java.sql.Timestamp"); 323 } 324 325 331 public void setValue(Date theValue) throws StandardException 332 { 333 setValue( theValue, (Calendar ) null); 334 } 335 336 343 public void setValue(Date theValue, Calendar cal) throws StandardException 344 { 345 throwLangSetMismatch("java.sql.Date"); 346 } 347 348 354 public void setValue(String theValue) throws StandardException 355 { 356 throwLangSetMismatch("java.lang.String"); 357 } 358 359 360 368 public void setValue(int theValue) throws StandardException 369 { 370 throwLangSetMismatch("int"); 371 } 372 373 381 public void setValue(double theValue) throws StandardException 382 { 383 throwLangSetMismatch("double"); 384 } 385 386 394 public void setValue(float theValue) throws StandardException 395 { 396 throwLangSetMismatch("float"); 397 } 398 399 407 public void setValue(short theValue) throws StandardException 408 { 409 throwLangSetMismatch("short"); 410 } 411 419 public void setValue(long theValue) throws StandardException 420 { 421 throwLangSetMismatch("long"); 422 } 423 424 432 public void setValue(byte theValue) throws StandardException 433 { 434 throwLangSetMismatch("byte"); 435 } 436 437 444 public void setValue(boolean theValue) throws StandardException 445 { 446 throwLangSetMismatch("boolean"); 447 } 448 449 456 public void setValue(byte[] theValue) throws StandardException 457 { 458 throwLangSetMismatch("byte[]"); 459 } 460 461 464 public void setBigDecimal(Number bigDecimal) throws StandardException 465 { 466 throwLangSetMismatch("java.math.BigDecimal"); 467 } 468 469 470 public final void setValue(DataValueDescriptor dvd) throws StandardException { 471 472 if (dvd.isNull()) 473 { 474 setToNull(); 475 return; 476 } 477 478 try { 479 setFrom(dvd); 480 } catch (StandardException se) { 481 String msgId = se.getMessageId(); 482 483 if (SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE.equals(msgId)) 484 throw outOfRange(); 485 486 if (SQLState.LANG_FORMAT_EXCEPTION.equals(msgId)) 487 throw invalidFormat(); 488 489 throw se; 490 491 } 492 } 493 494 502 protected void setFrom(DataValueDescriptor dvd) throws StandardException 503 { 504 throw StandardException.newException(SQLState.NOT_IMPLEMENTED); 505 } 506 507 510 public void setToNull() 511 { 512 restoreToNull(); 513 } 514 515 522 public void setObjectForCast(Object theValue, boolean instanceOfResultType, 523 String resultTypeClassName) throws StandardException { 524 525 if (theValue == null) 526 { 527 setToNull(); 528 return; 529 } 530 531 535 if (!instanceOfResultType) { 536 throw StandardException.newException( 537 SQLState.LANG_DATA_TYPE_SET_MISMATCH, 538 theValue.getClass().getName(), getTypeName(resultTypeClassName)); 539 } 540 541 setObject(theValue); 542 } 543 544 549 void setObject(Object theValue) 550 throws StandardException 551 { 552 genericSetObject(theValue); 553 } 554 555 560 String getTypeName(String className) 561 { 562 return getTypeName(); 563 } 564 565 566 574 public Object getObject() throws StandardException 575 { 576 throw dataTypeConversion("java.lang.Object"); 577 } 578 579 580 void genericSetObject(Object theValue) throws StandardException { 581 582 throwLangSetMismatch(theValue); 583 } 584 585 590 public Object cloneObject() 591 { 592 return getClone(); 593 } 594 595 597 603 protected String getNationalString(LocaleFinder localeFinder) throws StandardException 604 { 605 return getString(); 606 } 607 608 public void throwLangSetMismatch(Object value) throws StandardException { 609 throwLangSetMismatch(value.getClass().getName()); 610 } 611 612 void throwLangSetMismatch(String argTypeName) throws StandardException 613 { 614 throw StandardException.newException(SQLState.LANG_DATA_TYPE_SET_MISMATCH, 615 argTypeName, this.getTypeName()); 616 617 } 618 619 public void setInto(PreparedStatement ps, int position) throws SQLException , StandardException { 620 621 ps.setObject(position, getObject()); 622 } 623 624 631 public void setInto(ResultSet rs, int position) throws SQLException , StandardException { 632 rs.updateObject(position, getObject()); 633 } 634 635 636 645 646 public void normalize( 647 DataTypeDescriptor desiredType, 648 DataValueDescriptor source) 649 throws StandardException 650 { 651 ((DataValueDescriptor) this).setValue(source); 652 } 653 669 public int typePrecedence() { 670 return -1; 671 } 672 673 684 685 public BooleanDataValue equals(DataValueDescriptor left, 686 DataValueDescriptor right) 687 throws StandardException 688 { 689 return SQLBoolean.truthValue(left, 690 right, 691 left.compare(right) == 0); 692 } 693 694 706 707 public BooleanDataValue notEquals(DataValueDescriptor left, 708 DataValueDescriptor right) 709 throws StandardException 710 { 711 return SQLBoolean.truthValue(left, 712 right, 713 left.compare(right) != 0); 714 } 715 727 728 public BooleanDataValue lessThan(DataValueDescriptor left, 729 DataValueDescriptor right) 730 throws StandardException 731 { 732 return SQLBoolean.truthValue(left, 733 right, 734 left.compare(right) < 0); 735 } 736 748 749 public BooleanDataValue greaterThan(DataValueDescriptor left, 750 DataValueDescriptor right) 751 throws StandardException 752 { 753 return SQLBoolean.truthValue(left, 754 right, 755 left.compare(right) > 0); 756 } 757 758 770 771 public BooleanDataValue lessOrEquals(DataValueDescriptor left, 772 DataValueDescriptor right) 773 throws StandardException 774 { 775 return SQLBoolean.truthValue(left, 776 right, 777 left.compare(right) <= 0); 778 } 779 780 792 793 public BooleanDataValue greaterOrEquals(DataValueDescriptor left, 794 DataValueDescriptor right) 795 throws StandardException 796 { 797 return SQLBoolean.truthValue(left, 798 right, 799 left.compare(right) >= 0); 800 } 801 public boolean compare(int op, 802 DataValueDescriptor other, 803 boolean orderedNulls, 804 boolean unknownRV) 805 throws StandardException 806 { 807 810 if (typePrecedence() < other.typePrecedence()) 811 { 812 return other.compare(flip(op), this, orderedNulls, unknownRV); 813 } 814 815 int result = compare(other); 816 817 switch(op) 818 { 819 case ORDER_OP_LESSTHAN: 820 return (result < 0); case ORDER_OP_EQUALS: 822 return (result == 0); case ORDER_OP_LESSOREQUALS: 824 return (result <= 0); case ORDER_OP_GREATERTHAN: 827 return (result > 0); case ORDER_OP_GREATEROREQUALS: 829 return (result >= 0); default: 831 if (SanityManager.DEBUG) 832 SanityManager.THROWASSERT("Invalid Operator"); 833 return false; 834 } 835 } 836 837 846 protected static int flip(int operator) 847 { 848 switch (operator) 849 { 850 case Orderable.ORDER_OP_LESSTHAN: 851 return Orderable.ORDER_OP_GREATERTHAN; 853 case Orderable.ORDER_OP_LESSOREQUALS: 854 return Orderable.ORDER_OP_GREATEROREQUALS; 856 case Orderable.ORDER_OP_EQUALS: 857 return Orderable.ORDER_OP_EQUALS; 859 default: 860 if (SanityManager.DEBUG) 863 { 864 SanityManager.THROWASSERT( 865 "Attempting to flip an operator that is not " + 866 "expected to be flipped."); 867 } 868 return operator; 869 } 870 } 871 872 875 876 880 public DataValueDescriptor coalesce(DataValueDescriptor[] argumentsList, DataValueDescriptor returnValue) 881 throws StandardException 882 { 883 if (SanityManager.DEBUG) 885 { 886 SanityManager.ASSERT(argumentsList != null, 887 "argumentsList expected to be non-null"); 888 SanityManager.ASSERT(argumentsList.length > 1, 889 "argumentsList.length expected to be > 1"); 890 } 891 892 894 int index; 895 for (index = 0; index < argumentsList.length; index++) 896 { 897 if (!(argumentsList[index].isNull())) 898 { 899 returnValue.setValue(argumentsList[index]); 900 return returnValue; 901 } 902 } 903 904 returnValue.setToNull(); 905 return returnValue; 906 907 } 908 909 913 public BooleanDataValue in(DataValueDescriptor left, 914 DataValueDescriptor[] inList, 915 boolean orderedList) 916 throws StandardException 917 { 918 BooleanDataValue retval = null; 919 920 if (SanityManager.DEBUG) 922 { 923 SanityManager.ASSERT(inList != null, 924 "inList expected to be non-null"); 925 SanityManager.ASSERT(inList.length > 0, 926 "inList.length expected to be > 0"); 927 } 928 929 if (left.isNull()) 931 { 932 return SQLBoolean.truthValue(left, 933 inList[0], 934 false); 935 } 936 937 int start = 0; 938 int finish = inList.length; 939 940 947 if (orderedList) 948 { 949 while (finish - start > 2) 950 { 951 int mid = ((finish - start) / 2) + start; 952 retval = equals(left, inList[mid]); 954 if (retval.equals(true)) 955 { 956 return retval; 957 } 958 BooleanDataValue goLeft = greaterThan(inList[mid], left); 959 if (goLeft.equals(true)) 960 { 961 finish = mid; 963 } 964 else 965 { 966 start = mid; 968 } 969 } 970 } 971 972 976 for (int index = start; index < finish; index++) 977 { 978 retval = equals(left, inList[index]); 979 if (retval.equals(true)) 980 { 981 break; 982 } 983 984 if (orderedList) 986 { 987 BooleanDataValue stop = greaterThan(inList[index], left); 988 if (stop.equals(true)) 989 { 990 break; 991 } 992 } 993 } 994 995 return retval; 996 } 997 998 1001 public boolean equals(Object other) 1002 { 1003 if (! (other instanceof DataValueDescriptor)) 1004 { 1005 return false; 1006 } 1007 1008 try 1009 { 1010 return compare(ORDER_OP_EQUALS, (DataValueDescriptor) other, true, false); 1011 } 1012 catch (StandardException se) 1013 { 1014 return false; 1015 } 1016 } 1017 1018 public void setValue(InputStream theStream, int valueLength) throws StandardException 1019 { 1020 throwLangSetMismatch("java.io.InputStream"); 1021 } 1022 1023 1029 public void checkHostVariable(int declaredLength) throws StandardException 1030 { 1031 } 1032 1033 1034 1037 protected final StandardException dataTypeConversion(String targetType) { 1038 return StandardException.newException(SQLState.LANG_DATA_TYPE_GET_MISMATCH, 1039 targetType, this.getTypeName()); 1040 1041 } 1042 1043 1046 protected final StandardException outOfRange() 1047 { 1048 return StandardException.newException( 1049 SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE, getTypeName()); 1050 } 1051 1052 1055 protected final StandardException invalidFormat() 1056 { 1057 return StandardException.newException( 1058 SQLState.LANG_FORMAT_EXCEPTION, getTypeName()); 1059 } 1060} 1061 | Popular Tags |