Skip to content

Commit

Permalink
cleanup a little
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Aug 13, 2024
1 parent eae8461 commit 0a3189c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 222 deletions.
7 changes: 5 additions & 2 deletions diesel-wasm-sqlite/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ export class SQLite {
console.log("create function err");
}
}

//TODO: At some point need a way to register functions from rust
//but for just libxmtp this is fine.
register_diesel_sql_functions(database) {
try {
this.sqlite3.create_function(
Expand All @@ -314,7 +315,7 @@ export class SQLite {
(context, values) => {
const table_name = this.sqlite3.value_text(values[0]);

this.sqlite3.exec(
await this.sqlite3.exec(
context,
`CREATE TRIGGER __diesel_manage_updated_at_${table_name}
AFTER UPDATE ON ${table_name}
Expand All @@ -338,6 +339,8 @@ export class SQLite {
},
);
} catch (error) {
console.log("error creating diesel trigger");
throw error;
}
}

Expand Down
195 changes: 7 additions & 188 deletions diesel-wasm-sqlite/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use self::raw::RawConnection;
use self::stmt::{Statement, StatementUse};
use crate::query_builder::*;
use diesel::{connection::{statement_cache::StatementCacheKey, DefaultLoadingMode, LoadConnection}, deserialize::{FromSqlRow, StaticallySizedRow}, expression::QueryMetadata, query_builder::QueryBuilder as _, result::*, serialize::ToSql, sql_types::HasSqlType};
use futures::future::Either;
use futures::{FutureExt, TryFutureExt};
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -76,14 +75,7 @@ impl AsyncConnection for WasmSqliteConnection {
type Row<'conn, 'query> = SqliteRow<'conn, 'query>;

async fn establish(database_url: &str) -> diesel::prelude::ConnectionResult<Self> {
//TODO: Change to `establish_inner`
Ok(WasmSqliteConnection {
statement_cache: StmtCache::new(),
raw_connection: raw::RawConnection::establish(database_url).await.unwrap(),
transaction_state: Default::default(),
metadata_lookup: (),
instrumentation: Arc::new(Mutex::new(None))
})
WasmSqliteConnection::establish_inner(database_url).await
}

fn load<'conn, 'query, T>(&'conn mut self, _source: T) -> Self::LoadFuture<'conn, 'query>
Expand Down Expand Up @@ -280,194 +272,21 @@ impl WasmSqliteConnection {

Ok(StatementUse::bind(statement, source, self.instrumentation.as_ref())?)

}
/*
#[doc(hidden)]
pub fn register_sql_function<ArgsSqlType, RetSqlType, Args, Ret, F>(
&mut self,
fn_name: &str,
deterministic: bool,
mut f: F,
) -> QueryResult<()>
where
F: FnMut(Args) -> Ret + std::panic::UnwindSafe + Send + 'static,
Args: FromSqlRow<ArgsSqlType, WasmSqlite> + StaticallySizedRow<ArgsSqlType, WasmSqlite>,
Ret: ToSql<RetSqlType, WasmSqlite>,
WasmSqlite: HasSqlType<RetSqlType>,
{
functions::register(
&self.raw_connection,
fn_name,
deterministic,
move |_, args| f(args),
)
}

#[doc(hidden)]
pub fn register_noarg_sql_function<RetSqlType, Ret, F>(
&self,
fn_name: &str,
deterministic: bool,
f: F,
) -> QueryResult<()>
where
F: FnMut() -> Ret + std::panic::UnwindSafe + Send + 'static,
Ret: ToSql<RetSqlType, WasmSqlite>,
WasmSqlite: HasSqlType<RetSqlType>,
{
functions::register_noargs(&self.raw_connection, fn_name, deterministic, f)
}
#[doc(hidden)]
pub fn register_aggregate_function<ArgsSqlType, RetSqlType, Args, Ret, A>(
&mut self,
fn_name: &str,
) -> QueryResult<()>
where
A: SqliteAggregateFunction<Args, Output = Ret> + 'static + Send + std::panic::UnwindSafe,
Args: FromSqlRow<ArgsSqlType, Sqlite> + StaticallySizedRow<ArgsSqlType, Sqlite>,
Ret: ToSql<RetSqlType, Sqlite>,
Sqlite: HasSqlType<RetSqlType>,
{
functions::register_aggregate::<_, _, _, _, A>(&self.raw_connection, fn_name)
}
/// Register a collation function.
///
/// `collation` must always return the same answer given the same inputs.
/// If `collation` panics and unwinds the stack, the process is aborted, since it is used
/// across a C FFI boundary, which cannot be unwound across and there is no way to
/// signal failures via the SQLite interface in this case..
///
/// If the name is already registered it will be overwritten.
///
/// This method will return an error if registering the function fails, either due to an
/// out-of-memory situation or because a collation with that name already exists and is
/// currently being used in parallel by a query.
///
/// The collation needs to be specified when creating a table:
/// `CREATE TABLE my_table ( str TEXT COLLATE MY_COLLATION )`,
/// where `MY_COLLATION` corresponds to name passed as `collation_name`.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # let mut conn = SqliteConnection::establish(":memory:").unwrap();
/// // sqlite NOCASE only works for ASCII characters,
/// // this collation allows handling UTF-8 (barring locale differences)
/// conn.register_collation("RUSTNOCASE", |rhs, lhs| {
/// rhs.to_lowercase().cmp(&lhs.to_lowercase())
/// })
/// # }
/// ```
pub fn register_collation<F>(&mut self, collation_name: &str, collation: F) -> QueryResult<()>
where
F: Fn(&str, &str) -> std::cmp::Ordering + Send + 'static + std::panic::UnwindSafe,
{
self.raw_connection
.register_collation_function(collation_name, collation)
}
/// Serialize the current SQLite database into a byte buffer.
///
/// The serialized data is identical to the data that would be written to disk if the database
/// was saved in a file.
///
/// # Returns
///
/// This function returns a byte slice representing the serialized database.
pub fn serialize_database_to_buffer(&mut self) -> SerializedDatabase {
self.raw_connection.serialize()
}
/// Deserialize an SQLite database from a byte buffer.
///
/// This function takes a byte slice and attempts to deserialize it into a SQLite database.
/// If successful, the database is loaded into the connection. If the deserialization fails,
/// an error is returned.
///
/// The database is opened in READONLY mode.
///
/// # Example
///
/// ```no_run
/// # use diesel::sqlite::SerializedDatabase;
/// # use diesel::sqlite::SqliteConnection;
/// # use diesel::result::QueryResult;
/// # use diesel::sql_query;
/// # use diesel::Connection;
/// # use diesel::RunQueryDsl;
/// # fn main() {
/// let connection = &mut SqliteConnection::establish(":memory:").unwrap();
///
/// sql_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)")
/// .execute(connection).unwrap();
/// sql_query("INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]'), ('Jane Doe', '[email protected]')")
/// .execute(connection).unwrap();
///
/// // Serialize the database to a byte vector
/// let serialized_db: SerializedDatabase = connection.serialize_database_to_buffer();
///
/// // Create a new in-memory SQLite database
/// let connection = &mut SqliteConnection::establish(":memory:").unwrap();
///
/// // Deserialize the byte vector into the new database
/// connection.deserialize_readonly_database_from_buffer(serialized_db.as_slice()).unwrap();
/// #
/// # }
/// ```
pub fn deserialize_readonly_database_from_buffer(&mut self, data: &[u8]) -> QueryResult<()> {
self.raw_connection.deserialize(data)
}
*/
/*
async fn register_diesel_sql_functions(&self) -> QueryResult<()> {
use diesel::sql_types::{Integer, Text};
functions::register::<Text, Integer, _, _, _>(
&self.raw_connection,
"diesel_manage_updated_at",
false,
|conn, table_name: String| async {
conn.exec(&format!(
include_str!("diesel_manage_updated_at.sql"),
table_name = table_name
))
.await
.expect("Failed to create trigger");
0 // have to return *something*
}.boxed(),
)
}
*/

async fn establish_inner(database_url: &str) -> Result<WasmSqliteConnection, ConnectionError> {
use diesel::result::ConnectionError::CouldntSetupConfiguration;
let raw_connection = RawConnection::establish(database_url).await.unwrap();
let conn = Self {
let sqlite3 = crate::get_sqlite().await;

sqlite3.register_diesel_sql_functions(&raw_connection.internal_connection).map_err(WasmSqliteError::from)?;

Ok(Self {
statement_cache: StmtCache::new(),
raw_connection,
transaction_state: AnsiTransactionManager::default(),
metadata_lookup: (),
instrumentation: Arc::new(Mutex::new(None)),
};
/*
conn.register_diesel_sql_functions()
.map_err(CouldntSetupConfiguration).await?;
*/
Ok(conn)
})
}
}

/*
fn error_message(err_code: i32) -> &'static str {
let sqlite3 = diesel::get_sqlite_unchecked();
sqlite3.code_to_str(err_code)
}
*/
32 changes: 2 additions & 30 deletions diesel-wasm-sqlite/src/connection/raw.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
#![allow(unsafe_code)] // ffi calls

// use std::io::{stderr, Write};

// use super::functions::{build_sql_function_args, process_sql_function_result};
// use super::serialized_database::SerializedDatabase;
// use super::stmt::ensure_sqlite_ok;
// use super::{Sqlite, SqliteAggregateFunction};
// use crate::deserialize::FromSqlRow;
// use crate::result::Error::DatabaseError;
use crate::{
sqlite_types::{SqliteFlags, SqliteOpenFlags},
SqliteType, WasmSqlite, WasmSqliteError,
};
use diesel::{
connection::statement_cache::PrepareForCache,
result::*,
serialize::ToSql,
sql_types::{HasSqlType, TypeMetadata},
connection::statement_cache::PrepareForCache, result::*, serialize::ToSql,
sql_types::HasSqlType,
};
use futures::future::BoxFuture;
use tokio::sync::oneshot;
use wasm_bindgen::{closure::Closure, JsValue};

use super::stmt::Statement;

/*
/// For use in FFI function, which cannot unwind.
/// Print the message, ask to open an issue at Github and [`abort`](std::process::abort).
macro_rules! assert_fail {
($fmt:expr $(,$args:tt)*) => {
eprint!(concat!(
$fmt,
"If you see this message, please open an issue at https://github.com/diesel-rs/diesel/issues/new.\n",
"Source location: {}:{}\n",
), $($args,)* file!(), line!());
std::process::abort()
};
}
*/

#[allow(missing_copy_implementations)]
#[derive(Debug)]
pub(super) struct RawConnection {
Expand Down
7 changes: 5 additions & 2 deletions diesel-wasm-sqlite/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub mod consts {
// Constants
#[wasm_bindgen(module = "/src/wa-sqlite-diesel-bundle.js")]
extern "C" {
pub static SqliteDone: i32;
pub static SqliteRow: i32;
pub static SQLITE_DONE: i32;
pub static SQLITE_ROW: i32;

// Fundamental datatypes.
// https://www.sqlite.org/c3ref/c_blob.html
Expand Down Expand Up @@ -208,6 +208,9 @@ extern "C" {
x_step: Option<&Closure<dyn FnMut(JsValue, Vec<JsValue>) -> JsValue>>,
x_final: Option<&Closure<dyn FnMut(JsValue)>>,
) -> Result<(), JsValue>;

#[wasm_bindgen(method, catch)]
pub fn register_diesel_sql_functions(this: &SQLite, database: &JsValue) -> Result<(), JsValue>;
}

impl std::fmt::Debug for SQLite {
Expand Down

0 comments on commit 0a3189c

Please sign in to comment.