|
20 | 20 | import java.util.Arrays;
|
21 | 21 | import java.util.Collections;
|
22 | 22 | import java.util.List;
|
| 23 | +import java.util.function.BooleanSupplier; |
| 24 | +import java.util.function.Consumer; |
23 | 25 |
|
24 | 26 | /**
|
25 | 27 | * @author Clinton Begin
|
@@ -526,6 +528,71 @@ public <A extends Appendable> A usingAppender(A a) {
|
526 | 528 | return a;
|
527 | 529 | }
|
528 | 530 |
|
| 531 | + /** |
| 532 | + * Apply sql phrases that provide by SQL consumer if condition is matches. |
| 533 | + * |
| 534 | + * @param applyCondition |
| 535 | + * if {@code true} apply sql phrases |
| 536 | + * @param sqlConsumer |
| 537 | + * a consumer that append sql phrase to SQL instance |
| 538 | + * |
| 539 | + * @return a self instance |
| 540 | + * |
| 541 | + * @see #applyIf(BooleanSupplier, Consumer) |
| 542 | + * |
| 543 | + * @since 3.5.14 |
| 544 | + */ |
| 545 | + public T applyIf(boolean applyCondition, Consumer<T> sqlConsumer) { |
| 546 | + T self = getSelf(); |
| 547 | + if (applyCondition) { |
| 548 | + sqlConsumer.accept(self); |
| 549 | + } |
| 550 | + return self; |
| 551 | + } |
| 552 | + |
| 553 | + /** |
| 554 | + * Apply sql phrases that provide by SQL consumer if condition is matches. |
| 555 | + * |
| 556 | + * @param applyConditionSupplier |
| 557 | + * if supplier return {@code true} apply sql phrases |
| 558 | + * @param sqlConsumer |
| 559 | + * a consumer that append sql phrase to SQL instance |
| 560 | + * |
| 561 | + * @return a self instance |
| 562 | + * |
| 563 | + * @see #applyIf(boolean, Consumer) |
| 564 | + * |
| 565 | + * @since 3.5.14 |
| 566 | + */ |
| 567 | + public T applyIf(BooleanSupplier applyConditionSupplier, Consumer<T> sqlConsumer) { |
| 568 | + return applyIf(applyConditionSupplier.getAsBoolean(), sqlConsumer); |
| 569 | + } |
| 570 | + |
| 571 | + /** |
| 572 | + * Apply sql phrases that provide by SQL consumer for iterable. |
| 573 | + * |
| 574 | + * @param iterable |
| 575 | + * an iterable |
| 576 | + * @param forEachSqlConsumer |
| 577 | + * a consumer that append sql phrase to SQL instance |
| 578 | + * |
| 579 | + * @return a self instance |
| 580 | + * |
| 581 | + * @param <E> |
| 582 | + * element type of iterable |
| 583 | + * |
| 584 | + * @since 3.5.14 |
| 585 | + */ |
| 586 | + public <E> T applyForEach(Iterable<E> iterable, ForEachConsumer<T, E> forEachSqlConsumer) { |
| 587 | + T self = getSelf(); |
| 588 | + int elementIndex = 0; |
| 589 | + for (E element : iterable) { |
| 590 | + forEachSqlConsumer.accept(self, element, elementIndex); |
| 591 | + elementIndex++; |
| 592 | + } |
| 593 | + return self; |
| 594 | + } |
| 595 | + |
529 | 596 | @Override
|
530 | 597 | public String toString() {
|
531 | 598 | StringBuilder sb = new StringBuilder();
|
@@ -737,4 +804,31 @@ public String sql(Appendable a) {
|
737 | 804 | return answer;
|
738 | 805 | }
|
739 | 806 | }
|
| 807 | + |
| 808 | + /** |
| 809 | + * Consumer for 'forEach' operation. |
| 810 | + * |
| 811 | + * @param <T> |
| 812 | + * SQL type |
| 813 | + * @param <E> |
| 814 | + * Element type of iterable |
| 815 | + * |
| 816 | + * @since 3.5.14 |
| 817 | + */ |
| 818 | + public interface ForEachConsumer<T, E> { |
| 819 | + |
| 820 | + /** |
| 821 | + * Accept an iterable element with index. |
| 822 | + * |
| 823 | + * @param sql |
| 824 | + * SQL instance |
| 825 | + * @param element |
| 826 | + * an iterable element |
| 827 | + * @param elementIndex |
| 828 | + * an element index |
| 829 | + */ |
| 830 | + void accept(T sql, E element, int elementIndex); |
| 831 | + |
| 832 | + } |
| 833 | + |
740 | 834 | }
|
0 commit comments