Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIR-33431: implemented getObject with type map #421

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,21 @@ 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();
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) {
return JDBCType.valueOf(dataType.getSqlType()).getName();
}

@Override
Expand All @@ -975,10 +989,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 Expand Up @@ -1209,6 +1217,7 @@ void shouldGetDouble() throws SQLException {
assertEquals(1.23, resultSet.getDouble(3));

assertEquals(1.23456789012, resultSet.getObject(4, Double.class));
assertEquals(1.23456789012, (double)resultSet.getObject(4, Map.of("double precision", Double.class)), 0.01);
assertEquals(new BigDecimal("1.23456789012"), resultSet.getBigDecimal(4));

assertEquals(1231232.123459999990457054844258706536, resultSet.getObject(5, Double.class), 0.01);
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/firebolt/jdbc/type/FireboltDataTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.firebolt.jdbc.type;


import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import java.sql.JDBCType;

import static org.junit.jupiter.api.Assertions.assertNotNull;

class FireboltDataTypeTest {
/**
* This test validates that {@link FireboltDataType#getSqlType()} return standard type defined in {@link java.sql.Types} and {@link JDBCType}.
* @param type - the type
*/
@ParameterizedTest
@EnumSource(FireboltDataType.class)
void sqlType(FireboltDataType type) {
// assert here is just to satisfy static code analysis. valueOf() either succeeds or throws IllegalArgumentException
assertNotNull(JDBCType.valueOf(type.getSqlType()));
}
}
Loading