Skip to content

Commit 87aa682

Browse files
committed
Allow to use empty typeMap on Connection
`Connection#getTypeMap()` and `setTypeMap()` are two legacy methods that were added to Java in 1998 to support mapping composite DB fields to custom Java classes. With widespread use or object-relational mapping libraries (like Hibernate) these method remain virtually unused in Java applications. Still these method may be called by some tools, for example, when doing DB schema introspection. Looking how these methods work in other JDBC drivers, we can see that in Postgres' `pgjdbc` the type map is accepted, but not used for actual field mapping. And in SQL Server's `mssql-jdbc` only empty type map is accepted and empty type map is always returned. It is proposed to follow SQL Server's approach to not throw on `getTypeMap()` and to allow empty map input in `setTypeMap()`. Note: CMake change is unrelated, it is added to unbreak CI builds before duckdb#178 fix (or its variation from other PRs) is merged. Testing: new test added that checks that empty type map is allowed. Fixes: duckdb#150
1 parent 9cbfad4 commit 87aa682

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/org/duckdb/DuckDBConnection.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.sql.Savepoint;
2121
import java.sql.Statement;
2222
import java.sql.Struct;
23+
import java.util.HashMap;
2324
import java.util.Map;
2425
import java.util.Properties;
2526
import java.util.concurrent.Executor;
@@ -257,10 +258,22 @@ public CallableStatement prepareCall(String sql, int resultSetType, int resultSe
257258
}
258259

259260
public Map<String, Class<?>> getTypeMap() throws SQLException {
260-
throw new SQLFeatureNotSupportedException("getTypeMap");
261+
if (isClosed()) {
262+
throw new SQLException("Connection was closed");
263+
}
264+
return new HashMap<>();
261265
}
262266

263267
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
268+
if (isClosed()) {
269+
throw new SQLException("Connection was closed");
270+
}
271+
if (map != null && (map instanceof java.util.HashMap)) {
272+
// we return an empty Hash map if the user gives this back make sure we accept it.
273+
if (map.isEmpty()) {
274+
return;
275+
}
276+
}
264277
throw new SQLFeatureNotSupportedException("setTypeMap");
265278
}
266279

src/test/java/org/duckdb/TestDuckDBJDBC.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4793,6 +4793,19 @@ public static void test_client_config_retained_on_dup() throws Exception {
47934793
}
47944794
}
47954795

4796+
public static void test_empty_typemap_allowed() throws Exception {
4797+
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
4798+
Map<String, Class<?>> defaultMap = conn.getTypeMap();
4799+
assertEquals(defaultMap.size(), 0);
4800+
// check empty not throws
4801+
conn.setTypeMap(new HashMap<>());
4802+
// check custom map throws
4803+
Map<String, Class<?>> customMap = new HashMap<>();
4804+
customMap.put("foo", TestDuckDBJDBC.class);
4805+
assertThrows(() -> { conn.setTypeMap(customMap); }, SQLException.class);
4806+
}
4807+
}
4808+
47964809
public static void main(String[] args) throws Exception {
47974810
String arg1 = args.length > 0 ? args[0] : "";
47984811
final int statusCode;

0 commit comments

Comments
 (0)