1#include "duckdb/main/capi/capi_internal.hpp"
2
3void duckdb_destroy_value(duckdb_value *value) {
4 if (value && *value) {
5 auto val = reinterpret_cast<duckdb::Value *>(*value);
6 delete val;
7 *value = nullptr;
8 }
9}
10
11duckdb_value duckdb_create_varchar_length(const char *text, idx_t length) {
12 return reinterpret_cast<duckdb_value>(new duckdb::Value(std::string(text, length)));
13}
14
15duckdb_value duckdb_create_varchar(const char *text) {
16 return duckdb_create_varchar_length(text, length: strlen(s: text));
17}
18
19duckdb_value duckdb_create_int64(int64_t input) {
20 auto val = duckdb::Value::BIGINT(value: input);
21 return reinterpret_cast<duckdb_value>(new duckdb::Value(val));
22}
23
24char *duckdb_get_varchar(duckdb_value value) {
25 auto val = reinterpret_cast<duckdb::Value *>(value);
26 auto str_val = val->DefaultCastAs(target_type: duckdb::LogicalType::VARCHAR);
27 auto &str = duckdb::StringValue::Get(value: str_val);
28
29 auto result = reinterpret_cast<char *>(malloc(size: sizeof(char *) * (str.size() + 1)));
30 memcpy(dest: result, src: str.c_str(), n: str.size());
31 result[str.size()] = '\0';
32 return result;
33}
34
35int64_t duckdb_get_int64(duckdb_value value) {
36 auto val = reinterpret_cast<duckdb::Value *>(value);
37 if (!val->DefaultTryCastAs(target_type: duckdb::LogicalType::BIGINT)) {
38 return 0;
39 }
40 return duckdb::BigIntValue::Get(value: *val);
41}
42