|
| 1 | +# Adding Static Extensions |
| 2 | + |
| 3 | +This library supports statically linking SQLite extensions. This is necessary because WebAssembly environments typically do not support dynamic library loading (`dlopen`), so all extensions must be compiled and linked directly into the binary. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +We maintain a simple structure for single-file C extensions: |
| 8 | + |
| 9 | +- `sqlite3/ext/`: Directory for C extension source files. |
| 10 | + |
| 11 | +## Step-by-Step Guide |
| 12 | + |
| 13 | +### 1. Add the Source File |
| 14 | + |
| 15 | +Place your extension's C source code in `sqlite3/ext/`. |
| 16 | + |
| 17 | +For example, for the `uuid` extension: |
| 18 | + |
| 19 | +```bash |
| 20 | +cp path/to/uuid.c sqlite3/ext/uuid.c |
| 21 | +``` |
| 22 | + |
| 23 | +### 2. Define a Feature Flag |
| 24 | + |
| 25 | +Add a new feature to `Cargo.toml` to allow users to opt-in to this extension. |
| 26 | + |
| 27 | +```toml |
| 28 | +[features] |
| 29 | +# ... |
| 30 | +uuid = [] |
| 31 | +``` |
| 32 | + |
| 33 | +### 3. Register in `build.rs` |
| 34 | + |
| 35 | +Update `build.rs` to compile your extension when the feature is enabled. Looking for the `extensions` array in the `compile()` function: |
| 36 | + |
| 37 | +```rust |
| 38 | + // Extensions |
| 39 | + // (feature_name, source_file) |
| 40 | + let extensions = [ |
| 41 | + #[cfg(feature = "uuid")] |
| 42 | + ("uuid", "sqlite3/ext/uuid.c"), |
| 43 | + // Add your new extension here: |
| 44 | + #[cfg(feature = "your_feature")] |
| 45 | + ("your_feature", "sqlite3/ext/your_extension.c"), |
| 46 | + ]; |
| 47 | +``` |
| 48 | + |
| 49 | +The build script automatically handles: |
| 50 | + |
| 51 | +- Compiling the C file. |
| 52 | +- Adding `sqlite3/` to the include path (so `#include "sqlite3ext.h"` works). |
| 53 | +- Linking it into the final `wsqlite3` library. |
| 54 | + |
| 55 | +### 4. Expose Initialization in Rust |
| 56 | + |
| 57 | +In `src/lib.rs`, allow users to register the extension. |
| 58 | + |
| 59 | +First, declare the extension's initialization function. This name is defined in the C file (usually `sqlite3_EXTENSION_init`). |
| 60 | + |
| 61 | +```rust |
| 62 | +#[cfg(feature = "your_feature")] |
| 63 | +extern "C" { |
| 64 | + pub fn sqlite3_your_extension_init( |
| 65 | + db: *mut sqlite3, |
| 66 | + pzErrMsg: *mut *mut core::ffi::c_char, |
| 67 | + pApi: *const sqlite3_api_routines, |
| 68 | + ) -> core::ffi::c_int; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Then, provide a safe helper to register it using `sqlite3_auto_extension`: |
| 73 | + |
| 74 | +```rust |
| 75 | +#[cfg(feature = "your_feature")] |
| 76 | +pub fn register_your_extension() { |
| 77 | + unsafe { |
| 78 | + sqlite3_auto_extension(Some(core::mem::transmute( |
| 79 | + sqlite3_your_extension_init as *const (), |
| 80 | + ))); |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Usage |
| 86 | + |
| 87 | +Users can now use your extension by enabling the feature and registering it at startup: |
| 88 | + |
| 89 | +```toml |
| 90 | +[dependencies] |
| 91 | +sqlite-wasm-rs = { version = "...", features = ["uuid"] } |
| 92 | +``` |
| 93 | + |
| 94 | +```rust |
| 95 | +fn main() { |
| 96 | + sqlite_wasm_rs::register_uuid_extension(); |
| 97 | + |
| 98 | + // Open database and use uuid functions... |
| 99 | +} |
| 100 | +``` |
0 commit comments