// Turso C ABI 的 raw FFI 层。
//
// 约定(见 notes/turso-binding-guide.md):
// - C 侧从不返回 NULL 指针:失败通过 `last_error()` 报告(空串 = 成功);
// - `Db` / `Conn` 都是 GC 托管的外部对象,finalizer 负责释放原生资源;
// - MoonBit 的 `Bytes` 由运行时保证 NUL 结尾,可直接当 `const char *` 传给 C;
// - 一个 `Db` 可以 connect 出**多个** `Conn`(`turso_database_connect` 可重复调用)。
///|
/// 不透明的数据库句柄(背后是外部对象)。
pub type Db
///|
/// 不透明的连接句柄(背后是外部对象)。
pub type Conn
///|
/// Opaque sync database handle.
pub type SyncDb
///|
/// Opaque sync operation handle.
pub type SyncOp
///|
/// Opaque sync I/O queue item handle.
pub type SyncIo
///|
/// Opaque fetched sync changes handle.
pub type SyncChanges
///|
/// Opaque sync stats snapshot.
pub type SyncStats
///|
/// 打开数据库。`path` 为 `:memory:` 或文件路径。
#borrow(path)
pub extern "c" fn db_open(path : Bytes) -> Db = "mturso_db_open"
///|
/// 从数据库开一个连接。**可重复调用**,每个连接有独立的事务/语句状态。
#borrow(db)
pub extern "c" fn db_connect(db : Db) -> Conn = "mturso_db_connect"
///|
/// 显式关闭数据库(幂等;不调用也会由 finalizer 释放)。
#borrow(db)
pub extern "c" fn db_close(db : Db) = "mturso_db_close"
///|
#borrow(path, remote_url, client_name)
pub extern "c" fn sync_db_new(
path : Bytes,
remote_url : Bytes,
client_name : Bytes,
long_poll_timeout_ms : Int,
bootstrap_if_empty : Bool,
) -> SyncDb = "mturso_sync_db_new"
///|
#borrow(db)
pub extern "c" fn sync_db_create(db : SyncDb) -> SyncOp = "mturso_sync_db_create"
///|
#borrow(db)
pub extern "c" fn sync_db_connect(db : SyncDb) -> SyncOp = "mturso_sync_db_connect"
///|
#borrow(db)
pub extern "c" fn sync_db_push(db : SyncDb) -> SyncOp = "mturso_sync_db_push"
///|
#borrow(db)
pub extern "c" fn sync_db_wait_changes(db : SyncDb) -> SyncOp = "mturso_sync_db_wait_changes"
///|
#borrow(db)
pub extern "c" fn sync_db_checkpoint(db : SyncDb) -> SyncOp = "mturso_sync_db_checkpoint"
///|
#borrow(db)
pub extern "c" fn sync_db_stats(db : SyncDb) -> SyncOp = "mturso_sync_db_stats"
///|
#borrow(op)
pub extern "c" fn sync_op_resume(op : SyncOp) -> Int = "mturso_sync_op_resume"
///|
#borrow(op)
pub extern "c" fn sync_op_result_kind(op : SyncOp) -> Int = "mturso_sync_op_result_kind"
///|
#borrow(op, db)
pub extern "c" fn sync_op_extract_connection(op : SyncOp, db : SyncDb) -> Conn = "mturso_sync_op_extract_connection"
///|
#borrow(op)
pub extern "c" fn sync_op_extract_changes(op : SyncOp) -> SyncChanges = "mturso_sync_op_extract_changes"
///|
#borrow(db, changes)
pub extern "c" fn sync_db_apply_changes(
db : SyncDb,
changes : SyncChanges,
) -> SyncOp = "mturso_sync_db_apply_changes"
///|
#borrow(changes)
pub extern "c" fn sync_changes_is_null(changes : SyncChanges) -> Bool = "mturso_sync_changes_is_null"
///|
#borrow(changes)
pub extern "c" fn sync_changes_close(changes : SyncChanges) = "mturso_sync_changes_close"
///|
#borrow(op)
pub extern "c" fn sync_op_extract_stats(op : SyncOp) -> SyncStats = "mturso_sync_op_extract_stats"
///|
#borrow(stats)
pub extern "c" fn sync_stats_cdc_operations(stats : SyncStats) -> Int64 = "mturso_sync_stats_cdc_operations"
///|
#borrow(stats)
pub extern "c" fn sync_stats_main_wal_size(stats : SyncStats) -> Int64 = "mturso_sync_stats_main_wal_size"
///|
#borrow(stats)
pub extern "c" fn sync_stats_revert_wal_size(stats : SyncStats) -> Int64 = "mturso_sync_stats_revert_wal_size"
///|
#borrow(stats)
pub extern "c" fn sync_stats_last_pull_unix_time(stats : SyncStats) -> Int64 = "mturso_sync_stats_last_pull_unix_time"
///|
#borrow(stats)
pub extern "c" fn sync_stats_last_push_unix_time(stats : SyncStats) -> Int64 = "mturso_sync_stats_last_push_unix_time"
///|
#borrow(stats)
pub extern "c" fn sync_stats_network_sent_bytes(stats : SyncStats) -> Int64 = "mturso_sync_stats_network_sent_bytes"
///|
#borrow(stats)
pub extern "c" fn sync_stats_network_received_bytes(stats : SyncStats) -> Int64 = "mturso_sync_stats_network_received_bytes"
///|
#borrow(stats)
pub extern "c" fn sync_stats_revision(stats : SyncStats) -> Bytes = "mturso_sync_stats_revision"
///|
#borrow(stats)
pub extern "c" fn sync_stats_close(stats : SyncStats) = "mturso_sync_stats_close"
///|
#borrow(db)
pub extern "c" fn sync_db_close(db : SyncDb) = "mturso_sync_db_close"
///|
#borrow(op)
pub extern "c" fn sync_op_close(op : SyncOp) = "mturso_sync_op_close"
///|
#borrow(db)
pub extern "c" fn sync_db_take_io(db : SyncDb) -> SyncIo = "mturso_sync_db_take_io"
///|
#borrow(item)
pub extern "c" fn sync_io_is_null(item : SyncIo) -> Bool = "mturso_sync_io_is_null"
///|
#borrow(db)
pub extern "c" fn sync_db_step_callbacks(db : SyncDb) -> Int = "mturso_sync_db_step_callbacks"
///|
#borrow(item)
pub extern "c" fn sync_io_kind(item : SyncIo) -> Int = "mturso_sync_io_kind"
///|
#borrow(item)
pub extern "c" fn sync_io_http_url(item : SyncIo) -> Bytes = "mturso_sync_io_http_url"
///|
#borrow(item)
pub extern "c" fn sync_io_http_method(item : SyncIo) -> Bytes = "mturso_sync_io_http_method"
///|
#borrow(item)
pub extern "c" fn sync_io_http_path(item : SyncIo) -> Bytes = "mturso_sync_io_http_path"
///|
#borrow(item)
pub extern "c" fn sync_io_http_body(item : SyncIo) -> Bytes = "mturso_sync_io_http_body"
///|
#borrow(item)
pub extern "c" fn sync_io_http_header_count(item : SyncIo) -> Int = "mturso_sync_io_http_header_count"
///|
#borrow(item)
pub extern "c" fn sync_io_http_header_key(
item : SyncIo,
index : Int,
) -> Bytes = "mturso_sync_io_http_header_key"
///|
#borrow(item)
pub extern "c" fn sync_io_http_header_value(
item : SyncIo,
index : Int,
) -> Bytes = "mturso_sync_io_http_header_value"
///|
#borrow(item)
pub extern "c" fn sync_io_full_read_path(item : SyncIo) -> Bytes = "mturso_sync_io_full_read_path"
///|
#borrow(item)
pub extern "c" fn sync_io_full_write_path(item : SyncIo) -> Bytes = "mturso_sync_io_full_write_path"
///|
#borrow(item)
pub extern "c" fn sync_io_full_write_content(item : SyncIo) -> Bytes = "mturso_sync_io_full_write_content"
///|
#borrow(item)
pub extern "c" fn sync_io_status(item : SyncIo, status_code : Int) -> Int = "mturso_sync_io_status"
///|
#borrow(item, buffer)
pub extern "c" fn sync_io_push_buffer(item : SyncIo, buffer : Bytes) -> Int = "mturso_sync_io_push_buffer"
///|
#borrow(item, message)
pub extern "c" fn sync_io_poison(item : SyncIo, message : Bytes) -> Int = "mturso_sync_io_poison"
///|
#borrow(item)
pub extern "c" fn sync_io_done(item : SyncIo) -> Int = "mturso_sync_io_done"
///|
#borrow(item)
pub extern "c" fn sync_io_close(item : SyncIo) = "mturso_sync_io_close"
///|
/// 执行 INSERT/UPDATE/DELETE/DDL,返回受影响行数;失败返回 -1。
#borrow(conn, sql)
pub extern "c" fn conn_exec(conn : Conn, sql : Bytes) -> Int64 = "mturso_conn_exec"
// ---------------------------------------------------------------------------
// 语句句柄(薄壳):prepare / bind_positional_* / deinit,全是 1:1 镜像。
// ---------------------------------------------------------------------------
///|
/// 不透明的语句句柄。
pub type Stmt
///|
#borrow(conn, sql)
pub extern "c" fn conn_prepare(conn : Conn, sql : Bytes) -> Stmt = "mturso_conn_prepare"
///|
/// prepare_first:拿第一条语句;尾巴偏移存进 stmt(`stmt_tail` 读)。
#borrow(conn, sql)
pub extern "c" fn conn_prepare_first(conn : Conn, sql : Bytes) -> Stmt = "mturso_conn_prepare_first"
///|
#borrow(stmt)
pub extern "c" fn stmt_tail(stmt : Stmt) -> Int64 = "mturso_stmt_tail"
///|
#borrow(stmt)
pub extern "c" fn stmt_deinit(stmt : Stmt) = "mturso_stmt_deinit"
///|
#borrow(stmt)
pub extern "c" fn stmt_column_count(stmt : Stmt) -> Int = "mturso_stmt_column_count"
///|
#borrow(stmt)
pub extern "c" fn stmt_bind_null(stmt : Stmt, pos : Int) = "mturso_stmt_bind_null"
///|
#borrow(stmt)
pub extern "c" fn stmt_bind_int(stmt : Stmt, pos : Int, value : Int64) = "mturso_stmt_bind_int"
///|
#borrow(stmt)
pub extern "c" fn stmt_bind_double(stmt : Stmt, pos : Int, value : Double) = "mturso_stmt_bind_double"
///|
#borrow(stmt, value)
pub extern "c" fn stmt_bind_text(stmt : Stmt, pos : Int, value : Bytes) = "mturso_stmt_bind_text"
///|
#borrow(stmt, value)
pub extern "c" fn stmt_bind_blob(stmt : Stmt, pos : Int, value : Bytes) = "mturso_stmt_bind_blob"
///|
/// 流式跑一条已经 prepare + bind 好的语句;返回读端 fd/HANDLE,失败通过 last_error 报告。
#borrow(conn, stmt)
pub extern "c" fn submit_stmt_rows(conn : Conn, stmt : Stmt) -> @types.Fd = "mturso_submit_stmt_rows"
///|
/// Execute one prepared statement on the connection worker.
#borrow(conn, stmt)
pub extern "c" fn submit_stmt_execute(conn : Conn, stmt : Stmt) -> @types.Fd = "mturso_submit_stmt_execute"
///|
/// 等本连接当前 job 结束(`close()` 释放语句前调用)。
#borrow(conn)
pub extern "c" fn conn_wait_job(conn : Conn) = "mturso_conn_wait_job"
///|
#borrow(conn)
pub extern "c" fn conn_resume_sync_io(conn : Conn) = "mturso_conn_resume_sync_io"
///|
/// 最近一次 INSERT 的 rowid。
#borrow(conn)
pub extern "c" fn conn_last_insert_rowid(conn : Conn) -> Int64 = "mturso_conn_last_insert_rowid"
///|
/// Whether the connection is currently in autocommit mode.
#borrow(conn)
pub extern "c" fn conn_get_autocommit(conn : Conn) -> Int = "mturso_conn_get_autocommit"
///|
/// 显式关闭连接(幂等)。
#borrow(conn)
pub extern "c" fn conn_close(conn : Conn) = "mturso_conn_close"
///|
/// 最近一次错误信息(无错误时为空串)。
pub extern "c" fn last_error() -> Bytes = "mturso_last_error"
///|
/// 最近一次错误的**分类**(0 = 无错误;1 = Query,2 = Busy,3 = Conflict,
/// 4 = Connection)。消息仍是文本,分类给上层做模式匹配。
pub extern "c" fn last_error_kind() -> Int = "mturso_last_error_kind"