@@ -44,8 +44,12 @@ void DuckdbConnection::onError(
4444DuckdbConnection::DuckdbConnection (
4545 trantor::EventLoop *loop,
4646 const std::string &connInfo,
47- const std::shared_ptr<SharedMutex> &sharedMutex)
48- : DbConnection(loop), sharedMutexPtr_(sharedMutex), connInfo_(connInfo)
47+ const std::shared_ptr<SharedMutex> &sharedMutex,
48+ const std::unordered_map<std::string, std::string> &configOptions) // 新增配置参数支持 [dq 2025-11-19]
49+ : DbConnection(loop),
50+ sharedMutexPtr_(sharedMutex),
51+ connInfo_(connInfo),
52+ configOptions_(configOptions) // 存储配置选项 [dq 2025-11-19]
4953{
5054}
5155
@@ -74,17 +78,56 @@ void DuckdbConnection::init()
7478
7579 loop_->runInLoop ([this , filename = std::move (filename)]() {
7680 duckdb_database db;
81+ duckdb_config config;
7782 duckdb_connection conn;
7883
84+ // create the configuration object
85+ if (duckdb_create_config (&config) == DuckDBError) {
86+ LOG_FATAL << " Failed to config DuckDB database: " << filename;
87+ auto thisPtr = shared_from_this ();
88+ closeCallback_ (thisPtr);
89+ return ;
90+ }
91+
92+ // 应用配置选项(使用传入的配置,或使用默认值)[dq 2025-11-19]
93+ // 定义默认值
94+ std::unordered_map<std::string, std::string> defaultConfig = {
95+ {" access_mode" , " READ_WRITE" },
96+ {" threads" , " 4" }, // 默认改为4以避免过度占用资源
97+ {" max_memory" , " 4GB" },
98+ };
99+
100+ // 合并用户配置和默认配置(用户配置优先)
101+ for (const auto & [key, defaultValue] : defaultConfig) {
102+ auto it = configOptions_.find (key);
103+ const std::string& value = (it != configOptions_.end ()) ? it->second : defaultValue;
104+
105+ if (duckdb_set_config (config, key.c_str (), value.c_str ()) == DuckDBError) {
106+ LOG_WARN << " Failed to set DuckDB config option: " << key << " =" << value;
107+ }
108+ }
109+
110+ // 应用其他用户自定义的配置选项
111+ for (const auto & [key, value] : configOptions_) {
112+ if (defaultConfig.find (key) == defaultConfig.end ()) {
113+ if (duckdb_set_config (config, key.c_str (), value.c_str ()) == DuckDBError) {
114+ LOG_WARN << " Failed to set DuckDB config option: " << key << " =" << value;
115+ }
116+ }
117+ }
118+
79119 // 打开数据库
80- auto state = duckdb_open (filename.c_str (), &db);
120+ // auto state = duckdb_open(filename.c_str(), &db);
121+ auto state = duckdb_open_ext (filename.c_str (), &db, config, NULL );
81122 if (state == DuckDBError)
82123 {
83124 LOG_FATAL << " Failed to open DuckDB database: " << filename;
125+ duckdb_destroy_config (&config);
84126 auto thisPtr = shared_from_this ();
85127 closeCallback_ (thisPtr);
86128 return ;
87129 }
130+ duckdb_destroy_config (&config);
88131
89132 // 创建连接
90133 state = duckdb_connect (db, &conn);
0 commit comments