Skip to content

Commit

Permalink
feat: add libsql_connection_info
Browse files Browse the repository at this point in the history
  • Loading branch information
levydsa committed Oct 3, 2024
1 parent 3fa75cc commit 862f552
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 10 additions & 0 deletions libsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ typedef struct {
libsql_error_t *err;
} libsql_execute_t;

typedef struct {
int64_t last_inserted_rowid;
uint64_t total_changes;
libsql_error_t *err;
} libsql_connection_info_t;

/**
* Database description.
*/
Expand Down Expand Up @@ -161,6 +167,10 @@ libsql_transaction_t libsql_connection_transaction(libsql_connection_t self);
libsql_batch_t
libsql_connection_batch(libsql_connection_t self, const char *sql);

/** Send a batch statement in a connection */
libsql_connection_info_t
libsql_connection_info(libsql_connection_t self);

/** Send a batch statement in a transaction */
libsql_batch_t
libsql_transaction_batch(libsql_transaction_t self, const char *sql);
Expand Down
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,39 @@ pub extern "C" fn libsql_connection_batch(
}
}

#[no_mangle]
pub extern "C" fn libsql_connection_info(
conn: c::libsql_connection_t,
) -> c::libsql_connection_info_t {
match (move || -> anyhow::Result<(i64, u64)> {
if conn.inner.is_null() {
bail!("attempted to get info from a null connection")
}

let conn = ManuallyDrop::new(unsafe { Box::from_raw(conn.inner as *mut Connection) });

Ok((conn.last_insert_rowid(), conn.total_changes()))
})() {
Ok((last_inserted_rowid, total_changes)) => c::libsql_connection_info_t {
last_inserted_rowid,
total_changes,
..Default::default()
},
Err(err) => c::libsql_connection_info_t {
err: CString::new(err.to_string()).unwrap().into_raw() as *mut c::libsql_error_t,
..Default::default()
},
}
}

#[no_mangle]
pub extern "C" fn libsql_transaction_batch(
tx: c::libsql_transaction_t,
sql: *const c_char,
) -> c::libsql_batch_t {
match (move || -> anyhow::Result<_> {
if tx.inner.is_null() {
bail!("attempted to init a statement with a null connection")
bail!("attempted execute batch statements with a null transaction")
}

if sql.is_null() {
Expand Down

0 comments on commit 862f552

Please sign in to comment.