litesoft
@ 66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.orsup.selection; import java.util.*; import org.litesoft.orsup.selection.nonpublic.*; /** * A Factory to generate a representation of a SQL <i>WHERE</i> clause.<p> * <a href="../../Licence.txt">Licence</a><br> * <p/> * Using this Factory a complex SQL <i>WHERE</i> clause can be generated. * The concrete extensions of <b>WhereClause</b> generated by this Factory * can form a <i>tree</i> that represents any arbitrarily complex expression.<p> * <p/> * If you need to create/support a specific SQL <i>WHERE</i> clause that * is not provided, you would need to extend this class and probably one of * the abstract <b>WhereClause</b> helper classes listed in <b>WhereClause</b>.<p> * <p/> * See <a href="WhereClause.html">WhereClause</a><p> * <p/> * Exceptions: All problems caught when the parameter(s) are checked (as * indicated/implied in the @param tags) will generate an IllegalArgumentException, * and means the API user has a problem. If a NullPointerException (or some * others, like: ClassCastException or ArrayIndexOutOfBoundsException) is thrown, * it means the API developer has a problem. Any Exception that is explicitly * thrown in the API, but unrelated to a parameter, will be listed in the throws * clause (and hopefully in the tag @throws). These may (but probably won't) be * checked Exceptions. * * @author George Smith * @version 1.0 10/07/01 */ public class WhereClauseFactory { public static WhereClauseFactory INSTANCE = new WhereClauseFactory(); /** * Return an <b>OR</b> SQL <i>WHERE</i> clause of two other <i>WHERE</i> clauses.<p> * <p/> * Note: If either (or both) of the two <b>WhereClause</b>s are them selves * <i>OR</i> <b>WhereClause</b>s then, the <b>WhereClause</b>(s) are * <i>merged</i> (collapsed to this new one).<p> * * @param pWhereClause1 First of the two <i>WHERE</i> clauses (!null). * @param pWhereClause2 Second of the two <i>WHERE</i> clauses (!null).<p> * * @return <b>OR</b> merged <b>WhereClause</b>s. */ public WhereClause or( WhereClause pWhereClause1, WhereClause pWhereClause2, WhereClause... pWhereClauseMore ) { WhereClause wc = WC_OR.create( pWhereClause1, pWhereClause2 ); if ( (pWhereClauseMore != null) && (pWhereClauseMore.length != 0) ) { for ( WhereClause clause : pWhereClauseMore ) { wc = WC_OR.create( wc, clause ); } } return wc; } /** * Return an <b>AND</b> SQL <i>WHERE</i> clause of two other <i>WHERE</i> clauses.<p> * <p/> * Note: If either (or both) of the two <b>WhereClause</b>s are them selves * <i>AND</i> <b>WhereClause</b>s then, the <b>WhereClause</b>(s) are * <i>merged</i> (collapsed to this new one).<p> * * @param pWhereClause1 First of the two <i>WHERE</i> clauses (!null). * @param pWhereClause2 Second of the two <i>WHERE</i> clauses (!null).<p> * * @return <b>AND</b> merged <b>WhereClause</b>s. */ public WhereClause and( WhereClause pWhereClause1, WhereClause pWhereClause2, WhereClause... pWhereClauseMore ) { WhereClause wc = WC_AND.create( pWhereClause1, pWhereClause2 ); if ( (pWhereClauseMore != null) && (pWhereClauseMore.length != 0) ) { for ( WhereClause clause : pWhereClauseMore ) { wc = WC_AND.create( wc, clause ); } } return wc; } /** * Return a <b>NOT</b><i>ed</i> SQL <i>WHERE</i> clause.<p> * * @param pWhereClause The WhereClause that is to be <b>NOT</b><i>ed</i> (!null).<p> * * @return <b>NOT</b><i>ed</i> WhereClause. */ public WhereClause not( WhereClause pWhereClause ) { if ( pWhereClause instanceof WhereClauseNotable ) { ((WhereClauseNotable) pWhereClause).switchNot(); return pWhereClause; } if ( pWhereClause == WC_TRUE.INSTANCE ) { return WC_FALSE.INSTANCE; } if ( pWhereClause == WC_FALSE.INSTANCE ) { return WC_TRUE.INSTANCE; } return new WC_NOT( pWhereClause ); } public WhereClause alwaysTrue() { return WC_TRUE.INSTANCE; } public WhereClause alwaysFalse() { return WC_FALSE.INSTANCE; } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>NULL</i>.<p> * * @param pColumnDefinition Column Definition (!null).<p> * * @return NULL WhereClause. */ public WhereClause isNull( SimpleColumnDefinition pColumnDefinition ) { return new WC_IS_NULL( pColumnDefinition ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>NOT NULL</i>.<p> * * @param pColumnDefinition Column Definition (!null).<p> * * @return NOT NULL WhereClause.<p> * * @see #not(WhereClause) * @see #isNull(SimpleColumnDefinition) */ public WhereClause isNotNull( SimpleColumnDefinition pColumnDefinition ) { return not( isNull( pColumnDefinition ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>equal to</i> any of the collection's value(s).<p> * <p/> * Note: Due to the pecular nature of Java's <i>null</i> and the SQL NULL, * the resulting SQL <i>WHERE</i> clause may include "IS NULL".<p> * * @param pColumnDefinition Column Definition (!null). * @param pValues Compare with values (at least one value).<p> * * @return Any Of WhereClause.<p> * * @see #isNull(SimpleColumnDefinition) * @see #isEqual(SimpleColumnDefinition,Object) */ public WhereClause isAnyFrom( SimpleColumnDefinition pColumnDefinition, Collection<?> pValues ) { return LLisAnyFrom( pColumnDefinition, (pValues != null) ? pValues : Collections.EMPTY_LIST ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>NOT equal to</i> any of the collection's value(s).<p> * <p/> * Note: Due to the pecular nature of Java's <i>null</i> and the SQL NULL, * the resulting SQL <i>WHERE</i> clause may include "IS NOT NULL" or * will <i>or</i> in an "IS NULL".<p> * * @param pColumnDefinition Column Definition (!null). * @param pValues Compare with values (at least one value).<p> * * @return NOT Any Of WhereClause.<p> * * @see #not(WhereClause) * @see #isEqual(SimpleColumnDefinition,Object) * @see #isNotNull(SimpleColumnDefinition) * @see #isNull(SimpleColumnDefinition) * @see #or(WhereClause,WhereClause,WhereClause[]) */ public WhereClause isNotAnyFrom( SimpleColumnDefinition pColumnDefinition, Collection<Object> pValues ) { return not( isAnyFrom( pColumnDefinition, pValues ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>equal to</i> any of the Object value(s).<p> * <p/> * Note: Due to the pecular nature of Java's <i>null</i> and the SQL NULL, * the resulting SQL <i>WHERE</i> clause may include "IS NULL".<p> * * @param pColumnDefinition Column Definition (!null). * @param pValues Compare with values (at least one value).<p> * * @return Any Of WhereClause.<p> * * @see #isNull(SimpleColumnDefinition) * @see #isEqual(SimpleColumnDefinition,Object) */ public WhereClause isAnyOf( SimpleColumnDefinition pColumnDefinition, Object... pValues ) { return LLisAnyFrom( pColumnDefinition, // (pValues == null) || (pValues.length == 0) ? // Collections.EMPTY_LIST : // Arrays.asList( pValues ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>NOT equal to</i> any of the Object value(s).<p> * <p/> * Note: Due to the pecular nature of Java's <i>null</i> and the SQL NULL, * the resulting SQL <i>WHERE</i> clause may include "IS NOT NULL" or * will <i>or</i> in an "IS NULL".<p> * * @param pColumnDefinition Column Definition (!null). * @param pValues Compare with values (at least one value).<p> * * @return NOT Any Of WhereClause.<p> * * @see #not(WhereClause) * @see #isEqual(SimpleColumnDefinition,Object) * @see #isNotNull(SimpleColumnDefinition) * @see #isNull(SimpleColumnDefinition) * @see #or(WhereClause,WhereClause,WhereClause[]) */ public WhereClause isNotAnyOf( SimpleColumnDefinition pColumnDefinition, Object... pValues ) { return not( isAnyOf( pColumnDefinition, pValues ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>equal to</i> an Object value.<p> * <p/> * Note: Due to the pecular nature of Java's <i>null</i> and the SQL NULL, * the resulting SQL <i>WHERE</i> clause may become "IS NULL".<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value.<p> * * @return equal to WhereClause.<p> * * @see #isNull(SimpleColumnDefinition) */ public WhereClause isEqual( SimpleColumnDefinition pColumnDefinition, Object pValue ) { if ( pValue == null ) { return isNull( pColumnDefinition ); } return new WC_EQUALS( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>equal to</i> an <b>int</b> value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return equal to WhereClause.<p> * * @see #isEqual(SimpleColumnDefinition,Object) */ public WhereClause isEqual( SimpleColumnDefinition pColumnDefinition, int pInt ) { return isEqual( pColumnDefinition, new Integer( pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>NOT equal to</i> an Object value.<p> * <p/> * Note: Due to the pecular nature of Java's <i>null</i> and the SQL NULL, * the resulting SQL <i>WHERE</i> clause will either be "IS NOT NULL" or * will <i>or</i> in an "IS NULL".<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value.<p> * * @return NOT equal to WhereClause.<p> * * @see #not(WhereClause) * @see #isEqual(SimpleColumnDefinition,Object) * @see #isNotNull(SimpleColumnDefinition) * @see #isNull(SimpleColumnDefinition) * @see #or(WhereClause,WhereClause,WhereClause[]) */ public WhereClause isNotEqual( SimpleColumnDefinition pColumnDefinition, Object pValue ) { return not( isEqual( pColumnDefinition, pValue ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>NOT equal to</i> an <b>int</b> value.<p> * <p/> * Note: Due to the pecular nature of SQL NULLs, the resulting SQL * <i>WHERE</i> clause will <i>or</i> in an "IS NULL".<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return NOT equal to WhereClause.<p> * * @see #not(WhereClause) * @see #isEqual(SimpleColumnDefinition,int) * @see #isNull(SimpleColumnDefinition) * @see #or(WhereClause,WhereClause,WhereClause[]) */ public WhereClause isNotEqual( SimpleColumnDefinition pColumnDefinition, int pInt ) { return not( isEqual( pColumnDefinition, pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>less than or equal to</i> an Object value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value (!null).<p> * * @return less than WhereClause. */ public WhereClause isLessThanEqual( SimpleColumnDefinition pColumnDefinition, Object pValue ) { return isNotGreaterThan( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>less than or equal to</i> an <b>int</b> value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return less than to WhereClause.<p> * * @see #isLessThan(SimpleColumnDefinition,Object) */ public WhereClause isLessThanEqual( SimpleColumnDefinition pColumnDefinition, int pInt ) { return isLessThanEqual( pColumnDefinition, new Integer( pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>less than</i> an Object value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value (!null).<p> * * @return less than WhereClause. */ public WhereClause isLessThan( SimpleColumnDefinition pColumnDefinition, Object pValue ) { return new WC_LESSTHAN( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>less than</i> an <b>int</b> value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return less than to WhereClause.<p> * * @see #isLessThan(SimpleColumnDefinition,Object) */ public WhereClause isLessThan( SimpleColumnDefinition pColumnDefinition, int pInt ) { return isLessThan( pColumnDefinition, new Integer( pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>greater than or equal to</i> an Object value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value (!null).<p> * * @return greater than or equal to WhereClause.<p> * * @see #not(WhereClause) * @see #isLessThan(SimpleColumnDefinition,Object) */ public WhereClause isNotLessThan( SimpleColumnDefinition pColumnDefinition, Object pValue ) { return not( isLessThan( pColumnDefinition, pValue ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>greater than or equal to</i> an <b>int</b> value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return greater than or equal to WhereClause.<p> * * @see #not(WhereClause) * @see #isLessThan(SimpleColumnDefinition,int) */ public WhereClause isNotLessThan( SimpleColumnDefinition pColumnDefinition, int pInt ) { return not( isLessThan( pColumnDefinition, pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>greater than or equal to</i> an Object value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value (!null).<p> * * @return greater than to WhereClause. */ public WhereClause isGreaterThanEqual( SimpleColumnDefinition pColumnDefinition, Object pValue ) { return isNotLessThan( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>greater than or equal to</i> an <b>int</b> value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return greater than to WhereClause.<p> * * @see #isGreaterThan(SimpleColumnDefinition,Object) */ public WhereClause isGreaterThanEqual( SimpleColumnDefinition pColumnDefinition, int pInt ) { return isGreaterThanEqual( pColumnDefinition, new Integer( pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>greater than</i> an Object value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value (!null).<p> * * @return greater than to WhereClause. */ public WhereClause isGreaterThan( SimpleColumnDefinition pColumnDefinition, Object pValue ) { return new WC_GREATERTHAN( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>greater than</i> an <b>int</b> value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return greater than to WhereClause.<p> * * @see #isGreaterThan(SimpleColumnDefinition,Object) */ public WhereClause isGreaterThan( SimpleColumnDefinition pColumnDefinition, int pInt ) { return isGreaterThan( pColumnDefinition, new Integer( pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>less than or equal to</i> an Object value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue Compare with value (!null).<p> * * @return less than or equal to WhereClause.<p> * * @see #not(WhereClause) * @see #isGreaterThan(SimpleColumnDefinition,Object) */ public WhereClause isNotGreaterThan( SimpleColumnDefinition pColumnDefinition, Object pValue ) { return not( isGreaterThan( pColumnDefinition, pValue ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>less than or equal to</i> an <b>int</b> value.<p> * * @param pColumnDefinition Column Definition (!null). * @param pInt Compare with int.<p> * * @return less than or equal to WhereClause.<p> * * @see #not(WhereClause) * @see #isGreaterThan(SimpleColumnDefinition,int) */ public WhereClause isNotGreaterThan( SimpleColumnDefinition pColumnDefinition, int pInt ) { return not( isGreaterThan( pColumnDefinition, pInt ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>between</i> (INCLUSIVE) two (Object) values.<p> * <p/> * Note: if the <i>left</i> and <i>right</i> values are equal, then the * WhereClause is of type isEqual.<p> * * @param pColumnDefinition Column Definition (!null). * @param pGEvalue Greater Than or Equal (left) value (!null). * @param pLEvalue Less Than or Equal (right) value (!null).<p> * * @return <b>BETWEEN</b> (or equal) WhereClause.<p> * * @see #isEqual(SimpleColumnDefinition,Object) */ public WhereClause isBetween( SimpleColumnDefinition pColumnDefinition, Object pGEvalue, Object pLEvalue ) { if ( (pGEvalue == pLEvalue) || ((pGEvalue != null) && pGEvalue.equals( pLEvalue )) ) { return isEqual( pColumnDefinition, pGEvalue ); } return new WC_BETWEEN( pColumnDefinition, pGEvalue, pLEvalue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is * <i>between</i> (INCLUSIVE) two <b>int</b> values.<p> * <p/> * Note: if the <i>left</i> and <i>right</i> ints are equal, then the * WhereClause is of type isEqual.<p> * * @param pColumnDefinition Column Definition (!null). * @param pGEint Greater Than or Equal (left) value. * @param pLEint Less Than or Equal (right) value.<p> * * @return <b>BETWEEN</b> (or equal) WhereClause.<p> * * @see #isBetween(SimpleColumnDefinition,Object,Object) * @see #isEqual(SimpleColumnDefinition,int) */ public WhereClause isBetween( SimpleColumnDefinition pColumnDefinition, int pGEint, int pLEint ) { if ( pGEint == pLEint ) { return isEqual( pColumnDefinition, pGEint ); } return isBetween( pColumnDefinition, new Integer( pGEint ), new Integer( pLEint ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is Not * <i>between</i> (INCLUSIVE) two (Object) values.<p> * <p/> * Note: if the <i>left</i> and <i>right</i> values are equal, then the * WhereClause is of type isNotEqual.<p> * * @param pColumnDefinition Column Definition (!null). * @param pGEvalue Greater Than or Equal (left) value (!null). * @param pLEvalue Less Than or Equal (right) value (!null).<p> * * @return <b>NOT BETWEEN</b> (or not equal) WhereClause.<p> * * @see #not(WhereClause) * @see #isBetween(SimpleColumnDefinition,Object,Object) * @see #isNotEqual(SimpleColumnDefinition,Object) */ public WhereClause isNotBetween( SimpleColumnDefinition pColumnDefinition, Object pGEvalue, Object pLEvalue ) { return not( isBetween( pColumnDefinition, pGEvalue, pLEvalue ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a column is Not * <i>between</i> (INCLUSIVE) two <b>int</b> values.<p> * <p/> * Note: if the <i>left</i> and <i>right</i> ints are equal, then the * WhereClause is of type isNotEqual.<p> * * @param pColumnDefinition Column Definition (!null). * @param pGEint Greater Than or Equal (left) value. * @param pLEint Less Than or Equal (right) value.<p> * * @return <b>NOT BETWEEN</b> (or not equal) WhereClause.<p> * * @see #not(WhereClause) * @see #isBetween(SimpleColumnDefinition,int,int) * @see #isNotEqual(SimpleColumnDefinition,int) */ public WhereClause isNotBetween( SimpleColumnDefinition pColumnDefinition, int pGEint, int pLEint ) { return not( isBetween( pColumnDefinition, pGEint, pLEint ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * <i>contains</i> a particular set of characters.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue String that carries the particular set of characters for checking (!null).<p> * * @return <b>LIKE</b> (any where) WhereClause. */ public WhereClause contains( SimpleColumnDefinition pColumnDefinition, String pValue ) { return new WC_CONTAINS( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * does Not <i>contain</i> a particular set of characters.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue String that carries the particular set of characters for checking (!null).<p> * * @return <b>NOT LIKE</b> (any where) WhereClause.<p> * * @see #not(WhereClause) * @see #contains(SimpleColumnDefinition,String) */ public WhereClause doesNotContain( SimpleColumnDefinition pColumnDefinition, String pValue ) { return not( contains( pColumnDefinition, pValue ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * <i>Starts With</i> a particular set of characters.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue String that carries the particular set of characters for checking (!null).<p> * * @return <b>LIKE</b> (on front) WhereClause. */ public WhereClause startsWith( SimpleColumnDefinition pColumnDefinition, String pValue ) { return new WC_STARTS_WITH( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * does Not <i>Start With</i> a particular set of characters.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue String that carries the particular set of characters for checking (!null).<p> * * @return <b>NOT LIKE</b> (on front) WhereClause.<p> * * @see #not(WhereClause) * @see #startsWith(SimpleColumnDefinition,String) */ public WhereClause doesNotStartWith( SimpleColumnDefinition pColumnDefinition, String pValue ) { return not( startsWith( pColumnDefinition, pValue ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * <i>Ends With</i> a particular set of characters.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue String that carries the particular set of characters for checking (!null).<p> * * @return <b>LIKE</b> (on end) WhereClause. */ public WhereClause endsWith( SimpleColumnDefinition pColumnDefinition, String pValue ) { return new WC_ENDS_WITH( pColumnDefinition, pValue ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * does Not <i>End With</i> a particular set of characters.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValue String that carries the particular set of characters for checking (!null).<p> * * @return <b>NOT LIKE</b> (on end) WhereClause.<p> * * @see #not(WhereClause) * @see #endsWith(SimpleColumnDefinition,String) */ public WhereClause doesNotEndWith( SimpleColumnDefinition pColumnDefinition, String pValue ) { return not( endsWith( pColumnDefinition, pValue ) ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * <i>startsWith</i>, <i>contains</i>..., and <i>endsWith</i> a particular set of character strings.<p> * * @param pColumnDefinition Column Definition (!null). * @param pValues Strings that carries the particular set of character strings for checking (!null & !0 length).<p> * * @return <b>LIKE</b> (sequences) WhereClause. */ public WhereClause like( SimpleColumnDefinition pColumnDefinition, String... pValues ) { if ( (pValues == null) || (pValues.length == 0) ) { throw IllegalArgument.exception( "Values", "NOT at least 1 String" ); } if ( pValues.length > 2 ) { pValues = removeMiddleEmpties( pValues ); } switch ( pValues.length ) { case 0: // Not Possible - Guard Condition above case 1: return isEqual( pColumnDefinition, pValues[0] ); case 2: if ( isEmpty( pValues[0] ) ) { if ( isEmpty( pValues[1] ) ) { return alwaysTrue(); } return endsWith( pColumnDefinition, pValues[1] ); } if ( isEmpty( pValues[1] ) ) { return startsWith( pColumnDefinition, pValues[0] ); } break; case 3: if ( isEmpty( pValues[0] ) && isEmpty( pValues[2] ) ) { return contains( pColumnDefinition, pValues[1] ); } break; default: break; } return new WC_LIKE( pColumnDefinition, pValues ); } private String[] removeMiddleEmpties( String[] pValues ) { int i = pValues.length - 1; while ( --i > 0 ) { if ( isEmpty( pValues[i] ) ) { break; } } if ( i == 0 ) // No Middle Empties { return pValues; } List<String> zParts = new ArrayList<String>( Arrays.asList( pValues ) ); // i is Last empty zParts.remove( i ); while ( --i > 0 ) { if ( isEmpty( pValues[i] ) ) // Working from the back, both pValues & zParts remain in sync!! { zParts.remove( i ); } } return zParts.toArray( new String[zParts.size()] ); } /** * Create a SQL <i>WHERE</i> clause that checks that a String column * does Not <i>startsWith</i>, <i>contains</i>..., and <i>endsWith</i> a particular set of character strings. * Note <p> * * @param pColumnDefinition Column Definition (!null). * @param pValues Strings that carries the particular set of character strings for checking (!null & !0 length).<p> * * @return <b>NOT LIKE</b> (sequences) WhereClause.<p> * * @see #not(WhereClause) * @see #like(SimpleColumnDefinition,String[]) */ public WhereClause isNotLike( SimpleColumnDefinition pColumnDefinition, String... pValues ) { return not( like( pColumnDefinition, pValues ) ); } /** * Create a SQL <b>IN</b> <i>WHERE</i> clause.<p> * <p/> * Note: the Column Types of the ColumnDefinition and selected column of * the SingleColumnSelect MUST be equal, otherwise an IllegalArgumentException * is thrown.<p> * * @param pColumnDefinition Column Definition (!null). * @param pSingleColumnSelect A single column mini-SELECT command (!null).<p> * * @return <b>IN</b> WhereClause. */ public WhereClause isIn( SimpleColumnDefinition pColumnDefinition, SingleColumnSelect pSingleColumnSelect ) { return new WC_IS_IN( pColumnDefinition, pSingleColumnSelect ); } /** * Create a SQL <b>NOT IN</b> <i>WHERE</i> clause.<p> * <p/> * Note: the Column Types of the ColumnDefinition and selected column of * the SingleColumnSelect MUST be equal, otherwise an IllegalArgumentException * is thrown.<p> * * @param pColumnDefinition Column Definition (!null). * @param pSingleColumnSelect A single column mini-SELECT command (!null).<p> * * @return <b>NOT IN</b> WhereClause.<p> * * @see #not(WhereClause) * @see #isIn(SimpleColumnDefinition,SingleColumnSelect) */ public WhereClause isNotIn( SimpleColumnDefinition pColumnDefinition, SingleColumnSelect pSingleColumnSelect ) { return not( isIn( pColumnDefinition, pSingleColumnSelect ) ); } private boolean isEmpty( String pPart ) { return (pPart == null) || (pPart.length() == 0); } private WhereClause LLisAnyFrom( SimpleColumnDefinition pColumnDefinition, Collection<?> pValues ) { Set<Object> zSet = new LinkedHashSet<Object>( pValues ); boolean hasNull = false; if ( zSet.contains( null ) ) { hasNull = true; zSet.remove( null ); } if ( zSet.isEmpty() ) // All Nulls { return hasNull ? isNull( pColumnDefinition ) : alwaysFalse(); } Object[] zValues = zSet.toArray(); WhereClause wc = (zValues.length == 1) ? // isEqual( pColumnDefinition, zValues[0] ) : // new WC_IS_ANY_OF( pColumnDefinition, zValues ); return hasNull ? or( wc, isNull( pColumnDefinition ) ) : wc; } private static void verifyNotNull( WhereClause pWhereClause1, WhereClause pWhereClause2 ) { verifyNotNull( pWhereClause1 ); verifyNotNull( pWhereClause2 ); } private static void verifyNotNull( WhereClause pWhereClause ) { if ( pWhereClause == null ) { throw new NullPointerException(); } } // static inner classes to "leverage" the automatic namespacing... private static class WC_OR extends AbstractWhereClauseAssociativeList { static WhereClause create( WhereClause pWhereClause1, WhereClause pWhereClause2 ) { verifyNotNull( pWhereClause1, pWhereClause2 ); if ( pWhereClause1 == WC_TRUE.INSTANCE ) { return pWhereClause1; } if ( pWhereClause2 == WC_TRUE.INSTANCE ) { return pWhereClause2; } return (pWhereClause1 == WC_FALSE.INSTANCE) ? pWhereClause2 : // (pWhereClause2 == WC_FALSE.INSTANCE) ? pWhereClause1 : // new WC_OR( pWhereClause1, pWhereClause2 ); } private WC_OR( WhereClause pWhereClause1, WhereClause pWhereClause2 ) { super( WhereClauseType.OR, pWhereClause1, pWhereClause2 ); } } private static class WC_AND extends AbstractWhereClauseAssociativeList { static WhereClause create( WhereClause pWhereClause1, WhereClause pWhereClause2 ) { verifyNotNull( pWhereClause1, pWhereClause2 ); if ( pWhereClause1 == WC_FALSE.INSTANCE ) { return pWhereClause1; } if ( pWhereClause2 == WC_FALSE.INSTANCE ) { return pWhereClause2; } return (pWhereClause1 == WC_TRUE.INSTANCE) ? pWhereClause2 : // (pWhereClause2 == WC_TRUE.INSTANCE) ? pWhereClause1 : // new WC_AND( pWhereClause1, pWhereClause2 ); } private WC_AND( WhereClause pWhereClause1, WhereClause pWhereClause2 ) { super( WhereClauseType.AND, pWhereClause1, pWhereClause2 ); } } private static class WC_NOT extends AbstractWhereClauseWrapper { WC_NOT( WhereClause pWrappedWhereClause ) { super( WhereClauseType.NOT, pWrappedWhereClause ); } } private static class WC_IS_NULL extends AbstractWhereClauseColumnAndTypeTo { WC_IS_NULL( SimpleColumnDefinition pColumnDefinition ) { super( WhereClauseType.IS_NULL, pColumnDefinition ); } } private static class WC_EQUALS extends AbstractWhereClauseColumnAndValue { WC_EQUALS( SimpleColumnDefinition pColumnDefinition, Object pValue ) { super( WhereClauseType.EQUALS, pColumnDefinition, pValue ); } @Override protected void LLtoSqlHelper( StringBuilder pSB ) { if ( !isNot() ) // Regular { super.LLtoSqlHelper( pSB ); } else { pSB.append( '(' ); super.LLtoSqlHelper( pSB ); pSB.append( ") OR (" ); toSqlColumnReference( pSB ); pSB.append( " IS NULL)" ); } } } private static class WC_LESSTHAN extends AbstractWhereClauseColumnAndValue { WC_LESSTHAN( SimpleColumnDefinition pColumnDefinition, Object pValue ) { super( WhereClauseType.LESSTHAN, pColumnDefinition, pValue ); } } private static class WC_GREATERTHAN extends AbstractWhereClauseColumnAndValue { WC_GREATERTHAN( SimpleColumnDefinition pColumnDefinition, Object pValue ) { super( WhereClauseType.GREATERTHAN, pColumnDefinition, pValue ); } } private static class WC_BETWEEN extends AbstractWhereClauseColumnAndTwoValues { WC_BETWEEN( SimpleColumnDefinition pColumnDefinition, Object pLeftValue, Object pRightValue ) { super( WhereClauseType.BETWEEN, pColumnDefinition, pLeftValue, pRightValue ); } @Override protected void toStringHelper( StringBuilder pSB ) { if ( !isNot() ) { cuteStringHelper( pSB ); } else { pSB.append( "NOT (" ); cuteStringHelper( pSB ); pSB.append( ')' ); } } private void cuteStringHelper( StringBuilder pSB ) { WhereClauseColumnSupport.makeStringValue( pSB, getColumnDefinition(), getLeftValue() ); pSB.append( " <= " ); toStringColumnReference( pSB ); pSB.append( " <= " ); WhereClauseColumnSupport.makeStringValue( pSB, getColumnDefinition(), getRightValue() ); } @Override protected final void toSqlHelper( WCtoSqlHelper pWCtoSqlHelper, StringBuilder pSB ) { if ( !pWCtoSqlHelper.preRender( this, pSB ) ) { toSqlColumnReference( pSB ); pSB.append( ' ' ); pSB.append( isNot() ? getType().getToNotSql() : getType().getToSql() ); pSB.append( ' ' ); WhereClauseColumnSupport.makeSqlValue( pSB, getColumnDefinition(), true, getLeftValue() ); pSB.append( " AND " ); WhereClauseColumnSupport.makeSqlValue( pSB, getColumnDefinition(), true, getRightValue() ); pWCtoSqlHelper.postRender( this, pSB ); } } } private static class WC_IS_ANY_OF extends AbstractWhereClauseColumnAnd_N_Values { WC_IS_ANY_OF( SimpleColumnDefinition pColumnDefinition, Object... pValues ) { super( WhereClauseType.IS_ANY_OF, pColumnDefinition, pValues ); } } private static class WC_LIKE extends AbstractWhereClauseColumnAndLikeValues { WC_LIKE( SimpleColumnDefinition pColumnDefinition, String[] pValues ) { super( WhereClauseType.LIKE, pColumnDefinition, pValues ); } } private static class WC_CONTAINS extends AbstractWhereClauseColumnAndLikeValue { WC_CONTAINS( SimpleColumnDefinition pColumnDefinition, String pValue ) { super( WhereClauseType.CONTAINS, pColumnDefinition, pValue ); } @Override protected String[] getSqlLikeValues() { return new String[]{"", getValue(), ""}; } } private static class WC_STARTS_WITH extends AbstractWhereClauseColumnAndLikeValue { WC_STARTS_WITH( SimpleColumnDefinition pColumnDefinition, String pValue ) { super( WhereClauseType.STARTS_WITH, pColumnDefinition, pValue ); } @Override protected String[] getSqlLikeValues() { return new String[]{getValue(), ""}; } } private static class WC_ENDS_WITH extends AbstractWhereClauseColumnAndLikeValue { WC_ENDS_WITH( SimpleColumnDefinition pColumnDefinition, String pValue ) { super( WhereClauseType.ENDS_WITH, pColumnDefinition, pValue ); } @Override protected String[] getSqlLikeValues() { return new String[]{"", getValue()}; } } private static class WC_IS_IN extends AbstractWhereClauseColumnIsIn { WC_IS_IN( SimpleColumnDefinition pColumnDefinition, SingleColumnSelect pSingleColumnSelect ) { super( WhereClauseType.IS_IN, pColumnDefinition, pSingleColumnSelect ); } } private static class WC_TRUE extends AbstractWhereClauseTF { public static final WhereClause INSTANCE = new WC_TRUE(); private WC_TRUE() { super( WhereClauseType.TRUE ); } } private static class WC_FALSE extends AbstractWhereClauseTF { public static final WhereClause INSTANCE = new WC_FALSE(); private WC_FALSE() { super( WhereClauseType.FALSE ); } } } |