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;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20  import static org.mybatis.dynamic.sql.SqlBuilder.*;
21  
22  import java.sql.JDBCType;
23  import java.util.Optional;
24  
25  import org.junit.jupiter.api.Test;
26  import org.mybatis.dynamic.sql.SqlColumn;
27  import org.mybatis.dynamic.sql.SqlTable;
28  import org.mybatis.dynamic.sql.exception.NonRenderingWhereClauseException;
29  import org.mybatis.dynamic.sql.render.RenderingStrategies;
30  import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
31  
32  class WhereModelTest {
33  
34      @Test
35      void testThatParameterNameCarriesToSubCriteria() {
36          SqlTable table = SqlTable.of("foo");
37          SqlColumn<Integer> id = table.column("id", JDBCType.INTEGER);
38  
39          Optional<WhereClauseProvider> whereClause = where(id, isEqualTo(3), or(id, isEqualTo(4))).build()
40                  .render(RenderingStrategies.MYBATIS3, "myName");
41  
42          assertThat(whereClause.map(WhereClauseProvider::getWhereClause)).hasValueSatisfying(wc ->
43              assertThat(wc).isEqualTo("where id = #{myName.parameters.p1,jdbcType=INTEGER} or id = #{myName.parameters.p2,jdbcType=INTEGER}")
44          );
45      }
46  
47      @Test
48      void testNonRenderingWhereDisallowed() {
49          WhereModel model = where().build();
50  
51          assertThatExceptionOfType(NonRenderingWhereClauseException.class).isThrownBy(() ->
52                  model.render(RenderingStrategies.MYBATIS3)
53          );
54      }
55  
56      @Test
57      void testNonRenderingWhereAllowed() {
58          Optional<WhereClauseProvider> whereClause = new WhereDSL()
59                  .configureStatement(c -> c.setNonRenderingWhereClauseAllowed(true))
60                  .where()
61                  .build()
62                  .render(RenderingStrategies.MYBATIS3);
63  
64          assertThat(whereClause).isEmpty();
65      }
66  }