use std::rc::Rc;
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
use rusqlite::Connection;
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
type InitHook = Rc<dyn Fn(&Connection)>;
use crate::{
Capabilities, DbError, DbErrorKind, Location, OpenOptions, Row, Secret, SqliteConnection,
SqliteDriver, Value,
};
type TraceFn = Rc<dyn Fn(&str)>;
pub struct Sqlite {
opts: OpenOptions,
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
init: Option<InitHook>,
trace: Option<TraceFn>,
}
impl Sqlite {
fn from_opts(opts: OpenOptions) -> Self {
Sqlite {
opts,
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
init: None,
trace: None,
}
}
pub fn at(path: impl Into<std::path::PathBuf>) -> Self {
Sqlite::from_opts(OpenOptions::new(Location::File(path.into())))
}
pub fn memory() -> Self {
Sqlite::from_opts(OpenOptions::new(Location::Memory))
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
pub fn app_data(name: impl AsRef<str>) -> Result<Self, DbError> {
let name = name.as_ref();
if name.is_empty() || name.contains(['/', '\\']) || name.contains("..") {
return Err(DbError::new(
DbErrorKind::Unsupported,
format!("app_data takes a file name, not a path: {name:?}"),
));
}
let dir = Self::app_data_dir()?;
std::fs::create_dir_all(&dir)
.map_err(|e| DbError::driver(format!("creating {}: {e}", dir.display())))?;
Ok(Sqlite::at(dir.join(name)))
}
#[must_use]
pub fn key(mut self, key: Secret) -> Self {
self.opts.key = Some(key);
self
}
#[must_use]
pub fn cipher_migrate(mut self) -> Self {
self.opts.cipher_migrate = true;
self
}
#[must_use]
pub fn wal(mut self, on: bool) -> Self {
self.opts.wal = on;
self
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
pub fn app_data_dir() -> Result<std::path::PathBuf, DbError> {
app_data_root()
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
#[must_use]
pub fn with_init(mut self, f: impl Fn(&Connection) + 'static) -> Self {
self.init = Some(Rc::new(f));
self
}
#[must_use]
pub fn trace_sql(mut self, f: impl Fn(&str) + 'static) -> Self {
self.trace = Some(Rc::new(f));
self
}
}
impl SqliteDriver for Sqlite {
type Connection = RusqliteConn;
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
fn open(self) -> Result<RusqliteConn, DbError> {
use day_sqlite_worker::protocol::{Reply as WReply, Req as WReq};
if self.opts.key.is_some() {
return Err(DbError::new(
DbErrorKind::Unsupported,
"this build has no encryption — the web engine does not include SQLCipher",
));
}
let conn = match &self.opts.location {
Location::Memory => {
let mut conn = day_sqlite_worker::Connection::open_memory()
.map_err(|e| DbError::driver(e.to_string()))?;
if let Some(f) = &self.trace {
let f = f.clone();
conn.trace_stmt(Box::new(move |sql| f(sql)));
}
WebConn::Memory(conn)
}
Location::File(p) => {
let name = p.to_string_lossy().into_owned();
match web::channel::call(&WReq::Open {
name,
trace: self.trace.is_some(),
})? {
WReply::Conn(id) => WebConn::Remote(id),
other => return Err(web::unexpected(other)),
}
}
};
let mut conn = RusqliteConn { conn };
if self.opts.foreign_keys {
conn.execute("PRAGMA foreign_keys = ON", &[])?;
}
Ok(conn)
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
fn open(self) -> Result<RusqliteConn, DbError> {
let conn = match &self.opts.location {
Location::Memory => Connection::open_in_memory(),
Location::File(p) => Connection::open(p),
}
.map_err(|e| DbError::driver(e.to_string()))?;
if let Some(key) = &self.opts.key {
#[cfg(feature = "cipher")]
{
let quoted = key.reveal().replace('\'', "''");
conn.execute_batch(&format!("PRAGMA key = '{quoted}';"))
.map_err(|e| DbError::driver(e.to_string()))?;
if self.opts.cipher_migrate {
let _ = conn.execute_batch("PRAGMA cipher_migrate;");
}
}
#[cfg(not(feature = "cipher"))]
{
let _ = key;
return Err(DbError::new(
DbErrorKind::Unsupported,
"this build has no encryption — enable the `cipher` feature",
));
}
}
#[cfg(feature = "cipher")]
if conn
.prepare("SELECT count(*) FROM sqlite_master")
.and_then(|mut s| s.query_row([], |_| Ok(())))
.is_err()
{
return Err(DbError::new(
DbErrorKind::BadKey,
"the database would not open with this key",
));
}
let trace = self
.trace
.as_ref()
.map(|f| install_sql_trace(&conn, f.clone()));
{
use rusqlite::functions::FunctionFlags;
conn.create_scalar_function(
"day_fold",
1,
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|ctx| {
let folded = match ctx.get_raw(0) {
rusqlite::types::ValueRef::Text(t) => {
Some(String::from_utf8_lossy(t).to_lowercase())
}
_ => None,
};
Ok(folded)
},
)
.map_err(|e| DbError::driver(format!("registering day_fold: {e}")))?;
}
conn.busy_timeout(std::time::Duration::from_millis(u64::from(
self.opts.busy_timeout_ms,
)))
.map_err(|e| DbError::driver(e.to_string()))?;
if self.opts.foreign_keys {
conn.execute_batch("PRAGMA foreign_keys = ON;")
.map_err(|e| DbError::driver(e.to_string()))?;
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
if self.opts.wal && matches!(self.opts.location, Location::File(_)) {
conn.execute_batch("PRAGMA journal_mode = WAL;")
.map_err(|e| DbError::driver(e.to_string()))?;
}
if let Some(init) = &self.init {
init(&conn);
}
Ok(RusqliteConn {
conn,
_trace: trace,
})
}
fn capabilities(&self) -> Capabilities {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
{
Capabilities {
durable: matches!(self.opts.location, Location::File(_)) && web::channel::ready(),
encryption: false,
wal: false,
full_text_search: true,
rtree: true,
external_changes: false,
unicode_fold: false,
}
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
{
Capabilities {
durable: matches!(self.opts.location, Location::File(_)),
encryption: cfg!(feature = "cipher"),
wal: true,
full_text_search: cfg!(any(feature = "bundled", feature = "cipher")),
rtree: cfg!(any(feature = "bundled", feature = "cipher")),
external_changes: matches!(self.opts.location, Location::File(_)),
unicode_fold: true,
}
}
}
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
fn app_data_root() -> Result<std::path::PathBuf, DbError> {
day_process::storage::data_dir().ok_or_else(|| {
DbError::new(
DbErrorKind::Unsupported,
"no per-app data directory on this target",
)
})
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
pub struct RusqliteConn {
conn: Connection,
_trace: Option<Box<TraceCtx>>,
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
struct TraceCtx {
f: TraceFn,
last: std::cell::Cell<(usize, i32)>,
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
#[expect(
clippy::unnecessary_box_returns,
reason = "the engine keeps a pointer to the context, so it needs the stable heap address"
)]
fn install_sql_trace(conn: &Connection, f: TraceFn) -> Box<TraceCtx> {
use rusqlite::ffi;
unsafe extern "C" fn cb(
ev: std::ffi::c_uint,
ctx: *mut std::ffi::c_void,
p: *mut std::ffi::c_void,
x: *mut std::ffi::c_void,
) -> std::ffi::c_int {
if ev != ffi::SQLITE_TRACE_STMT || ctx.is_null() {
return 0;
}
let ctx = unsafe { &*(ctx as *const TraceCtx) };
let f = &ctx.f;
unsafe {
let unexpanded = (!x.is_null())
.then(|| std::ffi::CStr::from_ptr(x.cast()).to_str().ok())
.flatten();
if let Some(comment) = unexpanded.filter(|s| s.starts_with("--")) {
f(comment);
return 0;
}
let run = ffi::sqlite3_stmt_status(p.cast(), ffi::SQLITE_STMTSTATUS_VM_STEP, 0);
let this = (p as usize, run);
let fresh = ffi::sqlite3_stmt_status(p.cast(), ffi::SQLITE_STMTSTATUS_RUN, 0) == 0;
if !fresh && ctx.last.get() == this {
f("-- foreign-key action");
return 0;
}
ctx.last.set(this);
let expanded = ffi::sqlite3_expanded_sql(p.cast());
if !expanded.is_null() {
if let Ok(s) = std::ffi::CStr::from_ptr(expanded).to_str() {
f(s);
}
ffi::sqlite3_free(expanded.cast());
} else if let Some(s) = unexpanded {
f(s);
}
}
0
}
let boxed = Box::new(TraceCtx {
f,
last: std::cell::Cell::new((0, -1)),
});
let ctx = &raw const *boxed as *mut std::ffi::c_void;
unsafe {
ffi::sqlite3_trace_v2(conn.handle(), ffi::SQLITE_TRACE_STMT, Some(cb), ctx);
}
boxed
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
enum WebConn {
Memory(day_sqlite_worker::Connection),
Remote(u32),
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
pub struct RusqliteConn {
conn: WebConn,
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
fn to_sql(v: &Value) -> rusqlite::types::Value {
use rusqlite::types::Value as R;
match v {
Value::Null => R::Null,
Value::Int(i) => R::Integer(*i),
Value::Real(f) => R::Real(*f),
Value::Text(t) => R::Text(t.clone()),
Value::Blob(b) => R::Blob(b.clone()),
}
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
fn from_sql(v: rusqlite::types::ValueRef<'_>) -> Value {
use rusqlite::types::ValueRef as R;
match v {
R::Null => Value::Null,
R::Integer(i) => Value::Int(i),
R::Real(f) => Value::Real(f),
R::Text(t) => Value::Text(String::from_utf8_lossy(t).into_owned()),
R::Blob(b) => Value::Blob(b.to_vec()),
}
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
impl SqliteConnection for RusqliteConn {
fn execute_batch(&mut self, sql: &str) -> Result<(), DbError> {
use day_sqlite_worker::protocol::{Reply as WReply, Req as WReq};
match &self.conn {
WebConn::Memory(c) => c
.execute_batch(sql)
.map_err(|e| DbError::driver(format!("{e} in `{sql}`"))),
WebConn::Remote(id) => match web::channel::call(&WReq::Batch {
conn: *id,
sql: sql.to_owned(),
})? {
WReply::Ok => Ok(()),
other => Err(web::unexpected(other)),
},
}
}
fn execute(&mut self, sql: &str, params: &[Value]) -> Result<u64, DbError> {
use day_sqlite_worker::protocol::{Reply as WReply, Req as WReq};
match &self.conn {
WebConn::Memory(c) => c
.execute(sql, &web::to_wire(params))
.map_err(|e| DbError::driver(format!("{e} in `{sql}`"))),
WebConn::Remote(id) => match web::channel::call(&WReq::Exec {
conn: *id,
sql: sql.to_string(),
params: web::to_wire(params),
})? {
WReply::Changes(n) => Ok(n),
other => Err(web::unexpected(other)),
},
}
}
fn query(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&dyn Row),
) -> Result<(), DbError> {
use day_sqlite_worker::protocol::{Reply as WReply, Req as WReq};
match &self.conn {
WebConn::Memory(c) => c
.query(sql, &web::to_wire(params), &mut |r| {
let vals: Vec<Value> = r.into_iter().map(web::from_wire).collect();
row(&vals);
})
.map_err(|e| DbError::driver(format!("{e} in `{sql}`"))),
WebConn::Remote(id) => match web::channel::call(&WReq::Query {
conn: *id,
sql: sql.to_string(),
params: web::to_wire(params),
})? {
WReply::Rows(rows) => {
for r in rows {
let vals: Vec<Value> = r.into_iter().map(web::from_wire).collect();
row(&vals);
}
Ok(())
}
other => Err(web::unexpected(other)),
},
}
}
fn query_named(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&[String], &dyn Row),
) -> Result<(), DbError> {
use day_sqlite_worker::protocol::{Reply as WReply, Req as WReq};
match &self.conn {
WebConn::Memory(c) => c
.query_named(sql, &web::to_wire(params), &mut |names, r| {
let vals: Vec<Value> = r.into_iter().map(web::from_wire).collect();
row(names, &vals);
})
.map(|_| ())
.map_err(|e| DbError::driver(format!("{e} in `{sql}`"))),
WebConn::Remote(id) => match web::channel::call(&WReq::QueryNamed {
conn: *id,
sql: sql.to_owned(),
params: web::to_wire(params),
})? {
WReply::NamedRows { names, rows } => {
for r in rows {
let vals: Vec<Value> = r.into_iter().map(web::from_wire).collect();
row(&names, &vals);
}
Ok(())
}
other => Err(web::unexpected(other)),
},
}
}
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
impl Drop for RusqliteConn {
fn drop(&mut self) {
if let WebConn::Remote(id) = self.conn {
let _ = web::channel::call(&day_sqlite_worker::protocol::Req::Close { conn: id });
}
}
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
impl SqliteConnection for RusqliteConn {
fn execute(&mut self, sql: &str, params: &[Value]) -> Result<u64, DbError> {
let mut stmt = self
.conn
.prepare_cached(sql)
.map_err(|e| DbError::driver(format!("{e} in `{sql}`")))?;
let bound = rusqlite::params_from_iter(params.iter().map(to_sql));
stmt.execute(bound)
.map(|n| n as u64)
.map_err(|e| DbError::driver(format!("{e} in `{sql}`")))
}
fn query(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&dyn Row),
) -> Result<(), DbError> {
let mut stmt = self
.conn
.prepare_cached(sql)
.map_err(|e| DbError::driver(format!("{e} in `{sql}`")))?;
let bound = rusqlite::params_from_iter(params.iter().map(to_sql));
let mut rows = stmt
.query(bound)
.map_err(|e| DbError::driver(format!("{e} in `{sql}`")))?;
loop {
match rows.next() {
Ok(Some(r)) => {
let n = r.as_ref().column_count();
let vals: Vec<Value> = (0..n)
.map(|i| r.get_ref(i).map_or(Value::Null, from_sql))
.collect();
row(&vals);
}
Ok(None) => break,
Err(e) => return Err(DbError::driver(e.to_string())),
}
}
Ok(())
}
fn execute_batch(&mut self, sql: &str) -> Result<(), DbError> {
self.conn
.execute_batch(sql)
.map_err(|e| DbError::driver(format!("{e} in batch")))
}
fn query_named(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&[String], &dyn Row),
) -> Result<(), DbError> {
let mut stmt = self
.conn
.prepare(sql)
.map_err(|e| DbError::driver(format!("{e} in `{sql}`")))?;
let names: Vec<String> = stmt
.column_names()
.iter()
.map(std::string::ToString::to_string)
.collect();
let bound = rusqlite::params_from_iter(params.iter().map(to_sql));
let mut rows = stmt
.query(bound)
.map_err(|e| DbError::driver(format!("{e} in `{sql}`")))?;
loop {
match rows.next() {
Ok(Some(r)) => {
let n = r.as_ref().column_count();
let vals: Vec<Value> = (0..n)
.map(|i| r.get_ref(i).map_or(Value::Null, from_sql))
.collect();
row(&names, &vals);
}
Ok(None) => break,
Err(e) => return Err(DbError::driver(e.to_string())),
}
}
Ok(())
}
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
mod web {
use super::*;
use day_sqlite_worker::protocol::{self, Reply as WReply, Req as WReq, Value as WValue};
pub(super) mod channel {
use super::*;
pub(crate) fn ready() -> bool {
day_sqlite_worker::channel::ready()
}
pub(crate) fn call(req: &WReq) -> Result<WReply, DbError> {
let Some(reply) = day_sqlite_worker::channel::call(&protocol::encode_req(req)) else {
return Err(DbError::new(
DbErrorKind::Unsupported,
"no SQL worker — file databases on the web need the day-sql worker, \
which needs cross-origin isolation (COOP/COEP headers; docs/web.md)",
));
};
match protocol::decode_reply(&reply) {
Ok(WReply::Err(m)) => Err(DbError::driver(m)),
Ok(r) => Ok(r),
Err(_) => Err(DbError::driver("malformed reply from the SQL worker")),
}
}
}
pub(super) fn unexpected(r: WReply) -> DbError {
DbError::driver(format!("unexpected SQL worker reply: {r:?}"))
}
pub(super) fn to_wire(params: &[Value]) -> Vec<WValue> {
params
.iter()
.map(|v| match v {
Value::Null => WValue::Null,
Value::Int(i) => WValue::Int(*i),
Value::Real(r) => WValue::Real(*r),
Value::Text(t) => WValue::Text(t.clone()),
Value::Blob(b) => WValue::Blob(b.clone()),
})
.collect()
}
pub(super) fn from_wire(v: WValue) -> Value {
match v {
WValue::Null => Value::Null,
WValue::Int(i) => Value::Int(i),
WValue::Real(r) => Value::Real(r),
WValue::Text(t) => Value::Text(t),
WValue::Blob(b) => Value::Blob(b),
}
}
#[derive(Clone)]
pub struct WebStorage;
impl WebStorage {
pub fn exists(&self, name: &str) -> bool {
matches!(
channel::call(&WReq::Exists {
name: name.to_string()
}),
Ok(WReply::Bool(true))
)
}
pub fn list(&self) -> Vec<String> {
match channel::call(&WReq::List) {
Ok(WReply::Names(names)) => names,
_ => Vec::new(),
}
}
pub fn export_db(&self, name: &str) -> Result<Vec<u8>, DbError> {
match channel::call(&WReq::Export {
name: name.to_string(),
})? {
WReply::Bytes(b) => Ok(b),
other => Err(unexpected(other)),
}
}
pub fn import_db(&self, name: &str, bytes: &[u8]) -> Result<(), DbError> {
match channel::call(&WReq::Import {
name: name.to_string(),
bytes: bytes.to_vec(),
})? {
WReply::Ok => Ok(()),
other => Err(unexpected(other)),
}
}
pub fn delete_db(&self, name: &str) {
let _ = channel::call(&WReq::Delete {
name: name.to_string(),
});
}
}
impl Sqlite {
pub fn web_storage() -> Result<WebStorage, DbError> {
if channel::ready() {
Ok(WebStorage)
} else {
Err(DbError::new(
DbErrorKind::Unsupported,
"no SQL worker — file databases on the web need cross-origin isolation \
(COOP/COEP headers; docs/web.md)",
))
}
}
}
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
pub use web::WebStorage;