11#pragma once
22
33#ifdef __cplusplus
4- # include < condition_variable>
54# include < cstddef>
65# include < cstdint>
7- # include < mutex>
8- # include < queue>
9- # include < string>
106extern " C" {
117#else
128# include < stdbool.h>
@@ -55,26 +51,6 @@ CHDB_EXPORT void free_result(struct local_result * result);
5551CHDB_EXPORT struct local_result_v2 * query_stable_v2 (int argc, char ** argv);
5652CHDB_EXPORT void free_result_v2 (struct local_result_v2 * result);
5753
58- #ifdef __cplusplus
59- struct query_request
60- {
61- std::string query;
62- std::string format;
63- };
64-
65- struct query_queue
66- {
67- std::mutex mutex;
68- std::condition_variable query_cv; // For query submission
69- std::condition_variable result_cv; // For query result retrieval
70- query_request current_query;
71- local_result_v2 * current_result = nullptr ;
72- bool has_query = false ;
73- bool shutdown = false ;
74- bool cleanup_done = false ;
75- };
76- #endif
77-
7854/* *
7955 * Connection structure for chDB
8056 * Contains server instance, connection state, and query processing queue
@@ -86,11 +62,15 @@ struct chdb_conn
8662 void * queue; /* Query processing queue */
8763};
8864
65+ typedef struct {
66+ void * internal_data;
67+ } chdb_streaming_result;
68+
8969/* *
9070 * Creates a new chDB connection.
9171 * Only one active connection is allowed per process.
9272 * Creating a new connection with different path requires closing existing connection.
93- *
73+ *
9474 * @param argc Number of command-line arguments
9575 * @param argv Command-line arguments array (--path=<db_path> to specify database location)
9676 * @return Pointer to connection pointer, or NULL on failure
@@ -101,15 +81,15 @@ CHDB_EXPORT struct chdb_conn ** connect_chdb(int argc, char ** argv);
10181/* *
10282 * Closes an existing chDB connection and cleans up resources.
10383 * Thread-safe function that handles connection shutdown and cleanup.
104- *
84+ *
10585 * @param conn Pointer to connection pointer to close
10686 */
10787CHDB_EXPORT void close_conn (struct chdb_conn ** conn);
10888
10989/* *
11090 * Executes a query on the given connection.
11191 * Thread-safe function that handles query execution in a separate thread.
112- *
92+ *
11393 * @param conn Connection to execute query on
11494 * @param query SQL query string to execute
11595 * @param format Output format string (e.g., "CSV", default format)
@@ -118,6 +98,51 @@ CHDB_EXPORT void close_conn(struct chdb_conn ** conn);
11898 */
11999CHDB_EXPORT struct local_result_v2 * query_conn (struct chdb_conn * conn, const char * query, const char * format);
120100
101+ /* *
102+ * Executes a streaming query on the given connection.
103+ * @brief Initializes streaming query execution and returns result handle
104+ * @param conn Connection to execute query on
105+ * @param query SQL query string to execute
106+ * @param format Output format string (e.g. "CSV", default format)
107+ * @return Streaming result handle containing query state or error message
108+ * @note Returns error result if connection is invalid or closed
109+ */
110+ CHDB_EXPORT chdb_streaming_result * query_conn_streaming (struct chdb_conn * conn, const char * query, const char * format);
111+
112+ /* *
113+ * Retrieves error message from streaming result.
114+ * @brief Gets error message associated with streaming query execution
115+ * @param result Streaming result handle from query_conn_streaming()
116+ * @return Null-terminated error message string, or NULL if no error occurred
117+ */
118+ CHDB_EXPORT const char * chdb_streaming_result_error (chdb_streaming_result * result);
119+
120+ /* *
121+ * Fetches next chunk of streaming results.
122+ * @brief Iterates through streaming query results
123+ * @param conn Active connection handle
124+ * @param result Streaming result handle from query_conn_streaming()
125+ * @return Materialized result chunk with data
126+ * @note Returns empty result when stream ends
127+ */
128+ CHDB_EXPORT struct local_result_v2 * chdb_streaming_fetch_result (struct chdb_conn * conn, chdb_streaming_result * result);
129+
130+ /* *
131+ * Cancels ongoing streaming query.
132+ * @brief Aborts streaming query execution and cleans up resources
133+ * @param conn Active connection handle
134+ * @param result Streaming result handle to cancel
135+ */
136+ CHDB_EXPORT void chdb_streaming_cancel_query (struct chdb_conn * conn, chdb_streaming_result * result);
137+
138+ /* *
139+ * Releases resources associated with streaming result.
140+ * @brief Destroys streaming result handle and frees allocated memory
141+ * @param result Streaming result handle to destroy
142+ * @warning Must be called even if query was finished or canceled
143+ */
144+ CHDB_EXPORT void chdb_destroy_result (chdb_streaming_result * result);
145+
121146#ifdef __cplusplus
122147}
123- #endif
148+ #endif
0 commit comments