Skip to content

Commit

Permalink
C bindings: Add libsql_reset
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Jastrzebski <[email protected]>
  • Loading branch information
haaawk committed Feb 28, 2024
1 parent ee85056 commit 7a3482a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bindings/c/include/libsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void libsql_close(libsql_database_t db);

int libsql_connect(libsql_database_t db, libsql_connection_t *out_conn, const char **out_err_msg);

int libsql_reset(libsql_connection_t conn, const char **out_err_msg);

void libsql_disconnect(libsql_connection_t conn);

int libsql_prepare(libsql_connection_t conn, const char *sql, libsql_stmt_t *out_stmt, const char **out_err_msg);
Expand Down
14 changes: 14 additions & 0 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ pub unsafe extern "C" fn libsql_connect(
0
}

#[no_mangle]
pub unsafe extern "C" fn libsql_reset(
conn: libsql_connection_t,
out_err_msg: *mut *const std::ffi::c_char,
) -> std::ffi::c_int {
if conn.is_null() {
set_err_msg("Null connection".to_string(), out_err_msg);
return 1;
}
let conn = conn.get_ref();
RT.block_on(conn.reset());
0
}

#[no_mangle]
pub unsafe extern "C" fn libsql_disconnect(conn: libsql_connection_t) {
if conn.is_null() {
Expand Down
6 changes: 6 additions & 0 deletions libsql/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub(crate) trait Conn {
async fn changes(&self) -> u64;

async fn last_insert_rowid(&self) -> i64;

async fn reset(&self);
}

/// A connection to some libsql database, this can be a remote one or a local one.
Expand Down Expand Up @@ -111,4 +113,8 @@ impl Connection {
pub async fn last_insert_rowid(&self) -> i64 {
self.conn.last_insert_rowid().await
}

pub async fn reset(&self) {
self.conn.reset().await
}
}
8 changes: 8 additions & 0 deletions libsql/src/hrana/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ impl Conn for HttpConnection<HttpSender> {
async fn last_insert_rowid(&self) -> i64 {
self.last_insert_rowid()
}

async fn reset(&self) {
self.current_stream().reset().await;
}
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -284,4 +288,8 @@ impl Conn for HranaStream<HttpSender> {
async fn last_insert_rowid(&self) -> i64 {
self.last_insert_rowid()
}

async fn reset(&self) {
self.reset().await;
}
}
4 changes: 4 additions & 0 deletions libsql/src/hrana/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ where
pub fn is_autocommit(&self) -> bool {
self.inner.is_autocommit.load(Ordering::SeqCst)
}

pub async fn reset(&self) {
(*self.inner).stream.lock().await.reset();
}
}

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions libsql/src/local/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ impl Conn for LibsqlConnection {
async fn last_insert_rowid(&self) -> i64 {
self.conn.last_insert_rowid()
}

async fn reset(&self) {}
}

impl Drop for LibsqlConnection {
Expand Down
2 changes: 2 additions & 0 deletions libsql/src/replication/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ impl Conn for RemoteConnection {
async fn last_insert_rowid(&self) -> i64 {
self.inner.lock().last_insert_rowid
}

async fn reset(&self) {}
}

pub struct ColumnMeta {
Expand Down

0 comments on commit 7a3482a

Please sign in to comment.