Skip to content

Commit c0e947f

Browse files
authored
Merge pull request #183 from staticlibs/typemap_stub
Allow to use empty typeMap on Connection
2 parents bc08dd5 + 87aa682 commit c0e947f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

+14-1
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

+13
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)