Skip to content

Commit

Permalink
cleanup + crate
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Jul 17, 2024
1 parent 797b1bd commit 5981d65
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 104 deletions.
6 changes: 5 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions diesel-wasm-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 6 additions & 6 deletions diesel-wasm-sqlite/src/connection/bind_collector.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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};
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() }
}
}
Expand All @@ -21,7 +21,7 @@ impl SqliteBindCollector<'_> {
/// It can be constructed via the various `From<T>` implementations
#[derive(Debug)]
pub struct SqliteBindValue<'a> {
pub(in crate::storage::wasm_sqlite) inner: InternalSqliteBindValue<'a>,
pub(crate) inner: InternalSqliteBindValue<'a>,
}

impl<'a> From<i32> for SqliteBindValue<'a> {
Expand Down Expand Up @@ -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())
Expand Down
86 changes: 77 additions & 9 deletions diesel-wasm-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
ffi::establish(database_url)
.map_err(WasmSqliteError::from)
.map_err(Into::<diesel::result::ConnectionError>::into)?;
Ok(WasmSqliteConnection {})
}

fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>
where
T: QueryFragment<Self::Backend> + QueryId,
{
todo!()
}

fn transaction_state(
&mut self,
) -> &mut <Self::TransactionManager as TransactionManager<Self>>::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<WasmSqliteError> for diesel::result::Error {
fn from(value: WasmSqliteError) -> diesel::result::Error {
log::error!("NOT IMPLEMENTED, {:?}", value);
diesel::result::Error::NotFound
}
}

impl From<WasmSqliteError> 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<JsValue> for WasmSqliteError {
fn from(err: JsValue) -> WasmSqliteError {
WasmSqliteError(err)
}
}
82 changes: 0 additions & 82 deletions diesel-wasm-sqlite/src/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion diesel-wasm-sqlite/src/query_builder/limit_offset.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion diesel-wasm-sqlite/src/query_builder/returning.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
5 changes: 1 addition & 4 deletions xmtp_mls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit 5981d65

Please sign in to comment.