View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.where.render;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.assertj.core.api.Assertions.entry;
20  
21  import java.sql.JDBCType;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.junit.jupiter.api.Test;
26  import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
27  import org.mybatis.dynamic.sql.SqlColumn;
28  import org.mybatis.dynamic.sql.SqlTable;
29  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
30  import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
31  import org.mybatis.dynamic.sql.render.RenderingContext;
32  import org.mybatis.dynamic.sql.render.RenderingStrategies;
33  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
34  import org.mybatis.dynamic.sql.where.condition.IsEqualTo;
35  
36  class CriterionRendererTest {
37  
38      @Test
39      void testAliasWithIgnore() {
40          SqlTable table = SqlTable.of("foo");
41          SqlColumn<Integer> column = table.column("id", JDBCType.INTEGER);
42  
43          IsEqualTo<Integer> condition = IsEqualTo.of(3);
44          ColumnAndConditionCriterion<Integer> criterion = ColumnAndConditionCriterion.withColumn(column)
45                  .withCondition(condition)
46                  .build();
47  
48          RenderingContext renderingContext = RenderingContext
49                  .withRenderingStrategy(RenderingStrategies.MYBATIS3)
50                  .withStatementConfiguration(new StatementConfiguration())
51                  .build();
52  
53          CriterionRenderer renderer = new CriterionRenderer(renderingContext);
54  
55          assertThat(criterion.accept(renderer)).hasValueSatisfying(rc -> {
56              FragmentAndParameters fp = rc.fragmentAndParameters();
57              assertThat(fp.fragment()).isEqualTo("id = #{parameters.p1,jdbcType=INTEGER}");
58              assertThat(fp.parameters()).containsExactly(entry("p1", 3));
59          });
60      }
61  
62      @Test
63      void testAliasWithoutIgnore() {
64          SqlTable table = SqlTable.of("foo");
65          SqlColumn<Integer> column = table.column("id", JDBCType.INTEGER);
66          IsEqualTo<Integer> condition = IsEqualTo.of(3);
67          ColumnAndConditionCriterion<Integer> criterion = ColumnAndConditionCriterion.withColumn(column)
68                  .withCondition(condition)
69                  .build();
70  
71          Map<SqlTable, String> tableAliases = new HashMap<>();
72          tableAliases.put(table, "a");
73  
74          RenderingContext renderingContext = RenderingContext
75                  .withRenderingStrategy(RenderingStrategies.MYBATIS3)
76                  .withTableAliasCalculator(ExplicitTableAliasCalculator.of(tableAliases))
77                  .withStatementConfiguration(new StatementConfiguration())
78                  .build();
79  
80          CriterionRenderer renderer = new CriterionRenderer(renderingContext);
81  
82          assertThat(criterion.accept(renderer)).hasValueSatisfying(rc -> {
83              FragmentAndParameters fp = rc.fragmentAndParameters();
84              assertThat(fp.fragment()).isEqualTo("a.id = #{parameters.p1,jdbcType=INTEGER}");
85              assertThat(fp.parameters()).containsExactly(entry("p1", 3));
86          });
87      }
88  }