11#pragma once
22
33#ifdef __cplusplus
4- # include < cstddef>
5- # include < cstdint>
4+ #include < cstddef>
5+ #include < cstdint>
66extern " C" {
77#else
8- # include < stdbool.h>
9- # include < stddef.h>
10- # include < stdint.h>
8+ #include < stdbool.h>
9+ #include < stddef.h>
10+ #include < stdint.h>
1111#endif
1212
1313#define CHDB_EXPORT __attribute__ ((visibility(" default" )))
14+
15+ #ifndef CHDB_NO_DEPRECATED
16+ // WARNING: The following structs are deprecated and will be removed in a future version.
1417struct local_result
1518{
1619 char * buf;
@@ -45,12 +48,6 @@ struct local_result_v2
4548};
4649#endif
4750
48- CHDB_EXPORT struct local_result * query_stable (int argc, char ** argv);
49- CHDB_EXPORT void free_result (struct local_result * result);
50-
51- CHDB_EXPORT struct local_result_v2 * query_stable_v2 (int argc, char ** argv);
52- CHDB_EXPORT void free_result_v2 (struct local_result_v2 * result);
53-
5451/* *
5552 * Connection structure for chDB
5653 * Contains server instance, connection state, and query processing queue
@@ -62,10 +59,37 @@ struct chdb_conn
6259 void * queue; /* Query processing queue */
6360};
6461
62+ #endif
63+
64+ // Opaque handle for query results.
65+ // Internal data structure managed by chDB implementation.
66+ // Users should only interact through API functions.
67+ typedef struct {
68+ void * internal_data;
69+ } chdb_result;
70+
71+ // Connection handle wrapping database session state.
72+ // Internal data structure managed by chDB implementation.
73+ // Users should only interact through API functions.
74+ typedef struct _chdb_connection {
75+ void * internal_data;
76+ } * chdb_connection;
77+
78+ // Opaque handle for streaming query results.
79+ // Internal data structure managed by chDB implementation.
80+ // Users should only interact through API functions.
6581typedef struct {
6682 void * internal_data;
6783} chdb_streaming_result;
6884
85+ #ifndef CHDB_NO_DEPRECATED
86+ // WARNING: The following interfaces are deprecated and will be removed in a future version.
87+ CHDB_EXPORT struct local_result * query_stable (int argc, char ** argv);
88+ CHDB_EXPORT void free_result (struct local_result * result);
89+
90+ CHDB_EXPORT struct local_result_v2 * query_stable_v2 (int argc, char ** argv);
91+ CHDB_EXPORT void free_result_v2 (struct local_result_v2 * result);
92+
6993/* *
7094 * Creates a new chDB connection.
7195 * Only one active connection is allowed per process.
@@ -110,12 +134,67 @@ CHDB_EXPORT struct local_result_v2 * query_conn(struct chdb_conn * conn, const c
110134CHDB_EXPORT chdb_streaming_result * query_conn_streaming (struct chdb_conn * conn, const char * query, const char * format);
111135
112136/* *
113- * Retrieves error message from streaming result.
114- * @brief Gets error message associated with streaming query execution
137+ * Fetches next chunk of streaming results.
138+ * @brief Iterates through streaming query results
139+ * @param conn Active connection handle
115140 * @param result Streaming result handle from query_conn_streaming()
116- * @return Null-terminated error message string, or NULL if no error occurred
141+ * @return Materialized result chunk with data
142+ * @note Returns empty result when stream ends
117143 */
118- CHDB_EXPORT const char * chdb_streaming_result_error (chdb_streaming_result * result);
144+ CHDB_EXPORT struct local_result_v2 * chdb_streaming_fetch_result (struct chdb_conn * conn, chdb_streaming_result * result);
145+
146+ /* *
147+ * Cancels ongoing streaming query.
148+ * @brief Aborts streaming query execution and cleans up resources
149+ * @param conn Active connection handle
150+ * @param result Streaming result handle to cancel
151+ */
152+ CHDB_EXPORT void chdb_streaming_cancel_query (struct chdb_conn * conn, chdb_streaming_result * result);
153+
154+ #endif
155+
156+ /* *
157+ * Creates a new chDB connection.
158+ * Only one active connection is allowed per process.
159+ * Creating a new connection with different path requires closing existing connection.
160+ *
161+ * @param argc Number of command-line arguments
162+ * @param argv Command-line arguments array (--path=<db_path> to specify database location)
163+ * @return Pointer to connection pointer, or NULL on failure
164+ * @note Default path is ":memory:" if not specified
165+ */
166+ CHDB_EXPORT chdb_connection * chdb_connect (int argc, char ** argv);
167+
168+ /* *
169+ * Closes an existing chDB connection and cleans up resources.
170+ * Thread-safe function that handles connection shutdown and cleanup.
171+ *
172+ * @param conn Pointer to connection pointer to close
173+ */
174+ CHDB_EXPORT void chdb_close_conn (chdb_connection * conn);
175+
176+ /* *
177+ * Executes a query on the given connection.
178+ * Thread-safe function that handles query execution in a separate thread.
179+ *
180+ * @param conn Connection to execute query on
181+ * @param query SQL query string to execute
182+ * @param format Output format string (e.g., "CSV", default format)
183+ * @return Query result structure containing output or error message
184+ * @note Returns error result if connection is invalid or closed
185+ */
186+ CHDB_EXPORT chdb_result * chdb_query (chdb_connection conn, const char * query, const char * format);
187+
188+ /* *
189+ * Executes a streaming query on the given connection.
190+ * @brief Initializes streaming query execution and returns result handle
191+ * @param conn Connection to execute query on
192+ * @param query SQL query string to execute
193+ * @param format Output format string (e.g. "CSV", default format)
194+ * @return Streaming result handle containing query state or error message
195+ * @note Returns error result if connection is invalid or closed
196+ */
197+ CHDB_EXPORT chdb_streaming_result * chdb_stream_query (chdb_connection conn, const char * query, const char * format);
119198
120199/* *
121200 * Fetches next chunk of streaming results.
@@ -125,15 +204,23 @@ CHDB_EXPORT const char * chdb_streaming_result_error(chdb_streaming_result * res
125204 * @return Materialized result chunk with data
126205 * @note Returns empty result when stream ends
127206 */
128- CHDB_EXPORT struct local_result_v2 * chdb_streaming_fetch_result ( struct chdb_conn * conn, chdb_streaming_result * result);
207+ CHDB_EXPORT chdb_result * chdb_stream_fetch_result (chdb_connection conn, chdb_streaming_result * result);
129208
130209/* *
131210 * Cancels ongoing streaming query.
132211 * @brief Aborts streaming query execution and cleans up resources
133212 * @param conn Active connection handle
134213 * @param result Streaming result handle to cancel
135214 */
136- CHDB_EXPORT void chdb_streaming_cancel_query (struct chdb_conn * conn, chdb_streaming_result * result);
215+ CHDB_EXPORT void chdb_stream_cancel_query (chdb_connection conn, chdb_streaming_result * result);
216+
217+ /* *
218+ * Retrieves error message from streaming result.
219+ * @brief Gets error message associated with streaming query execution
220+ * @param result Streaming result handle from query_conn_streaming()
221+ * @return Null-terminated error message string, or NULL if no error occurred
222+ */
223+ CHDB_EXPORT const char * chdb_streaming_result_error (chdb_streaming_result * result);
137224
138225/* *
139226 * Releases resources associated with streaming result.
0 commit comments