Skip to main content

weirdnet_storage/
lib.rs

1//! General storage for SQLite databases encrypted with SQLCipher.
2//!
3//! Schema-free and brand-free: callers supply their migrations, name their database in
4//! error messages and register the tables they sync.
5//!
6//! Key boundary: the caller owns every key. This crate only applies a raw 32-byte key it is
7//! handed (zeroizing the literals it builds); it never derives, wraps, stores, recovers or
8//! exports one, and never sees a password.
9#![forbid(unsafe_code)]
10
11mod db;
12mod error;
13pub mod files;
14#[cfg(feature = "sync")]
15pub mod sync;
16mod tx;
17
18pub use db::{
19    RawKey, apply_key, encrypt_plaintext_in_place, has_cipher, migrate, open, open_foreign_readonly, open_in_memory, open_readonly, rekey,
20    schema_version, snapshot_to,
21};
22pub use error::{Result, StorageError};
23pub use rusqlite;
24pub use tx::{begin, release, rollback, savepoint};
25
26/// Quote an SQL identifier (`"name"`, inner quotes doubled).
27pub fn quote_ident(ident: &str) -> String {
28    format!("\"{}\"", ident.replace('"', "\"\""))
29}
30
31/// Whether a table exists.
32pub fn table_exists(conn: &rusqlite::Connection, name: &str) -> Result<bool> {
33    Ok(conn.query_row("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?1", [name], |r| r.get::<_, i64>(0))? > 0)
34}