From 5981d65c97a340e3904792ad2213964239218908 Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Wed, 17 Jul 2024 15:32:44 -0400 Subject: [PATCH] cleanup + crate --- Cargo.lock | 6 +- diesel-wasm-sqlite/Cargo.toml | 3 + .../src/connection/bind_collector.rs | 12 +-- diesel-wasm-sqlite/src/lib.rs | 86 +++++++++++++++++-- diesel-wasm-sqlite/src/mod.rs | 82 ------------------ .../src/query_builder/limit_offset.rs | 2 +- .../src/query_builder/returning.rs | 2 +- xmtp_mls/Cargo.toml | 5 +- 8 files changed, 94 insertions(+), 104 deletions(-) delete mode 100644 diesel-wasm-sqlite/src/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 127a89a12..fd0c847b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1075,6 +1075,11 @@ dependencies = [ [[package]] name = "diesel-wasm-sqlite" version = "0.1.0" +dependencies = [ + "diesel", + "log", + "wasm-bindgen", +] [[package]] name = "diesel_derives" @@ -6608,7 +6613,6 @@ dependencies = [ "tracing-log", "tracing-subscriber", "tracing-test", - "wasm-bindgen", "xmtp_api_grpc", "xmtp_cryptography", "xmtp_id", diff --git a/diesel-wasm-sqlite/Cargo.toml b/diesel-wasm-sqlite/Cargo.toml index 28c02a0db..5f68163b6 100644 --- a/diesel-wasm-sqlite/Cargo.toml +++ b/diesel-wasm-sqlite/Cargo.toml @@ -4,3 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] +diesel = { git = "https://github.com/xmtp/diesel", branch = "insipx/wasm-backend", features = ["r2d2", "i-implement-a-third-party-backend-and-opt-into-breaking-changes"] } +wasm-bindgen = "0.2" +log = "0.4" diff --git a/diesel-wasm-sqlite/src/connection/bind_collector.rs b/diesel-wasm-sqlite/src/connection/bind_collector.rs index 68a827d08..377647e03 100644 --- a/diesel-wasm-sqlite/src/connection/bind_collector.rs +++ b/diesel-wasm-sqlite/src/connection/bind_collector.rs @@ -1,4 +1,4 @@ -use crate::storage::wasm_sqlite::{SqliteType, WasmSqlite}; +use crate::{SqliteType, WasmSqlite}; use diesel::query_builder::{BindCollector, MoveableBindCollector}; use diesel::result::QueryResult; use diesel::serialize::{IsNull, Output}; @@ -6,11 +6,11 @@ use diesel::sql_types::HasSqlType; #[derive(Debug, Default)] pub struct SqliteBindCollector<'a> { - pub(in crate::storage::wasm_sqlite) binds: Vec<(InternalSqliteBindValue<'a>, SqliteType)>, + pub(crate) binds: Vec<(InternalSqliteBindValue<'a>, SqliteType)>, } impl SqliteBindCollector<'_> { - pub(in crate::storage::wasm_sqlite) fn new() -> Self { + pub(crate) fn new() -> Self { Self { binds: Vec::new() } } } @@ -21,7 +21,7 @@ impl SqliteBindCollector<'_> { /// It can be constructed via the various `From` implementations #[derive(Debug)] pub struct SqliteBindValue<'a> { - pub(in crate::storage::wasm_sqlite) inner: InternalSqliteBindValue<'a>, + pub(crate) inner: InternalSqliteBindValue<'a>, } impl<'a> From for SqliteBindValue<'a> { @@ -125,8 +125,8 @@ impl std::fmt::Display for InternalSqliteBindValue<'_> { impl InternalSqliteBindValue<'_> { #[allow(unsafe_code)] // ffi function calls - pub(in crate::storage::wasm_sqlite) fn result_of(self, ctx: &mut i32) { - use crate::storage::wasm_sqlite::ffi; + pub(crate) fn result_of(self, ctx: &mut i32) { + use crate::ffi; match self { InternalSqliteBindValue::BorrowedString(s) => { ffi::sqlite3_result_text(*ctx, s.to_string()) diff --git a/diesel-wasm-sqlite/src/lib.rs b/diesel-wasm-sqlite/src/lib.rs index 7d12d9af8..f6033799f 100644 --- a/diesel-wasm-sqlite/src/lib.rs +++ b/diesel-wasm-sqlite/src/lib.rs @@ -1,14 +1,82 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +//! Module for an SQLite backend accesible from the web. +pub mod backend; +pub mod connection; +pub mod ffi; +pub mod query_builder; +pub mod sqlite_types; + +use diesel::{ + connection::{AnsiTransactionManager, Instrumentation, SimpleConnection, TransactionManager}, + query_builder::{QueryFragment, QueryId}, + result::QueryResult, + Connection, +}; +use wasm_bindgen::JsValue; + +pub use backend::{SqliteType, WasmSqlite}; +pub struct WasmSqliteConnection {} +#[derive(Debug)] +pub struct WasmSqliteError(JsValue); + +impl SimpleConnection for WasmSqliteConnection { + fn batch_execute(&mut self, query: &str) -> diesel::prelude::QueryResult<()> { + ffi::batch_execute(query) + .map_err(WasmSqliteError::from) + .map_err(Into::into) + } +} + +impl diesel::connection::ConnectionSealed for WasmSqliteConnection {} + +impl Connection for WasmSqliteConnection { + type Backend = WasmSqlite; + type TransactionManager = AnsiTransactionManager; + + fn establish(database_url: &str) -> diesel::prelude::ConnectionResult { + ffi::establish(database_url) + .map_err(WasmSqliteError::from) + .map_err(Into::::into)?; + Ok(WasmSqliteConnection {}) + } + + fn execute_returning_count(&mut self, source: &T) -> QueryResult + where + T: QueryFragment + QueryId, + { + todo!() + } + + fn transaction_state( + &mut self, + ) -> &mut >::TransactionStateData { + todo!() + } + + fn instrumentation(&mut self) -> &mut dyn Instrumentation { + todo!() + } + + fn set_instrumentation(&mut self, instrumentation: impl diesel::connection::Instrumentation) { + todo!() + } } -#[cfg(test)] -mod tests { - use super::*; +impl From for diesel::result::Error { + fn from(value: WasmSqliteError) -> diesel::result::Error { + log::error!("NOT IMPLEMENTED, {:?}", value); + diesel::result::Error::NotFound + } +} + +impl From for diesel::result::ConnectionError { + fn from(value: WasmSqliteError) -> diesel::result::ConnectionError { + log::error!("NOT IMPLEMENTED, {:?}", value); + diesel::result::ConnectionError::BadConnection("Not implemented".to_string()) + } +} - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); +impl From for WasmSqliteError { + fn from(err: JsValue) -> WasmSqliteError { + WasmSqliteError(err) } } diff --git a/diesel-wasm-sqlite/src/mod.rs b/diesel-wasm-sqlite/src/mod.rs deleted file mode 100644 index f6033799f..000000000 --- a/diesel-wasm-sqlite/src/mod.rs +++ /dev/null @@ -1,82 +0,0 @@ -//! Module for an SQLite backend accesible from the web. -pub mod backend; -pub mod connection; -pub mod ffi; -pub mod query_builder; -pub mod sqlite_types; - -use diesel::{ - connection::{AnsiTransactionManager, Instrumentation, SimpleConnection, TransactionManager}, - query_builder::{QueryFragment, QueryId}, - result::QueryResult, - Connection, -}; -use wasm_bindgen::JsValue; - -pub use backend::{SqliteType, WasmSqlite}; -pub struct WasmSqliteConnection {} -#[derive(Debug)] -pub struct WasmSqliteError(JsValue); - -impl SimpleConnection for WasmSqliteConnection { - fn batch_execute(&mut self, query: &str) -> diesel::prelude::QueryResult<()> { - ffi::batch_execute(query) - .map_err(WasmSqliteError::from) - .map_err(Into::into) - } -} - -impl diesel::connection::ConnectionSealed for WasmSqliteConnection {} - -impl Connection for WasmSqliteConnection { - type Backend = WasmSqlite; - type TransactionManager = AnsiTransactionManager; - - fn establish(database_url: &str) -> diesel::prelude::ConnectionResult { - ffi::establish(database_url) - .map_err(WasmSqliteError::from) - .map_err(Into::::into)?; - Ok(WasmSqliteConnection {}) - } - - fn execute_returning_count(&mut self, source: &T) -> QueryResult - where - T: QueryFragment + QueryId, - { - todo!() - } - - fn transaction_state( - &mut self, - ) -> &mut >::TransactionStateData { - todo!() - } - - fn instrumentation(&mut self) -> &mut dyn Instrumentation { - todo!() - } - - fn set_instrumentation(&mut self, instrumentation: impl diesel::connection::Instrumentation) { - todo!() - } -} - -impl From for diesel::result::Error { - fn from(value: WasmSqliteError) -> diesel::result::Error { - log::error!("NOT IMPLEMENTED, {:?}", value); - diesel::result::Error::NotFound - } -} - -impl From for diesel::result::ConnectionError { - fn from(value: WasmSqliteError) -> diesel::result::ConnectionError { - log::error!("NOT IMPLEMENTED, {:?}", value); - diesel::result::ConnectionError::BadConnection("Not implemented".to_string()) - } -} - -impl From for WasmSqliteError { - fn from(err: JsValue) -> WasmSqliteError { - WasmSqliteError(err) - } -} diff --git a/diesel-wasm-sqlite/src/query_builder/limit_offset.rs b/diesel-wasm-sqlite/src/query_builder/limit_offset.rs index 734a933a3..65740b9fd 100644 --- a/diesel-wasm-sqlite/src/query_builder/limit_offset.rs +++ b/diesel-wasm-sqlite/src/query_builder/limit_offset.rs @@ -1,4 +1,4 @@ -use crate::storage::wasm_sqlite::WasmSqlite; +use crate::WasmSqlite; use diesel::query_builder::{AstPass, IntoBoxedClause, QueryFragment}; use diesel::query_builder::{BoxedLimitOffsetClause, LimitOffsetClause}; use diesel::query_builder::{LimitClause, NoLimitClause}; diff --git a/diesel-wasm-sqlite/src/query_builder/returning.rs b/diesel-wasm-sqlite/src/query_builder/returning.rs index fef4c8751..8709c1787 100644 --- a/diesel-wasm-sqlite/src/query_builder/returning.rs +++ b/diesel-wasm-sqlite/src/query_builder/returning.rs @@ -1,4 +1,4 @@ -use crate::storage::wasm_sqlite::backend::{SqliteReturningClause, WasmSqlite}; +use crate::backend::{SqliteReturningClause, WasmSqlite}; // use diesel::backend::Backend; use diesel::query_builder::ReturningClause; use diesel::query_builder::{AstPass, QueryFragment}; diff --git a/xmtp_mls/Cargo.toml b/xmtp_mls/Cargo.toml index 511302d5c..b25eddc58 100644 --- a/xmtp_mls/Cargo.toml +++ b/xmtp_mls/Cargo.toml @@ -14,7 +14,7 @@ grpc = ["xmtp_proto/grpc"] native = ["libsqlite3-sys/bundled-sqlcipher-vendored-openssl", "diesel_migrations/sqlite", "diesel/sqlite", "diesel/returning_clauses_for_sqlite_3_35", "tokio/rt-multi-thread", "tokio/macros"] test-utils = ["xmtp_api_grpc"] bench = ["test-utils", "indicatif", "tracing-subscriber", "anyhow", "tracing-flame", "once_cell"] -web = ["wasm-bindgen", "diesel/i-implement-a-third-party-backend-and-opt-into-breaking-changes", "openmls/js", "tokio/macros"] +web = ["openmls/js", "tokio/macros"] [dependencies] aes = "0.8.4" @@ -56,9 +56,6 @@ xmtp_v2 = { path = "../xmtp_v2" } # Native libsqlite3-sys = { version = "0.28.0", optional = true } -# Web -wasm-bindgen = { version = "0.2", optional = true } - # Test/Bench Utils xmtp_api_grpc = { path = "../xmtp_api_grpc", optional = true } tracing-subscriber = { workspace = true, optional = true }