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