1 21 22 package org.apache.derby.iapi.types; 23 24 import org.apache.derby.iapi.types.NumberDataValue; 25 import org.apache.derby.iapi.types.BooleanDataValue; 26 import org.apache.derby.iapi.types.BitDataValue; 27 import org.apache.derby.iapi.types.DateTimeDataValue; 28 import org.apache.derby.iapi.types.StringDataValue; 29 import org.apache.derby.iapi.types.UserDataValue; 30 import org.apache.derby.iapi.types.RefDataValue; 31 32 import org.apache.derby.iapi.types.DataValueFactory; 33 import org.apache.derby.iapi.types.DataValueDescriptor; 34 35 import org.apache.derby.iapi.types.RowLocation; 36 37 import org.apache.derby.iapi.error.StandardException; 38 39 import org.apache.derby.iapi.services.sanity.SanityManager; 40 41 import org.apache.derby.iapi.services.i18n.LocaleFinder; 42 import org.apache.derby.iapi.services.io.RegisteredFormatIds; 43 import org.apache.derby.iapi.services.io.StoredFormatIds; 44 import org.apache.derby.iapi.services.monitor.ModuleControl; 45 46 import java.sql.Date ; 47 import java.sql.Time ; 48 import java.sql.Timestamp ; 49 import java.util.Properties ; 50 51 52 import org.apache.derby.iapi.db.DatabaseContext; 53 import org.apache.derby.iapi.services.context.ContextService; 54 55 62 abstract class DataValueFactoryImpl implements DataValueFactory, ModuleControl 63 { 64 LocaleFinder localeFinder; 65 66 DataValueFactoryImpl() 67 { 68 } 69 70 73 74 77 public void boot(boolean create, Properties properties) throws StandardException { 78 79 DataValueDescriptor decimalImplementation = getNullDecimal(null); 80 81 TypeId.decimalImplementation = decimalImplementation; 82 RegisteredFormatIds.TwoByte[StoredFormatIds.SQL_DECIMAL_ID] 83 = decimalImplementation.getClass().getName(); 84 85 86 decimalImplementation = decimalImplementation.getNewNull(); 88 decimalImplementation.setValue(0L); 89 NumberDataType.ZERO_DECIMAL = decimalImplementation; 90 91 92 93 } 94 95 98 public void stop() { 99 } 100 101 105 public NumberDataValue getDataValue(int value) 106 { 107 return new SQLInteger(value); 108 } 109 110 public NumberDataValue getDataValue(int value, NumberDataValue previous) 111 throws StandardException 112 { 113 if (previous == null) 114 return new SQLInteger(value); 115 previous.setValue(value); 116 return previous; 117 } 118 119 public NumberDataValue getDataValue(Integer value) 120 { 121 if (value != null) 122 return new SQLInteger(value.intValue()); 123 else 124 return new SQLInteger(); 125 } 126 127 public NumberDataValue getDataValue(Integer value, NumberDataValue previous) 128 throws StandardException 129 { 130 if (previous == null) 131 { 132 return getDataValue(value); 133 } 134 135 previous.setValue(value); 136 return previous; 137 } 138 139 public NumberDataValue getDataValue(char value) 140 { 141 return new SQLInteger(value); 142 } 143 144 public NumberDataValue getDataValue(char value, NumberDataValue previous) 145 throws StandardException 146 { 147 if (previous == null) 148 return new SQLInteger(value); 149 previous.setValue(value); 150 return previous; 151 } 152 153 public NumberDataValue getDataValue(short value) 154 { 155 return new SQLSmallint(value); 156 } 157 158 public NumberDataValue getDataValue(short value, NumberDataValue previous) 159 throws StandardException 160 { 161 if (previous == null) 162 return new SQLSmallint(value); 163 previous.setValue(value); 164 return previous; 165 } 166 167 public NumberDataValue getDataValue(Short value) 168 { 169 if (value != null) 170 return new SQLSmallint(value.shortValue()); 171 else 172 return new SQLSmallint(); 173 } 174 175 public NumberDataValue getDataValue(Short value, NumberDataValue previous) 176 throws StandardException 177 { 178 if (previous == null) 179 return getDataValue(value); 180 181 previous.setValue(value); 182 return previous; 183 } 184 185 public NumberDataValue getDataValue(byte value) 186 { 187 return new SQLTinyint(value); 188 } 189 190 public NumberDataValue getDataValue(byte value, NumberDataValue previous) 191 throws StandardException 192 { 193 if (previous == null) 194 return new SQLTinyint(value); 195 previous.setValue(value); 196 return previous; 197 } 198 199 public NumberDataValue getDataValue(Byte value) 200 { 201 if (value != null) 202 return new SQLTinyint(value.byteValue()); 203 else 204 return new SQLTinyint(); 205 } 206 207 public NumberDataValue getDataValue(Byte value, NumberDataValue previous) 208 throws StandardException 209 { 210 if (previous == null) 211 return getDataValue(value); 212 213 previous.setValue(value); 214 return previous; 215 } 216 217 public NumberDataValue getDataValue(long value) 218 { 219 return new SQLLongint(value); 220 } 221 222 public NumberDataValue getDataValue(long value, NumberDataValue previous) 223 throws StandardException 224 { 225 if (previous == null) 226 return new SQLLongint(value); 227 previous.setValue(value); 228 return previous; 229 } 230 231 public NumberDataValue getDataValue(Long value) 232 { 233 if (value != null) 234 return new SQLLongint(value.longValue()); 235 else 236 return new SQLLongint(); 237 } 238 239 public NumberDataValue getDataValue(Long value, NumberDataValue previous) 240 throws StandardException 241 { 242 if (previous == null) 243 return getDataValue(value); 244 245 previous.setValue(value); 246 return previous; 247 } 248 249 public NumberDataValue getDataValue(float value) 250 throws StandardException 251 { 252 return new SQLReal(value); 253 } 254 255 public NumberDataValue getDataValue(float value, NumberDataValue previous) 256 throws StandardException 257 { 258 if (previous == null) 259 return new SQLReal(value); 260 previous.setValue(value); 261 return previous; 262 } 263 264 public NumberDataValue getDataValue(Float value) 265 throws StandardException 266 { 267 if (value != null) 268 return new SQLReal(value.floatValue()); 269 else 270 return new SQLReal(); 271 } 272 273 public NumberDataValue getDataValue(Float value, NumberDataValue previous) 274 throws StandardException 275 { 276 if (previous == null) 277 return getDataValue(value); 278 279 previous.setValue(value); 280 return previous; 281 } 282 283 public NumberDataValue getDataValue(double value) throws StandardException 284 { 285 return new SQLDouble(value); 286 } 287 288 public NumberDataValue getDataValue(double value, NumberDataValue previous) 289 throws StandardException 290 { 291 if (previous == null) 292 return new SQLDouble(value); 293 previous.setValue(value); 294 return previous; 295 } 296 297 public NumberDataValue getDataValue(Double value) throws StandardException 298 { 299 if (value != null) 300 return new SQLDouble(value.doubleValue()); 301 else 302 return new SQLDouble(); 303 } 304 305 public NumberDataValue getDataValue(Double value, NumberDataValue previous) 306 throws StandardException 307 { 308 if (previous == null) 309 return getDataValue(value); 310 311 previous.setValue(value); 312 return previous; 313 } 314 public final NumberDataValue getDecimalDataValue(Number value) 315 throws StandardException 316 { 317 NumberDataValue ndv = getNullDecimal((NumberDataValue) null); 318 ndv.setValue(value); 319 return ndv; 320 } 321 322 public final NumberDataValue getDecimalDataValue(Number value, NumberDataValue previous) 323 throws StandardException 324 { 325 if (previous == null) 326 return getDecimalDataValue(value); 327 328 previous.setValue(value); 329 return previous; 330 } 331 332 public final NumberDataValue getDecimalDataValue(String value, 333 NumberDataValue previous) 334 throws StandardException 335 { 336 if (previous == null) 337 return getDecimalDataValue(value); 338 339 previous.setValue(value); 340 return previous; 341 } 342 343 public BooleanDataValue getDataValue(boolean value) 344 { 345 return new SQLBoolean(value); 346 } 347 348 public BooleanDataValue getDataValue(boolean value, 349 BooleanDataValue previous) 350 throws StandardException 351 { 352 if (previous == null) 353 return new SQLBoolean(value); 354 355 previous.setValue(value); 356 return previous; 357 } 358 359 public BooleanDataValue getDataValue(Boolean value) 360 { 361 if (value != null) 362 return new SQLBoolean(value.booleanValue()); 363 else 364 return new SQLBoolean(); 365 } 366 367 public BooleanDataValue getDataValue(Boolean value, 368 BooleanDataValue previous) 369 throws StandardException 370 { 371 if (previous == null) 372 return getDataValue(value); 373 374 previous.setValue(value); 375 return previous; 376 } 377 378 public BooleanDataValue getDataValue(BooleanDataValue value) 379 { 380 if (value != null) 381 return value; 382 else 383 return new SQLBoolean(); 384 } 385 386 public BitDataValue getBitDataValue(byte[] value) throws StandardException 387 { 388 return new SQLBit(value); 389 } 390 391 public BitDataValue getBitDataValue(byte[] value, BitDataValue previous) 392 throws StandardException 393 { 394 if (previous == null) 395 return new SQLBit(value); 396 previous.setValue(value); 397 return previous; 398 } 399 400 public BitDataValue getVarbitDataValue(byte[] value) 401 { 402 return new SQLVarbit(value); 403 } 404 405 public BitDataValue getVarbitDataValue(byte[] value, BitDataValue previous) 406 throws StandardException 407 { 408 if (previous == null) 409 return new SQLVarbit(value); 410 previous.setValue(value); 411 return previous; 412 } 413 414 415 417 public BitDataValue getLongVarbitDataValue(byte[] value) throws StandardException 418 { 419 return new SQLLongVarbit(value); 420 } 421 422 public BitDataValue getLongVarbitDataValue(byte[] value, BitDataValue previous) 423 throws StandardException 424 { 425 if (previous == null) 426 return new SQLLongVarbit(value); 427 previous.setValue(value); 428 return previous; 429 } 430 431 public BitDataValue getBlobDataValue(byte[] value) throws StandardException 433 { 434 return new SQLBlob(value); 435 } 436 437 public BitDataValue getBlobDataValue(byte[] value, BitDataValue previous) 438 throws StandardException 439 { 440 if (previous == null) 441 return new SQLBlob(value); 442 previous.setValue(value); 443 return previous; 444 } 445 446 public StringDataValue getCharDataValue(String value) 448 { 449 return new SQLChar(value); 450 } 451 452 public StringDataValue getCharDataValue(String value, 453 StringDataValue previous) 454 throws StandardException 455 { 456 if (previous == null) 457 return new SQLChar(value); 458 previous.setValue(value); 459 return previous; 460 } 461 462 public StringDataValue getVarcharDataValue(String value) 463 { 464 return new SQLVarchar(value); 465 } 466 467 public StringDataValue getVarcharDataValue(String value, 468 StringDataValue previous) 469 throws StandardException 470 { 471 if (previous == null) 472 return new SQLVarchar(value); 473 previous.setValue(value); 474 return previous; 475 } 476 477 public StringDataValue getLongvarcharDataValue(String value) 478 { 479 return new SQLLongvarchar(value); 480 } 481 482 public StringDataValue getClobDataValue(String value) 483 { 484 return new SQLClob(value); 485 } 486 487 public StringDataValue getLongvarcharDataValue(String value, 488 StringDataValue previous) 489 throws StandardException 490 { 491 if (previous == null) 492 return new SQLLongvarchar(value); 493 previous.setValue(value); 494 return previous; 495 } 496 497 public StringDataValue getClobDataValue(String value, StringDataValue previous) throws StandardException 498 { 499 if (previous == null) 500 return new SQLClob(value); 501 previous.setValue(value); 502 return previous; 503 } 504 505 public StringDataValue getNationalCharDataValue(String value) 507 { 508 return new SQLNationalChar(value, getLocaleFinder()); 509 } 510 511 public StringDataValue getNationalCharDataValue(String value, 512 StringDataValue previous) 513 throws StandardException 514 { 515 if (previous == null) 516 return new SQLNationalChar(value, getLocaleFinder()); 517 previous.setValue(value); 518 return previous; 519 } 520 521 public StringDataValue getNationalVarcharDataValue(String value) 522 { 523 return new SQLNationalVarchar(value, getLocaleFinder()); 524 } 525 526 public StringDataValue getNationalVarcharDataValue(String value, 527 StringDataValue previous) 528 throws StandardException 529 { 530 if (previous == null) 531 return new SQLNationalVarchar(value, getLocaleFinder()); 532 previous.setValue(value); 533 return previous; 534 } 535 536 public StringDataValue getNationalLongvarcharDataValue(String value) 537 { 538 return new SQLNationalLongvarchar(value, getLocaleFinder()); 539 } 540 541 public StringDataValue getNationalLongvarcharDataValue(String value, 542 StringDataValue previous) 543 throws StandardException 544 { 545 if (previous == null) 546 return new SQLNationalLongvarchar(value, getLocaleFinder()); 547 previous.setValue(value); 548 return previous; 549 } 550 551 public StringDataValue getNClobDataValue(String value) 552 { 553 return new SQLNClob(value, getLocaleFinder()); 554 } 555 556 public StringDataValue getNClobDataValue(String value, StringDataValue previous) 557 throws StandardException 558 { 559 if (previous == null) 560 return new SQLNClob(value, getLocaleFinder()); 561 previous.setValue(value); 562 return previous; 563 } 564 565 public DateTimeDataValue getDataValue(Date value) throws StandardException 566 { 567 return new SQLDate(value); 568 } 569 570 public DateTimeDataValue getDataValue(Date value, 571 DateTimeDataValue previous) 572 throws StandardException 573 { 574 if (previous == null) 575 return new SQLDate(value); 576 previous.setValue(value); 577 return previous; 578 } 579 580 public DateTimeDataValue getDataValue(Time value) throws StandardException 581 { 582 return new SQLTime(value); 583 } 584 585 public DateTimeDataValue getDataValue(Time value, 586 DateTimeDataValue previous) 587 throws StandardException 588 { 589 if (previous == null) 590 return new SQLTime(value); 591 previous.setValue(value); 592 return previous; 593 } 594 595 public DateTimeDataValue getDataValue(Timestamp value) throws StandardException 596 { 597 return new SQLTimestamp(value); 598 } 599 600 public DateTimeDataValue getDataValue(Timestamp value, 601 DateTimeDataValue previous) 602 throws StandardException 603 { 604 if (previous == null) 605 return new SQLTimestamp(value); 606 previous.setValue(value); 607 return previous; 608 } 609 610 617 public DateTimeDataValue getDate( DataValueDescriptor operand) throws StandardException 618 { 619 return SQLDate.computeDateFunction( operand, this); 620 } 621 622 629 public DateTimeDataValue getTimestamp( DataValueDescriptor operand) throws StandardException 630 { 631 return SQLTimestamp.computeTimestampFunction( operand, this); 632 } 633 634 public DateTimeDataValue getTimestamp( DataValueDescriptor date, DataValueDescriptor time) throws StandardException 635 { 636 return new SQLTimestamp( date, time); 637 } 638 639 public UserDataValue getDataValue(Object value) 640 { 641 return new UserType(value); 642 } 643 644 public UserDataValue getDataValue(Object value, 645 UserDataValue previous) 646 { 647 if (previous == null) 648 return new UserType(value); 649 ((UserType) previous).setValue(value); 650 return previous; 651 } 652 653 public RefDataValue getDataValue(RowLocation value) 654 { 655 return new SQLRef(value); 656 } 657 658 public RefDataValue getDataValue(RowLocation value, RefDataValue previous) 659 { 660 if (previous == null) 661 return new SQLRef(value); 662 previous.setValue(value); 663 return previous; 664 } 665 666 public NumberDataValue getNullInteger(NumberDataValue dataValue) 667 { 668 if (dataValue == null) 669 { 670 return new SQLInteger(); 671 } 672 else 673 { 674 dataValue.setToNull(); 675 return dataValue; 676 } 677 } 678 679 public NumberDataValue getNullShort(NumberDataValue dataValue) 680 { 681 if (dataValue == null) 682 { 683 return new SQLSmallint(); 684 } 685 else 686 { 687 dataValue.setToNull(); 688 return dataValue; 689 } 690 } 691 692 public NumberDataValue getNullLong(NumberDataValue dataValue) 693 { 694 if (dataValue == null) 695 { 696 return new SQLLongint(); 697 } 698 else 699 { 700 dataValue.setToNull(); 701 return dataValue; 702 } 703 } 704 705 public NumberDataValue getNullByte(NumberDataValue dataValue) 706 { 707 if (dataValue == null) 708 { 709 return new SQLTinyint(); 710 } 711 else 712 { 713 dataValue.setToNull(); 714 return dataValue; 715 } 716 } 717 718 public NumberDataValue getNullFloat(NumberDataValue dataValue) 719 { 720 if (dataValue == null) 721 { 722 return new SQLReal(); 723 } 724 else 725 { 726 dataValue.setToNull(); 727 return dataValue; 728 } 729 } 730 731 public NumberDataValue getNullDouble(NumberDataValue dataValue) 732 { 733 if (dataValue == null) 734 { 735 return new SQLDouble(); 736 } 737 else 738 { 739 dataValue.setToNull(); 740 return dataValue; 741 } 742 } 743 744 public BooleanDataValue getNullBoolean(BooleanDataValue dataValue) 745 { 746 if (dataValue == null) 747 { 748 return new SQLBoolean(); 749 } 750 else 751 { 752 dataValue.setToNull(); 753 return dataValue; 754 } 755 } 756 757 public BitDataValue getNullBit(BitDataValue dataValue) throws StandardException 758 { 759 if (dataValue == null) 760 { 761 return getBitDataValue((byte[]) null); 762 } 763 else 764 { 765 dataValue.setToNull(); 766 return dataValue; 767 } 768 } 769 770 771 public BitDataValue getNullVarbit(BitDataValue dataValue) throws StandardException 772 { 773 if (dataValue == null) 774 { 775 return getVarbitDataValue((byte[]) null); 776 } 777 else 778 { 779 dataValue.setToNull(); 780 return dataValue; 781 } 782 } 783 784 public BitDataValue getNullLongVarbit(BitDataValue dataValue) throws StandardException 786 { 787 if (dataValue == null) 788 { 789 return getLongVarbitDataValue((byte[]) null); 790 } 791 else 792 { 793 dataValue.setToNull(); 794 return dataValue; 795 } 796 } 797 798 public BitDataValue getNullBlob(BitDataValue dataValue) throws StandardException 800 { 801 if (dataValue == null) 802 { 803 return getBlobDataValue((byte[]) null); 804 } 805 else 806 { 807 dataValue.setToNull(); 808 return dataValue; 809 } 810 } 811 812 public StringDataValue getNullChar(StringDataValue dataValue) 814 { 815 if (dataValue == null) 816 { 817 return getCharDataValue((String ) null); 818 } 819 else 820 { 821 dataValue.setToNull(); 822 return dataValue; 823 } 824 } 825 826 public StringDataValue getNullVarchar(StringDataValue dataValue) 827 { 828 if (dataValue == null) 829 { 830 return getVarcharDataValue((String ) null); 831 } 832 else 833 { 834 dataValue.setToNull(); 835 return dataValue; 836 } 837 } 838 839 public StringDataValue getNullLongvarchar(StringDataValue dataValue) 840 { 841 if (dataValue == null) 842 { 843 return getLongvarcharDataValue((String ) null); 844 } 845 else 846 { 847 dataValue.setToNull(); 848 return dataValue; 849 } 850 } 851 852 public StringDataValue getNullClob(StringDataValue dataValue) 853 { 854 if (dataValue == null) 855 { 856 return getClobDataValue((String ) null); 857 } 858 else 859 { 860 dataValue.setToNull(); 861 return dataValue; 862 } 863 } 864 865 public StringDataValue getNullNationalChar(StringDataValue dataValue) 866 { 867 if (dataValue == null) 868 { 869 return getNationalCharDataValue((String ) null); 870 } 871 else 872 { 873 dataValue.setToNull(); 874 return dataValue; 875 } 876 } 877 878 public StringDataValue getNullNationalVarchar(StringDataValue dataValue) 879 { 880 if (dataValue == null) 881 { 882 return getNationalVarcharDataValue((String ) null); 883 } 884 else 885 { 886 dataValue.setToNull(); 887 return dataValue; 888 } 889 } 890 891 public StringDataValue getNullNationalLongvarchar(StringDataValue dataValue) 892 { 893 if (dataValue == null) 894 { 895 return getNationalLongvarcharDataValue((String ) null); 896 } 897 else 898 { 899 dataValue.setToNull(); 900 return dataValue; 901 } 902 } 903 904 public StringDataValue getNullNClob(StringDataValue dataValue) 905 { 906 if (dataValue == null) 907 { 908 return getNClobDataValue((String ) null); 909 } 910 else 911 { 912 dataValue.setToNull(); 913 return dataValue; 914 } 915 } 916 917 public UserDataValue getNullObject(UserDataValue dataValue) 918 { 919 if (dataValue == null) 920 { 921 return getDataValue((Object ) null); 922 } 923 else 924 { 925 dataValue.setToNull(); 926 return dataValue; 927 } 928 } 929 930 public RefDataValue getNullRef(RefDataValue dataValue) 931 { 932 if (dataValue == null) 933 { 934 return getDataValue((RowLocation) null); 935 } 936 else 937 { 938 dataValue.setToNull(); 939 return dataValue; 940 } 941 } 942 943 public DateTimeDataValue getNullDate(DateTimeDataValue dataValue) 944 { 945 if (dataValue == null) 946 { 947 try 948 { 949 return getDataValue((Date ) null); 950 } 951 catch( StandardException se) 952 { 953 if( SanityManager.DEBUG) 954 { 955 SanityManager.THROWASSERT( "Could not get a null date.", se); 956 } 957 return null; 958 } 959 } 960 else 961 { 962 dataValue.setToNull(); 963 return dataValue; 964 } 965 } 966 967 public DateTimeDataValue getNullTime(DateTimeDataValue dataValue) 968 { 969 if (dataValue == null) 970 { 971 try 972 { 973 return getDataValue((Time ) null); 974 } 975 catch( StandardException se) 976 { 977 if( SanityManager.DEBUG) 978 { 979 SanityManager.THROWASSERT( "Could not get a null time.", se); 980 } 981 return null; 982 } 983 } 984 else 985 { 986 dataValue.setToNull(); 987 return dataValue; 988 } 989 } 990 991 public DateTimeDataValue getNullTimestamp(DateTimeDataValue dataValue) 992 { 993 if (dataValue == null) 994 { 995 try 996 { 997 return getDataValue((Timestamp ) null); 998 } 999 catch( StandardException se) 1000 { 1001 if( SanityManager.DEBUG) 1002 { 1003 SanityManager.THROWASSERT( "Could not get a null timestamp.", se); 1004 } 1005 return null; 1006 } 1007 } 1008 else 1009 { 1010 dataValue.setToNull(); 1011 return dataValue; 1012 } 1013 } 1014 1015 public DateTimeDataValue getDateValue( String dateStr, boolean isJdbcEscape) throws StandardException 1016 { 1017 return new SQLDate( dateStr, isJdbcEscape, getLocaleFinder()); 1018 } 1020 public DateTimeDataValue getTimeValue( String timeStr, boolean isJdbcEscape) throws StandardException 1021 { 1022 return new SQLTime( timeStr, isJdbcEscape, getLocaleFinder()); 1023 } 1025 public DateTimeDataValue getTimestampValue( String timestampStr, boolean isJdbcEscape) throws StandardException 1026 { 1027 return new SQLTimestamp( timestampStr, isJdbcEscape, getLocaleFinder()); 1028 } 1030 1031 1037 public XMLDataValue getXMLDataValue() 1038 { 1039 return new XML(); 1040 } 1041 1042 1055 public XMLDataValue getXMLDataValue(XMLDataValue previous) 1056 throws StandardException 1057 { 1058 return getNullXML(previous); 1059 } 1060 1061 1072 public XMLDataValue getNullXML(XMLDataValue dataValue) 1073 { 1074 if (dataValue == null) 1075 return getXMLDataValue(); 1076 else { 1077 dataValue.setToNull(); 1078 return dataValue; 1079 } 1080 } 1081 1082 private LocaleFinder getLocaleFinder() 1087 { 1088 if (localeFinder == null) 1089 { 1090 DatabaseContext dc = (DatabaseContext) ContextService.getContext(DatabaseContext.CONTEXT_ID); 1091 if( dc != null) 1092 localeFinder = dc.getDatabase(); 1093 } 1094 1095 return localeFinder; 1096 } 1097} 1098 | Popular Tags |