Skip to content
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
34 changes: 34 additions & 0 deletions src/jni/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ static duckdb::Value jobj_to_value(JNIEnv *env, const std::string &key, jobject
}
}

static bool is_allowed_paths_option(const std::string &key) {
return duckdb::StringUtil::CIEquals(key, "allowed_directories") ||
duckdb::StringUtil::CIEquals(key, "allowed_paths");
}

// DuckDB refuses to set 'allowed_directories' and 'allowed_paths' through
// SetOptionByName before the database is started. Set them directly on the
// DBConfig instead, its options are copied to the database on startup.
// The check of 'enable_external_access' is skipped intentionally, at startup
// these options are commonly used together with 'enable_external_access=false'
// and the order in which properties are applied is not defined.
static void set_allowed_paths_option(duckdb::DBConfig &config, const std::string &key, const duckdb::Value &value) {
if (value.IsNull()) {
throw duckdb::InvalidInputException("%s setting cannot be NULL", key);
}
auto list_value = value.DefaultCastAs(duckdb::LogicalType::LIST(duckdb::LogicalType::VARCHAR));
duckdb::vector<std::string> paths;
for (auto &val : duckdb::ListValue::GetChildren(list_value)) {
paths.push_back(val.GetValue<std::string>());
}
if (duckdb::StringUtil::CIEquals(key, "allowed_directories")) {
config.SetAllowedDirectories(paths);
} else {
config.SetAllowedPaths(paths);
}
// keep the option in 'user_options' like SetOptionByName does for a user config,
// it is used by the instance cache to compare configs
config.options.user_options[duckdb::Identifier(key)] = value;
}

std::unique_ptr<duckdb::DBConfig> create_db_config(JNIEnv *env, jboolean read_only, jobject java_config) {
auto config = std::unique_ptr<duckdb::DBConfig>(new duckdb::DBConfig());
// Required for setting like 'allowed_directories' that use
Expand Down Expand Up @@ -117,6 +147,10 @@ std::unique_ptr<duckdb::DBConfig> create_db_config(JNIEnv *env, jboolean read_on
env->DeleteLocalRef(value);

try {
if (is_allowed_paths_option(key_str)) {
set_allowed_paths_option(*config, key_str, dvalue);
continue;
}
config->SetOptionByName(duckdb::Identifier(key_str), dvalue);
} catch (const std::exception &e) {
duckdb::ErrorData error(e);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/duckdb/TestClosure.java
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ public static void test_open_connection_factories_return_objects() throws Except
}
}

@SuppressWarnings("try")
public static void test_closed_connection_getMetaData_throws() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
conn.close();
Expand All @@ -696,6 +697,7 @@ public static void test_closed_connection_getMetaData_throws() throws Exception
}
}

@SuppressWarnings("try")
public static void test_closed_connection_createArrayOf_throws() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
conn.close();
Expand All @@ -710,6 +712,7 @@ public static void test_closed_connection_createArrayOf_throws() throws Exceptio
}
}

@SuppressWarnings("try")
public static void test_closed_connection_createStruct_throws() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
conn.close();
Expand Down
Loading