1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.dynamic.sql.insert;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.mybatis.dynamic.sql.SqlBuilder.insertInto;
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.GeneralInsertStatementProvider;
27 import org.mybatis.dynamic.sql.render.RenderingStrategies;
28
29 class GeneralInsertStatementTest {
30
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
40 GeneralInsertStatementProvider insertStatement = insertInto(foo)
41 .set(id).toValue(2)
42 .set(firstName).toValue("Jones")
43 .set(occupation).toValue("dino driver")
44 .build()
45 .render(RenderingStrategies.MYBATIS3);
46
47 String expectedStatement = "insert into foo "
48 + "(id, first_name, occupation) "
49 + "values (#{parameters.p1,jdbcType=INTEGER}, #{parameters.p2,jdbcType=VARCHAR}, #{parameters.p3,jdbcType=VARCHAR})";
50
51 assertThat(insertStatement.getInsertStatement()).isEqualTo(expectedStatement);
52 }
53
54 @Test
55 void testInsertStatementBuilderWithNulls() {
56
57 GeneralInsertStatementProvider insertStatement = insertInto(foo)
58 .set(id).toValue(1)
59 .set(firstName).toValue("Fred")
60 .set(lastName).toValue("Smith")
61 .set(occupation).toNull()
62 .build()
63 .render(RenderingStrategies.MYBATIS3);
64
65 String expected = "insert into foo (id, first_name, last_name, occupation) "
66 + "values (#{parameters.p1,jdbcType=INTEGER}, #{parameters.p2,jdbcType=VARCHAR}, #{parameters.p3,jdbcType=VARCHAR}, null)";
67 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
68 }
69
70 @Test
71 void testInsertStatementBuilderWithConstants() {
72
73 GeneralInsertStatementProvider insertStatement = insertInto(foo)
74 .set(id).toConstant("3")
75 .set(firstName).toValue("Fred")
76 .set(lastName).toValue("Jones")
77 .set(occupation).toStringConstant("Y")
78 .build()
79 .render(RenderingStrategies.MYBATIS3);
80
81 String expected = "insert into foo (id, first_name, last_name, occupation) "
82 + "values (3, #{parameters.p1,jdbcType=VARCHAR}, #{parameters.p2,jdbcType=VARCHAR}, 'Y')";
83 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
84 }
85
86 @Test
87 void testSelectiveInsertStatementBuilder() {
88 String myLastName = "jones";
89 String myOccupation = "dino driver";
90
91 GeneralInsertStatementProvider insertStatement = insertInto(foo)
92 .set(id).toValueWhenPresent(() -> null)
93 .set(firstName).toValueWhenPresent((String) null)
94 .set(lastName).toValueWhenPresent(() -> myLastName)
95 .set(occupation).toValueWhenPresent(myOccupation)
96 .build()
97 .render(RenderingStrategies.MYBATIS3);
98
99 String expected = "insert into foo (last_name, occupation) "
100 + "values (#{parameters.p1,jdbcType=VARCHAR}, #{parameters.p2,jdbcType=VARCHAR})";
101 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
102 }
103 }