Skip to content

Commit

Permalink
Drop for Statement
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Aug 12, 2024
1 parent 71fdcff commit 746aeec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 67 deletions.
57 changes: 15 additions & 42 deletions diesel-wasm-sqlite/src/connection/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,21 @@ impl RawConnection {
| SqliteOpenFlags::SQLITE_OPEN_URI;

let (tx, rx) = oneshot::channel::<JsValue>();
// TODO: can make this into function/macro
wasm_bindgen_futures::spawn_local(async move {
let conn = rx.await;

let sqlite3 = crate::get_sqlite_unchecked();

match sqlite3.close(&conn.unwrap()).await {
Ok(_) => log::debug!("db closed"),
Err(e) => {
log::error!("error during db close");
web_sys::console::log_1(&e);
match rx.await {
Ok(conn) => {
let sqlite3 = crate::get_sqlite_unchecked();
match sqlite3.close(&conn).await {
Ok(_) => log::debug!("db closed"),
Err(e) => {
log::error!("error during db close");
web_sys::console::log_1(&e);
}
}
}
Err(_) => {
log::error!("RawConnection never dropped.");
}
}
});
Expand Down Expand Up @@ -171,41 +176,10 @@ impl Drop for RawConnection {
.drop_signal
.take()
.expect("Drop is only unwrapped once");
sender.send(self.internal_connection.clone());
}
}

/*
enum SqliteCallbackError {
Abort(&'static str),
DieselError(crate::result::Error),
Panic(String),
}
impl SqliteCallbackError {
fn emit(&self, ctx: *mut ffi::sqlite3_context) {
let s;
let msg = match self {
SqliteCallbackError::Abort(msg) => *msg,
SqliteCallbackError::DieselError(e) => {
s = e.to_string();
&s
}
SqliteCallbackError::Panic(msg) => msg,
};
unsafe {
context_error_str(ctx, msg);
}
let _ = sender.send(self.internal_connection.clone());
}
}

impl From<crate::result::Error> for SqliteCallbackError {
fn from(e: crate::result::Error) -> Self {
Self::DieselError(e)
}
}
*/
/*
#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -226,4 +200,3 @@ mod tests {
});
}
}
*/
39 changes: 17 additions & 22 deletions diesel-wasm-sqlite/src/connection/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ use diesel::{
result::{Error::DatabaseError, *},
};
use std::cell::OnceCell;
use std::sync::Arc;

use wasm_bindgen::JsValue;

pub(super) struct Statement {
inner_statement: JsValue,
// drop_signal: Option<tokio::sync::oneshot::Sender<Arc<Self>>>,
drop_signal: Option<tokio::sync::oneshot::Sender<JsValue>>,
}

impl Statement {
Expand Down Expand Up @@ -51,15 +50,20 @@ impl Statement {
)
.await
.unwrap();
/*
let (tx, rx) = tokio::sync::oneshot::channel::<Arc<Self>>();

let (tx, rx) = tokio::sync::oneshot::channel::<JsValue>();
// We don't have `AsyncDrop` in rust yet.
// instead, we `send` a signal on Drop for the destructor to run in an
// asynchronously-spawned task.

tokio::spawn(async move {
wasm_bindgen_futures::spawn_local(async move {
match rx.await {
Ok(this) => {
Ok(inner_statement) => {
let this = Statement {
inner_statement,
drop_signal: None,
};

this.reset().await;
this.clear_bindings();
}
Expand All @@ -68,11 +72,10 @@ impl Statement {
}
}
});
*/

Ok(Statement {
inner_statement: stmt,
// drop_signal: Some(tx),
drop_signal: Some(tx),
})
}

Expand Down Expand Up @@ -271,20 +274,12 @@ impl<'stmt, 'query> BoundStatement<'stmt, 'query> {
// Eventually replace with `AsyncDrop`: https://github.com/rust-lang/rust/issues/126482
impl<'stmt, 'query> Drop for BoundStatement<'stmt, 'query> {
fn drop(&mut self) {
/*
let sender = self
.statement
.drop_signal
.take()
.expect("Drop may only be ran once");
*/
// sender.send(Arc::new(*(&mut self.statement));
/*
// First reset the statement, otherwise the bind calls
// below will fail
self.statement.reset();
self.statement.clear_bindings();
*/
let sender = self
.statement
.drop_signal
.take()
.expect("Drop may only be ran once");
let _ = sender.send(self.statement.inner_statement.clone());
self.query.take();
}
}
Expand Down
7 changes: 4 additions & 3 deletions diesel-wasm-sqlite/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use diesel_wasm_sqlite::connection::{AsyncConnection, WasmSqliteConnection};
use wasm_bindgen_test::*;
use web_sys::console;
wasm_bindgen_test_configure!(run_in_dedicated_worker);

/*
#[wasm_bindgen_test]
async fn test_establish_and_exec() {
let rng: u16 = rand::random();
Expand All @@ -17,7 +17,7 @@ async fn test_establish_and_exec() {
console::log_1(&"CREATE".into());
raw.exec(
"
"
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
Expand All @@ -30,7 +30,7 @@ async fn test_establish_and_exec() {
console::log_1(&"INSERT".into());
raw.exec(
"
"
INSERT INTO books (title, author, published_year, genre) VALUES
('To Kill a Mockingbird', 'Harper Lee', 1960, 'Fiction'),
('1984', 'George Orwell', 1949, 'Dystopian'),
Expand Down Expand Up @@ -58,3 +58,4 @@ async fn test_establish_and_exec() {
raw.exec("SELECT title, author FROM books WHERE published_year > 1950;".into())
.await;
}
*/

0 comments on commit 746aeec

Please sign in to comment.