|
| 1 | +/** |
| 2 | + * Copyright 2016-2020 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 | + * http://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.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 | +public 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 | + public void testFullInsertStatementBuilder() { |
| 39 | + |
| 40 | + GeneralInsertStatementProvider insertStatement = insertInto(foo) |
| 41 | + .set(id).equalTo(2) |
| 42 | + .set(firstName).equalTo("Jones") |
| 43 | + .set(occupation).equalTo("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 | + public void testInsertStatementBuilderWithNulls() { |
| 56 | + |
| 57 | + GeneralInsertStatementProvider insertStatement = insertInto(foo) |
| 58 | + .set(id).equalTo(1) |
| 59 | + .set(firstName).equalTo("Fred") |
| 60 | + .set(lastName).equalTo("Smith") |
| 61 | + .set(occupation).equalToNull() |
| 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 | + public void testInsertStatementBuilderWithConstants() { |
| 72 | + |
| 73 | + GeneralInsertStatementProvider insertStatement = insertInto(foo) |
| 74 | + .set(id).equalToConstant("3") |
| 75 | + .set(firstName).equalTo("Fred") |
| 76 | + .set(lastName).equalTo("Jones") |
| 77 | + .set(occupation).equalToStringConstant("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 | + public void testSelectiveInsertStatementBuilder() { |
| 88 | + Integer myId = null; |
| 89 | + String myFirstName = null; |
| 90 | + String myLastName = "jones"; |
| 91 | + String myOccupation = "dino driver"; |
| 92 | + |
| 93 | + GeneralInsertStatementProvider insertStatement = insertInto(foo) |
| 94 | + .set(id).equalToWhenPresent(() -> myId) |
| 95 | + .set(firstName).equalToWhenPresent(myFirstName) |
| 96 | + .set(lastName).equalToWhenPresent(() -> myLastName) |
| 97 | + .set(occupation).equalToWhenPresent(myOccupation) |
| 98 | + .build() |
| 99 | + .render(RenderingStrategies.MYBATIS3); |
| 100 | + |
| 101 | + String expected = "insert into foo (last_name, occupation) " |
| 102 | + + "values (#{parameters.p1,jdbcType=VARCHAR}, #{parameters.p2,jdbcType=VARCHAR})"; |
| 103 | + assertThat(insertStatement.getInsertStatement()).isEqualTo(expected); |
| 104 | + } |
| 105 | +} |
0 commit comments