View Javadoc
1   /*
2    *    Copyright 2016-2026 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.insert;
20  
21  import java.sql.JDBCType;
22  
23  import org.jspecify.annotations.Nullable;
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.insert.render.InsertStatementProvider;
28  import org.mybatis.dynamic.sql.render.RenderingStrategies;
29  
30  class InsertStatementTest {
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("first_name", JDBCType.VARCHAR);
34      private static final SqlColumn<String> lastName = foo.column("last_name", JDBCType.VARCHAR);
35      private static final SqlColumn<String> occupation = foo.column("occupation", JDBCType.VARCHAR);
36  
37      @Test
38      void testFullInsertStatementBuilder() {
39          TestRecord row = new TestRecord();
40          row.setLastName("jones");
41          row.setOccupation("dino driver");
42  
43          InsertStatementProvider<?> insertStatement = insert(row)
44                  .into(foo)
45                  .map(id).toProperty("id")
46                  .map(firstName).toProperty("firstName")
47                  .map(lastName).toProperty("lastName")
48                  .map(occupation).toProperty("occupation")
49                  .build()
50                  .render(RenderingStrategies.MYBATIS3);
51  
52          String expected = "insert into foo (id, first_name, last_name, occupation) "
53                  + "values (#{row.id,jdbcType=INTEGER}, "
54                  + "#{row.firstName,jdbcType=VARCHAR}, " + "#{row.lastName,jdbcType=VARCHAR}, "
55                  + "#{row.occupation,jdbcType=VARCHAR})";
56          assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
57      }
58  
59      @Test
60      void testSelectiveInsertStatementBuilder() {
61          TestRecord row = new TestRecord();
62          row.setLastName("jones");
63          row.setOccupation("dino driver");
64  
65          InsertStatementProvider<?> insertStatement = insert(row)
66                  .into(foo)
67                  .map(id).toPropertyWhenPresent("id", row::getId)
68                  .map(firstName).toPropertyWhenPresent("firstName", row::getFirstName)
69                  .map(lastName).toPropertyWhenPresent("lastName", row::getLastName)
70                  .map(occupation).toPropertyWhenPresent("occupation", row::getOccupation)
71                  .build()
72                  .render(RenderingStrategies.MYBATIS3);
73  
74          String expected = "insert into foo (last_name, occupation) "
75                  + "values (#{row.lastName,jdbcType=VARCHAR}, "
76                  + "#{row.occupation,jdbcType=VARCHAR})";
77          assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
78      }
79  
80      static class TestRecord {
81          private @Nullable Integer id;
82          private @Nullable String firstName;
83          private @Nullable String lastName;
84          private @Nullable String occupation;
85  
86          @Nullable Integer getId() {
87              return id;
88          }
89  
90          void setId(Integer id) {
91              this.id = id;
92          }
93  
94          @Nullable String getFirstName() {
95              return firstName;
96          }
97  
98          void setFirstName(String firstName) {
99              this.firstName = firstName;
100         }
101 
102         @Nullable String getLastName() {
103             return lastName;
104         }
105 
106         void setLastName(String lastName) {
107             this.lastName = lastName;
108         }
109 
110         @Nullable String getOccupation() {
111             return occupation;
112         }
113 
114         void setOccupation(String occupation) {
115             this.occupation = occupation;
116         }
117     }
118 }