Skip to content

Commit

Permalink
FIR-33431: implemented getObject with type map
Browse files Browse the repository at this point in the history
  • Loading branch information
alexradzin committed Jun 2, 2024
1 parent 908125b commit 1bd18d8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,19 @@ Stream<Arguments> numericTypes() {
(CheckedTriFunction<PreparedStatement, Integer, Integer, Void>) (s, i, v) -> {
s.setLong(i, v.longValue());
return null;
}, (CheckedBiFunction<ResultSet, Integer, Number>) (rs, i) -> (int) rs.getLong(i))
}, (CheckedBiFunction<ResultSet, Integer, Number>) (rs, i) -> (int) rs.getLong(i)),

Arguments.of("getObject(Long.class)",
(CheckedTriFunction<PreparedStatement, Integer, Integer, Void>) (s, i, v) -> {
s.setLong(i, v.longValue());
return null;
}, (CheckedBiFunction<ResultSet, Integer, Number>) (rs, i) -> rs.getObject(i, Long.class).intValue()),

Arguments.of("getObject(i, java.util.Map.of(\"long\", Integer.class)",
(CheckedTriFunction<PreparedStatement, Integer, Integer, Void>) (s, i, v) -> {
s.setLong(i, v.longValue());
return null;
}, (CheckedBiFunction<ResultSet, Integer, Number>) (rs, i) -> (int) rs.getObject(i, java.util.Map.of("long", Integer.class)))
);
}

Expand Down
29 changes: 23 additions & 6 deletions src/main/java/com/firebolt/jdbc/resultset/FireboltResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.JDBCType;
import java.sql.NClob;
import java.sql.Ref;
import java.sql.ResultSet;
Expand All @@ -50,13 +51,15 @@
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.firebolt.jdbc.type.BaseType.isNull;
import static com.firebolt.jdbc.util.StringUtil.splitAll;
Expand Down Expand Up @@ -951,10 +954,26 @@ public Statement getStatement() {
}

@Override
@NotImplemented
@ExcludeFromJacocoGeneratedReport
public Object getObject(int columnIndex, Map<String, Class<?>> map) throws SQLException {
throw new FireboltSQLFeatureNotSupportedException();
FireboltDataType dataType = resultSetMetaData.getColumn(columnIndex).getType().getDataType();
BaseType columnType = dataType.getBaseType();
Map<String, Class<?>> caseInsensitiveMap = new TreeMap<>(CASE_INSENSITIVE_ORDER);
caseInsensitiveMap.putAll(map);
Class<?> type = getAllNames(dataType).map(caseInsensitiveMap::get).filter(Objects::nonNull).findFirst()
.orElseThrow(() -> new FireboltException(format("Cannot find type %s in provided types map", dataType)));
return getObject(columnIndex, type);
}

private Stream<String> getAllNames(FireboltDataType dataType) {
return Stream.concat(Stream.of(dataType.getDisplayName(), getJdbcType(dataType)).filter(Objects::nonNull), Stream.of(dataType.getAliases()));
}

private String getJdbcType(FireboltDataType dataType) {
try {
return JDBCType.valueOf(dataType.getSqlType()).getName();
} catch (IllegalArgumentException e) {
return null;
}
}

@Override
Expand All @@ -975,10 +994,8 @@ public Clob getClob(int columnIndex) throws SQLException {
}

@Override
@NotImplemented
@ExcludeFromJacocoGeneratedReport
public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQLException {
throw new FireboltSQLFeatureNotSupportedException();
return getObject(findColumn(columnLabel), map);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -192,7 +193,7 @@ void unsupported() throws SQLException {
assertThrows(SQLFeatureNotSupportedException.class, () -> resultSet.updateFloat("label", 0.0f));
assertThrows(SQLFeatureNotSupportedException.class, () -> resultSet.updateDouble("label", 0.0));
assertThrows(SQLFeatureNotSupportedException.class, () -> resultSet.updateBigDecimal("label", new BigDecimal(0)));
;

assertThrows(SQLFeatureNotSupportedException.class, () -> resultSet.updateString("label", ""));
assertThrows(SQLFeatureNotSupportedException.class, () -> resultSet.updateBytes("label", new byte[0]));
assertThrows(SQLFeatureNotSupportedException.class, () -> resultSet.updateDate("label", new Date(0)));
Expand Down Expand Up @@ -428,6 +429,11 @@ void shouldReturnInt() throws SQLException {
assertEquals(1, resultSet.getInt(1));
assertEquals(1, resultSet.getInt("id"));
assertEquals(1, resultSet.getObject(1, Long.class));
assertEquals(1L, resultSet.getObject(1, Map.of("int", Long.class)));
assertEquals(1L, resultSet.getObject("id", Map.of("INTEGER", Long.class)));
assertEquals(1., resultSet.getObject(1, Map.of("int32", Double.class)));
assertThrows(SQLException.class, () -> resultSet.getObject(1, Map.of("real", Double.class))); // exising type that does not match column type
assertThrows(SQLException.class, () -> resultSet.getObject(1, Map.of("notatype", Double.class))); // type alias that does not exist

resultSet.next();
assertEquals(2, resultSet.getInt(1));
Expand All @@ -446,6 +452,8 @@ void shouldReturnFloat() throws SQLException {
assertEquals(14.6f, resultSet.getFloat(6));
assertEquals(14.6f, resultSet.getFloat("a_double"));
assertEquals(14.6f, resultSet.getObject(6, Float.class));
assertEquals(14.6, resultSet.getObject(6, Map.of("Float32", Double.class)));
assertEquals((short)14, resultSet.getObject(6, Map.of("Float32", Short.class)));

resultSet.next();
assertEquals(0, resultSet.getFloat(6));
Expand Down

0 comments on commit 1bd18d8

Please sign in to comment.