|
27 | 27 | import java.sql.Connection;
|
28 | 28 | import java.sql.DriverManager;
|
29 | 29 | import java.util.ArrayList;
|
| 30 | +import java.util.Arrays; |
30 | 31 | import java.util.Collection;
|
31 | 32 | import java.util.Collections;
|
32 | 33 | import java.util.List;
|
33 | 34 | import java.util.Map;
|
| 35 | +import java.util.Objects; |
34 | 36 |
|
35 | 37 | import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
|
36 | 38 | import org.apache.ibatis.exceptions.PersistenceException;
|
|
57 | 59 | import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
|
58 | 60 | import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
|
59 | 61 | import org.mybatis.dynamic.sql.where.condition.IsIn;
|
| 62 | +import org.mybatis.dynamic.sql.where.condition.IsNotIn; |
60 | 63 | import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
|
61 | 64 |
|
62 | 65 | class AnimalDataTest {
|
@@ -569,6 +572,47 @@ void testInCondition() {
|
569 | 572 | }
|
570 | 573 | }
|
571 | 574 |
|
| 575 | + @Test |
| 576 | + void testInConditionWithEventuallyEmptyList() { |
| 577 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 578 | + AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); |
| 579 | + |
| 580 | + SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight) |
| 581 | + .from(animalData) |
| 582 | + .where(id, isIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22))) |
| 583 | + .build() |
| 584 | + .render(RenderingStrategies.MYBATIS3); |
| 585 | + |
| 586 | + assertThat(selectStatement.getSelectStatement()) |
| 587 | + .isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData"); |
| 588 | + List<AnimalData> animals = mapper.selectMany(selectStatement); |
| 589 | + assertThat(animals).hasSize(65); |
| 590 | + } |
| 591 | + } |
| 592 | + |
| 593 | + @Test |
| 594 | + void testInConditionWithEventuallyEmptyListForceRendering() { |
| 595 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 596 | + AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); |
| 597 | + |
| 598 | + List<Integer> inValues = new ArrayList<>(); |
| 599 | + inValues.add(null); |
| 600 | + inValues.add(22); |
| 601 | + inValues.add(null); |
| 602 | + |
| 603 | + SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight) |
| 604 | + .from(animalData) |
| 605 | + .where(id, IsInRequired.isIn(inValues).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22))) |
| 606 | + .build() |
| 607 | + .render(RenderingStrategies.MYBATIS3); |
| 608 | + |
| 609 | + assertThat(selectStatement.getSelectStatement()) |
| 610 | + .isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id in ()"); |
| 611 | + |
| 612 | + assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement)); |
| 613 | + } |
| 614 | + } |
| 615 | + |
572 | 616 | @Test
|
573 | 617 | void testInConditionWithEmptyList() {
|
574 | 618 | try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
|
@@ -659,6 +703,54 @@ void testNotInCaseSensitiveConditionWithNull() {
|
659 | 703 | }
|
660 | 704 | }
|
661 | 705 |
|
| 706 | + @Test |
| 707 | + void testNotInConditionWithEventuallyEmptyList() { |
| 708 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 709 | + AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); |
| 710 | + |
| 711 | + SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight) |
| 712 | + .from(animalData) |
| 713 | + .where(id, isNotIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22))) |
| 714 | + .build() |
| 715 | + .render(RenderingStrategies.MYBATIS3); |
| 716 | + |
| 717 | + assertThat(selectStatement.getSelectStatement()) |
| 718 | + .isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData"); |
| 719 | + List<AnimalData> animals = mapper.selectMany(selectStatement); |
| 720 | + assertThat(animals).hasSize(65); |
| 721 | + } |
| 722 | + } |
| 723 | + |
| 724 | + @Test |
| 725 | + void testNotInConditionWithEventuallyEmptyListForceRendering() { |
| 726 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 727 | + AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class); |
| 728 | + |
| 729 | + SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight) |
| 730 | + .from(animalData) |
| 731 | + .where(id, IsNotInRequired.isNotIn(null, 22, null) |
| 732 | + .then(s -> s.filter(Objects::nonNull).filter(i -> i != 22))) |
| 733 | + .build() |
| 734 | + .render(RenderingStrategies.MYBATIS3); |
| 735 | + |
| 736 | + assertThat(selectStatement.getSelectStatement()) |
| 737 | + .isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id not in ()"); |
| 738 | + |
| 739 | + assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement)); |
| 740 | + } |
| 741 | + } |
| 742 | + |
| 743 | + public static class IsNotInRequired<T> extends IsNotIn<T> { |
| 744 | + protected IsNotInRequired(Collection<T> values) { |
| 745 | + super(values); |
| 746 | + forceRenderingWhenEmpty(); |
| 747 | + } |
| 748 | + |
| 749 | + @SafeVarargs |
| 750 | + public static <T> IsNotInRequired<T> isNotIn(T...values) { |
| 751 | + return new IsNotInRequired<>(Arrays.asList(values)); |
| 752 | + } |
| 753 | + } |
662 | 754 | @Test
|
663 | 755 | void testLikeCondition() {
|
664 | 756 | try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
|
|
0 commit comments