I would like read call directly duckdb c-api, but I have some difficulties with structs. I try to do the following c-code in Julia.
#include "duckdb.h"
#include <stdio.h>
int main() {
duckdb_database db = NULL;
duckdb_connection con = NULL;
duckdb_result result;
if (duckdb_open(NULL, &db) == DuckDBError) {
fprintf(stderr, "Failed to open database\n");
goto cleanup;
}
if (duckdb_connect(db, &con) == DuckDBError) {
fprintf(stderr, "Failed to open connection\n");
goto cleanup;
}
if (duckdb_query(con, "CREATE TABLE integers(i INTEGER, j INTEGER);", NULL) == DuckDBError) {
fprintf(stderr, "Failed to query database\n");
goto cleanup;
}
if (duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", NULL) == DuckDBError) {
fprintf(stderr, "Failed to query database\n");
goto cleanup;
}
if (duckdb_query(con, "SELECT * FROM integers", &result) == DuckDBError) {
fprintf(stderr, "Failed to query database\n");
goto cleanup;
}
// print the names of the result
for (size_t i = 0; i < result.column_count; i++) {
printf("%s ", result.columns[i].name);
}
printf("\n");
// print the data of the result
for (size_t row_idx = 0; row_idx < result.row_count; row_idx++) {
for (size_t col_idx = 0; col_idx < result.column_count; col_idx++) {
char *val = duckdb_value_varchar(&result, col_idx, row_idx);
printf("%s ", val);
duckdb_free(val);
}
printf("\n");
}
// duckdb_print_result(result);
cleanup:
duckdb_destroy_result(&result);
duckdb_disconnect(&con);
duckdb_close(&db);
}
I’m able to log in and read the first element but I don’t know how to read array of duckdb_columns
libduckdb="/home/kimmo/duckdb/build/release/src/libduckdb.so"
handle = Ref{Ptr{Cvoid}}();
ccall( (:duckdb_open,libduckdb),Cint,(Ptr{UInt8},Ptr{Cvoid}),":memory:",handle)
con = Ref{Ptr{Cvoid}}();
ccall( (:duckdb_connect,libduckdb),Cint,(Ptr{Cvoid},Ptr{Cvoid}),handle[],con)
struct duckdb_type
duck_type::Ptr{Cint}
end
struct duckdb_column
data::Ptr{Cvoid}
nullmask::Ptr{Cint}
type::Ptr{duckdb_type}
name::Ptr{UInt8}
internal_data::Ptr{Cvoid}
end
struct duckdb_result
column_count::Ptr{UInt64}
row_count::Ptr{UInt64}
rows_changed::Ptr{UInt64}
columns::Ptr{duckdb_column}
error_message::Ptr{UInt8}
internal_data::Ptr{Cvoid}
end
ccall( (:duckdb_query,libduckdb),Cint,(Ptr{Cvoid},Ptr{UInt8},Ptr{Cvoid}),con[],"CREATE TABLE integers(icol INTEGER, jcol INTEGER);",C_NULL)
ccall( (:duckdb_query,libduckdb),Cint,(Ptr{Cvoid},Ptr{UInt8},Ptr{Cvoid}),con[],"INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);",C_NULL)
res = Ref{duckdb_result}();
ccall( (:duckdb_query,libduckdb),Cint,(Ptr{Cvoid},Ptr{UInt8},Ptr{Cvoid}),con[],"SELECT * FROM integers",res)
cols=unsafe_load(res[].columns);
unsafe_string(cols.name)