1//===----------------------------------------------------------------------===//
2//
3// DuckDB
4//
5// duckdb.h
6//
7// Author: Mark Raasveldt
8//
9//===----------------------------------------------------------------------===//
10
11#pragma once
12
13#include <stdbool.h>
14#include <stdint.h>
15#include <stdlib.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21typedef uint64_t idx_t;
22
23typedef enum DUCKDB_TYPE {
24 DUCKDB_TYPE_INVALID = 0,
25 // bool
26 DUCKDB_TYPE_BOOLEAN,
27 // int8_t
28 DUCKDB_TYPE_TINYINT,
29 // int16_t
30 DUCKDB_TYPE_SMALLINT,
31 // int32_t
32 DUCKDB_TYPE_INTEGER,
33 // int64_t
34 DUCKDB_TYPE_BIGINT,
35 // float
36 DUCKDB_TYPE_FLOAT,
37 // double
38 DUCKDB_TYPE_DOUBLE,
39 // duckdb_timestamp
40 DUCKDB_TYPE_TIMESTAMP,
41 // duckdb_date
42 DUCKDB_TYPE_DATE,
43 // duckdb_time
44 DUCKDB_TYPE_TIME,
45 // const char*
46 DUCKDB_TYPE_VARCHAR
47} duckdb_type;
48
49typedef struct {
50 int32_t year;
51 int8_t month;
52 int8_t day;
53} duckdb_date;
54
55typedef struct {
56 int8_t hour;
57 int8_t min;
58 int8_t sec;
59 int16_t msec;
60} duckdb_time;
61
62typedef struct {
63 duckdb_date date;
64 duckdb_time time;
65} duckdb_timestamp;
66
67typedef struct {
68 void *data;
69 bool *nullmask;
70 duckdb_type type;
71 char *name;
72} duckdb_column;
73
74typedef struct {
75 idx_t column_count;
76 idx_t row_count;
77 duckdb_column *columns;
78 char *error_message;
79} duckdb_result;
80
81// typedef struct {
82// void *data;
83// bool *nullmask;
84// } duckdb_column_data;
85
86// typedef struct {
87// int column_count;
88// int count;
89// duckdb_column_data *columns;
90// } duckdb_chunk;
91
92typedef void *duckdb_database;
93typedef void *duckdb_connection;
94typedef void *duckdb_prepared_statement;
95
96typedef enum { DuckDBSuccess = 0, DuckDBError = 1 } duckdb_state;
97
98//! Opens a database file at the given path (nullptr for in-memory). Returns DuckDBSuccess on success, or DuckDBError on
99//! failure. [OUT: database]
100duckdb_state duckdb_open(const char *path, duckdb_database *out_database);
101//! Closes the database.
102void duckdb_close(duckdb_database *database);
103
104//! Creates a connection to the specified database. [OUT: connection]
105duckdb_state duckdb_connect(duckdb_database database, duckdb_connection *out_connection);
106//! Closes the specified connection handle
107void duckdb_disconnect(duckdb_connection *connection);
108
109//! Executes the specified SQL query in the specified connection handle. [OUT: result descriptor]
110duckdb_state duckdb_query(duckdb_connection connection, const char *query, duckdb_result *out_result);
111//! Destroys the specified result
112void duckdb_destroy_result(duckdb_result *result);
113
114// SAFE fetch functions
115// These functions will perform conversions if necessary. On failure (e.g. if conversion cannot be performed) a special
116// value is returned.
117
118//! Converts the specified value to a bool. Returns false on failure or NULL.
119bool duckdb_value_boolean(duckdb_result *result, idx_t col, idx_t row);
120//! Converts the specified value to an int8_t. Returns 0 on failure or NULL.
121int8_t duckdb_value_int8(duckdb_result *result, idx_t col, idx_t row);
122//! Converts the specified value to an int16_t. Returns 0 on failure or NULL.
123int16_t duckdb_value_int16(duckdb_result *result, idx_t col, idx_t row);
124//! Converts the specified value to an int64_t. Returns 0 on failure or NULL.
125int32_t duckdb_value_int32(duckdb_result *result, idx_t col, idx_t row);
126//! Converts the specified value to an int64_t. Returns 0 on failure or NULL.
127int64_t duckdb_value_int64(duckdb_result *result, idx_t col, idx_t row);
128//! Converts the specified value to a float. Returns 0.0 on failure or NULL.
129float duckdb_value_float(duckdb_result *result, idx_t col, idx_t row);
130//! Converts the specified value to a double. Returns 0.0 on failure or NULL.
131double duckdb_value_double(duckdb_result *result, idx_t col, idx_t row);
132//! Converts the specified value to a string. Returns nullptr on failure or NULL. The result must be freed with free.
133char *duckdb_value_varchar(duckdb_result *result, idx_t col, idx_t row);
134
135// Prepared Statements
136
137//! prepares the specified SQL query in the specified connection handle. [OUT: prepared statement descriptor]
138duckdb_state duckdb_prepare(duckdb_connection connection, const char *query,
139 duckdb_prepared_statement *out_prepared_statement);
140
141duckdb_state duckdb_nparams(duckdb_prepared_statement prepared_statement, idx_t *nparams_out);
142
143//! binds parameters to prepared statement
144duckdb_state duckdb_bind_boolean(duckdb_prepared_statement prepared_statement, idx_t param_idx, bool val);
145duckdb_state duckdb_bind_int8(duckdb_prepared_statement prepared_statement, idx_t param_idx, int8_t val);
146duckdb_state duckdb_bind_int16(duckdb_prepared_statement prepared_statement, idx_t param_idx, int16_t val);
147duckdb_state duckdb_bind_int32(duckdb_prepared_statement prepared_statement, idx_t param_idx, int32_t val);
148duckdb_state duckdb_bind_int64(duckdb_prepared_statement prepared_statement, idx_t param_idx, int64_t val);
149duckdb_state duckdb_bind_float(duckdb_prepared_statement prepared_statement, idx_t param_idx, float val);
150duckdb_state duckdb_bind_double(duckdb_prepared_statement prepared_statement, idx_t param_idx, double val);
151duckdb_state duckdb_bind_varchar(duckdb_prepared_statement prepared_statement, idx_t param_idx, const char *val);
152duckdb_state duckdb_bind_null(duckdb_prepared_statement prepared_statement, idx_t param_idx);
153
154//! Executes the prepared statements with currently bound parameters
155duckdb_state duckdb_execute_prepared(duckdb_prepared_statement prepared_statement, duckdb_result *out_result);
156
157//! Destroys the specified prepared statement descriptor
158void duckdb_destroy_prepare(duckdb_prepared_statement *prepared_statement);
159
160#ifdef __cplusplus
161};
162#endif
163