From 3e948485015e1f322f11fa627ad19ac7dca5baa4 Mon Sep 17 00:00:00 2001 From: rusher Date: Sun, 20 Oct 2024 19:29:19 +0200 Subject: [PATCH] [CONJ-1205] permit use of Array parameter --- .../mariadb/jdbc/BasePreparedStatement.java | 15 ++- .../java/org/mariadb/jdbc/Connection.java | 34 ++++- .../mariadb/jdbc/client/result/Result.java | 17 ++- .../jdbc/client/result/UpdatableResult.java | 13 ++ .../mariadb/jdbc/plugin/array/FloatArray.java | 119 ++++++++++++++++++ .../jdbc/plugin/codec/FloatArrayCodec.java | 6 +- .../plugin/codec/FloatObjectArrayCodec.java | 6 +- .../jdbc/integration/ConnectionTest.java | 3 +- .../PreparedStatementParametersTest.java | 68 +++++++++- .../jdbc/integration/UpdateResultSetTest.java | 61 ++++++++- .../codec/FloatArrayCodecTest.java | 91 +++++++++++++- .../integration/codec/FloatCodecTest.java | 4 +- .../jdbc/integration/codec/IntCodecTest.java | 4 +- .../jdbc/integration/codec/LongCodecTest.java | 4 +- .../integration/codec/MediumIntCodecTest.java | 6 +- .../integration/codec/SmallIntCodecTest.java | 6 +- .../integration/codec/TinyIntCodecTest.java | 4 +- 17 files changed, 430 insertions(+), 31 deletions(-) create mode 100644 src/main/java/org/mariadb/jdbc/plugin/array/FloatArray.java diff --git a/src/main/java/org/mariadb/jdbc/BasePreparedStatement.java b/src/main/java/org/mariadb/jdbc/BasePreparedStatement.java index c3be4827e..bd3be5f37 100644 --- a/src/main/java/org/mariadb/jdbc/BasePreparedStatement.java +++ b/src/main/java/org/mariadb/jdbc/BasePreparedStatement.java @@ -29,6 +29,7 @@ import org.mariadb.jdbc.message.server.OkPacket; import org.mariadb.jdbc.message.server.PrepareResultPacket; import org.mariadb.jdbc.plugin.Codec; +import org.mariadb.jdbc.plugin.array.FloatArray; import org.mariadb.jdbc.plugin.codec.*; import org.mariadb.jdbc.util.ParameterList; import org.mariadb.jdbc.util.constants.ColumnFlags; @@ -756,7 +757,19 @@ public void setClob(int parameterIndex, Clob x) throws SQLException { */ @Override public void setArray(int parameterIndex, Array x) throws SQLException { - throw exceptionFactory().notSupported("Array parameter are not supported"); + checkIndex(parameterIndex); + if (x == null) { + parameters.set(parameterIndex - 1, Parameter.NULL_PARAMETER); + return; + } + if (x instanceof FloatArray) { + parameters.set( + parameterIndex - 1, new Parameter<>(FloatArrayCodec.INSTANCE, (float[]) x.getArray())); + return; + } + throw exceptionFactory() + .notSupported( + String.format("this type of Array parameter %s is not supported", x.getClass())); } /** diff --git a/src/main/java/org/mariadb/jdbc/Connection.java b/src/main/java/org/mariadb/jdbc/Connection.java index 39a40e53b..a3cd6eb31 100644 --- a/src/main/java/org/mariadb/jdbc/Connection.java +++ b/src/main/java/org/mariadb/jdbc/Connection.java @@ -3,6 +3,7 @@ // Copyright (c) 2015-2024 MariaDB Corporation Ab package org.mariadb.jdbc; +import java.nio.FloatBuffer; import java.sql.*; import java.util.*; import java.util.concurrent.Executor; @@ -19,6 +20,7 @@ import org.mariadb.jdbc.message.client.PingPacket; import org.mariadb.jdbc.message.client.QueryPacket; import org.mariadb.jdbc.message.client.ResetPacket; +import org.mariadb.jdbc.plugin.array.FloatArray; import org.mariadb.jdbc.util.NativeSql; import org.mariadb.jdbc.util.constants.Capabilities; import org.mariadb.jdbc.util.constants.CatalogTerm; @@ -742,7 +744,37 @@ public void setClientInfo(Properties properties) { @Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { - throw exceptionFactory.notSupported("Array type is not supported"); + return createArrayOf(typeName, (Object) elements); + } + + public Array createArrayOf(String typeName, Object elements) throws SQLException { + if (typeName == null) throw exceptionFactory.notSupported("typeName is not mandatory"); + if (elements == null) return null; + + switch (typeName) { + case "float": + case "Float": + if (float[].class.equals(elements.getClass())) { + return new FloatArray((float[]) elements, client.getContext()); + } + if (Float[].class.equals(elements.getClass())) { + float[] result = + Arrays.stream(((Float[]) elements)) + .collect( + () -> FloatBuffer.allocate(((Float[]) elements).length), + FloatBuffer::put, + (left, right) -> { + throw new UnsupportedOperationException(); + }) + .array(); + return new FloatArray(result, client.getContext()); + } + throw exceptionFactory.notSupported( + "elements class is expect to be float[]/Float[] for 'float/Float' typeName"); + default: + throw exceptionFactory.notSupported( + String.format("typeName %s is not supported", typeName)); + } } @Override diff --git a/src/main/java/org/mariadb/jdbc/client/result/Result.java b/src/main/java/org/mariadb/jdbc/client/result/Result.java index 27a7df3c1..7c9ada10c 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/Result.java +++ b/src/main/java/org/mariadb/jdbc/client/result/Result.java @@ -25,6 +25,7 @@ import org.mariadb.jdbc.export.ExceptionFactory; import org.mariadb.jdbc.message.server.ErrorPacket; import org.mariadb.jdbc.plugin.Codec; +import org.mariadb.jdbc.plugin.array.FloatArray; import org.mariadb.jdbc.plugin.codec.*; import org.mariadb.jdbc.util.constants.ServerStatus; @@ -1236,7 +1237,17 @@ public Clob getClob(int columnIndex) throws SQLException { @Override public Array getArray(int columnIndex) throws SQLException { - throw exceptionFactory.notSupported("Method ResultSet.getArray not supported"); + checkIndex(columnIndex); + fieldLength.set( + rowDecoder.setPosition( + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { + return null; + } + float[] val = + rowDecoder.decode( + FloatArrayCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); + return new FloatArray(val, context); } @Override @@ -1265,7 +1276,7 @@ public Clob getClob(String columnLabel) throws SQLException { @Override public Array getArray(String columnLabel) throws SQLException { - throw exceptionFactory.notSupported("Method ResultSet.getArray not supported"); + return getArray(findColumn(columnLabel)); } @Override @@ -1382,7 +1393,7 @@ public void updateArray(int columnIndex, Array x) throws SQLException { @Override public void updateArray(String columnLabel, Array x) throws SQLException { - throw exceptionFactory.notSupported("Array are not supported"); + updateArray(findColumn(columnLabel), x); } @Override diff --git a/src/main/java/org/mariadb/jdbc/client/result/UpdatableResult.java b/src/main/java/org/mariadb/jdbc/client/result/UpdatableResult.java index 8c6239584..8e83a3ff2 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/UpdatableResult.java +++ b/src/main/java/org/mariadb/jdbc/client/result/UpdatableResult.java @@ -20,6 +20,7 @@ import org.mariadb.jdbc.client.result.rowdecoder.BinaryRowDecoder; import org.mariadb.jdbc.codec.*; import org.mariadb.jdbc.plugin.Codec; +import org.mariadb.jdbc.plugin.array.FloatArray; import org.mariadb.jdbc.plugin.codec.*; import org.mariadb.jdbc.util.ParameterList; @@ -291,6 +292,18 @@ public void updateTime(int columnIndex, Time x) throws SQLException { parameters.set(columnIndex - 1, new Parameter<>(TimeCodec.INSTANCE, x)); } + @Override + public void updateArray(int columnIndex, Array x) throws SQLException { + checkUpdatable(columnIndex); + if (x instanceof FloatArray) { + parameters.set( + columnIndex - 1, new Parameter<>(FloatArrayCodec.INSTANCE, (float[]) x.getArray())); + return; + } + throw exceptionFactory.notSupported( + String.format("this type of Array parameter %s is not supported", x.getClass())); + } + @Override public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException { checkUpdatable(columnIndex); diff --git a/src/main/java/org/mariadb/jdbc/plugin/array/FloatArray.java b/src/main/java/org/mariadb/jdbc/plugin/array/FloatArray.java new file mode 100644 index 000000000..ddb9a8802 --- /dev/null +++ b/src/main/java/org/mariadb/jdbc/plugin/array/FloatArray.java @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (c) 2012-2014 Monty Program Ab +// Copyright (c) 2015-2024 MariaDB Corporation Ab +package org.mariadb.jdbc.plugin.array; + +import java.nio.charset.StandardCharsets; +import java.sql.*; +import java.util.Arrays; +import java.util.Map; +import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; +import org.mariadb.jdbc.client.DataType; +import org.mariadb.jdbc.client.result.CompleteResult; +import org.mariadb.jdbc.util.constants.ColumnFlags; + +public class FloatArray implements Array { + + private final float[] val; + private Context context; + + public FloatArray(float[] val, Context context) { + this.val = val; + this.context = context; + } + + @Override + public String getBaseTypeName() throws SQLException { + return "float[]"; + } + + @Override + public int getBaseType() throws SQLException { + return Types.FLOAT; + } + + @Override + public Object getArray() throws SQLException { + return this.val; + } + + @Override + public Object getArray(Map> map) throws SQLException { + throw new SQLFeatureNotSupportedException( + "getArray(Map> map) is not supported"); + } + + @Override + public Object getArray(long index, int count) throws SQLException { + if (index < 1 || index > val.length) { + throw new SQLException( + String.format( + "Wrong index position. Is %s but must be in 1-%s range", index, val.length)); + } + if (count < 0 || (index - 1 + count) > val.length) { + throw new SQLException( + String.format( + "Count value is too big. Count is %s but cannot be > to %s", + count, val.length - (index - 1))); + } + + return Arrays.copyOfRange(val, (int) index - 1, (int) (index - 1) + count); + } + + @Override + public Object getArray(long index, int count, Map> map) throws SQLException { + throw new SQLFeatureNotSupportedException( + "getArray(long index, int count, Map> map) is not supported"); + } + + @Override + public ResultSet getResultSet() throws SQLException { + return getResultSet(1, this.val.length); + } + + @Override + public ResultSet getResultSet(Map> map) throws SQLException { + throw new SQLFeatureNotSupportedException( + "getResultSet(Map> map) is not supported"); + } + + @Override + public ResultSet getResultSet(long index, int count) throws SQLException { + byte[][] rows = new byte[count][]; + for (int i = 0; i < count; i++) { + rows[i] = Float.toString(this.val[(int) index - 1 + i]).getBytes(StandardCharsets.US_ASCII); + } + + return new CompleteResult( + new ColumnDecoder[] {ColumnDecoder.create("Array", DataType.FLOAT, ColumnFlags.NOT_NULL)}, + rows, + context, + ResultSet.TYPE_SCROLL_INSENSITIVE); + } + + @Override + public ResultSet getResultSet(long index, int count, Map> map) + throws SQLException { + throw new SQLFeatureNotSupportedException( + "getResultSet(long index, int count, Map> map) is not supported"); + } + + @Override + public void free() {} + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + FloatArray that = (FloatArray) o; + + return Arrays.equals(val, that.val); + } + + @Override + public int hashCode() { + return Arrays.hashCode(val); + } +} diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/FloatArrayCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/FloatArrayCodec.java index 39a98b0e7..c1f3a9740 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/FloatArrayCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/FloatArrayCodec.java @@ -107,7 +107,7 @@ private byte[] getBytes(ReadableByteBuf buf, MutableInt length, ColumnDecoder co default: buf.skip(length.get()); throw new SQLDataException( - String.format("Data type %s cannot be decoded as byte[]", column.getType())); + String.format("Data type %s cannot be decoded as float[]", column.getType())); } } @@ -131,7 +131,9 @@ public void encodeBinary( final Calendar cal, final Long maxLength) throws IOException { - encoder.writeBytes(toByteArray((float[]) value)); + byte[] arr = toByteArray((float[]) value); + encoder.writeLength(arr.length); + encoder.writeBytes(arr); } public int getBinaryEncodeType() { diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/FloatObjectArrayCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/FloatObjectArrayCodec.java index 0be5fc549..bad603aaa 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/FloatObjectArrayCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/FloatObjectArrayCodec.java @@ -127,7 +127,7 @@ private byte[] getBytes(ReadableByteBuf buf, MutableInt length, ColumnDecoder co default: buf.skip(length.get()); throw new SQLDataException( - String.format("Data type %s cannot be decoded as byte[]", column.getType())); + String.format("Data type %s cannot be decoded as float[]", column.getType())); } } @@ -151,7 +151,9 @@ public void encodeBinary( final Calendar cal, final Long maxLength) throws IOException { - encoder.writeBytes(toByteArray((Float[]) value)); + byte[] arr = toByteArray((Float[]) value); + encoder.writeLength(arr.length); + encoder.writeBytes(arr); } public int getBinaryEncodeType() { diff --git a/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java b/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java index 4998c9e85..01feb80b9 100644 --- a/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java @@ -398,7 +398,8 @@ public void checkFixedData() throws SQLException { assertTrue(sharedConn.createClob() instanceof Clob); assertTrue(sharedConn.createNClob() instanceof NClob); assertThrows(SQLException.class, () -> sharedConn.createSQLXML()); - assertThrows(SQLException.class, () -> sharedConn.createArrayOf("", null)); + assertNull(sharedConn.createArrayOf("", null)); + assertThrows(SQLException.class, () -> sharedConn.createArrayOf("string", "ddd")); assertThrows(SQLException.class, () -> sharedConn.createStruct("", null)); assertNull(sharedConn.getSchema()); sharedConn.setSchema("fff"); diff --git a/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java b/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java index 7159495bd..73297a7b2 100644 --- a/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java @@ -15,6 +15,7 @@ import java.time.LocalDateTime; import java.time.ZonedDateTime; import java.util.Calendar; +import java.util.Map; import java.util.TimeZone; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assumptions; @@ -354,10 +355,73 @@ private void checkNotSupported(Connection con) throws SQLException { SQLException.class, () -> preparedStatement.setRef(1, null), "REF parameter are not supported"); + preparedStatement.setArray(1, null); Common.assertThrowsContains( SQLException.class, - () -> preparedStatement.setArray(1, null), - "Array parameter are not supported"); + () -> con.createArrayOf("String", new Float[] {1f}), + "typeName String is not supported"); + Common.assertThrowsContains( + SQLException.class, + () -> + preparedStatement.setArray( + 1, + new Array() { + @Override + public String getBaseTypeName() throws SQLException { + return null; + } + + @Override + public int getBaseType() throws SQLException { + return 0; + } + + @Override + public Object getArray() throws SQLException { + return null; + } + + @Override + public Object getArray(Map> map) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count, Map> map) + throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet() throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(Map> map) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count, Map> map) + throws SQLException { + return null; + } + + @Override + public void free() throws SQLException {} + }), + "this type of Array parameter"); Common.assertThrowsContains( SQLException.class, () -> preparedStatement.setRowId(1, null), diff --git a/src/test/java/org/mariadb/jdbc/integration/UpdateResultSetTest.java b/src/test/java/org/mariadb/jdbc/integration/UpdateResultSetTest.java index 2b7bd555b..01f3684e6 100644 --- a/src/test/java/org/mariadb/jdbc/integration/UpdateResultSetTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/UpdateResultSetTest.java @@ -11,6 +11,7 @@ import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.sql.*; +import java.util.Map; import org.junit.jupiter.api.*; public class UpdateResultSetTest extends Common { @@ -1164,9 +1165,9 @@ public void expectedErrorField() throws SQLException { Common.assertThrowsContains( SQLException.class, () -> rs.updateRef("t1", null), "not supported"); Common.assertThrowsContains( - SQLException.class, () -> rs.updateArray(2, null), "not supported"); + SQLException.class, () -> rs.updateArray(2, new FakeArray()), "not supported"); Common.assertThrowsContains( - SQLException.class, () -> rs.updateArray("t1", null), "not supported"); + SQLException.class, () -> rs.updateArray("t1", new FakeArray()), "not supported"); Common.assertThrowsContains( SQLException.class, () -> rs.updateRowId(2, null), "not supported"); Common.assertThrowsContains( @@ -1203,4 +1204,60 @@ public void addAfterDataFull() throws SQLException { } sharedConn.rollback(); } + + private class FakeArray implements Array { + @Override + public String getBaseTypeName() throws SQLException { + return null; + } + + @Override + public int getBaseType() throws SQLException { + return 0; + } + + @Override + public Object getArray() throws SQLException { + return null; + } + + @Override + public Object getArray(Map> map) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count, Map> map) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet() throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(Map> map) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count, Map> map) + throws SQLException { + return null; + } + + @Override + public void free() throws SQLException {} + } } diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/FloatArrayCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/FloatArrayCodecTest.java index 7a3073672..ae60f3b0b 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/FloatArrayCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/FloatArrayCodecTest.java @@ -24,7 +24,8 @@ public static void beforeAll2() throws SQLException { drop(); Statement stmt = sharedConn.createStatement(); stmt.execute( - "CREATE TABLE BinaryCodec (t1 blob) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); + "CREATE TABLE BinaryCodec (t0 int not null primary key auto_increment, t1 blob) CHARACTER" + + " SET utf8mb4 COLLATE utf8mb4_unicode_ci"); stmt.execute("FLUSH TABLES"); } @@ -47,12 +48,92 @@ private void floatArray(Connection con) throws SQLException { try (PreparedStatement prep = con.prepareStatement("SELECT * FROM BinaryCodec")) { ResultSet rs = prep.executeQuery(); assertTrue(rs.next()); - assertArrayEquals(expectedConverstion, rs.getBytes(1)); - float[] res = rs.getObject(1, float[].class); + assertArrayEquals(expectedConverstion, rs.getBytes(2)); + float[] res = rs.getObject(2, float[].class); assertArrayEquals(val, res); } } + @Test + public void floatArrayArrayObj() throws SQLException { + Statement stmt = sharedConn.createStatement(); + stmt.execute("TRUNCATE TABLE BinaryCodec"); + floatArrayArrayObj(sharedConn); + stmt.execute("TRUNCATE TABLE BinaryCodec"); + floatArrayArrayObj(sharedConnBinary); + } + + private void floatArrayArrayObj(org.mariadb.jdbc.Connection con) throws SQLException { + float[] val = new float[] {1, 2, 3}; + float[] val2 = new float[] {4, 5, 6}; + + Array valArray = con.createArrayOf("float", val); + Array valArray2 = con.createArrayOf("float", val2); + byte[] expectedConverstion = + new byte[] {0x00, 0x00, (byte) 0x80, 0x3F, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40}; + try (PreparedStatement prep = + con.prepareStatement("INSERT INTO BinaryCodec(t0, t1) VALUES (?, ?)")) { + prep.setInt(1, 1); + prep.setArray(2, valArray); + prep.execute(); + } + try (PreparedStatement prep = + con.prepareStatement( + "SELECT * FROM BinaryCodec", + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_UPDATABLE)) { + ResultSet rs = prep.executeQuery(); + assertTrue(rs.next()); + assertArrayEquals(expectedConverstion, rs.getBytes(2)); + float[] res = rs.getObject(2, float[].class); + assertArrayEquals(val, res); + Array resArray = rs.getArray(2); + assertEquals(valArray, resArray); + rs.updateArray(2, valArray2); + rs.updateRow(); + } + try (PreparedStatement prep = + con.prepareStatement( + "SELECT * FROM BinaryCodec", + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_UPDATABLE)) { + ResultSet rs = prep.executeQuery(); + assertTrue(rs.next()); + float[] res = rs.getObject(2, float[].class); + assertArrayEquals(val2, res); + Array resArray = rs.getArray(2); + assertEquals(valArray2, resArray); + } + } + + @Test + public void floatArrayObjArray() throws SQLException { + Statement stmt = sharedConn.createStatement(); + stmt.execute("TRUNCATE TABLE BinaryCodec"); + floatArrayObjArray(sharedConn); + floatArrayObjArray(sharedConnBinary); + } + + private void floatArrayObjArray(org.mariadb.jdbc.Connection con) throws SQLException { + Float[] val = new Float[] {1f, 2f, 3f}; + Array valArray = con.createArrayOf("Float", val); + byte[] expectedConverstion = + new byte[] {0x00, 0x00, (byte) 0x80, 0x3F, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40}; + try (PreparedStatement prep = con.prepareStatement("INSERT INTO BinaryCodec(t1) VALUES (?)")) { + prep.setArray(1, valArray); + prep.execute(); + } + try (PreparedStatement prep = con.prepareStatement("SELECT * FROM BinaryCodec")) { + ResultSet rs = prep.executeQuery(); + assertTrue(rs.next()); + assertArrayEquals(expectedConverstion, rs.getBytes(2)); + Float[] res = rs.getObject(2, Float[].class); + assertArrayEquals(val, res); + Array resArray = rs.getArray(2); + assertEquals(valArray, resArray); + } + } + @Test public void floatObjectArray() throws SQLException { Statement stmt = sharedConn.createStatement(); @@ -72,8 +153,8 @@ private void floatObjectArray(Connection con) throws SQLException { try (PreparedStatement prep = con.prepareStatement("SELECT * FROM BinaryCodec")) { ResultSet rs = prep.executeQuery(); assertTrue(rs.next()); - assertArrayEquals(expectedConverstion, rs.getBytes(1)); - Float[] res = rs.getObject(1, Float[].class); + assertArrayEquals(expectedConverstion, rs.getBytes(2)); + Float[] res = rs.getObject(2, Float[].class); assertArrayEquals(val, res); } } diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/FloatCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/FloatCodecTest.java index 9dd2f04d9..587130e21 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/FloatCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/FloatCodecTest.java @@ -645,11 +645,11 @@ public void getNClob(ResultSet rs) { public void getArray() throws SQLException { ResultSet rs = get(); Common.assertThrowsContains( - SQLException.class, () -> rs.getArray(1), "Method ResultSet.getArray not supported"); + SQLException.class, () -> rs.getArray(1), "Data type FLOAT cannot be decoded as float[]"); Common.assertThrowsContains( SQLException.class, () -> rs.getArray("t1alias"), - "Method ResultSet.getArray not supported"); + "Data type FLOAT cannot be decoded as float[]"); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/IntCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/IntCodecTest.java index 16d652e12..3875a3a17 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/IntCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/IntCodecTest.java @@ -903,11 +903,11 @@ public void getArrayPrepared() throws SQLException { private void getArray(ResultSet rs) { Common.assertThrowsContains( - SQLException.class, () -> rs.getArray(1), "Method ResultSet.getArray not supported"); + SQLException.class, () -> rs.getArray(1), "Data type INTEGER cannot be decoded as float[]"); Common.assertThrowsContains( SQLException.class, () -> rs.getArray("t1alias"), - "Method ResultSet.getArray not supported"); + "Data type INTEGER cannot be decoded as float[]"); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/LongCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/LongCodecTest.java index f7979b366..979b222fb 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/LongCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/LongCodecTest.java @@ -884,11 +884,11 @@ public void getArrayPrepared() throws SQLException { private void getArray(ResultSet rs) { Common.assertThrowsContains( - SQLException.class, () -> rs.getArray(1), "Method ResultSet.getArray not supported"); + SQLException.class, () -> rs.getArray(1), "Data type BIGINT cannot be decoded as float[]"); Common.assertThrowsContains( SQLException.class, () -> rs.getArray("t1alias"), - "Method ResultSet.getArray not supported"); + "Data type BIGINT cannot be decoded as float[]"); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/MediumIntCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/MediumIntCodecTest.java index 6a70907de..332c410dc 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/MediumIntCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/MediumIntCodecTest.java @@ -862,11 +862,13 @@ public void getArrayPrepared() throws SQLException { private void getArray(ResultSet rs) { Common.assertThrowsContains( - SQLException.class, () -> rs.getArray(1), "Method ResultSet.getArray not supported"); + SQLException.class, + () -> rs.getArray(1), + "Data type MEDIUMINT cannot be decoded as float[]"); Common.assertThrowsContains( SQLException.class, () -> rs.getArray("t1alias"), - "Method ResultSet.getArray not supported"); + "Data type MEDIUMINT cannot be decoded as float[]"); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/SmallIntCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/SmallIntCodecTest.java index aeec6aec9..aec221358 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/SmallIntCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/SmallIntCodecTest.java @@ -855,11 +855,13 @@ public void getArrayPrepared() throws SQLException { private void getArray(ResultSet rs) { Common.assertThrowsContains( - SQLException.class, () -> rs.getArray(1), "Method ResultSet.getArray not supported"); + SQLException.class, + () -> rs.getArray(1), + "Data type SMALLINT cannot be decoded as float[]"); Common.assertThrowsContains( SQLException.class, () -> rs.getArray("t1alias"), - "Method ResultSet.getArray not supported"); + "Data type SMALLINT cannot be decoded as float[]"); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/TinyIntCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/TinyIntCodecTest.java index 5496d5d21..4e77d64b5 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/TinyIntCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/TinyIntCodecTest.java @@ -873,11 +873,11 @@ public void getArrayPrepare() throws SQLException { public void getArray(ResultSet rs) { Common.assertThrowsContains( - SQLException.class, () -> rs.getArray(1), "Method ResultSet.getArray not supported"); + SQLException.class, () -> rs.getArray(1), "Data type TINYINT cannot be decoded as float[]"); Common.assertThrowsContains( SQLException.class, () -> rs.getArray("t1alias"), - "Method ResultSet.getArray not supported"); + "Data type TINYINT cannot be decoded as float[]"); } @Test