Skip to content

Commit 53e26ca

Browse files
committed
Add UUID conversion to class-specific rs get (1.3)
This is a backport of the PR duckdb#243 to `v1.3-ossivalis` stable branch. Result set method `getObject(idx, class)` requires the driver to convert the requested value to the specified class. This PR `java.util.UUID` conversion that was missed there. Fixes: duckdb#240
1 parent 3702a9e commit 53e26ca

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/main/java/org/duckdb/DuckDBResultSet.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,10 +1359,16 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
13591359
throw new SQLException("Can't convert value to Blob, Java type: " + type + ", SQL type: " + sqlType);
13601360
// return type.cast(getLocalDateTime(columnIndex));
13611361
} else {
1362-
throw new SQLException("Can't convert value to Blob " + type);
1362+
throw new SQLException("Can't convert value to Blob, SQL type: " + sqlType);
1363+
}
1364+
} else if (type == UUID.class) {
1365+
if (sqlType == DuckDBColumnType.UUID || sqlType == DuckDBColumnType.VARCHAR) {
1366+
return type.cast(getUuid(columnIndex));
1367+
} else {
1368+
throw new SQLException("Can't convert value to UUID, SQL type: " + sqlType);
13631369
}
13641370
} else {
1365-
throw new SQLException("Can't convert value to " + type + " " + type);
1371+
throw new SQLException("Can't convert value to " + type + ", SQL type: " + sqlType);
13661372
}
13671373
}
13681374

src/test/java/org/duckdb/TestResults.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.sql.*;
1010
import java.time.LocalDateTime;
1111
import java.util.Properties;
12+
import java.util.UUID;
1213

1314
public class TestResults {
1415

@@ -99,13 +100,14 @@ public static void test_duckdb_get_object_with_class() throws Exception {
99100
"CREATE TABLE b (vchar VARCHAR, bo BOOLEAN, sint SMALLINT, nint INTEGER, bigi BIGINT,"
100101
+ " flt FLOAT, dbl DOUBLE, dte DATE, tme TIME, ts TIMESTAMP, dec16 DECIMAL(3,1),"
101102
+ " dec32 DECIMAL(9,8), dec64 DECIMAL(16,1), dec128 DECIMAL(30,10), tint TINYINT, utint UTINYINT,"
102-
+ " usint USMALLINT, uint UINTEGER, ubig UBIGINT, hin HUGEINT, uhin UHUGEINT, blo BLOB)");
103+
+ " usint USMALLINT, uint UINTEGER, ubig UBIGINT, hin HUGEINT, uhin UHUGEINT, blo BLOB, uid UUID)");
103104
stmt.execute(
104105
"INSERT INTO b VALUES ('varchary', true, 6, 42, 666, 42.666, 666.42,"
105106
+
106107
" '1970-01-02', '01:00:34', '1970-01-03 03:42:23', 42.2, 1.23456789, 987654321012345.6, 111112222233333.44444, "
107108
+ " -4, 200, 50001, 4000111222, 18446744073709551615, 18446744073709551616, "
108-
+ " 170141183460469231731687303715884105728, 'yeah'::BLOB)");
109+
+ " 170141183460469231731687303715884105728, 'yeah'::BLOB, "
110+
+ "'5b7bce70-4238-43d1-81d2-6e62f23bf9bd'::UUID)");
109111

110112
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM b"); ResultSet rs = ps.executeQuery()) {
111113
rs.next();
@@ -125,8 +127,15 @@ public static void test_duckdb_get_object_with_class() throws Exception {
125127
assertEquals(rs.getBigDecimal(12), rs.getObject(12, BigDecimal.class));
126128
assertEquals(rs.getBigDecimal(13), rs.getObject(13, BigDecimal.class));
127129
assertEquals(rs.getBigDecimal(14), rs.getObject(14, BigDecimal.class));
130+
assertEquals(rs.getObject(23), rs.getObject(23, UUID.class));
128131
}
129132
}
133+
134+
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement();
135+
ResultSet rs = stmt.executeQuery("SELECT '5b7bce70-4238-43d1-81d2-6e62f23bf9bd'")) {
136+
rs.next();
137+
assertEquals(UUID.fromString(rs.getString(1)), rs.getObject(1, UUID.class));
138+
}
130139
}
131140

132141
public static void test_duckdb_get_object_with_class_null() throws Exception {

0 commit comments

Comments
 (0)