-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathduckdb_stubs.cpp
More file actions
68 lines (53 loc) · 1.9 KB
/
Copy pathduckdb_stubs.cpp
File metadata and controls
68 lines (53 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "duckdb.hpp"
#include <cstring>
extern "C" int duckdb_has_active_transaction(duckdb_connection conn) {
try {
auto *conn_ptr = reinterpret_cast<duckdb::Connection *>(conn);
return conn_ptr->context->transaction.HasActiveTransaction() ? 1 : 0;
} catch (...) {
return 0;
}
}
extern "C" char *duckdb_get_json_string(duckdb_connection conn, duckdb_vector vec, idx_t row) {
if (!vec) return NULL;
try {
auto *conn_ptr = reinterpret_cast<duckdb::Connection *>(conn);
auto &context = *conn_ptr->context;
auto *vec_ptr = reinterpret_cast<duckdb::Vector *>(vec);
auto value = vec_ptr->GetValue(row);
if (value.IsNull()) return NULL;
auto json_str = value.CastAs(context, duckdb::LogicalType::JSON()).ToString();
// If the JSON result starts with '"', the variant contained a VARCHAR value
// (DuckDB wraps VARCHAR content in a JSON string). Fall back to VARCHAR cast
// to preserve the raw content for php_json_decode_ex in the caller.
if (!json_str.empty() && json_str[0] == '"') {
json_str = value.CastAs(context, duckdb::LogicalType::VARCHAR).ToString();
}
auto *result = (char *)duckdb_malloc(json_str.size() + 1);
if (result) {
memcpy(result, json_str.c_str(), json_str.size());
result[json_str.size()] = '\0';
}
return result;
} catch (...) {
return NULL;
}
}
extern "C" char *duckdb_get_string(duckdb_connection conn, duckdb_vector vec, idx_t row) {
if (!vec) return NULL;
try {
auto *vec_ptr = reinterpret_cast<duckdb::Vector *>(vec);
auto value = vec_ptr->GetValue(row);
if (value.IsNull()) return NULL;
auto *conn_ptr = reinterpret_cast<duckdb::Connection *>(conn);
auto str = value.CastAs(*conn_ptr->context, duckdb::LogicalType::VARCHAR).ToString();
auto *result = (char *)duckdb_malloc(str.size() + 1);
if (result) {
memcpy(result, str.c_str(), str.size());
result[str.size()] = '\0';
}
return result;
} catch (...) {
return NULL;
}
}