From e72f9b9254dea1f0c099db9026d4b5b4a0caa951 Mon Sep 17 00:00:00 2001 From: Przemyslaw Motacki Date: Fri, 15 Mar 2024 11:43:49 +0100 Subject: [PATCH] SNOW-1232333 - add mapping for basic types in method getObject(int, type) # Conflicts: # src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java --- .../client/jdbc/SnowflakeBaseResultSet.java | 32 ++++++- .../client/jdbc/ResultSetLatestIT.java | 87 +++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java b/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java index 7958a5b77..60918ae64 100644 --- a/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java +++ b/src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java @@ -1354,8 +1354,38 @@ public T getObject(int columnIndex, Class type) throws SQLException { } else if (Map.class.isAssignableFrom(type)) { JsonNode jsonNode = ((JsonSqlInput) getObject(columnIndex)).getInput(); return (T) OBJECT_MAPPER.convertValue(jsonNode, new TypeReference>() {}); + } else if (String.class.isAssignableFrom(type)) { + return (T) getString(columnIndex); + } else if (Boolean.class.isAssignableFrom(type)) { + return (T) (Boolean) getBoolean(columnIndex); + } else if (Byte.class.isAssignableFrom(type)) { + return (T) (Byte) getByte(columnIndex); + } else if (Short.class.isAssignableFrom(type)) { + return (T) (Short) getShort(columnIndex); + } else if (Integer.class.isAssignableFrom(type)) { + return (T) (Integer) getInt(columnIndex); + } else if (Long.class.isAssignableFrom(type)) { + return (T) (Long) getLong(columnIndex); + } else if (Float.class.isAssignableFrom(type)) { + return (T) (Float) getFloat(columnIndex); + } else if (Double.class.isAssignableFrom(type)) { + return (T) (Double) getDouble(columnIndex); + } else if (Date.class.isAssignableFrom(type)) { + return (T) getDate(columnIndex); + } else if (Date.class.isAssignableFrom(type)) { + return (T) getDate(columnIndex); + } else if (Time.class.isAssignableFrom(type)) { + return (T) getTime(columnIndex); + } else if (Timestamp.class.isAssignableFrom(type)) { + return (T) getTimestamp(columnIndex); + } else if (BigDecimal.class.isAssignableFrom(type)) { + return (T) getBigDecimal(columnIndex); } else { - return (T) getObject(columnIndex); + logger.debug( + "Unsupported type passed to getObject(int columnIndex,Class type): " + type.getName()); + throw new SQLException( + "Type passed to 'getObject(int columnIndex,Class type)' is unsupported. Type: " + + type.getName()); } } diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java index da91f2c28..51fd295e9 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java @@ -22,6 +22,7 @@ import java.sql.Clob; import java.sql.Connection; import java.sql.DatabaseMetaData; +import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; @@ -31,6 +32,9 @@ import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -1069,4 +1073,87 @@ public void testGetObjectForJSONResultFormatUsingJDBCDecimalAsInt() throws SQLEx } } } + + @Test + public void testGetObjectWithType() throws SQLException { + try (Connection connection = init(); + Statement statement = connection.createStatement()) { + statement.execute( + " CREATE OR REPLACE TABLE test_all_types (" + + " string VARCHAR, " + + " b TINYINT, " + + " s SMALLINT, " + + " i INTEGER, " + + " l BIGINT, " + + " f FLOAT, " + + " d DOUBLE, " + + " bd DOUBLE, " + + " bool BOOLEAN, " + + " timestampLtz TIMESTAMP_LTZ, " + + " timestampNtz TIMESTAMP_NTZ, " + + " timestampTz TIMESTAMP_TZ, " + + " date DATE," + + " time TIME " + + " )"); + statement.execute( + "insert into test_all_types values('aString',1,2,3,4,1.1,2.2,3.3, false, " + + "'2021-12-22 09:43:44','2021-12-22 09:43:44','2021-12-22 09:43:44', " + + "'2023-12-24','12:34:56')"); + + assertResultValueAndType(statement, "aString", "string", String.class); + assertResultValueAndType(statement, new Byte("1"), "b", Byte.class); + assertResultValueAndType(statement, Short.valueOf("2"), "s", Short.class); + assertResultValueAndType(statement, Integer.valueOf("2"), "s", Integer.class); + assertResultValueAndType(statement, Integer.valueOf("3"), "i", Integer.class); + assertResultValueAndType(statement, Long.valueOf("4"), "l", Long.class); + assertResultValueAndType(statement, BigDecimal.valueOf(4), "l", BigDecimal.class); + assertResultValueAndType(statement, Float.valueOf("1.1"), "f", Float.class); + assertResultValueAndType(statement, Double.valueOf("1.1"), "f", Double.class); + assertResultValueAndType(statement, Double.valueOf("2.2"), "d", Double.class); + assertResultValueAndType(statement, BigDecimal.valueOf(3.3), "bd", BigDecimal.class); + assertResultValueAndType(statement, "FALSE", "bool", String.class); + assertResultValueAndType(statement, Boolean.FALSE, "bool", Boolean.class); + assertResultValueAndType(statement, Long.valueOf(0), "bool", Long.class); + assertResultValueAsString( + statement, + new SnowflakeTimestampWithTimezone( + Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 9, 43, 44)), TimeZone.getDefault()), + "timestampLtz", + Timestamp.class); + assertResultValueAsString( + statement, + new SnowflakeTimestampWithTimezone( + Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 9, 43, 44)), TimeZone.getDefault()), + "timestampNtz", + Timestamp.class); + assertResultValueAsString( + statement, + new SnowflakeTimestampWithTimezone( + Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 9, 43, 44)), TimeZone.getDefault()), + "timestampTz", + Timestamp.class); + assertResultValueAndType( + statement, Date.valueOf(LocalDate.of(2023, 12, 24)), "date", Date.class); + assertResultValueAndType( + statement, Time.valueOf(LocalTime.of(12, 34, 56)), "time", Time.class); + } + } + + private void assertResultValueAndType( + Statement statement, Object expected, String columnName, Class type) throws SQLException { + try (ResultSet resultSetString = + statement.executeQuery(String.format("select %s from test_all_types", columnName))) { + resultSetString.next(); + assertEquals(expected, resultSetString.getObject(1, type)); + } + } + + private void assertResultValueAsString( + Statement statement, Object expected, String columnName, Class type) throws SQLException { + try (ResultSet resultSetString = + statement.executeQuery(String.format("select %s from test_all_types", columnName))) { + resultSetString.next(); + assertEquals(expected.toString(), resultSetString.getObject(1, type).toString()); + } + } }