|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.plugin.exasol; |
| 15 | + |
| 16 | +import io.airlift.slice.Slice; |
| 17 | +import io.airlift.slice.Slices; |
| 18 | +import io.trino.plugin.base.mapping.DefaultIdentifierMapping; |
| 19 | +import io.trino.plugin.jdbc.BaseJdbcConfig; |
| 20 | +import io.trino.plugin.jdbc.BooleanWriteFunction; |
| 21 | +import io.trino.plugin.jdbc.DefaultQueryBuilder; |
| 22 | +import io.trino.plugin.jdbc.DoubleWriteFunction; |
| 23 | +import io.trino.plugin.jdbc.JdbcClient; |
| 24 | +import io.trino.plugin.jdbc.LongWriteFunction; |
| 25 | +import io.trino.plugin.jdbc.ObjectWriteFunction; |
| 26 | +import io.trino.plugin.jdbc.SliceWriteFunction; |
| 27 | +import io.trino.plugin.jdbc.WriteMapping; |
| 28 | +import io.trino.plugin.jdbc.logging.RemoteQueryModifier; |
| 29 | +import io.trino.spi.connector.ConnectorSession; |
| 30 | +import io.trino.spi.type.Int128; |
| 31 | +import io.trino.spi.type.Type; |
| 32 | +import io.trino.testing.TestingConnectorSession; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import java.math.BigDecimal; |
| 36 | +import java.sql.PreparedStatement; |
| 37 | +import java.sql.SQLException; |
| 38 | +import java.sql.Types; |
| 39 | + |
| 40 | +import static com.google.common.reflect.Reflection.newProxy; |
| 41 | +import static io.trino.spi.type.BigintType.BIGINT; |
| 42 | +import static io.trino.spi.type.BooleanType.BOOLEAN; |
| 43 | +import static io.trino.spi.type.CharType.createCharType; |
| 44 | +import static io.trino.spi.type.DecimalType.createDecimalType; |
| 45 | +import static io.trino.spi.type.DoubleType.DOUBLE; |
| 46 | +import static io.trino.spi.type.IntegerType.INTEGER; |
| 47 | +import static io.trino.spi.type.SmallintType.SMALLINT; |
| 48 | +import static io.trino.spi.type.TinyintType.TINYINT; |
| 49 | +import static io.trino.spi.type.VarcharType.createUnboundedVarcharType; |
| 50 | +import static io.trino.spi.type.VarcharType.createVarcharType; |
| 51 | +import static org.assertj.core.api.Assertions.assertThat; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 53 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 54 | + |
| 55 | +public class TestExasolClient |
| 56 | +{ |
| 57 | + private static final JdbcClient CLIENT = new ExasolClient( |
| 58 | + new BaseJdbcConfig(), |
| 59 | + session -> { |
| 60 | + throw new UnsupportedOperationException(); |
| 61 | + }, |
| 62 | + new DefaultQueryBuilder(RemoteQueryModifier.NONE), |
| 63 | + new DefaultIdentifierMapping(), |
| 64 | + RemoteQueryModifier.NONE); |
| 65 | + |
| 66 | + private static final ConnectorSession SESSION = TestingConnectorSession.SESSION; |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testTypedWriteMapping() |
| 70 | + throws SQLException |
| 71 | + { |
| 72 | + testLongWriteMapping(TINYINT, Types.TINYINT, "tinyint", "setByte", 1L, Byte.valueOf("1")); |
| 73 | + testLongWriteMapping(SMALLINT, Types.SMALLINT, "smallint", "setShort", 256L, Short.valueOf("256")); |
| 74 | + testLongWriteMapping(INTEGER, Types.INTEGER, "integer", "setInt", 123456L, 123456); |
| 75 | + testLongWriteMapping(BIGINT, Types.BIGINT, "bigint", "setLong", 123456789L, 123456789L); |
| 76 | + testLongWriteMapping(createDecimalType(16, 6), Types.DECIMAL, "decimal(16, 6)", "setBigDecimal", 123456123456L, BigDecimal.valueOf(123456.123456)); |
| 77 | + testInt128WriteMapping(createDecimalType(36, 12), Types.DECIMAL, "decimal(36, 12)", "setBigDecimal", Int128.valueOf("123456789012345612345678901234567890"), new BigDecimal("123456789012345612345678.901234567890")); |
| 78 | + |
| 79 | + testBooleanWriteMapping(Boolean.TRUE); |
| 80 | + testDoubleWriteMapping(1.2567); |
| 81 | + |
| 82 | + testSliceWriteMapping(createCharType(25), Types.NCHAR, "char(25)", "test"); |
| 83 | + testSliceWriteMapping(createUnboundedVarcharType(), Types.VARCHAR, "varchar(20000)", "u".repeat(20000)); |
| 84 | + testSliceWriteMapping(createVarcharType(123), Types.VARCHAR, "varchar(123)", "9".repeat(123)); |
| 85 | + } |
| 86 | + |
| 87 | + private void testLongWriteMapping(Type type, int expectedJdbcType, String expectedDataType, String expectedJdbcMethodName, |
| 88 | + Long inputValue, Object expectedJdbcValue) |
| 89 | + throws SQLException |
| 90 | + { |
| 91 | + WriteMapping writeMapping = CLIENT.toWriteMapping(SESSION, type); |
| 92 | + assertEquals(writeMapping.getWriteFunction().getJavaType(), long.class); |
| 93 | + assertEquals(writeMapping.getDataType(), expectedDataType); |
| 94 | + assertTrue(writeMapping.getWriteFunction() instanceof LongWriteFunction); |
| 95 | + assertThat(writeMapping.getWriteFunction().getBindExpression()).isEqualTo("?"); |
| 96 | + PreparedStatement statementMock = preparedStatementMock(expectedJdbcType, expectedJdbcMethodName, expectedJdbcValue); |
| 97 | + LongWriteFunction longWriteFunction = (LongWriteFunction) writeMapping.getWriteFunction(); |
| 98 | + longWriteFunction.set(statementMock, expectedJdbcType, inputValue); |
| 99 | + } |
| 100 | + |
| 101 | + private void testBooleanWriteMapping(boolean inputValue) |
| 102 | + throws SQLException |
| 103 | + { |
| 104 | + WriteMapping writeMapping = CLIENT.toWriteMapping(SESSION, BOOLEAN); |
| 105 | + assertEquals(writeMapping.getWriteFunction().getJavaType(), boolean.class); |
| 106 | + assertEquals(writeMapping.getDataType(), "boolean"); |
| 107 | + assertTrue(writeMapping.getWriteFunction() instanceof BooleanWriteFunction); |
| 108 | + assertThat(writeMapping.getWriteFunction().getBindExpression()).isEqualTo("?"); |
| 109 | + PreparedStatement statementMock = preparedStatementMock(Types.BOOLEAN, "setBoolean", inputValue); |
| 110 | + BooleanWriteFunction booleanWriteFunction = (BooleanWriteFunction) writeMapping.getWriteFunction(); |
| 111 | + booleanWriteFunction.set(statementMock, Types.BOOLEAN, inputValue); |
| 112 | + } |
| 113 | + |
| 114 | + private void testDoubleWriteMapping(double inputValue) |
| 115 | + throws SQLException |
| 116 | + { |
| 117 | + WriteMapping writeMapping = CLIENT.toWriteMapping(SESSION, DOUBLE); |
| 118 | + assertEquals(writeMapping.getWriteFunction().getJavaType(), double.class); |
| 119 | + assertEquals(writeMapping.getDataType(), "double"); |
| 120 | + assertTrue(writeMapping.getWriteFunction() instanceof DoubleWriteFunction); |
| 121 | + assertThat(writeMapping.getWriteFunction().getBindExpression()).isEqualTo("?"); |
| 122 | + PreparedStatement statementMock = preparedStatementMock(Types.DOUBLE, "setDouble", inputValue); |
| 123 | + DoubleWriteFunction doubleWriteFunction = (DoubleWriteFunction) writeMapping.getWriteFunction(); |
| 124 | + doubleWriteFunction.set(statementMock, Types.DOUBLE, inputValue); |
| 125 | + } |
| 126 | + |
| 127 | + private void testInt128WriteMapping(Type type, int expectedJdbcType, String expectedDataType, String expectedJdbcMethodName, |
| 128 | + Int128 inputValue, BigDecimal expectedJdbcValue) |
| 129 | + throws SQLException |
| 130 | + { |
| 131 | + WriteMapping writeMapping = CLIENT.toWriteMapping(SESSION, type); |
| 132 | + assertEquals(writeMapping.getWriteFunction().getJavaType(), Int128.class); |
| 133 | + assertEquals(writeMapping.getDataType(), expectedDataType); |
| 134 | + assertTrue(writeMapping.getWriteFunction() instanceof ObjectWriteFunction); |
| 135 | + assertThat(writeMapping.getWriteFunction().getBindExpression()).isEqualTo("?"); |
| 136 | + PreparedStatement statementMock = preparedStatementMock(expectedJdbcType, expectedJdbcMethodName, expectedJdbcValue); |
| 137 | + ObjectWriteFunction objectWriteFunction = (ObjectWriteFunction) writeMapping.getWriteFunction(); |
| 138 | + objectWriteFunction.set(statementMock, expectedJdbcType, inputValue); |
| 139 | + } |
| 140 | + |
| 141 | + private void testSliceWriteMapping(Type type, int expectedJdbcType, String expectedDataType, String inputValue) |
| 142 | + throws SQLException |
| 143 | + { |
| 144 | + WriteMapping writeMapping = CLIENT.toWriteMapping(SESSION, type); |
| 145 | + assertEquals(writeMapping.getWriteFunction().getJavaType(), Slice.class); |
| 146 | + assertEquals(writeMapping.getDataType(), expectedDataType); |
| 147 | + assertTrue(writeMapping.getWriteFunction() instanceof SliceWriteFunction); |
| 148 | + assertThat(writeMapping.getWriteFunction().getBindExpression()).isEqualTo("?"); |
| 149 | + PreparedStatement statementMock = preparedStatementMock(expectedJdbcType, "setString", inputValue); |
| 150 | + SliceWriteFunction sliceWriteFunction = (SliceWriteFunction) writeMapping.getWriteFunction(); |
| 151 | + sliceWriteFunction.set(statementMock, expectedJdbcType, Slices.utf8Slice(inputValue)); |
| 152 | + } |
| 153 | + |
| 154 | + private PreparedStatement preparedStatementMock(int expectedJdbcType, String expectedMethodName, Object expectedValue) |
| 155 | + { |
| 156 | + return newProxy(PreparedStatement.class, (proxy, method, args) -> |
| 157 | + { |
| 158 | + assertThat(method.getName()).isEqualTo(expectedMethodName); |
| 159 | + assertThat(args.length).isEqualTo(2); |
| 160 | + assertThat(args[0]).isEqualTo(expectedJdbcType); |
| 161 | + assertThat(args[1]) |
| 162 | + .describedAs("expected jdbc value") |
| 163 | + .isEqualTo(expectedValue); |
| 164 | + |
| 165 | + return null; |
| 166 | + }); |
| 167 | + } |
| 168 | +} |
0 commit comments