Skip to content

Commit eb8359b

Browse files
committed
Update comments
1 parent 914e744 commit eb8359b

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

SQLite/async_sqlite3.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,22 @@ namespace async
113113
return SQLITE_OK;
114114
}
115115

116+
/// Registers a trace callback function.
117+
///
118+
/// @warning **Callback Thread Context**: The `xCallback` function is invoked on the
119+
/// **private worker thread**, NOT the calling thread.
120+
/// - Ensure strictly thread-safe access to any shared data inside the callback.
121+
/// - Do NOT attempt to update UI elements directly from this callback.
116122
SQLITE_API int sqlite3_trace_v2(sqlite3* db, unsigned mTrace, int(*xCallback)(unsigned, void*, void*, void*), void* pCtx, dmq::Duration timeout) {
117123
return AsyncInvoke(::sqlite3_trace_v2, timeout, db, mTrace, xCallback, pCtx);
118124
}
119125

126+
/// Registers a progress handler callback.
127+
///
128+
/// @warning **Callback Thread Context**: The `xProgress` function is invoked on the
129+
/// **private worker thread** during long-running queries.
130+
/// - Do NOT access thread-local storage of the main thread.
131+
/// - Keep operations fast to avoid stalling the database worker.
120132
SQLITE_API void sqlite3_progress_handler(sqlite3* db, int nOps, int(*xProgress)(void*), void* pArg, dmq::Duration timeout) {
121133
AsyncInvoke(::sqlite3_progress_handler, timeout, db, nOps, xProgress, pArg);
122134
}
@@ -269,6 +281,23 @@ namespace async
269281
return AsyncInvoke(::sqlite3_column_name16, timeout, pStmt, N);
270282
}
271283

284+
/// Executes SQL on the dedicated worker thread and blocks the calling thread until completion.
285+
///
286+
/// @note **Blocking Behavior**: This function posts the task to the worker thread and
287+
/// waits (sleeps) until the task completes or the timeout expires.
288+
///
289+
/// @warning **Callback Thread Context**: The `callback` function is invoked on the
290+
/// **private worker thread**, NOT the calling thread.
291+
/// - Ensure strictly thread-safe access to any shared data inside the callback.
292+
/// - Do NOT attempt to update UI elements directly from the callback.
293+
///
294+
/// @param[in] db Database handle.
295+
/// @param[in] sql SQL script to be evaluated.
296+
/// @param[in] callback Callback function invoked for each result row (runs on worker thread).
297+
/// @param[in] pArg 1st argument to callback function.
298+
/// @param[out] errMsg Error msg written here.
299+
/// @param[in] timeout Maximum duration to wait for completion. Returns SQLITE_BUSY if expired.
300+
/// @return SQLite result code (e.g., SQLITE_OK) or SQLITE_BUSY on timeout.
272301
SQLITE_API int sqlite3_exec(sqlite3* db, const char* sql, sqlite3_callback callback, void* pArg, char** errMsg, dmq::Duration timeout) {
273302
return AsyncInvoke(::sqlite3_exec, timeout, db, sql, callback, pArg, errMsg);
274303
}
@@ -433,6 +462,11 @@ namespace async
433462
return AsyncInvoke(::sqlite3_randomness, timeout, N, P);
434463
}
435464

465+
/// Registers an authorizer callback.
466+
///
467+
/// @warning **Callback Thread Context**: The `xAuth` function is invoked on the
468+
/// **private worker thread** when a SQL statement is being compiled.
469+
/// - Ensure strictly thread-safe access if `pUserData` points to shared state.
436470
SQLITE_API int sqlite3_set_authorizer(sqlite3* db, int (*xAuth)(void*, int, const char*, const char*, const char*, const char*), void* pArg, dmq::Duration timeout) {
437471
return AsyncInvoke(::sqlite3_set_authorizer, timeout, db, xAuth, pArg);
438472
}
@@ -453,6 +487,12 @@ namespace async
453487
return AsyncInvoke(::sqlite3_busy_handler, timeout, db, xBusy, pArg);
454488
}
455489

490+
/// Binds a blob to a prepared statement.
491+
///
492+
/// @warning **Buffer Safety**: If passing `SQLITE_STATIC` as the destructor (`xDel`),
493+
/// you promise that `zData` will not change or be deleted until the statement is finalized.
494+
/// In a multi-threaded app, use `SQLITE_TRANSIENT` to force SQLite to make its own copy
495+
/// immediately, preventing race conditions if the caller modifies the buffer later.
456496
SQLITE_API int sqlite3_bind_blob(sqlite3_stmt* pStmt, int i, const void* zData, int nData, void (*xDel)(void*), dmq::Duration timeout) {
457497
return AsyncInvoke(::sqlite3_bind_blob, timeout, pStmt, i, zData, nData, xDel);
458498
}
@@ -477,6 +517,12 @@ namespace async
477517
return AsyncInvoke(::sqlite3_bind_null, timeout, pStmt, i);
478518
}
479519

520+
/// Binds text to a prepared statement.
521+
///
522+
/// @warning **Buffer Safety**: If passing `SQLITE_STATIC` as the destructor (`xDel`),
523+
/// you promise that `zData` will not change or be deleted until the statement is finalized.
524+
/// In a multi-threaded app, use `SQLITE_TRANSIENT` to force SQLite to make its own copy
525+
/// immediately, preventing race conditions if the caller modifies the string later.
480526
SQLITE_API int sqlite3_bind_text(sqlite3_stmt* pStmt, int i, const char* zData, int nData, void (*xDel)(void*), dmq::Duration timeout) {
481527
return AsyncInvoke(::sqlite3_bind_text, timeout, pStmt, i, zData, nData, xDel);
482528
}
@@ -821,6 +867,26 @@ namespace async
821867
return AsyncInvoke(::sqlite3_deserialize, timeout, db, zSchema, pData, szDb, szBuf, mFlags);
822868
}
823869

870+
/// Asynchronously executes SQL on the dedicated worker thread (Non-Blocking).
871+
///
872+
/// @note **Fire-and-Forget**: This function returns immediately. The main thread continues
873+
/// execution while the SQL query runs in the background.
874+
///
875+
/// @warning **LIFETIME SAFETY WARNING**:
876+
/// Because execution is deferred, you must ensure that **ALL pointers passed as arguments**
877+
/// (`sql`, `pArg`, `errMsg`) point to memory that remains valid until the operation completes.
878+
/// - **BAD:** Passing a pointer to a local stack variable that goes out of scope.
879+
/// - **GOOD:** Passing string literals, global variables, or heap-allocated objects managed by shared_ptr.
880+
///
881+
/// @warning **Callback Thread Context**: The `callback` function is invoked on the
882+
/// **private worker thread**, NOT the calling thread.
883+
///
884+
/// @param[in] db Database handle.
885+
/// @param[in] sql SQL script to be evaluated. Must remain valid until execution completes.
886+
/// @param[in] callback Callback function invoked for each result row (runs on worker thread).
887+
/// @param[in] pArg 1st argument to callback function. Must remain valid until execution completes.
888+
/// @param[out] errMsg Pointer to a char* where error message is written. The location must remain valid.
889+
/// @return std::future<int> A future that resolves to the SQLite result code (e.g., SQLITE_OK).
824890
std::future<int> sqlite3_exec_future(sqlite3* db, const char* sql, sqlite3_callback callback, void* pArg, char** errMsg) {
825891
return AsyncInvokeFuture(::sqlite3_exec, db, sql, callback, pArg, errMsg);
826892
}
@@ -845,10 +911,23 @@ namespace async
845911
return AsyncInvokeFuture(::sqlite3_backup_step, p, nPage);
846912
}
847913

914+
/// Asynchronously serializes the database content.
915+
///
916+
/// @warning **LIFETIME SAFETY WARNING**:
917+
/// This function returns immediately, but the worker thread accesses the arguments later.
918+
/// - `zSchema` MUST point to a string that remains valid until the future completes.
919+
/// - `piSize` MUST point to a valid integer location until the future completes.
920+
/// - **Do NOT pass pointers to local stack variables.**
848921
std::future<unsigned char*> sqlite3_serialize_future(sqlite3* db, const char* zSchema, sqlite3_int64* piSize, unsigned int mFlags) {
849922
return AsyncInvokeFuture(::sqlite3_serialize, db, zSchema, piSize, mFlags);
850923
}
851924

925+
/// Asynchronously deserializes a buffer into an in-memory database.
926+
///
927+
/// @warning **LIFETIME SAFETY WARNING**:
928+
/// This function returns immediately, but the worker thread accesses the arguments later.
929+
/// - `zSchema` and `pData` MUST remain valid pointers until the future completes.
930+
/// - If you allocated `pData`, do not `free()` it until you have checked the future result.
852931
std::future<int> sqlite3_deserialize_future(sqlite3* db, const char* zSchema, unsigned char* pData, sqlite3_int64 szDb, sqlite3_int64 szBuf, unsigned mFlags) {
853932
return AsyncInvokeFuture(::sqlite3_deserialize, db, zSchema, pData, szDb, szBuf, mFlags);
854933
}

0 commit comments

Comments
 (0)