Skip to content

Commit

Permalink
add connection files, compile a few fns in raw connection
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Jul 25, 2024
1 parent c7a3731 commit d145da6
Show file tree
Hide file tree
Showing 22 changed files with 3,765 additions and 216 deletions.
81 changes: 75 additions & 6 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion diesel-wasm-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ version = "0.1.1"
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"] }
# diesel = { git = "https://github.com/xmtp/diesel", branch = "insipx/wasm-backend", default-features = false, features = ["i-implement-a-third-party-backend-and-opt-into-breaking-changes"] }
diesel = "2.2"
diesel-async = { git = "https://github.com/insipx/diesel_async", branch = "insipx/wasm-async" }
wasm-bindgen = "0.2.92"
wasm-bindgen-futures = "0.4.42"
log = "0.4"
Expand All @@ -15,6 +17,8 @@ web-sys = { version = "0.3", features = ["console"] }
console_error_panic_hook = { version = "0.1", optional = true }
tokio = { version = "1.38", default-features = false, features = ["rt", "macros", "sync", "io-util", "time"] }
futures = "0.3"
async-trait = "0.1"
bitflags = "2.6"

[dev-dependencies]
rand = "0.8"
Expand All @@ -28,3 +32,4 @@ crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

17 changes: 6 additions & 11 deletions diesel-wasm-sqlite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ use std::env;
use std::process::Command;

fn main() {
// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo::rerun-if-changed=package.js");
// println!("cargo::rerun-if-changed=package.js");
// can run yarn run build here too
// let out_dir = env::var("OUT_DIR").unwrap();

Command::new("yarn")
.args(["run", "build"])
.status()
.unwrap();
/*
Command::new("cp")
.args(["src/wa-sqlite.wasm"])
.arg(&format!("{}/wa-sqlite.wasm", out_dir))
.status()
.unwrap();
*/

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if target_arch != "wasm32" {
// Emit a compile error if the target is not wasm32-unknown-unknown
panic!("This crate only supports the wasm32 architecture");
}
}
107 changes: 68 additions & 39 deletions diesel-wasm-sqlite/package.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as SQLite from "@xmtp/wa-sqlite";
import * as WasmSQLiteLibrary from "@xmtp/wa-sqlite";
import SQLiteESMFactory from "./node_modules/@xmtp/wa-sqlite/dist/wa-sqlite.mjs";
import base64Wasm from "./node_modules/@xmtp/wa-sqlite/dist/wa-sqlite.wasm";

Expand All @@ -14,53 +14,82 @@ function base64Decode(str) {
return bytes.buffer;
}

const module = await SQLiteESMFactory({
"wasmBinary": base64Decode(base64Wasm),
});
export class SQLite {
#module;
#sqlite3;
constructor(module) {
if (typeof module === "undefined") {
throw new Error("Cannot be called directly");
}

// const module = await initWasmModule();
const sqlite3 = SQLite.Factory(module);
this.sqlite3 = WasmSQLiteLibrary.Factory(module);
}

export function sqlite3_result_text(context, value) {
sqlite3.result_text(context, value);
}
static async wasm_module() {
return await SQLiteESMFactory({
"wasmBinary": base64Decode(base64Wasm),
});
}

export function sqlite3_result_int(context, value) {
sqlite3.result_int(context, value);
}
static async build() {
const module = await SQLiteESMFactory({
"wasmBinary": base64Decode(base64Wasm),
});
return new WasmSQLiteLibrary(module);
}

export function sqlite3_result_int64(context, value) {
sqlite3.result_int64(context, value);
}
result_text(context, value) {
this.sqlite3.result_text(context, value);
}

export function sqlite3_result_double(context, value) {
sqlite3.result_double(context, value);
}
result_int(context, value) {
this.sqlite3.result_int(context, value);
}

export function sqlite3_result_blob(context, value) {
sqlite3.result_blob(context, value);
}
result_int64(context, value) {
this.sqlite3.result_int64(context, value);
}

export function sqlite3_result_null(context) {
sqlite3.result_null(context);
}
result_double(context, value) {
this.sqlite3.result_double(context, value);
}

export async function establish(database_url) {
try {
console.log("Opening database!", database_url);
let db = await sqlite3.open_v2(database_url);
console.log(db);
return db;
} catch {
console.log("establish err");
result_blob(context, value) {
this.sqlite3.result_blob(context, value);
}

result_null(context) {
this.sqlite3.result_null(context);
}

async open_v2(database_url, iflags) {
try {
console.log("Opening database!", database_url);
let db = await this.sqlite3.open_v2(database_url, iflags);
return db;
} catch {
console.log("openv2 error");
}
}

async exec(db, query) {
try {
return await this.sqlite3.exec(db, query);
} catch {
console.log('exec err');
}
}

changes(db) {
return this.sqlite3.changes(db);
}
}

export function batch_execute(database, query) {
try {
sqlite3.exec(database, query);
console.log("Batch exec'ed");
} catch {
console.log("exec err");
batch_execute(database, query) {
try {
sqlite3.exec(database, query);
console.log("Batch exec'ed");
} catch {
console.log("exec err");
}
}
}
2 changes: 0 additions & 2 deletions diesel-wasm-sqlite/src/connection.rs

This file was deleted.

22 changes: 9 additions & 13 deletions diesel-wasm-sqlite/src/connection/bind_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,16 @@ impl std::fmt::Display for InternalSqliteBindValue<'_> {
impl InternalSqliteBindValue<'_> {
#[allow(unsafe_code)] // ffi function calls
pub(crate) fn result_of(self, ctx: &mut i32) {
use crate::ffi;
let sqlite3 = crate::get_sqlite_unchecked();
match self {
InternalSqliteBindValue::BorrowedString(s) => {
ffi::sqlite3_result_text(*ctx, s.to_string())
}
InternalSqliteBindValue::String(s) => ffi::sqlite3_result_text(*ctx, s.to_string()),
InternalSqliteBindValue::Binary(b) => ffi::sqlite3_result_blob(*ctx, b.to_vec()),
InternalSqliteBindValue::BorrowedBinary(b) => {
ffi::sqlite3_result_blob(*ctx, b.to_vec())
}
InternalSqliteBindValue::I32(i) => ffi::sqlite3_result_int(*ctx, i),
InternalSqliteBindValue::I64(l) => ffi::sqlite3_result_int64(*ctx, l),
InternalSqliteBindValue::F64(d) => ffi::sqlite3_result_double(*ctx, d),
InternalSqliteBindValue::Null => ffi::sqlite3_result_null(*ctx),
InternalSqliteBindValue::BorrowedString(s) => sqlite3.result_text(*ctx, s.to_string()),
InternalSqliteBindValue::String(s) => sqlite3.result_text(*ctx, s.to_string()),
InternalSqliteBindValue::Binary(b) => sqlite3.result_blob(*ctx, b.to_vec()),
InternalSqliteBindValue::BorrowedBinary(b) => sqlite3.result_blob(*ctx, b.to_vec()),
InternalSqliteBindValue::I32(i) => sqlite3.result_int(*ctx, i),
InternalSqliteBindValue::I64(l) => sqlite3.result_int64(*ctx, l),
InternalSqliteBindValue::F64(d) => sqlite3.result_double(*ctx, d),
InternalSqliteBindValue::Null => sqlite3.result_null(*ctx),
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions diesel-wasm-sqlite/src/connection/diesel_manage_updated_at.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TRIGGER __diesel_manage_updated_at_{table_name}
AFTER UPDATE ON {table_name}
FOR EACH ROW WHEN
old.updated_at IS NULL AND
new.updated_at IS NULL OR
old.updated_at == new.updated_at
BEGIN
UPDATE {table_name}
SET updated_at = CURRENT_TIMESTAMP
WHERE ROWID = new.ROWID;
END
Loading

0 comments on commit d145da6

Please sign in to comment.