|
| 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 examples.type_conversion; |
| 17 | + |
| 18 | +import static examples.type_conversion.MyFilesDynamicSqlSupport.*; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.entry; |
| 21 | +import static org.mybatis.dynamic.sql.SqlBuilder.*; |
| 22 | +import static examples.type_conversion.ToBase64.toBase64; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.sql.Connection; |
| 26 | +import java.sql.DriverManager; |
| 27 | +import java.util.Base64; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Random; |
| 30 | + |
| 31 | +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; |
| 32 | +import org.apache.ibatis.jdbc.ScriptRunner; |
| 33 | +import org.apache.ibatis.mapping.Environment; |
| 34 | +import org.apache.ibatis.session.Configuration; |
| 35 | +import org.apache.ibatis.session.SqlSession; |
| 36 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 37 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 38 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 39 | +import org.junit.jupiter.api.BeforeEach; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; |
| 42 | +import org.mybatis.dynamic.sql.render.RenderingStrategies; |
| 43 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 44 | + |
| 45 | +class TypeConversionTest { |
| 46 | + |
| 47 | + private static final String JDBC_URL = "jdbc:hsqldb:mem:aname"; |
| 48 | + private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver"; |
| 49 | + |
| 50 | + private SqlSessionFactory sqlSessionFactory; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void setup() throws Exception { |
| 54 | + Class.forName(JDBC_DRIVER); |
| 55 | + InputStream is = getClass().getResourceAsStream("/examples/type_conversion/CreateDB.sql"); |
| 56 | + try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) { |
| 57 | + ScriptRunner sr = new ScriptRunner(connection); |
| 58 | + sr.setLogWriter(null); |
| 59 | + sr.runScript(new InputStreamReader(is)); |
| 60 | + } |
| 61 | + |
| 62 | + UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); |
| 63 | + Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); |
| 64 | + Configuration config = new Configuration(environment); |
| 65 | + config.addMapper(MyFilesMapper.class); |
| 66 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testFunctionInSelect() { |
| 71 | + try (SqlSession session = sqlSessionFactory.openSession()) { |
| 72 | + MyFilesMapper mapper = session.getMapper(MyFilesMapper.class); |
| 73 | + |
| 74 | + Random random = new Random(); |
| 75 | + byte[] randomBlob = new byte[1024]; |
| 76 | + random.nextBytes(randomBlob); |
| 77 | + |
| 78 | + GeneralInsertStatementProvider insertStatement = insertInto(myfiles) |
| 79 | + .set(fileId).toValue(1) |
| 80 | + .set(fileContents).toValue(randomBlob) |
| 81 | + .build() |
| 82 | + .render(RenderingStrategies.MYBATIS3); |
| 83 | + |
| 84 | + int rows = mapper.insert(insertStatement); |
| 85 | + assertThat(rows).isEqualTo(1); |
| 86 | + |
| 87 | + SelectStatementProvider selectStatement = select(fileId, fileContents) |
| 88 | + .from(myfiles) |
| 89 | + .where(fileId, isEqualTo(1)) |
| 90 | + .build() |
| 91 | + .render(RenderingStrategies.MYBATIS3); |
| 92 | + |
| 93 | + Map<String, Object> row = mapper.generalSelect(selectStatement); |
| 94 | + assertThat(row).containsExactly(entry("FILE_ID", 1), entry("FILE_CONTENTS", randomBlob)); |
| 95 | + |
| 96 | + selectStatement = select(fileId, toBase64(fileContents).as("checksum")) |
| 97 | + .from(myfiles) |
| 98 | + .where(fileId, isEqualTo(1)) |
| 99 | + .build() |
| 100 | + .render(RenderingStrategies.MYBATIS3); |
| 101 | + |
| 102 | + String expected = "select file_id, TO_BASE64(file_contents) as checksum from MyFiles " |
| 103 | + + "where file_id = #{parameters.p1,jdbcType=INTEGER}"; |
| 104 | + assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); |
| 105 | + |
| 106 | + row = mapper.generalSelect(selectStatement); |
| 107 | + |
| 108 | + String base64 = Base64.getEncoder().encodeToString(randomBlob); |
| 109 | + assertThat(row).contains(entry("FILE_ID", 1), entry("CHECKSUM", base64)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testFunctionInWhere() { |
| 115 | + try (SqlSession session = sqlSessionFactory.openSession()) { |
| 116 | + MyFilesMapper mapper = session.getMapper(MyFilesMapper.class); |
| 117 | + |
| 118 | + Random random = new Random(); |
| 119 | + byte[] randomBlob = new byte[1024]; |
| 120 | + random.nextBytes(randomBlob); |
| 121 | + |
| 122 | + GeneralInsertStatementProvider insertStatement = insertInto(myfiles) |
| 123 | + .set(fileId).toValue(1) |
| 124 | + .set(fileContents).toValue(randomBlob) |
| 125 | + .build() |
| 126 | + .render(RenderingStrategies.MYBATIS3); |
| 127 | + |
| 128 | + int rows = mapper.insert(insertStatement); |
| 129 | + assertThat(rows).isEqualTo(1); |
| 130 | + |
| 131 | + SelectStatementProvider selectStatement = select(fileId, fileContents) |
| 132 | + .from(myfiles) |
| 133 | + .where(fileId, isEqualTo(1)) |
| 134 | + .build() |
| 135 | + .render(RenderingStrategies.MYBATIS3); |
| 136 | + |
| 137 | + Map<String, Object> row = mapper.generalSelect(selectStatement); |
| 138 | + assertThat(row).contains(entry("FILE_ID", 1), entry("FILE_CONTENTS", randomBlob)); |
| 139 | + |
| 140 | + String base64 = Base64.getEncoder().encodeToString(randomBlob); |
| 141 | + selectStatement = select(fileId, fileContents) |
| 142 | + .from(myfiles) |
| 143 | + .where(toBase64(fileContents), isEqualTo(base64)) |
| 144 | + .build() |
| 145 | + .render(RenderingStrategies.MYBATIS3); |
| 146 | + |
| 147 | + String expected = "select file_id, file_contents from MyFiles " |
| 148 | + + "where TO_BASE64(file_contents) = #{parameters.p1,jdbcType=VARCHAR}"; |
| 149 | + assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); |
| 150 | + |
| 151 | + row = mapper.generalSelect(selectStatement); |
| 152 | + |
| 153 | + assertThat(row).contains(entry("FILE_ID", 1), entry("FILE_CONTENTS", randomBlob)); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments