Skip to content

Commit

Permalink
SNOW-1232333 - add mapping for basic types in method getObject (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pmotacki authored Mar 18, 2024
1 parent 2d14efc commit 066e25a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,38 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
} else if (Map.class.isAssignableFrom(type)) {
JsonNode jsonNode = ((JsonSqlInput) getObject(columnIndex)).getInput();
return (T) OBJECT_MAPPER.convertValue(jsonNode, new TypeReference<Map<String, Object>>() {});
} 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<T> type): " + type.getName());
throw new SQLException(
"Type passed to 'getObject(int columnIndex,Class<T> type)' is unsupported. Type: "
+ type.getName());
}
}

Expand Down
87 changes: 87 additions & 0 deletions src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 066e25a

Please sign in to comment.