/src/duckdb/src/main/capi/config-c.cpp
Line | Count | Source |
1 | | #include "duckdb/main/capi/capi_internal.hpp" |
2 | | #include "duckdb/main/config.hpp" |
3 | | #include "duckdb/common/types/value.hpp" |
4 | | #include "duckdb/main/extension_helper.hpp" |
5 | | |
6 | | using duckdb::DBConfig; |
7 | | using duckdb::Value; |
8 | | |
9 | | // config |
10 | 0 | duckdb_state duckdb_create_config(duckdb_config *out_config) { |
11 | 0 | if (!out_config) { |
12 | 0 | return DuckDBError; |
13 | 0 | } |
14 | 0 | try { |
15 | 0 | *out_config = nullptr; |
16 | 0 | auto config = new DBConfig(); |
17 | 0 | *out_config = reinterpret_cast<duckdb_config>(config); |
18 | 0 | config->SetOptionByName("duckdb_api", "capi"); |
19 | 0 | } catch (...) { // LCOV_EXCL_START |
20 | 0 | return DuckDBError; |
21 | 0 | } // LCOV_EXCL_STOP |
22 | 0 | return DuckDBSuccess; |
23 | 0 | } |
24 | | |
25 | 0 | size_t duckdb_config_count() { |
26 | 0 | return DBConfig::GetOptionCount() + DBConfig::GetAliasCount() + |
27 | 0 | duckdb::ExtensionHelper::ArraySize(duckdb::EXTENSION_SETTINGS); |
28 | 0 | } |
29 | | |
30 | 0 | duckdb_state duckdb_get_config_flag(size_t index, const char **out_name, const char **out_description) { |
31 | 0 | auto option = DBConfig::GetOptionByIndex(index); |
32 | 0 | if (option) { |
33 | 0 | if (out_name) { |
34 | 0 | *out_name = option->name; |
35 | 0 | } |
36 | 0 | if (out_description) { |
37 | 0 | *out_description = option->description; |
38 | 0 | } |
39 | 0 | return DuckDBSuccess; |
40 | 0 | } |
41 | | // alias |
42 | 0 | index -= DBConfig::GetOptionCount(); |
43 | 0 | auto alias = DBConfig::GetAliasByIndex(index); |
44 | 0 | if (alias) { |
45 | 0 | if (out_name) { |
46 | 0 | *out_name = alias->alias; |
47 | 0 | } |
48 | 0 | option = DBConfig::GetOptionByIndex(alias->option_index); |
49 | 0 | if (out_description) { |
50 | 0 | *out_description = option->description; |
51 | 0 | } |
52 | 0 | return DuckDBSuccess; |
53 | 0 | } |
54 | 0 | index -= DBConfig::GetAliasCount(); |
55 | | |
56 | | // extension index |
57 | 0 | auto entry = duckdb::ExtensionHelper::GetArrayEntry(duckdb::EXTENSION_SETTINGS, index); |
58 | 0 | if (!entry) { |
59 | 0 | return DuckDBError; |
60 | 0 | } |
61 | 0 | if (out_name) { |
62 | 0 | *out_name = entry->name; |
63 | 0 | } |
64 | 0 | if (out_description) { |
65 | 0 | *out_description = entry->extension; |
66 | 0 | } |
67 | 0 | return DuckDBSuccess; |
68 | 0 | } |
69 | | |
70 | 0 | duckdb_state duckdb_set_config(duckdb_config config, const char *name, const char *option) { |
71 | 0 | if (!config || !name || !option) { |
72 | 0 | return DuckDBError; |
73 | 0 | } |
74 | | |
75 | 0 | try { |
76 | 0 | auto db_config = (DBConfig *)config; |
77 | 0 | db_config->SetOptionByName(name, Value(option)); |
78 | 0 | } catch (...) { |
79 | 0 | return DuckDBError; |
80 | 0 | } |
81 | 0 | return DuckDBSuccess; |
82 | 0 | } |
83 | | |
84 | 0 | void duckdb_destroy_config(duckdb_config *config) { |
85 | 0 | if (!config) { |
86 | 0 | return; |
87 | 0 | } |
88 | 0 | if (*config) { |
89 | 0 | auto db_config = (DBConfig *)*config; |
90 | 0 | delete db_config; |
91 | 0 | *config = nullptr; |
92 | 0 | } |
93 | 0 | } |