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.mybatis3;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
20  import static org.mybatis.dynamic.sql.SqlBuilder.update;
21  
22  import java.sql.JDBCType;
23  
24  import org.junit.jupiter.api.Test;
25  import org.mybatis.dynamic.sql.SqlColumn;
26  import org.mybatis.dynamic.sql.SqlTable;
27  import org.mybatis.dynamic.sql.render.RenderingStrategies;
28  import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
29  
30  class UpdateStatementTest {
31      private static final SqlTable foo = SqlTable.of("foo");
32      private static final SqlColumn<Integer> id = foo.column("id", JDBCType.INTEGER);
33      private static final SqlColumn<String> firstName = foo.column("firstName", JDBCType.VARCHAR);
34      private static final SqlColumn<String> lastName = foo.column("lastName", JDBCType.VARCHAR);
35      private static final SqlColumn<String> occupation = foo.column("occupation", JDBCType.VARCHAR);
36  
37      @Test
38      void testUpdateParameter() {
39          UpdateStatementProvider updateStatement = update(foo)
40                  .set(firstName).equalTo("fred")
41                  .set(lastName).equalTo("jones")
42                  .set(occupation).equalToNull()
43                  .where(id, isEqualTo(3))
44                  .build()
45                  .render(RenderingStrategies.MYBATIS3);
46  
47          String expected = "update foo set firstName = #{parameters.p1,jdbcType=VARCHAR}, "
48                  + "lastName = #{parameters.p2,jdbcType=VARCHAR}, "
49                  + "occupation = null "
50                  + "where id = #{parameters.p3,jdbcType=INTEGER}";
51  
52          assertThat(updateStatement.getUpdateStatement()).isEqualTo(expected);
53          assertThat(updateStatement.getParameters()).hasSize(3);
54          assertThat(updateStatement.getParameters()).containsEntry("p1", "fred");
55          assertThat(updateStatement.getParameters()).containsEntry("p2", "jones");
56          assertThat(updateStatement.getParameters()).containsEntry("p3", 3);
57      }
58  
59      @Test
60      void testUpdateParameterStartWithNull() {
61          UpdateStatementProvider updateStatement = update(foo)
62                  .set(occupation).equalToNull()
63                  .set(firstName).equalTo("fred")
64                  .set(lastName).equalTo("jones")
65                  .where(id, isEqualTo(3))
66                  .and(firstName, isEqualTo("barney"))
67                  .build()
68                  .render(RenderingStrategies.MYBATIS3);
69  
70          String expectedSetClause = "update foo set occupation = null, "
71                  + "firstName = #{parameters.p1,jdbcType=VARCHAR}, "
72                  + "lastName = #{parameters.p2,jdbcType=VARCHAR} "
73                  + "where id = #{parameters.p3,jdbcType=INTEGER} "
74                  + "and firstName = #{parameters.p4,jdbcType=VARCHAR}";
75  
76          assertThat(updateStatement.getUpdateStatement()).isEqualTo(expectedSetClause);
77          assertThat(updateStatement.getParameters()).hasSize(4);
78          assertThat(updateStatement.getParameters()).containsEntry("p1", "fred");
79          assertThat(updateStatement.getParameters()).containsEntry("p2", "jones");
80          assertThat(updateStatement.getParameters()).containsEntry("p3", 3);
81          assertThat(updateStatement.getParameters()).containsEntry("p4", "barney");
82      }
83  }