diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 4b405dfa02..d87dd846e7 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -267,6 +267,9 @@ jobs: - name: Setup Rust run: rustup show active-toolchain || rustup toolchain install + + - name: Add wasm32-unknown-unknown toolchain + run: rustup target add wasm32-unknown-unknown - uses: Swatinem/rust-cache@v2 @@ -279,3 +282,13 @@ jobs: env: DATABASE_URL: sqlite://todos.sqlite run: cargo run -p sqlx-example-sqlite-todos + + - name: TODOs WASM (Setup) + env: + DATABASE_URL: sqlite://todos-wasm.sqlite + run: sqlx db setup --source=examples/sqlite/todos-wasm/migrations + + - name: TODOs WASM (Build) + env: + DATABASE_URL: sqlite://todos-wasm.sqlite + run: cargo build -p sqlx-example-sqlite-todos-wasm --target wasm32-unknown-unknown diff --git a/Cargo.lock b/Cargo.lock index 74f9054912..c30dd14403 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1374,9 +1374,9 @@ dependencies = [ [[package]] name = "fragile" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa" +checksum = "28dd6caf6059519a65843af8fe2a3ae298b14b80179855aeb4adc2c1934ee619" [[package]] name = "fs_extra" @@ -1698,7 +1698,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.5.8", "tokio", "tower-service", "tracing", @@ -1925,6 +1925,17 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "io-uring" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "046fa2d4d00aea763528b4950358d0ead425372445dc8ff86312b3c69ff7727b" +dependencies = [ + "bitflags 2.7.0", + "cfg-if", + "libc", +] + [[package]] name = "ipnet" version = "2.10.1" @@ -1998,9 +2009,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.76" +version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" dependencies = [ "once_cell", "wasm-bindgen", @@ -2032,9 +2043,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.169" +version = "0.2.175" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" [[package]] name = "libloading" @@ -2363,9 +2374,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.20.2" +version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "oorandom" @@ -3354,6 +3365,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "socket2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233504af464074f9d066d7b5416c5f9b894a5862a6506e306f7b816cdd6f1807" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "spin" version = "0.9.8" @@ -3373,6 +3394,24 @@ dependencies = [ "der", ] +[[package]] +name = "sqlite-wasm-rs" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0894a1b91dc660fbf1e6ea6f287562708e01ca1a18fa4e2c6dae0df5a05199c5" +dependencies = [ + "fragile", + "js-sys", + "once_cell", + "parking_lot", + "thiserror 2.0.16", + "tokio", + "wasm-array-cp", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "sqlx" version = "0.9.0-alpha.1" @@ -3465,7 +3504,7 @@ dependencies = [ "sha2", "smallvec", "sqlx", - "thiserror 2.0.11", + "thiserror 2.0.16", "time", "tokio", "tokio-stream", @@ -3500,7 +3539,7 @@ dependencies = [ "serde_json", "serde_with", "sqlx", - "thiserror 2.0.11", + "thiserror 2.0.16", "time", "tokio", "tower", @@ -3720,6 +3759,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "sqlx-example-sqlite-todos-wasm" +version = "0.1.0" +dependencies = [ + "anyhow", + "sqlx", + "tokio", +] + [[package]] name = "sqlx-macros" version = "0.9.0-alpha.1" @@ -3792,7 +3840,7 @@ dependencies = [ "sqlx", "sqlx-core", "stringprep", - "thiserror 2.0.11", + "thiserror 2.0.16", "time", "tracing", "uuid", @@ -3837,7 +3885,7 @@ dependencies = [ "sqlx", "sqlx-core", "stringprep", - "thiserror 2.0.11", + "thiserror 2.0.16", "time", "tracing", "uuid", @@ -3862,9 +3910,10 @@ dependencies = [ "regex", "serde", "serde_urlencoded", + "sqlite-wasm-rs", "sqlx", "sqlx-core", - "thiserror 2.0.11", + "thiserror 2.0.16", "time", "tracing", "url", @@ -4127,11 +4176,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.11" +version = "2.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" +checksum = "3467d614147380f2e4e374161426ff399c91084acd2363eaf549172b3d5e60c0" dependencies = [ - "thiserror-impl 2.0.11", + "thiserror-impl 2.0.16", ] [[package]] @@ -4147,9 +4196,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.11" +version = "2.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" +checksum = "6c5e1be1c48b9172ee610da68fd9cd2770e7a4056cb3fc98710ee6906f0c7960" dependencies = [ "proc-macro2", "quote", @@ -4234,20 +4283,22 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.43.0" +version = "1.47.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e" +checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038" dependencies = [ "backtrace", "bytes", + "io-uring", "libc", "mio 1.0.3", "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2", + "slab", + "socket2 0.6.0", "tokio-macros", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -4690,22 +4741,33 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" +[[package]] +name = "wasm-array-cp" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb633b3e235f0ebe0a35162adc1e0293fc4b7e3f3a6fc7b5374d80464267ff84" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "wasm-bindgen" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" dependencies = [ "cfg-if", "once_cell", + "rustversion", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" dependencies = [ "bumpalo", "log", @@ -4717,9 +4779,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.49" +version = "0.4.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2" +checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61" dependencies = [ "cfg-if", "js-sys", @@ -4730,9 +4792,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -4740,9 +4802,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", @@ -4753,15 +4815,18 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] [[package]] name = "web-sys" -version = "0.3.76" +version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" +checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 9c37eb4a5d..ab8a1d912f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ members = [ "examples/postgres/transaction", "examples/sqlite/todos", "examples/sqlite/extension", + "examples/sqlite/todos-wasm", ] [workspace.package] diff --git a/examples/sqlite/todos-wasm/Cargo.toml b/examples/sqlite/todos-wasm/Cargo.toml new file mode 100644 index 0000000000..decff7dae4 --- /dev/null +++ b/examples/sqlite/todos-wasm/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "sqlx-example-sqlite-todos-wasm" +version = "0.1.0" +edition = "2018" +workspace = "../../../" + +[dependencies] +anyhow = "1.0" +sqlx = { path = "../../../", features = [ "sqlite-bundled", "sqlite-deserialize", "sqlite-unlock-notify", "tls-none" ] } +tokio = { version = "1.20.0", features = ["rt", "macros"]} diff --git a/examples/sqlite/todos-wasm/README.md b/examples/sqlite/todos-wasm/README.md new file mode 100644 index 0000000000..8bbb4d15be --- /dev/null +++ b/examples/sqlite/todos-wasm/README.md @@ -0,0 +1,41 @@ +# TODOs Example + +## Setup + +1. Declare the database URL + + ``` + export DATABASE_URL="sqlite:todos.db" + ``` + +2. Create the database. + + ``` + $ sqlx db create + ``` + +3. Run sql migrations + + ``` + $ sqlx migrate run + ``` + +## Usage + +Add a todo + +``` +cargo run -- add "todo description" +``` + +Complete a todo. + +``` +cargo run -- done +``` + +List all todos + +``` +cargo run +``` diff --git a/examples/sqlite/todos-wasm/migrations/20200718111257_todos.sql b/examples/sqlite/todos-wasm/migrations/20200718111257_todos.sql new file mode 100644 index 0000000000..5248bb9a44 --- /dev/null +++ b/examples/sqlite/todos-wasm/migrations/20200718111257_todos.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS todos +( + id INTEGER PRIMARY KEY NOT NULL, + description TEXT NOT NULL, + done BOOLEAN NOT NULL DEFAULT 0 +); diff --git a/examples/sqlite/todos-wasm/src/main.rs b/examples/sqlite/todos-wasm/src/main.rs new file mode 100644 index 0000000000..4f7a3c6e03 --- /dev/null +++ b/examples/sqlite/todos-wasm/src/main.rs @@ -0,0 +1,67 @@ +use sqlx::sqlite::SqlitePool; + +const DATABASE_URL: &str = "file:session?vfs=kvvs"; + +#[tokio::main(flavor = "current_thread")] +async fn main() -> anyhow::Result<()> { + let pool = SqlitePool::connect(DATABASE_URL).await?; + + Ok(()) +} + +async fn add_todo(pool: &SqlitePool, description: String) -> anyhow::Result { + let mut conn = pool.acquire().await?; + + // Insert the task, then obtain the ID of this row + let id = sqlx::query!( + r#" +INSERT INTO todos ( description ) +VALUES ( ?1 ) + "#, + description + ) + .execute(&mut *conn) + .await? + .last_insert_rowid(); + + Ok(id) +} + +async fn complete_todo(pool: &SqlitePool, id: i64) -> anyhow::Result { + let rows_affected = sqlx::query!( + r#" +UPDATE todos +SET done = TRUE +WHERE id = ?1 + "#, + id + ) + .execute(pool) + .await? + .rows_affected(); + + Ok(rows_affected > 0) +} + +async fn list_todos(pool: &SqlitePool) -> anyhow::Result<()> { + let recs = sqlx::query!( + r#" +SELECT id, description, done +FROM todos +ORDER BY id + "# + ) + .fetch_all(pool) + .await?; + + for rec in recs { + println!( + "- [{}] {}: {}", + if rec.done { "x" } else { " " }, + rec.id, + &rec.description, + ); + } + + Ok(()) +} diff --git a/examples/x.py b/examples/x.py index aaf4170c77..bc8f6355d6 100755 --- a/examples/x.py +++ b/examples/x.py @@ -21,6 +21,9 @@ parser = argparse.ArgumentParser() parser.add_argument("-p", "--project") parser.add_argument("-l", "--list-projects", action="store_true") +parser.add_argument("-b", "--build", action="store_true") +parser.add_argument("--locked", action="store_true") +parser.add_argument("--offline", action="store_true") argv, unknown = parser.parse_known_args() @@ -47,7 +50,7 @@ def sqlx(command, url, cwd=None): display=f"sqlx {command}") -def project(name, database=None, driver=None): +def project(name, database=None, driver=None, target=None): if argv.list_projects: print(f"{name}") return @@ -77,8 +80,21 @@ def project(name, database=None, driver=None): # migrate sqlx("migrate run", database_url, cwd=cwd) + args = "" + if target is not None: + args += f" --target {target}" + + if argv.locked: + args += " --locked" + + if argv.offline: + args += " --offline" + # check - run("cargo check", cwd=cwd, env=env) + run("cargo check" + args, cwd=cwd, env=env) + + if argv.build: + run("cargo build" + args, cwd=cwd, env=env) # todos @@ -86,3 +102,4 @@ def project(name, database=None, driver=None): project("postgres/todos", driver="postgres_12", database="todos") project("sqlite/todos", driver="sqlite", database="todos.db") project("sqlite/extension", driver="sqlite", database="extension.db") +project("sqlite/todos-wasm", driver="sqlite", database="wasm.db", target="wasm32-unknown-unknown") diff --git a/sqlx-sqlite/Cargo.toml b/sqlx-sqlite/Cargo.toml index 4508e19ff6..3c3e9c2bab 100644 --- a/sqlx-sqlite/Cargo.toml +++ b/sqlx-sqlite/Cargo.toml @@ -51,10 +51,10 @@ _unstable-all-sqlite-features = [ _unstable-docs = [ "bundled", "any", "_unstable-all-types", - "_unstable-all-sqlite-features" + "_unstable-all-sqlite-features", ] -[dependencies.libsqlite3-sys] +[target.'cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))'.dependencies.libsqlite3-sys] # See `sqlx-sqlite/src/lib.rs` for details. version = ">=0.30.0, <0.36.0" default-features = false @@ -63,6 +63,9 @@ features = [ "vcpkg", ] +[target.'cfg(any(target_arch = "wasm32", target_arch = "wasm64"))'.dependencies] +sqlite-wasm-rs = { version = "0.4.3", default-features = false, features = ["precompiled"] } + [dependencies] futures-core = { version = "0.3.19", default-features = false } futures-channel = { version = "0.3.19", default-features = false, features = ["sink", "alloc", "std"] } diff --git a/sqlx-sqlite/src/arguments.rs b/sqlx-sqlite/src/arguments.rs index 6354cbebc9..6b48300e6a 100644 --- a/sqlx-sqlite/src/arguments.rs +++ b/sqlx-sqlite/src/arguments.rs @@ -3,7 +3,9 @@ use crate::error::Error; use crate::statement::StatementHandle; use crate::Sqlite; use atoi::atoi; -use libsqlite3_sys::SQLITE_OK; + +use crate::sqlite_lib::SQLITE_OK; + use std::sync::Arc; pub(crate) use sqlx_core::arguments::*; diff --git a/sqlx-sqlite/src/connection/collation.rs b/sqlx-sqlite/src/connection/collation.rs index e7422138bc..0b56728f5b 100644 --- a/sqlx-sqlite/src/connection/collation.rs +++ b/sqlx-sqlite/src/connection/collation.rs @@ -6,7 +6,7 @@ use std::slice; use std::str::from_utf8_unchecked; use std::sync::Arc; -use libsqlite3_sys::{sqlite3_create_collation_v2, SQLITE_OK, SQLITE_UTF8}; +use crate::sqlite_lib::{sqlite3_create_collation_v2, SQLITE_OK, SQLITE_UTF8}; use crate::connection::handle::ConnectionHandle; use crate::error::Error; diff --git a/sqlx-sqlite/src/connection/deserialize.rs b/sqlx-sqlite/src/connection/deserialize.rs index 5056eb6784..ab67074fec 100644 --- a/sqlx-sqlite/src/connection/deserialize.rs +++ b/sqlx-sqlite/src/connection/deserialize.rs @@ -1,10 +1,10 @@ use super::ConnectionState; -use crate::{error::Error, SqliteConnection, SqliteError}; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3_deserialize, sqlite3_free, sqlite3_malloc64, sqlite3_serialize, SQLITE_DESERIALIZE_FREEONCLOSE, SQLITE_DESERIALIZE_READONLY, SQLITE_DESERIALIZE_RESIZEABLE, SQLITE_NOMEM, SQLITE_OK, }; +use crate::{error::Error, SqliteConnection, SqliteError}; use std::ffi::c_char; use std::fmt::Debug; use std::{ diff --git a/sqlx-sqlite/src/connection/establish.rs b/sqlx-sqlite/src/connection/establish.rs index d811275409..344667959d 100644 --- a/sqlx-sqlite/src/connection/establish.rs +++ b/sqlx-sqlite/src/connection/establish.rs @@ -2,12 +2,12 @@ use crate::connection::handle::ConnectionHandle; use crate::connection::LogSettings; use crate::connection::{ConnectionState, Statements}; use crate::error::Error; -use crate::SqliteConnectOptions; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3_busy_timeout, SQLITE_OPEN_CREATE, SQLITE_OPEN_FULLMUTEX, SQLITE_OPEN_MEMORY, SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE, SQLITE_OPEN_SHAREDCACHE, SQLITE_OPEN_URI, }; +use crate::SqliteConnectOptions; use percent_encoding::NON_ALPHANUMERIC; use std::collections::BTreeMap; use std::ffi::CString; @@ -163,7 +163,7 @@ impl EstablishParams { if self.register_regexp_function { // configure a `regexp` function for sqlite, it does not come with one by default let status = crate::regexp::register(handle.as_ptr()); - if status != libsqlite3_sys::SQLITE_OK { + if status != crate::sqlite_lib::SQLITE_OK { return Err(Error::Database(Box::new(handle.expect_error()))); } } @@ -193,7 +193,7 @@ impl EstablishParams { #[cfg(feature = "load-extension")] unsafe fn apply_extensions(&self, handle: &mut ConnectionHandle) -> Result<(), Error> { - use libsqlite3_sys::{sqlite3_free, sqlite3_load_extension}; + use crate::sqlite_lib::{sqlite3_free, sqlite3_load_extension}; use std::ffi::{c_int, CStr}; use std::ptr; @@ -202,7 +202,7 @@ impl EstablishParams { handle: &mut ConnectionHandle, enabled: bool, ) -> Result<(), Error> { - use libsqlite3_sys::{sqlite3_db_config, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION}; + use crate::sqlite_lib::{sqlite3_db_config, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION}; // SAFETY: we have exclusive access and this matches the expected signature // diff --git a/sqlx-sqlite/src/connection/handle.rs b/sqlx-sqlite/src/connection/handle.rs index 7df3b1b717..19e20019c2 100644 --- a/sqlx-sqlite/src/connection/handle.rs +++ b/sqlx-sqlite/src/connection/handle.rs @@ -3,7 +3,7 @@ use std::ptr::NonNull; use std::{io, ptr}; use crate::error::Error; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3, sqlite3_close, sqlite3_exec, sqlite3_extended_result_codes, sqlite3_get_autocommit, sqlite3_last_insert_rowid, sqlite3_open_v2, SQLITE_OK, }; @@ -122,7 +122,7 @@ impl ConnectionHandle { match status { SQLITE_OK => return Ok(()), #[cfg(feature = "unlock-notify")] - libsqlite3_sys::SQLITE_LOCKED_SHAREDCACHE => { + crate::sqlite_lib::SQLITE_LOCKED_SHAREDCACHE => { crate::statement::unlock_notify::wait(self.as_ptr())? } _ => return Err(SqliteError::new(self.as_ptr()).into()), diff --git a/sqlx-sqlite/src/connection/mod.rs b/sqlx-sqlite/src/connection/mod.rs index 218c747143..ef2de5bb98 100644 --- a/sqlx-sqlite/src/connection/mod.rs +++ b/sqlx-sqlite/src/connection/mod.rs @@ -8,11 +8,11 @@ use std::panic::catch_unwind; use std::ptr; use std::ptr::NonNull; -use futures_intrusive::sync::MutexGuard; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3, sqlite3_commit_hook, sqlite3_progress_handler, sqlite3_rollback_hook, sqlite3_update_hook, SQLITE_DELETE, SQLITE_INSERT, SQLITE_UPDATE, }; +use futures_intrusive::sync::MutexGuard; #[cfg(feature = "preupdate-hook")] pub use preupdate_hook::*; @@ -149,7 +149,11 @@ impl ConnectionState { pub(crate) fn remove_preupdate_hook(&mut self) { if let Some(mut handler) = self.preupdate_hook_callback.take() { unsafe { - libsqlite3_sys::sqlite3_preupdate_hook(self.handle.as_ptr(), None, ptr::null_mut()); + crate::sqlite_lib::sqlite3_preupdate_hook( + self.handle.as_ptr(), + None, + ptr::null_mut(), + ); let _ = { Box::from_raw(handler.0.as_mut()) }; } } @@ -457,7 +461,7 @@ impl LockedSqliteHandle<'_> { self.guard.remove_preupdate_hook(); self.guard.preupdate_hook_callback = Some(PreupdateHookHandler(callback)); - libsqlite3_sys::sqlite3_preupdate_hook( + crate::sqlite_lib::sqlite3_preupdate_hook( self.as_raw_handle().as_mut(), Some(preupdate_hook::), handler, diff --git a/sqlx-sqlite/src/connection/preupdate_hook.rs b/sqlx-sqlite/src/connection/preupdate_hook.rs index 2759588873..94b2b6ce49 100644 --- a/sqlx-sqlite/src/connection/preupdate_hook.rs +++ b/sqlx-sqlite/src/connection/preupdate_hook.rs @@ -1,7 +1,7 @@ use super::SqliteOperation; use crate::{SqliteError, SqliteValueRef}; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3, sqlite3_preupdate_count, sqlite3_preupdate_depth, sqlite3_preupdate_new, sqlite3_preupdate_old, sqlite3_value, SQLITE_OK, }; diff --git a/sqlx-sqlite/src/error.rs b/sqlx-sqlite/src/error.rs index b4373d7a07..9b8c2ff8f3 100644 --- a/sqlx-sqlite/src/error.rs +++ b/sqlx-sqlite/src/error.rs @@ -4,7 +4,7 @@ use std::fmt::{self, Display, Formatter}; use std::os::raw::c_int; use std::{borrow::Cow, str}; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3, sqlite3_errmsg, sqlite3_errstr, sqlite3_extended_errcode, SQLITE_CONSTRAINT_CHECK, SQLITE_CONSTRAINT_FOREIGNKEY, SQLITE_CONSTRAINT_NOTNULL, SQLITE_CONSTRAINT_PRIMARYKEY, SQLITE_CONSTRAINT_UNIQUE, SQLITE_ERROR, SQLITE_NOMEM, diff --git a/sqlx-sqlite/src/lib.rs b/sqlx-sqlite/src/lib.rs index 17b4bed1ca..2fa18ff74d 100644 --- a/sqlx-sqlite/src/lib.rs +++ b/sqlx-sqlite/src/lib.rs @@ -72,6 +72,18 @@ #[macro_use] extern crate sqlx_core; +#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] +pub(crate) use libsqlite3_sys as sqlite_lib; + +#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] +pub(crate) use sqlite_wasm_rs as sqlite_lib; + +#[cfg(all( + feature = "load-extension", + any(target_arch = "wasm32", target_arch = "wasm64") +))] +compile_error!("Sqlite on WASM does not support `load-extension`"); + use std::sync::atomic::AtomicBool; pub use arguments::{SqliteArgumentValue, SqliteArguments, SqliteArgumentsBuffer}; diff --git a/sqlx-sqlite/src/regexp.rs b/sqlx-sqlite/src/regexp.rs index eb14fffc77..a91a481732 100644 --- a/sqlx-sqlite/src/regexp.rs +++ b/sqlx-sqlite/src/regexp.rs @@ -14,7 +14,8 @@ //! - //! - -use libsqlite3_sys as ffi; +use crate::sqlite_lib as ffi; + use log::error; use regex::Regex; use std::sync::Arc; diff --git a/sqlx-sqlite/src/statement/handle.rs b/sqlx-sqlite/src/statement/handle.rs index 7985ff9d36..5461f353af 100644 --- a/sqlx-sqlite/src/statement/handle.rs +++ b/sqlx-sqlite/src/statement/handle.rs @@ -4,7 +4,8 @@ use std::ffi::CStr; use crate::error::{BoxDynError, Error}; use crate::type_info::DataType; use crate::{SqliteError, SqliteTypeInfo}; -use libsqlite3_sys::{ + +use crate::sqlite_lib::{ sqlite3, sqlite3_bind_blob64, sqlite3_bind_double, sqlite3_bind_int, sqlite3_bind_int64, sqlite3_bind_null, sqlite3_bind_parameter_count, sqlite3_bind_parameter_name, sqlite3_bind_text64, sqlite3_changes, sqlite3_clear_bindings, sqlite3_column_blob, @@ -398,7 +399,7 @@ impl StatementHandle { SQLITE_DONE => return Ok(false), SQLITE_MISUSE => panic!("misuse!"), #[cfg(feature = "unlock-notify")] - libsqlite3_sys::SQLITE_LOCKED_SHAREDCACHE => { + crate::sqlite_lib::SQLITE_LOCKED_SHAREDCACHE => { // The shared cache is locked by another connection. Wait for unlock // notification and try again. super::unlock_notify::wait(self.db_handle())?; diff --git a/sqlx-sqlite/src/statement/unlock_notify.rs b/sqlx-sqlite/src/statement/unlock_notify.rs index 5821c23ae3..7d05860df5 100644 --- a/sqlx-sqlite/src/statement/unlock_notify.rs +++ b/sqlx-sqlite/src/statement/unlock_notify.rs @@ -3,7 +3,7 @@ use std::os::raw::c_int; use std::slice; use std::sync::{Condvar, Mutex}; -use libsqlite3_sys::{sqlite3, sqlite3_unlock_notify, SQLITE_OK}; +use crate::sqlite_lib::{sqlite3, sqlite3_unlock_notify, SQLITE_OK}; use crate::SqliteError; diff --git a/sqlx-sqlite/src/statement/virtual.rs b/sqlx-sqlite/src/statement/virtual.rs index b25aa69e47..25bb181d17 100644 --- a/sqlx-sqlite/src/statement/virtual.rs +++ b/sqlx-sqlite/src/statement/virtual.rs @@ -5,7 +5,7 @@ use std::os::raw::c_char; use std::ptr::{null, null_mut, NonNull}; use std::sync::Arc; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3, sqlite3_prepare_v3, sqlite3_stmt, SQLITE_OK, SQLITE_PREPARE_PERSISTENT, }; diff --git a/sqlx-sqlite/src/type_info.rs b/sqlx-sqlite/src/type_info.rs index 38a0fc4169..a22c4c6f8a 100644 --- a/sqlx-sqlite/src/type_info.rs +++ b/sqlx-sqlite/src/type_info.rs @@ -2,7 +2,7 @@ use std::fmt::{self, Display, Formatter}; use std::os::raw::c_int; use std::str::FromStr; -use libsqlite3_sys::{SQLITE_BLOB, SQLITE_FLOAT, SQLITE_INTEGER, SQLITE_NULL, SQLITE_TEXT}; +use crate::sqlite_lib::{SQLITE_BLOB, SQLITE_FLOAT, SQLITE_INTEGER, SQLITE_NULL, SQLITE_TEXT}; use crate::error::BoxDynError; diff --git a/sqlx-sqlite/src/value.rs b/sqlx-sqlite/src/value.rs index bdf376c187..c7653144c3 100644 --- a/sqlx-sqlite/src/value.rs +++ b/sqlx-sqlite/src/value.rs @@ -4,10 +4,11 @@ use std::ptr::NonNull; use std::slice; use std::str; -use libsqlite3_sys::{ +use crate::sqlite_lib::{ sqlite3_value, sqlite3_value_blob, sqlite3_value_bytes, sqlite3_value_double, sqlite3_value_dup, sqlite3_value_free, sqlite3_value_int64, sqlite3_value_type, }; + use sqlx_core::type_info::TypeInfo; pub(crate) use sqlx_core::value::{Value, ValueRef};