use std::any::{Any, TypeId};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use day_model::Source;
use day_model::{Identified, Key, Keyed, ModelId, Op, Store};
#[cfg(not(target_arch = "wasm32"))]
mod worker;
#[cfg(not(target_arch = "wasm32"))]
pub use worker::{
DatabaseWorker, WorkerOptions, WorkerRequest, WorkerSnapshot, WorkerSubscription,
};
mod queries;
pub use queries::{
Col, Delta, Deps, Fetch, FtsRef, GeoRect, GeoRef, Pred, Quant, RelatedDep, RelationCol,
ResultSet, RowView, SetChange, Sort, compare_values, encode_column, rank,
};
use queries::{CompileErr, RelSql, SqlIndex, compile_count, compile_fallback, compile_fetch};
mod relations;
pub use relations::{
DeleteRule, Many, One, Registrar, RelationDef, RelationRef, wire_join, wire_to_many,
};
#[cfg(feature = "driver-rusqlite")]
mod rusqlite_driver;
#[cfg(feature = "driver-rusqlite")]
pub use rusqlite_driver::Sqlite;
#[cfg(all(
feature = "driver-rusqlite",
target_family = "wasm",
target_os = "unknown"
))]
pub use rusqlite_driver::WebStorage;
pub use day_persistence_macros::Model;
#[doc(hidden)]
pub use day_macros::observable_impls as __observable_impls;
pub mod prelude {
#[cfg(feature = "driver-rusqlite")]
pub use crate::Sqlite;
pub use crate::{DeleteRule, Many, Model, ModelContainer, One, Recorder, Secret, schema};
}
#[derive(Clone, Debug)]
pub struct DbError {
pub kind: DbErrorKind,
pub message: String,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DbErrorKind {
Closed,
Busy,
Cancelled,
Panicked,
Driver,
Schema,
BadKey,
Decode,
Unsupported,
Deny,
}
impl DbError {
pub fn new(kind: DbErrorKind, message: impl Into<String>) -> Self {
DbError {
kind,
message: message.into(),
}
}
pub fn driver(message: impl Into<String>) -> Self {
Self::new(DbErrorKind::Driver, message)
}
}
impl fmt::Display for DbError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", self.kind, self.message)
}
}
impl std::error::Error for DbError {}
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Null,
Int(i64),
Real(f64),
Text(String),
Blob(Vec<u8>),
}
impl Value {
pub fn as_int(&self) -> Result<i64, DbError> {
match self {
Value::Int(i) => Ok(*i),
other => Err(DbError::new(
DbErrorKind::Decode,
format!("expected INTEGER, found {other:?}"),
)),
}
}
pub fn as_real(&self) -> Result<f64, DbError> {
match self {
Value::Real(r) => Ok(*r),
Value::Int(i) => Ok(*i as f64),
other => Err(DbError::new(
DbErrorKind::Decode,
format!("expected REAL, found {other:?}"),
)),
}
}
pub fn as_text(&self) -> Result<&str, DbError> {
match self {
Value::Text(t) => Ok(t),
other => Err(DbError::new(
DbErrorKind::Decode,
format!("expected TEXT, found {other:?}"),
)),
}
}
pub fn as_blob(&self) -> Result<&[u8], DbError> {
match self {
Value::Blob(b) => Ok(b),
other => Err(DbError::new(
DbErrorKind::Decode,
format!("expected BLOB, found {other:?}"),
)),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SqlType {
Integer,
Real,
Text,
Blob,
}
impl SqlType {
pub fn ddl(self) -> &'static str {
match self {
SqlType::Integer => "INTEGER",
SqlType::Real => "REAL",
SqlType::Text => "TEXT",
SqlType::Blob => "BLOB",
}
}
}
pub trait Row {
fn len(&self) -> usize;
fn get(&self, i: usize) -> Value;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl Row for Vec<Value> {
fn len(&self) -> usize {
Vec::len(self)
}
fn get(&self, i: usize) -> Value {
self.as_slice().get(i).cloned().unwrap_or(Value::Null)
}
}
#[derive(Clone, Debug)]
pub enum Location {
Memory,
File(PathBuf),
}
#[derive(Clone)]
pub struct Secret(String);
impl Secret {
pub fn new(key: impl Into<String>) -> Self {
Secret(key.into())
}
#[cfg(feature = "cipher")]
pub(crate) fn reveal(&self) -> &str {
&self.0
}
}
impl fmt::Debug for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Secret(…)")
}
}
impl Drop for Secret {
fn drop(&mut self) {
unsafe {
for b in self.0.as_bytes_mut() {
*b = 0;
}
}
}
}
#[derive(Clone, Debug)]
pub struct OpenOptions {
pub location: Location,
pub key: Option<Secret>,
pub wal: bool,
pub foreign_keys: bool,
pub busy_timeout_ms: u32,
pub cipher_migrate: bool,
}
impl OpenOptions {
pub fn new(location: Location) -> Self {
OpenOptions {
location,
key: None,
wal: true,
foreign_keys: true,
busy_timeout_ms: 5_000,
cipher_migrate: false,
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Capabilities {
pub durable: bool,
pub encryption: bool,
pub wal: bool,
pub full_text_search: bool,
pub rtree: bool,
pub external_changes: bool,
pub unicode_fold: bool,
}
pub trait SqliteDriver {
type Connection: SqliteConnection;
fn open(self) -> Result<Self::Connection, DbError>;
fn capabilities(&self) -> Capabilities;
}
impl SqliteConnection for Box<dyn SqliteConnection> {
fn execute(&mut self, sql: &str, params: &[Value]) -> Result<u64, DbError> {
(**self).execute(sql, params)
}
fn query(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&dyn Row),
) -> Result<(), DbError> {
(**self).query(sql, params, row)
}
fn execute_batch(&mut self, sql: &str) -> Result<(), DbError> {
(**self).execute_batch(sql)
}
fn query_named(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&[String], &dyn Row),
) -> Result<(), DbError> {
(**self).query_named(sql, params, row)
}
}
pub trait SqliteConnection: 'static {
fn execute(&mut self, sql: &str, params: &[Value]) -> Result<u64, DbError>;
fn query(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&dyn Row),
) -> Result<(), DbError>;
fn begin(&mut self) -> Result<(), DbError> {
self.execute("BEGIN", &[]).map(|_| ())
}
fn commit(&mut self) -> Result<(), DbError> {
self.execute("COMMIT", &[]).map(|_| ())
}
fn rollback(&mut self) -> Result<(), DbError> {
self.execute("ROLLBACK", &[]).map(|_| ())
}
fn execute_batch(&mut self, sql: &str) -> Result<(), DbError> {
self.execute(sql, &[]).map(|_| ())
}
fn query_named(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&[String], &dyn Row),
) -> Result<(), DbError> {
let _ = (sql, params, row);
Err(DbError::new(
DbErrorKind::Unsupported,
"this driver does not report column names",
))
}
}
pub struct Recorder {
state: Rc<RecorderState>,
}
struct RecorderState {
log: RefCell<Vec<(String, Vec<Value>)>>,
fixtures: RefCell<HashMap<String, Vec<Vec<Value>>>>,
}
#[derive(Clone)]
pub struct RecorderLog {
state: Rc<RecorderState>,
}
impl RecorderLog {
pub fn sql(&self) -> Vec<String> {
self.state
.log
.borrow()
.iter()
.map(|(s, _)| s.clone())
.collect()
}
pub fn entries(&self) -> Vec<(String, Vec<Value>)> {
self.state.log.borrow().clone()
}
pub fn clear(&self) {
self.state.log.borrow_mut().clear();
}
}
impl Recorder {
pub fn new() -> (Recorder, RecorderLog) {
let state = Rc::new(RecorderState {
log: RefCell::new(Vec::new()),
fixtures: RefCell::new(HashMap::new()),
});
(
Recorder {
state: state.clone(),
},
RecorderLog { state },
)
}
pub fn with_table(self, table: &str, rows: Vec<Vec<Value>>) -> Self {
self.state.fixtures.borrow_mut().insert(table.into(), rows);
self
}
}
pub struct RecorderConn {
state: Rc<RecorderState>,
}
impl SqliteDriver for Recorder {
type Connection = RecorderConn;
fn open(self) -> Result<RecorderConn, DbError> {
Ok(RecorderConn { state: self.state })
}
fn capabilities(&self) -> Capabilities {
Capabilities {
durable: false,
wal: false,
full_text_search: true,
rtree: true,
unicode_fold: true,
..Default::default()
}
}
}
impl SqliteConnection for RecorderConn {
fn execute(&mut self, sql: &str, params: &[Value]) -> Result<u64, DbError> {
self.state
.log
.borrow_mut()
.push((sql.to_string(), params.to_vec()));
Ok(0)
}
fn query(
&mut self,
sql: &str,
params: &[Value],
row: &mut dyn FnMut(&dyn Row),
) -> Result<(), DbError> {
self.state
.log
.borrow_mut()
.push((sql.to_string(), params.to_vec()));
if let Some(from) = sql.split(" FROM ").nth(1) {
let table = from.split_whitespace().next().unwrap_or("");
if let Some(rows) = self.state.fixtures.borrow().get(table) {
for r in rows {
row(r);
}
}
}
Ok(())
}
}
pub trait ColumnValue: Sized + 'static {
const SQL_TYPE: SqlType;
fn to_sqlite_value(&self) -> Value;
fn from_sqlite_value(v: Value) -> Result<Self, DbError>;
}
pub trait ValueCodec<T>: 'static {
const SQL_TYPE: SqlType;
fn to_sqlite_value(v: &T) -> Value;
fn from_sqlite_value(v: Value) -> Result<T, DbError>;
}
macro_rules! int_column {
($($t:ty),*) => {$(
impl ColumnValue for $t {
const SQL_TYPE: SqlType = SqlType::Integer;
fn to_sqlite_value(&self) -> Value {
Value::Int(*self as i64)
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
Ok(v.as_int()? as $t)
}
}
)*};
}
int_column!(i8, i16, i32, i64, u8, u16, u32, u64, usize, isize);
impl ColumnValue for bool {
const SQL_TYPE: SqlType = SqlType::Integer;
fn to_sqlite_value(&self) -> Value {
Value::Int(*self as i64)
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
Ok(v.as_int()? != 0)
}
}
impl ColumnValue for f64 {
const SQL_TYPE: SqlType = SqlType::Real;
fn to_sqlite_value(&self) -> Value {
Value::Real(*self)
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
v.as_real()
}
}
impl ColumnValue for f32 {
const SQL_TYPE: SqlType = SqlType::Real;
fn to_sqlite_value(&self) -> Value {
Value::Real(*self as f64)
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
Ok(v.as_real()? as f32)
}
}
impl ColumnValue for String {
const SQL_TYPE: SqlType = SqlType::Text;
fn to_sqlite_value(&self) -> Value {
Value::Text(self.clone())
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
Ok(v.as_text()?.to_string())
}
}
impl ColumnValue for Vec<u8> {
const SQL_TYPE: SqlType = SqlType::Blob;
fn to_sqlite_value(&self) -> Value {
Value::Blob(self.clone())
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
Ok(v.as_blob()?.to_vec())
}
}
impl ColumnValue for day_model::Uuid {
const SQL_TYPE: SqlType = SqlType::Blob;
fn to_sqlite_value(&self) -> Value {
Value::Blob(self.as_bytes().to_vec())
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
day_model::Uuid::from_slice(v.as_blob()?)
.map_err(|e| DbError::new(DbErrorKind::Decode, format!("uuid: {e}")))
}
}
fn key_param(handle: u64) -> Value {
match Key::of_handle(handle) {
Some(Key::U64(k)) => Value::Int(k as i64),
Some(Key::Uuid(u)) => Value::Blob(day_model::Uuid::from_u128(u).as_bytes().to_vec()),
Some(Key::Str(s)) => Value::Text(s.to_string()),
Some(Key::Pair(..)) | None => Value::Null,
}
}
fn value_to_handle(v: &Value) -> Option<u64> {
match v {
Value::Int(i) if *i >= 0 => Some(Key::U64(*i as u64).handle()),
Value::Blob(b) if b.len() == 16 => day_model::Uuid::from_slice(b)
.ok()
.map(|u| Key::Uuid(u.as_u128()).handle()),
Value::Text(t) => Some(Key::Str(t.as_str().into()).handle()),
_ => None,
}
}
impl<T: ColumnValue> ColumnValue for Option<T> {
const SQL_TYPE: SqlType = T::SQL_TYPE;
fn to_sqlite_value(&self) -> Value {
match self {
Some(v) => v.to_sqlite_value(),
None => Value::Null,
}
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
match v {
Value::Null => Ok(None),
v => Ok(Some(T::from_sqlite_value(v)?)),
}
}
}
pub struct Json;
impl<T> ValueCodec<T> for Json
where
T: serde::Serialize + serde::de::DeserializeOwned + 'static,
{
const SQL_TYPE: SqlType = SqlType::Text;
fn to_sqlite_value(v: &T) -> Value {
match serde_json::to_string(v) {
Ok(s) => Value::Text(s),
Err(_) => Value::Null,
}
}
fn from_sqlite_value(v: Value) -> Result<T, DbError> {
serde_json::from_str(v.as_text()?)
.map_err(|e| DbError::new(DbErrorKind::Decode, format!("json: {e}")))
}
}
#[derive(Clone, Copy, Debug)]
pub struct ColumnDef {
pub name: &'static str,
pub field: &'static str,
pub sql: SqlType,
pub not_null: bool,
pub unique: bool,
pub indexed: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SpatialCols {
pub lat: &'static str,
pub lon: &'static str,
}
#[derive(Clone, Copy, Debug)]
pub struct LinkDef {
pub field: &'static str,
pub local: &'static str,
pub target_table: &'static str,
pub remote: &'static str,
}
pub struct Linked<M: ?Sized> {
_m: std::marker::PhantomData<fn() -> M>,
}
impl<M: ?Sized> Clone for Linked<M> {
fn clone(&self) -> Self {
*self
}
}
impl<M: ?Sized> Copy for Linked<M> {}
impl<M: ?Sized> Default for Linked<M> {
fn default() -> Self {
Linked {
_m: std::marker::PhantomData,
}
}
}
impl<M: ?Sized> PartialEq for Linked<M> {
fn eq(&self, _: &Self) -> bool {
true
}
}
impl<M: ?Sized> std::fmt::Debug for Linked<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Linked")
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct LinkInfo {
pub(crate) owner_table: &'static str,
pub(crate) field: &'static str,
pub(crate) local: &'static str,
pub(crate) target_table: &'static str,
pub(crate) remote: &'static str,
}
pub trait Model: Identified + day_model::ApplyField + Clone + 'static {
const TABLE: &'static str;
const KEY: &'static str;
const COLUMNS: &'static [ColumnDef];
const COMPOSITE_INDEXES: &'static [&'static [&'static str]] = &[];
const FTS_COLUMNS: &'static [&'static str] = &[];
const FTS_TOKENIZE: Option<&'static str> = None;
const SPATIAL: Option<SpatialCols> = None;
const KEY_SQL: SqlType = SqlType::Integer;
const RELATIONS: &'static [RelationDef] = &[];
const LINKS: &'static [LinkDef] = &[];
const EXTERNAL: bool = false;
fn to_row(&self) -> Vec<Value>;
fn from_row(row: &dyn Row) -> Result<Self, DbError>;
fn default_row() -> Vec<Value>;
fn wire(reg: &mut Registrar<'_>) {
let _ = reg;
}
}
pub fn model_fingerprint<M: Model>() -> u64 {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
let mut eat = |bytes: &[u8]| {
for b in bytes {
h ^= *b as u64;
h = h.wrapping_mul(0x1000_0000_01b3);
}
};
eat(M::TABLE.as_bytes());
eat(M::KEY.as_bytes());
for c in M::COLUMNS {
eat(c.name.as_bytes());
eat(c.sql.ddl().as_bytes());
eat(&[c.not_null as u8, c.unique as u8, c.indexed as u8]);
}
for idx in M::COMPOSITE_INDEXES {
for c in *idx {
eat(c.as_bytes());
}
eat(b"|");
}
for c in M::FTS_COLUMNS {
eat(b"fts:");
eat(c.as_bytes());
}
if !M::FTS_COLUMNS.is_empty()
&& M::COLUMNS
.iter()
.any(|c| c.name == M::KEY && c.sql != SqlType::Integer)
{
eat(b"wide-fts-key-map-v1");
}
if let Some(t) = M::FTS_TOKENIZE {
eat(b"tok:");
eat(t.as_bytes());
}
if let Some(s) = M::SPATIAL {
eat(b"geo:");
eat(s.lat.as_bytes());
eat(s.lon.as_bytes());
}
h
}
#[derive(Default)]
pub struct Schema {
installers: Vec<Installer>,
wirers: Vec<Installer>,
fk_specs: Vec<FkSpec>,
}
type Installer = Box<dyn FnOnce(&ModelContainer) -> Result<(), DbError>>;
type AbsorbFn = Rc<dyn Fn(Vec<Vec<Value>>) -> Result<Vec<u64>, DbError>>;
type RefreshFn = Rc<dyn Fn(Vec<Vec<Value>>) -> Result<bool, DbError>>;
type KeyWhere = Rc<dyn Fn(u64) -> (String, Vec<Value>)>;
type KeysWhere = Rc<dyn Fn(&[u64]) -> (String, Vec<Value>)>;
type EvictFn = Rc<dyn Fn(&[u64], usize) -> usize>;
#[derive(Clone, Copy, Debug)]
struct FkSpec {
child_table: &'static str,
child_field: &'static str,
parent_table: &'static str,
parent_key: &'static str,
rule: DeleteRule,
}
impl Schema {
pub fn new() -> Self {
Self::default()
}
pub fn with<M: Model>(mut self) -> Self {
for def in M::RELATIONS {
if def.join.is_none() {
self.fk_specs.push(FkSpec {
child_table: def.target_table,
child_field: def.inverse,
parent_table: M::TABLE,
parent_key: M::KEY,
rule: def.delete,
});
}
}
self.installers.push(Box::new(|c| c.attach::<M>()));
self.wirers.push(Box::new(|c| {
let mut reg = Registrar {
container: c,
error: None,
};
M::wire(&mut reg);
match reg.error {
Some(e) => Err(e),
None => Ok(()),
}
}));
self
}
}
#[macro_export]
macro_rules! schema {
($($m:ty),+ $(,)?) => {{
let s = $crate::Schema::new();
$(let s = s.with::<$m>();)+
s
}};
}
#[derive(Default)]
pub struct MigrationPlan {
stages: Vec<Stage>,
}
type StageFn = Box<dyn FnOnce(&mut dyn SqliteConnection) -> Result<(), DbError>>;
struct Stage {
from: u32,
to: u32,
run: StageFn,
}
impl MigrationPlan {
pub fn new() -> Self {
Self::default()
}
pub fn custom(
mut self,
from: u32,
to: u32,
f: impl FnOnce(&mut dyn SqliteConnection) -> Result<(), DbError> + 'static,
) -> Self {
self.stages.push(Stage {
from,
to,
run: Box::new(f),
});
self
}
fn target_version(&self) -> u32 {
self.stages.iter().map(|s| s.to).max().unwrap_or(0)
}
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum DirtyRow {
Insert,
Update(Vec<&'static str>),
Delete,
}
#[derive(Clone, Default)]
struct DirtyState {
rows: HashMap<(u64, u64), DirtyRow>,
order: Vec<(u64, u64)>,
full: Vec<u64>,
}
impl DirtyState {
fn merge(&mut self, newer: Self) {
for store in newer.full {
if !self.full.contains(&store) {
self.full.push(store);
}
}
for id in newer.order {
let next = newer.rows[&id].clone();
if !self.rows.contains_key(&id) {
self.order.push(id);
}
match (self.rows.get_mut(&id), next) {
(Some(DirtyRow::Update(old)), DirtyRow::Update(cols)) => {
for col in cols {
if !old.contains(&col) {
old.push(col);
}
}
}
(Some(DirtyRow::Insert | DirtyRow::Delete), DirtyRow::Update(_)) => {}
(_, next) => {
self.rows.insert(id, next);
}
}
}
}
fn is_empty(&self) -> bool {
self.rows.is_empty() && self.full.is_empty()
}
fn note(&mut self, change: &day_model::Change, known_store: impl Fn(u64) -> bool) {
let Some(&store) = change.components.first() else {
return;
};
if !known_store(store) {
return;
}
let Some(&key) = change.components.get(1) else {
if !self.full.contains(&store) {
self.full.push(store);
}
return;
};
if key == day_model::STRUCTURE {
return; }
let id = (store, key);
if !self.rows.contains_key(&id) {
self.order.push(id);
}
let slot = self.rows.entry(id).or_insert(DirtyRow::Update(Vec::new()));
match (change.op, &mut *slot) {
(Op::Delete, _) => *slot = DirtyRow::Delete,
(Op::Insert, _) => *slot = DirtyRow::Insert,
(Op::Set, DirtyRow::Insert) | (Op::Set, DirtyRow::Delete) => {}
(Op::Set, DirtyRow::Update(columns)) => {
if change.components.len() >= 3 && !columns.contains(&change.label) {
columns.push(change.label);
}
}
(Op::Move, _) => {}
}
}
}
pub(crate) struct TableHooks {
pub(crate) table: &'static str,
pub(crate) key_cols: Vec<String>,
pub(crate) key_where: KeyWhere,
pub(crate) columns: Vec<String>,
pub(crate) fields: Vec<String>,
pub(crate) fts: Option<String>,
pub(crate) fts_key: &'static str,
pub(crate) spatial: Option<(String, String, String)>,
pub(crate) row_for: Rc<dyn Fn(u64) -> Option<Vec<Value>>>,
pub(crate) all_rows: Rc<dyn Fn() -> Vec<(u64, Vec<Value>)>>,
pub(crate) resident_keys: Rc<dyn Fn() -> Vec<u64>>,
pub(crate) resident_len: Rc<dyn Fn() -> usize>,
pub(crate) is_resident: Rc<dyn Fn(u64) -> bool>,
pub(crate) absorb: AbsorbFn,
pub(crate) refresh: RefreshFn,
pub(crate) evict: EvictFn,
#[cfg(not(target_arch = "wasm32"))]
pub(crate) dispose_cache: Rc<dyn Fn()>,
pub(crate) watch_undo: Rc<dyn Fn(&day_model::UndoStack)>,
}
pub(crate) struct ContainerInner {
conn: RefCell<Box<dyn SqliteConnection>>,
caps: Capabilities,
tables: RefCell<HashMap<u64, TableHooks>>,
stores: RefCell<HashMap<TypeId, Box<dyn Any>>>,
dirty: RefCell<DirtyState>,
queries: RefCell<Vec<std::rc::Weak<QueryState>>>,
data_version: Cell<Option<i64>>,
fk_specs: RefCell<Vec<FkSpec>>,
pub(crate) relations: RefCell<Vec<Rc<relations::ToOneRel>>>,
pub(crate) joins: RefCell<Vec<Rc<relations::JoinRel>>>,
pub(crate) links: RefCell<Vec<LinkInfo>>,
attached: RefCell<Vec<String>>,
sink: Cell<Option<day_model::ChangeSinkId>>,
autosave: Cell<bool>,
worker_transaction: Cell<bool>,
cache_limit: Cell<usize>,
eviction_retry: RefCell<HashMap<u64, usize>>,
pub(crate) error: day_reactive::Signal<Option<String>>,
flushing: Cell<bool>,
}
#[derive(Clone)]
pub struct ModelContainer {
pub(crate) inner: Rc<ContainerInner>,
}
pub const DEFAULT_CACHE_LIMIT: usize = 8_192;
impl ModelContainer {
pub fn open<D: SqliteDriver>(driver: D, schema: Schema) -> Result<ModelContainer, DbError>
where
D::Connection: 'static,
{
Self::open_with(driver, schema, MigrationPlan::new())
}
pub fn open_with<D: SqliteDriver>(
driver: D,
schema: Schema,
plan: MigrationPlan,
) -> Result<ModelContainer, DbError>
where
D::Connection: 'static,
{
let caps = driver.capabilities();
let conn: Box<dyn SqliteConnection> = Box::new(driver.open()?);
let error = day_reactive::Scope::detached().enter(|| day_reactive::Signal::new(None));
let container = ModelContainer {
inner: Rc::new(ContainerInner {
conn: RefCell::new(conn),
caps,
tables: RefCell::new(HashMap::new()),
stores: RefCell::new(HashMap::new()),
dirty: RefCell::new(DirtyState::default()),
queries: RefCell::new(Vec::new()),
data_version: Cell::new(None),
fk_specs: RefCell::new(Vec::new()),
relations: RefCell::new(Vec::new()),
joins: RefCell::new(Vec::new()),
links: RefCell::new(Vec::new()),
attached: RefCell::new(Vec::new()),
sink: Cell::new(None),
autosave: Cell::new(true),
worker_transaction: Cell::new(false),
cache_limit: Cell::new(DEFAULT_CACHE_LIMIT),
eviction_retry: RefCell::new(HashMap::new()),
error,
flushing: Cell::new(false),
}),
};
container.ensure_schema_table()?;
container.run_stages(plan)?;
let Schema {
installers,
wirers,
fk_specs,
} = schema;
*container.inner.fk_specs.borrow_mut() = fk_specs;
for install in installers {
install(&container)?;
}
for wire in wirers {
wire(&container)?;
}
relations::register_container(&container.inner);
container.install_sink();
container.install_autosave();
if caps.external_changes {
container
.inner
.data_version
.set(container.file_data_version()?);
}
Ok(container)
}
pub const EXTERNAL_AUTHOR: &'static str = "database";
pub fn cache<M: Model>(&self) -> Store<Keyed<M>> {
let stores = self.inner.stores.borrow();
let any = stores
.get(&TypeId::of::<M>())
.unwrap_or_else(|| panic!("model `{}` is not in this container's schema!", M::TABLE));
*any.downcast_ref::<Store<Keyed<M>>>()
.expect("store map holds the exact type it was keyed by")
}
pub fn capabilities(&self) -> Capabilities {
self.inner.caps
}
pub fn set_autosave(&self, on: bool) {
self.inner.autosave.set(on);
}
pub fn set_cache_limit(&self, rows: usize) {
self.inner.cache_limit.set(rows.max(1));
let store_ids: Vec<u64> = self.inner.tables.borrow().keys().copied().collect();
for id in store_ids {
self.enforce_cache_limit(id, &[], true);
}
}
pub fn last_error(&self) -> day_reactive::Signal<Option<String>> {
self.inner.error
}
pub fn save(&self) -> Result<(), DbError> {
self.save_now().map(|_| ())
}
pub fn record_sql(&self, f: impl FnOnce()) -> Result<Vec<String>, DbError> {
let prior = self.inner.autosave.replace(false);
f();
let result = self.save_now();
self.inner.autosave.set(prior);
result.map(|stmts| stmts.into_iter().map(|(sql, _)| sql).collect())
}
fn save_now(&self) -> Result<Vec<(String, Vec<Value>)>, DbError> {
if self.inner.flushing.get() {
return Ok(Vec::new());
}
let dirty = {
let mut d = self.inner.dirty.borrow_mut();
if d.is_empty() {
drop(d);
self.run_deferred_requeries(&[]);
return Ok(Vec::new());
}
std::mem::take(&mut *d)
};
self.inner.flushing.set(true);
let result = self.flush(&dirty);
self.inner.flushing.set(false);
match &result {
Ok(stmts) => {
self.inner.eviction_retry.borrow_mut().clear();
self.inner.error.set_if_changed(None);
self.run_deferred_requeries(stmts);
}
Err(e) => {
{
let mut pending = self.inner.dirty.borrow_mut();
let newer = std::mem::take(&mut *pending);
*pending = dirty;
pending.merge(newer);
}
self.inner.error.set(Some(e.to_string()));
}
}
result
}
pub fn get<M: Model>(&self, id: impl Into<ModelId<M>>) -> Option<day_model::Elem<M>> {
match self.try_get::<M>(id) {
Ok(row) => row,
Err(error) => {
self.inner.error.set(Some(error.to_string()));
None
}
}
}
pub fn try_get<M: Model>(
&self,
id: impl Into<ModelId<M>>,
) -> Result<Option<day_model::Elem<M>>, DbError> {
let store = self.cache::<M>();
let h = id.into().handle();
if !store.with_untracked(|k| k.get(h).is_some()) {
self.ensure_resident::<M>(&[h])?;
}
Ok(store
.with_untracked(|k| k.get(h).is_some())
.then(|| store.elem(h)))
}
pub fn ensure_resident<M: Model>(&self, keys: &[u64]) -> Result<(), DbError> {
let store = self.cache::<M>();
self.fault_into(store.store_id(), keys)
}
pub fn ensure<M: Model>(
&self,
ids: impl IntoIterator<Item = impl Into<ModelId<M>>>,
) -> Result<(), DbError> {
let keys: Vec<u64> = ids.into_iter().map(|i| i.into().handle()).collect();
self.ensure_resident::<M>(&keys)
}
pub fn warm<M: Model>(&self) -> Result<usize, DbError> {
let store = self.cache::<M>();
let keys = self.select_id_column_checked(&format!(
"SELECT {} FROM {} ORDER BY {}",
M::KEY,
M::TABLE,
M::KEY
))?;
self.fault_into(store.store_id(), &keys)?;
Ok(store.with_untracked(|k| k.len()))
}
pub fn insert<M: Model>(&self, row: M) {
let store = self.cache::<M>();
let h = row.handle();
store.restructure("create", Op::Insert, h, move |k| k.push(row));
}
pub fn table_count<M: Model>(&self) -> Result<u64, DbError> {
if self.inner.autosave.get() && !self.inner.dirty.borrow().is_empty() {
self.save()?;
}
let mut n = 0i64;
self.conn().query(
&format!("SELECT COUNT(*) FROM {}", M::TABLE),
&[],
&mut |row| {
n = row.get(0).as_int().unwrap_or(0);
},
)?;
Ok(n.max(0) as u64)
}
pub(crate) fn fault_into(&self, store_id: u64, keys: &[u64]) -> Result<(), DbError> {
let (is_resident, absorb, columns, table, keys_where) = {
let tables = self.inner.tables.borrow();
let Some(hooks) = tables.get(&store_id) else {
return Ok(());
};
(
hooks.is_resident.clone(),
hooks.absorb.clone(),
hooks.columns.join(", "),
hooks.table,
keys_where_of(hooks),
)
};
let missing: Vec<u64> = {
let dirty = self.inner.dirty.borrow();
keys.iter()
.copied()
.filter(|k| !is_resident(*k))
.filter(|k| dirty.rows.get(&(store_id, *k)) != Some(&DirtyRow::Delete))
.collect()
};
if missing.is_empty() {
return Ok(());
}
let mut faulted: Vec<u64> = Vec::with_capacity(missing.len());
for chunk in missing.chunks(MAX_BOUND_PARAMS / 2) {
let (clause, params) = keys_where(chunk);
let mut rows: Vec<Vec<Value>> = Vec::new();
self.conn().query(
&format!("SELECT {columns} FROM {table} WHERE {clause}"),
¶ms,
&mut |row| {
let n = Row::len(row);
rows.push((0..n).map(|i| row.get(i)).collect());
},
)?;
faulted.extend(absorb(rows)?);
}
let wanted: std::collections::HashSet<u64> = missing.iter().copied().collect();
faulted.retain(|k| wanted.contains(k));
self.enforce_cache_limit(store_id, &faulted, false);
Ok(())
}
fn enforce_cache_limit(&self, store_id: u64, protect: &[u64], strict: bool) {
let limit = self.inner.cache_limit.get();
if limit == usize::MAX {
return;
}
let (resident_len, evict) = {
let tables = self.inner.tables.borrow();
let Some(hooks) = tables.get(&store_id) else {
return;
};
((hooks.resident_len)(), hooks.evict.clone())
};
let slack = if strict {
0
} else {
(limit / 8).clamp(64, 4096)
};
let threshold = if strict {
limit
} else {
self.inner
.eviction_retry
.borrow()
.get(&store_id)
.copied()
.unwrap_or_else(|| limit.saturating_add(slack))
};
if resident_len <= threshold {
return;
}
self.inner.eviction_retry.borrow_mut().insert(
store_id,
resident_len.saturating_add(slack.max(resident_len / 8).max(1)),
);
let resident = {
let tables = self.inner.tables.borrow();
match tables.get(&store_id) {
Some(hooks) => (hooks.resident_keys)(),
None => return,
}
};
let excess = resident.len().saturating_sub(limit);
let candidates: Vec<u64> = {
let dirty = self.inner.dirty.borrow();
if dirty.full.contains(&store_id) {
return; }
let protect: std::collections::HashSet<u64> = protect.iter().copied().collect();
resident
.into_iter()
.filter(|key| !protect.contains(key) && !dirty.rows.contains_key(&(store_id, *key)))
.collect()
};
let evicted = evict(&candidates, excess);
let remaining = resident_len.saturating_sub(evicted);
self.inner.eviction_retry.borrow_mut().insert(
store_id,
remaining
.max(limit)
.saturating_add(slack.max(remaining / 8).max(1)),
);
}
pub(crate) fn conn(&self) -> std::cell::RefMut<'_, Box<dyn SqliteConnection>> {
self.inner.conn.borrow_mut()
}
fn select_id_column_checked(&self, sql: &str) -> Result<Vec<u64>, DbError> {
let mut ids = Vec::new();
self.conn().query(sql, &[], &mut |row| {
if let Some(h) = value_to_handle(&row.get(0)) {
ids.push(h);
}
})?;
Ok(ids)
}
fn select_ids_checked(&self, sql: &str, params: &[Value]) -> Result<Vec<u64>, DbError> {
let mut ids = Vec::new();
self.conn().query(sql, params, &mut |row| {
if let Some(h) = value_to_handle(&row.get(0)) {
ids.push(h);
}
})?;
Ok(ids)
}
pub(crate) fn select_id_column(&self, sql: &str, params: &[Value]) -> Vec<u64> {
match self.select_ids_checked(sql, params) {
Ok(ids) => ids,
Err(e) => {
self.inner.error.set(Some(e.to_string()));
Vec::new()
}
}
}
pub(crate) fn select_real(&self, sql: &str, params: &[Value]) -> Option<f64> {
let mut v = None;
if let Err(e) = self.conn().query(sql, params, &mut |row| {
v = row.get(0).as_real().ok();
}) {
self.inner.error.set(Some(e.to_string()));
}
v
}
pub(crate) fn dirty_rows_of(&self, store_id: u64) -> Vec<(u64, DirtyRow)> {
self.inner
.dirty
.borrow()
.rows
.iter()
.filter(|((s, _), _)| *s == store_id)
.map(|((_, k), state)| (*k, state.clone()))
.collect()
}
pub(crate) fn dirty_state_of(&self, store_id: u64, key: u64) -> Option<DirtyRow> {
self.inner
.dirty
.borrow()
.rows
.get(&(store_id, key))
.cloned()
}
fn ensure_schema_table(&self) -> Result<(), DbError> {
self.conn().execute(
"CREATE TABLE IF NOT EXISTS _day_schema (\
table_name TEXT PRIMARY KEY, fingerprint TEXT NOT NULL, version INTEGER NOT NULL)",
&[],
)?;
Ok(())
}
fn db_version(&self) -> Result<u32, DbError> {
let mut version = 0u32;
self.conn().query(
"SELECT version FROM _day_schema WHERE table_name = '_db'",
&[],
&mut |row| {
version = row.get(0).as_int().unwrap_or(0) as u32;
},
)?;
Ok(version)
}
fn set_db_version(&self, v: u32) -> Result<(), DbError> {
self.conn().execute(
"INSERT INTO _day_schema (table_name, fingerprint, version) VALUES ('_db', '', ?) \
ON CONFLICT(table_name) DO UPDATE SET version = excluded.version",
&[Value::Int(v as i64)],
)?;
Ok(())
}
fn run_stages(&self, plan: MigrationPlan) -> Result<(), DbError> {
let target = plan.target_version();
let mut current = self.db_version()?;
if current > target && target > 0 {
return Err(DbError::new(
DbErrorKind::Schema,
format!(
"database is at version {current}, newer than this build's {target} — \
refusing to open (an old app writing a new schema is how data rots)"
),
));
}
let mut stages = plan.stages;
stages.sort_by_key(|s| s.from);
for stage in stages {
if stage.from != current {
if stage.to <= current {
continue; }
return Err(DbError::new(
DbErrorKind::Schema,
format!(
"migration stage {}→{} does not start at the database's version {current}",
stage.from, stage.to
),
));
}
let mut conn = self.conn();
conn.begin()?;
match (stage.run)(conn.as_mut()) {
Ok(()) => {
conn.commit()?;
drop(conn);
current = stage.to;
self.set_db_version(current)?;
}
Err(e) => {
let _ = conn.rollback();
return Err(e);
}
}
}
if target > 0 {
self.set_db_version(target)?;
}
Ok(())
}
fn attach<M: Model>(&self) -> Result<(), DbError> {
let key_sql = M::COLUMNS
.iter()
.find(|c| c.name == M::KEY)
.map(|c| c.sql)
.unwrap_or(SqlType::Integer);
if key_sql != SqlType::Integer && M::SPATIAL.is_some() {
return Err(DbError::new(
DbErrorKind::Unsupported,
format!(
"`{}` declares spatial(…), which addresses rows by ROWID — those \
need an integer `#[model(id)]`, not a Uuid or String key",
M::TABLE
),
));
}
if self.inner.stores.borrow().contains_key(&TypeId::of::<M>()) {
let store = self.cache::<M>();
let keys: Vec<u64> = store.with_untracked(|k| k.keys());
store.depopulate_many(&keys);
return Ok(());
}
if !M::EXTERNAL {
self.ensure_table::<M>()?;
}
for l in M::LINKS {
self.inner.links.borrow_mut().push(LinkInfo {
owner_table: M::TABLE,
field: l.field,
local: l.local,
target_table: l.target_table,
remote: l.remote,
});
}
let store: Store<Keyed<M>> = Store::new(Keyed::new(Vec::new()));
let store_id = store.store_id();
let columns: Vec<String> = M::COLUMNS.iter().map(|c| c.name.to_string()).collect();
let fields: Vec<String> = M::COLUMNS.iter().map(|c| c.field.to_string()).collect();
let row_for = {
Rc::new(move |key: u64| store.with_untracked(|k| k.get(key).map(|m| m.to_row())))
as Rc<dyn Fn(u64) -> Option<Vec<Value>>>
};
let all_rows = {
Rc::new(move || {
store.with_untracked(|k| {
k.items().iter().map(|m| (m.handle(), m.to_row())).collect()
})
}) as Rc<dyn Fn() -> Vec<(u64, Vec<Value>)>>
};
let resident_keys =
Rc::new(move || store.with_untracked(|k| k.keys())) as Rc<dyn Fn() -> Vec<u64>>;
let resident_len =
Rc::new(move || store.with_untracked(|k| k.len())) as Rc<dyn Fn() -> usize>;
let is_resident = Rc::new(move |h: u64| store.with_untracked(|k| k.get(h).is_some()))
as Rc<dyn Fn(u64) -> bool>;
let absorb = {
Rc::new(
move |raw_rows: Vec<Vec<Value>>| -> Result<Vec<u64>, DbError> {
let mut decoded: Vec<M> = Vec::with_capacity(raw_rows.len());
for r in raw_rows {
decoded.push(M::from_row(&r)?);
}
let keys: Vec<u64> = decoded.iter().map(|m| m.handle()).collect();
store.populate(decoded);
Ok(keys)
},
) as AbsorbFn
};
let refresh = {
Rc::new(move |raw_rows: Vec<Vec<Value>>| -> Result<bool, DbError> {
let mut fresh: Vec<M> = Vec::with_capacity(raw_rows.len());
for r in raw_rows {
fresh.push(M::from_row(&r)?);
}
let existing: Vec<u64> = store.with_untracked(|k| k.keys());
let fresh_keys: std::collections::HashSet<u64> =
fresh.iter().map(|m| m.handle()).collect();
let mut changed = false;
day_model::with_author(ModelContainer::EXTERNAL_AUTHOR, || {
for m in fresh {
let key = m.handle();
match store.with_untracked(|k| k.get(key).map(|old| old.to_row())) {
None => {
changed = true;
store.restructure("external", Op::Insert, key, |k| k.push(m));
}
Some(old) => {
let new_row = m.to_row();
let labels: Vec<&'static str> = M::COLUMNS
.iter()
.enumerate()
.filter(|(i, _)| Row::get(&old, *i) != Row::get(&new_row, *i))
.map(|(_, c)| c.field)
.collect();
if !labels.is_empty() {
changed = true;
store.merge_row(key, m, &labels);
}
}
}
}
for gone in existing.into_iter().filter(|k| !fresh_keys.contains(k)) {
changed = true;
store.restructure("external", Op::Delete, gone, |k| {
k.remove(gone);
});
}
});
Ok(changed)
}) as RefreshFn
};
let evict = Rc::new(move |candidates: &[u64], want: usize| {
let doomed: Vec<u64> = candidates
.iter()
.copied()
.filter(|h| !store.is_observed(*h))
.take(want)
.collect();
store.depopulate_many(&doomed)
}) as EvictFn;
let watch_undo = Rc::new(move |stack: &day_model::UndoStack| stack.watch(store))
as Rc<dyn Fn(&day_model::UndoStack)>;
self.inner.tables.borrow_mut().insert(
store_id,
TableHooks {
table: M::TABLE,
key_cols: vec![M::KEY.to_string()],
key_where: Rc::new(|h| (format!("{} = ?", M::KEY), vec![key_param(h)])),
columns,
fields,
resident_len,
fts: (!M::FTS_COLUMNS.is_empty()).then(|| format!("{}_fts", M::TABLE)),
fts_key: if key_sql == SqlType::Integer {
"rowid"
} else {
"day_key"
},
spatial: M::SPATIAL.map(|s| {
(
s.lat.to_string(),
s.lon.to_string(),
format!("{}_geo", M::TABLE),
)
}),
row_for,
all_rows,
resident_keys,
is_resident,
absorb,
refresh,
evict,
#[cfg(not(target_arch = "wasm32"))]
dispose_cache: Rc::new(move || store.dispose_contents()),
watch_undo,
},
);
self.inner
.stores
.borrow_mut()
.insert(TypeId::of::<M>(), Box::new(store));
Ok(())
}
fn ensure_table<M: Model>(&self) -> Result<(), DbError> {
let mut fp_val = model_fingerprint::<M>();
for s in self
.inner
.fk_specs
.borrow()
.iter()
.filter(|s| s.child_table == M::TABLE)
{
for b in s
.child_field
.bytes()
.chain(s.parent_table.bytes())
.chain(s.rule.sql().bytes())
{
fp_val ^= b as u64;
fp_val = fp_val.wrapping_mul(0x1000_0000_01b3);
}
}
let fp = format!("{fp_val:016x}");
let stored = self.stored_fingerprint(M::TABLE)?;
if stored.as_deref() == Some(fp.as_str()) {
return Ok(());
}
self.conn().begin()?;
let result = (|| {
let t = M::TABLE;
if self.table_exists(&format!("{t}_fts"))? {
for suffix in ["ai", "ad", "au"] {
self.conn()
.execute(&format!("DROP TRIGGER IF EXISTS {t}_fts_{suffix}"), &[])?;
}
self.conn().execute(&format!("DROP TABLE {t}_fts"), &[])?;
self.conn()
.execute(&format!("DROP TABLE IF EXISTS {t}_fts_keys"), &[])?;
}
if !self.table_exists(M::TABLE)? {
self.create_table::<M>()?;
} else {
self.lightweight_migrate::<M>()?;
}
self.create_indexes::<M>()?;
self.create_shadow_tables::<M>()?;
self.store_fingerprint(M::TABLE, &fp)?;
self.conn().commit()
})();
if result.is_err() {
let _ = self.conn().rollback();
}
result
}
fn create_shadow_tables<M: Model>(&self) -> Result<(), DbError> {
let t = M::TABLE;
let key = M::KEY;
if !M::FTS_COLUMNS.is_empty() {
if !self.inner.caps.full_text_search {
return Err(DbError::new(
DbErrorKind::Unsupported,
format!(
"`{t}` declares fts(…) but this driver's engine has no FTS5 — the \
bundled and cipher engines compile it in"
),
));
}
let cols = M::FTS_COLUMNS.join(", ");
let fresh = !self.table_exists(&format!("{t}_fts"))?;
let tokenize = match M::FTS_TOKENIZE {
Some(tok) => format!(", tokenize='{}'", tok.replace('\'', "''")),
None => String::new(),
};
let wide = M::COLUMNS
.iter()
.find(|c| c.name == key)
.is_some_and(|c| c.sql != SqlType::Integer);
if wide {
if M::FTS_COLUMNS.contains(&"day_key") {
return Err(DbError::new(
DbErrorKind::Unsupported,
"day_key is reserved in a non-integer-key FTS index",
));
}
let key_type = M::COLUMNS
.iter()
.find(|c| c.name == key)
.expect("key column")
.sql
.ddl();
self.conn().execute(&format!("CREATE TABLE IF NOT EXISTS {t}_fts_keys(day_rowid INTEGER PRIMARY KEY, day_key {key_type} NOT NULL UNIQUE)"), &[])?;
self.conn().execute(&format!("CREATE VIRTUAL TABLE IF NOT EXISTS {t}_fts USING fts5(day_key UNINDEXED, {cols}{tokenize})"), &[])?;
let new_cols = M::FTS_COLUMNS
.iter()
.map(|c| format!("new.{c}"))
.collect::<Vec<_>>()
.join(", ");
let insert = format!(
"INSERT INTO {t}_fts_keys(day_key) VALUES (new.{key}); INSERT INTO {t}_fts(rowid, day_key, {cols}) VALUES ((SELECT day_rowid FROM {t}_fts_keys WHERE day_key = new.{key}), new.{key}, {new_cols});"
);
let delete = format!(
"DELETE FROM {t}_fts WHERE rowid = (SELECT day_rowid FROM {t}_fts_keys WHERE day_key = old.{key}); DELETE FROM {t}_fts_keys WHERE day_key = old.{key};"
);
for (suffix, event, body) in [
("ai", "INSERT", insert.clone()),
("ad", "DELETE", delete.clone()),
("au", "UPDATE", format!("{delete} {insert}")),
] {
self.conn().execute(&format!("CREATE TRIGGER IF NOT EXISTS {t}_fts_{suffix} AFTER {event} ON {t} BEGIN {body} END"), &[])?;
}
if fresh {
self.conn().execute(
&format!("INSERT INTO {t}_fts_keys(day_key) SELECT {key} FROM {t}"),
&[],
)?;
let qualified = M::FTS_COLUMNS
.iter()
.map(|c| format!("b.{c}"))
.collect::<Vec<_>>()
.join(", ");
self.conn().execute(&format!("INSERT INTO {t}_fts(rowid, day_key, {cols}) SELECT k.day_rowid, b.{key}, {qualified} FROM {t} b JOIN {t}_fts_keys k ON k.day_key = b.{key}"), &[])?;
}
} else {
self.conn().execute(
&format!(
"CREATE VIRTUAL TABLE IF NOT EXISTS {t}_fts USING fts5({cols}, \
content={t}, content_rowid={key}{tokenize})"
),
&[],
)?;
let new_cols = M::FTS_COLUMNS
.iter()
.map(|c| format!("new.{c}"))
.collect::<Vec<_>>()
.join(", ");
let old_cols = M::FTS_COLUMNS
.iter()
.map(|c| format!("old.{c}"))
.collect::<Vec<_>>()
.join(", ");
self.conn().execute(
&format!(
"CREATE TRIGGER IF NOT EXISTS {t}_fts_ai AFTER INSERT ON {t} BEGIN \
INSERT INTO {t}_fts(rowid, {cols}) VALUES (new.{key}, {new_cols}); END"
),
&[],
)?;
self.conn().execute(
&format!(
"CREATE TRIGGER IF NOT EXISTS {t}_fts_ad AFTER DELETE ON {t} BEGIN \
INSERT INTO {t}_fts({t}_fts, rowid, {cols}) VALUES ('delete', old.{key}, {old_cols}); END"
),
&[],
)?;
self.conn().execute(
&format!(
"CREATE TRIGGER IF NOT EXISTS {t}_fts_au AFTER UPDATE ON {t} BEGIN \
INSERT INTO {t}_fts({t}_fts, rowid, {cols}) VALUES ('delete', old.{key}, {old_cols}); \
INSERT INTO {t}_fts(rowid, {cols}) VALUES (new.{key}, {new_cols}); END"
),
&[],
)?;
if fresh {
self.conn().execute(
&format!("INSERT INTO {t}_fts({t}_fts) VALUES ('rebuild')"),
&[],
)?;
}
}
}
if let Some(s) = M::SPATIAL {
if !self.inner.caps.rtree {
return Err(DbError::new(
DbErrorKind::Unsupported,
format!(
"`{t}` declares spatial(…) but this driver's engine has no R*Tree — \
the bundled and cipher engines compile it in"
),
));
}
let (lat, lon) = (s.lat, s.lon);
let fresh = !self.table_exists(&format!("{t}_geo"))?;
self.conn().execute(
&format!(
"CREATE VIRTUAL TABLE IF NOT EXISTS {t}_geo USING rtree({key}, \
min_lat, max_lat, min_lon, max_lon)"
),
&[],
)?;
self.conn().execute(
&format!(
"CREATE TRIGGER IF NOT EXISTS {t}_geo_ai AFTER INSERT ON {t} BEGIN \
INSERT OR REPLACE INTO {t}_geo VALUES (new.{key}, new.{lat}, new.{lat}, new.{lon}, new.{lon}); END"
),
&[],
)?;
self.conn().execute(
&format!(
"CREATE TRIGGER IF NOT EXISTS {t}_geo_au AFTER UPDATE OF {lat}, {lon} ON {t} BEGIN \
INSERT OR REPLACE INTO {t}_geo VALUES (new.{key}, new.{lat}, new.{lat}, new.{lon}, new.{lon}); END"
),
&[],
)?;
self.conn().execute(
&format!(
"CREATE TRIGGER IF NOT EXISTS {t}_geo_ad AFTER DELETE ON {t} BEGIN \
DELETE FROM {t}_geo WHERE {key} = old.{key}; END"
),
&[],
)?;
if fresh {
self.conn().execute(
&format!(
"INSERT OR REPLACE INTO {t}_geo \
SELECT {key}, {lat}, {lat}, {lon}, {lon} FROM {t}"
),
&[],
)?;
}
}
Ok(())
}
fn table_exists(&self, table: &str) -> Result<bool, DbError> {
let mut found = false;
self.conn().query(
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?",
&[Value::Text(table.into())],
&mut |_| found = true,
)?;
Ok(found)
}
fn create_table<M: Model>(&self) -> Result<(), DbError> {
let specs = self.inner.fk_specs.borrow();
let cols: Vec<String> = M::COLUMNS
.iter()
.map(|c| {
let mut s = format!("{} {}", c.name, c.sql.ddl());
if c.name == M::KEY {
s.push_str(" PRIMARY KEY");
} else if c.not_null {
s.push_str(" NOT NULL");
}
if c.unique && c.name != M::KEY {
s.push_str(" UNIQUE");
}
if let Some(spec) = specs
.iter()
.find(|f| f.child_table == M::TABLE && f.child_field == c.field)
{
s.push_str(&format!(
" REFERENCES {}({}) ON DELETE {} DEFERRABLE INITIALLY DEFERRED",
spec.parent_table,
spec.parent_key,
spec.rule.sql()
));
}
s
})
.collect();
let body = format!("CREATE TABLE {} ({})", M::TABLE, cols.join(", "));
if self.conn().execute(&format!("{body} STRICT"), &[]).is_err() {
self.conn().execute(&body, &[])?;
}
Ok(())
}
fn create_indexes<M: Model>(&self) -> Result<(), DbError> {
let fk_cols: Vec<&str> = self
.inner
.fk_specs
.borrow()
.iter()
.filter(|s| s.child_table == M::TABLE)
.filter_map(|s| {
M::COLUMNS
.iter()
.find(|c| c.field == s.child_field)
.map(|c| c.name)
})
.collect();
for c in M::COLUMNS
.iter()
.filter(|c| c.indexed || (fk_cols.contains(&c.name) && c.name != M::KEY))
{
self.conn().execute(
&format!(
"CREATE INDEX IF NOT EXISTS day_idx_{t}_{c} ON {t}({c})",
t = M::TABLE,
c = c.name
),
&[],
)?;
}
for idx in M::COMPOSITE_INDEXES {
let cols = idx.join(", ");
let name = idx.join("_");
self.conn().execute(
&format!(
"CREATE INDEX IF NOT EXISTS day_idx_{t}_{name} ON {t}({cols})",
t = M::TABLE
),
&[],
)?;
}
Ok(())
}
fn lightweight_migrate<M: Model>(&self) -> Result<(), DbError> {
let mut existing: Vec<String> = Vec::new();
self.conn().query(
&format!("PRAGMA table_info({})", M::TABLE),
&[],
&mut |row| {
if let Ok(name) = row.get(1).as_text() {
existing.push(name.to_string());
}
},
)?;
let declared: Vec<&'static str> = M::COLUMNS.iter().map(|c| c.name).collect();
let defaults = M::default_row();
for (i, col) in M::COLUMNS.iter().enumerate() {
if !existing.iter().any(|e| e == col.name) {
self.conn().execute(
&format!(
"ALTER TABLE {} ADD COLUMN {} {}",
M::TABLE,
col.name,
col.sql.ddl()
),
&[],
)?;
let default = Row::get(&defaults, i);
if default != Value::Null {
self.conn().execute(
&format!("UPDATE {} SET {} = ?", M::TABLE, col.name),
&[default],
)?;
}
}
}
for gone in existing.iter().filter(|e| !declared.iter().any(|d| d == e)) {
self.conn()
.execute(&format!("ALTER TABLE {} DROP COLUMN {gone}", M::TABLE), &[])?;
}
Ok(())
}
fn stored_fingerprint(&self, table: &str) -> Result<Option<String>, DbError> {
let mut fp = None;
self.conn().query(
"SELECT fingerprint FROM _day_schema WHERE table_name = ?",
&[Value::Text(table.into())],
&mut |row| {
if let Ok(t) = row.get(0).as_text() {
fp = Some(t.to_string());
}
},
)?;
Ok(fp)
}
fn store_fingerprint(&self, table: &str, fp: &str) -> Result<(), DbError> {
self.conn().execute(
"INSERT INTO _day_schema (table_name, fingerprint, version) VALUES (?, ?, 0) \
ON CONFLICT(table_name) DO UPDATE SET fingerprint = excluded.fingerprint",
&[Value::Text(table.into()), Value::Text(fp.into())],
)?;
Ok(())
}
fn install_sink(&self) {
let inner = Rc::downgrade(&self.inner);
let sink = day_model::install_change_sink(move |change| {
if let Some(inner) = inner.upgrade() {
if change.author != Some(ModelContainer::EXTERNAL_AUTHOR) {
let tables = inner.tables.borrow();
inner
.dirty
.borrow_mut()
.note(change, |store| tables.contains_key(&store));
}
let container = ModelContainer { inner };
container.mark_queries_stale(change);
container.relations_on_change(change);
}
});
self.inner.sink.set(Some(sink));
}
fn install_autosave(&self) {
let inner = Rc::downgrade(&self.inner);
day_reactive::on_turn_end(move || {
let Some(inner) = inner.upgrade() else { return };
let this = ModelContainer { inner };
if this.inner.autosave.get() && !this.inner.dirty.borrow().is_empty() {
let _ = this.save();
}
});
}
fn flush(&self, dirty: &DirtyState) -> Result<Vec<(String, Vec<Value>)>, DbError> {
let stmts = self.fold(dirty)?;
if stmts.is_empty() {
return Ok(stmts);
}
let mut conn = self.conn();
let outer = self.inner.worker_transaction.get();
if !outer {
conn.begin()?;
}
for (sql, params) in &stmts {
if let Err(e) = conn.execute(sql, params) {
if !outer {
let _ = conn.rollback();
}
return Err(e);
}
}
if !outer && let Err(error) = conn.commit() {
let _ = conn.rollback();
return Err(error);
}
Ok(stmts)
}
#[cfg(not(target_arch = "wasm32"))]
fn worker_dispose_caches(&self) {
for hooks in self.inner.tables.borrow().values() {
(hooks.dispose_cache)();
}
}
#[cfg(not(target_arch = "wasm32"))]
fn worker_write<R>(
&self,
write: impl FnOnce(&Self) -> Result<R, DbError>,
) -> Result<R, DbError> {
self.save()?;
self.conn().begin()?;
self.inner.worker_transaction.set(true);
let result = day_reactive::batch(|| {
let value = write(self)?;
self.save()?;
Ok(value)
})
.and_then(|value| {
self.save()?;
self.conn().commit()?;
Ok(value)
});
self.inner.worker_transaction.set(false);
if result.is_err() {
self.conn().rollback().map_err(|error| {
DbError::new(
DbErrorKind::Closed,
format!("transaction rollback failed: {error}"),
)
})?;
self.worker_discard_changes()?;
}
result
}
#[cfg(not(target_arch = "wasm32"))]
fn worker_read<R>(&self, read: impl FnOnce(&Self) -> Result<R, DbError>) -> Result<R, DbError> {
self.save()?;
self.conn().execute_batch("PRAGMA query_only = ON")?;
self.conn().begin().map_err(|error| {
DbError::new(
DbErrorKind::Closed,
format!("could not begin read snapshot: {error}"),
)
})?;
let autosave = self.inner.autosave.replace(false);
let mut result =
day_model::with_author("worker-read", || day_reactive::batch(|| read(self)));
let mutated = !self.inner.dirty.borrow().is_empty();
self.conn().rollback().map_err(|error| {
DbError::new(
DbErrorKind::Closed,
format!("could not end read snapshot: {error}"),
)
})?;
self.conn()
.execute_batch("PRAGMA query_only = OFF")
.map_err(|error| {
DbError::new(
DbErrorKind::Closed,
format!("could not restore write mode: {error}"),
)
})?;
if mutated {
self.worker_discard_changes()?;
result = Err(DbError::driver(
"a worker read attempted to mutate model state",
));
}
self.inner.autosave.set(autosave);
result
}
#[cfg(not(target_arch = "wasm32"))]
fn worker_discard_changes(&self) -> Result<(), DbError> {
let autosave = self.inner.autosave.replace(false);
*self.inner.dirty.borrow_mut() = DirtyState::default();
let result = self.refresh_resident();
self.invalidate_relation_memos();
if result.is_ok() {
self.requery_all();
}
self.inner.autosave.set(autosave);
result.map(|_| ()).map_err(|error| {
DbError::new(
DbErrorKind::Closed,
format!("transaction cache recovery failed: {error}"),
)
})
}
fn fold(&self, dirty: &DirtyState) -> Result<Vec<(String, Vec<Value>)>, DbError> {
let tables = self.inner.tables.borrow();
let mut stmts: Vec<(String, Vec<Value>)> = Vec::new();
for store_id in &dirty.full {
let Some(hooks) = tables.get(store_id) else {
continue;
};
for (_, row) in (hooks.all_rows)() {
stmts.push(upsert_stmt(hooks, &row));
}
}
let mut batches: Vec<Batch> = Vec::new();
let mut deletes: Vec<Batch> = Vec::new();
let mut open: HashMap<(u64, String, u64), usize> = HashMap::new();
let mut open_deletes: HashMap<(u64, String, u64), usize> = HashMap::new();
for id in &dirty.order {
let Some(state) = dirty.rows.get(id) else {
continue;
};
let (store_id, key) = *id;
if dirty.full.contains(&store_id) {
continue; }
let Some(hooks) = tables.get(&store_id) else {
continue;
};
let batchable = hooks.key_cols.len() == 1;
match state {
DirtyRow::Delete => {
if !batchable {
let (clause, params) = (hooks.key_where)(key);
deletes.push(Batch::Single(
format!("DELETE FROM {} WHERE {clause}", hooks.table),
params,
));
continue;
}
join_batch(&mut deletes, &mut open_deletes, store_id, None, key);
}
DirtyRow::Insert => {
if let Some(row) = (hooks.row_for)(key) {
let (sql, params) = upsert_stmt(hooks, &row);
batches.push(Batch::Single(sql, params));
}
}
DirtyRow::Update(cols) => {
let Some(row) = (hooks.row_for)(key) else {
continue; };
if cols.is_empty() {
let (sql, params) = upsert_stmt(hooks, &row);
batches.push(Batch::Single(sql, params));
continue;
}
let mut sets: Vec<String> = Vec::with_capacity(cols.len());
let mut set_params: Vec<Value> = Vec::with_capacity(cols.len());
for c in cols {
let Some(i) = hooks.fields.iter().position(|n| n == c) else {
continue; };
sets.push(format!("{} = ?", hooks.columns[i]));
set_params.push(row.get(i));
}
if set_params.is_empty() {
continue; }
let clause = sets.join(", ");
if !batchable {
let (where_clause, mut where_params) = (hooks.key_where)(key);
let mut params = set_params;
params.append(&mut where_params);
batches.push(Batch::Single(
format!("UPDATE {} SET {clause} WHERE {where_clause}", hooks.table),
params,
));
continue;
}
join_batch(
&mut batches,
&mut open,
store_id,
Some((clause, set_params)),
key,
);
}
}
}
for batch in batches.into_iter().chain(deletes) {
match batch {
Batch::Single(sql, params) => stmts.push((sql, params)),
Batch::Keyed { store, set, keys } => {
let Some(hooks) = tables.get(&store) else {
continue;
};
materialize(hooks, set, &keys, &mut stmts);
}
}
}
Ok(stmts)
}
pub fn backup_to(&self, path: &Path) -> Result<(), DbError> {
self.save()?;
self.conn().execute(
"VACUUM INTO ?",
&[Value::Text(path.to_string_lossy().into_owned())],
)?;
Ok(())
}
pub fn integrity_check(&self) -> Result<Vec<String>, DbError> {
let mut findings = Vec::new();
self.conn()
.query("PRAGMA integrity_check", &[], &mut |row| {
if let Ok(t) = row.get(0).as_text()
&& t != "ok"
{
findings.push(t.to_string());
}
})?;
Ok(findings)
}
pub fn checkpoint(&self) -> Result<(), DbError> {
self.conn()
.query("PRAGMA wal_checkpoint(TRUNCATE)", &[], &mut |_| {})
}
pub fn vacuum(&self) -> Result<(), DbError> {
self.save()?;
self.conn().execute("VACUUM", &[]).map(|_| ())
}
#[cfg(feature = "cipher")]
pub fn rekey(&self, new: Secret) -> Result<(), DbError> {
self.save()?;
let quoted = new.reveal().replace('\'', "''");
self.conn()
.query(&format!("PRAGMA rekey = '{quoted}'"), &[], &mut |_| {})
}
#[cfg(feature = "cipher")]
pub fn encrypt_to(&self, path: &Path, key: Secret) -> Result<(), DbError> {
self.export_to(path, Some(key))
}
#[cfg(feature = "cipher")]
pub fn decrypt_to(&self, path: &Path) -> Result<(), DbError> {
self.export_to(path, None)
}
#[cfg(feature = "cipher")]
fn export_to(&self, path: &Path, key: Option<Secret>) -> Result<(), DbError> {
self.save()?;
let quoted_path = path.to_string_lossy().replace('\'', "''");
let quoted_key = key
.as_ref()
.map(|k| k.reveal().replace('\'', "''"))
.unwrap_or_default();
let mut conn = self.conn();
conn.execute(
&format!("ATTACH DATABASE '{quoted_path}' AS day_export KEY '{quoted_key}'"),
&[],
)?;
conn.query("SELECT sqlcipher_export('day_export')", &[], &mut |_| {})
.and_then(|_| conn.execute("DETACH DATABASE day_export", &[]).map(|_| ()))
}
pub fn size_bytes(&self) -> Result<u64, DbError> {
let mut pages = 0i64;
let mut page_size = 0i64;
self.conn().query("PRAGMA page_count", &[], &mut |row| {
pages = row.get(0).as_int().unwrap_or(0);
})?;
self.conn().query("PRAGMA page_size", &[], &mut |row| {
page_size = row.get(0).as_int().unwrap_or(0);
})?;
Ok((pages * page_size).max(0) as u64)
}
}
fn keys_where_of(hooks: &TableHooks) -> KeysWhere {
if hooks.key_cols.len() == 1 {
let col = hooks.key_cols[0].clone();
Rc::new(move |keys: &[u64]| {
let marks = vec!["?"; keys.len()].join(", ");
(
format!("{col} IN ({marks})"),
keys.iter().map(|k| key_param(*k)).collect(),
)
})
} else {
let key_where = hooks.key_where.clone();
Rc::new(move |keys: &[u64]| {
let mut clauses = Vec::with_capacity(keys.len());
let mut params = Vec::with_capacity(keys.len() * 2);
for k in keys {
let (c, mut p) = key_where(*k);
clauses.push(format!("({c})"));
params.append(&mut p);
}
(clauses.join(" OR "), params)
})
}
}
enum Batch {
Single(String, Vec<Value>),
Keyed {
store: u64,
set: Option<(String, Vec<Value>)>,
keys: Vec<u64>,
},
}
const MAX_BOUND_PARAMS: usize = 900;
fn join_batch(
batches: &mut Vec<Batch>,
open: &mut HashMap<(u64, String, u64), usize>,
store: u64,
set: Option<(String, Vec<Value>)>,
key: u64,
) {
let clause = set.as_ref().map(|(c, _)| c.clone()).unwrap_or_default();
let print = set.as_ref().map(|(_, p)| fingerprint(p)).unwrap_or(0);
let slot = (store, clause, print);
if let Some(&i) = open.get(&slot)
&& let Batch::Keyed {
set: existing,
keys,
..
} = &mut batches[i]
&& existing.as_ref().map(|(_, p)| p.as_slice()) == set.as_ref().map(|(_, p)| p.as_slice())
{
keys.push(key);
return;
}
open.insert(slot, batches.len());
batches.push(Batch::Keyed {
store,
set,
keys: vec![key],
});
}
fn fingerprint(values: &[Value]) -> u64 {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
let mut eat = |bytes: &[u8]| {
for b in bytes {
h ^= *b as u64;
h = h.wrapping_mul(0x1000_0000_01b3);
}
};
for v in values {
match v {
Value::Null => eat(&[0]),
Value::Int(i) => {
eat(&[1]);
eat(&i.to_le_bytes());
}
Value::Real(r) => {
eat(&[2]);
eat(&r.to_bits().to_le_bytes());
}
Value::Text(t) => {
eat(&[3]);
eat(t.as_bytes());
}
Value::Blob(b) => {
eat(&[4]);
eat(b);
}
}
}
h
}
fn materialize(
hooks: &TableHooks,
set: Option<(String, Vec<Value>)>,
keys: &[u64],
out: &mut Vec<(String, Vec<Value>)>,
) {
if keys.len() == 1 {
let (where_clause, where_params) = (hooks.key_where)(keys[0]);
match set {
Some((clause, mut params)) => {
params.extend(where_params);
out.push((
format!("UPDATE {} SET {clause} WHERE {where_clause}", hooks.table),
params,
));
}
None => out.push((
format!("DELETE FROM {} WHERE {where_clause}", hooks.table),
where_params,
)),
}
return;
}
let key_col = &hooks.key_cols[0];
let set_len = set.as_ref().map(|(_, p)| p.len()).unwrap_or(0);
let cap = MAX_BOUND_PARAMS.saturating_sub(set_len).max(1);
for chunk in keys.chunks(cap) {
let mut params: Vec<Value> = Vec::with_capacity(set_len + chunk.len());
if let Some((_, set_params)) = &set {
params.extend(set_params.iter().cloned());
}
params.extend(chunk.iter().map(|k| key_param(*k)));
let marks = vec!["?"; chunk.len()].join(", ");
let sql = match &set {
Some((clause, _)) => format!(
"UPDATE {} SET {clause} WHERE {key_col} IN ({marks})",
hooks.table
),
None => format!("DELETE FROM {} WHERE {key_col} IN ({marks})", hooks.table),
};
out.push((sql, params));
}
}
fn upsert_stmt(hooks: &TableHooks, row: &[Value]) -> (String, Vec<Value>) {
let cols = hooks.columns.join(", ");
let marks = vec!["?"; hooks.columns.len()].join(", ");
let sets = hooks
.columns
.iter()
.filter(|c| !hooks.key_cols.contains(c))
.map(|c| format!("{c} = excluded.{c}"))
.collect::<Vec<_>>()
.join(", ");
let conflict = if sets.is_empty() {
"DO NOTHING".to_string()
} else {
format!("DO UPDATE SET {sets}")
};
(
format!(
"INSERT INTO {} ({cols}) VALUES ({marks}) ON CONFLICT({}) {conflict}",
hooks.table,
hooks.key_cols.join(", ")
),
row.to_vec(),
)
}
impl Drop for ContainerInner {
fn drop(&mut self) {
if let Some(sink) = self.sink.take() {
day_model::remove_change_sink(sink);
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum QueryEvents {
None,
Deltas(Vec<Delta>),
Reload,
}
impl QueryEvents {
fn push(&mut self, deltas: &[Delta]) {
match self {
QueryEvents::None => *self = QueryEvents::Deltas(deltas.to_vec()),
QueryEvents::Deltas(d) => d.extend_from_slice(deltas),
QueryEvents::Reload => {}
}
}
fn reload(&mut self) {
*self = QueryEvents::Reload;
}
}
struct RelWatch {
store: u64,
join_store: Option<u64>,
fields: Vec<&'static str>,
columns: Vec<&'static str>,
}
struct QueryState {
store_id: u64,
table: &'static str,
watches: RefCell<Vec<RelWatch>>,
local_extra: RefCell<Vec<&'static str>>,
set: RefCell<ResultSet>,
version: day_reactive::Signal<u64>,
pending: RefCell<QueryEvents>,
needs_sql: Cell<bool>,
pending_commit: Cell<bool>,
error: day_reactive::Signal<Option<DbError>>,
raw: Option<RawQuery>,
count_only: bool,
count: Cell<usize>,
}
struct RawQuery {
sql: String,
params: Vec<Value>,
tables: Vec<String>,
}
pub struct Query<M: Model> {
state: Rc<QueryState>,
container: ModelContainer,
_p: std::marker::PhantomData<fn() -> M>,
}
impl<M: Model> Clone for Query<M> {
fn clone(&self) -> Self {
Query {
state: self.state.clone(),
container: self.container.clone(),
_p: std::marker::PhantomData,
}
}
}
impl<M: Model> Query<M> {
pub fn error(&self) -> day_reactive::Signal<Option<DbError>> {
self.state.error
}
pub fn refresh(&self) {
self.state.needs_sql.set(true);
self.refresh_if_stale();
}
pub fn try_ids(&self) -> Result<Vec<ModelId<M>>, DbError> {
let ids = self.ids();
match self.state.error.get() {
Some(e) => Err(e),
None => Ok(ids),
}
}
pub fn try_collect(&self) -> Result<Vec<M>, DbError> {
let ids = self.try_ids()?;
let mut rows = Vec::with_capacity(ids.len());
for chunk in ids.chunks(64) {
self.container
.ensure_resident::<M>(&chunk.iter().map(|id| id.handle()).collect::<Vec<_>>())?;
for id in chunk {
if let Some(row) = self.container.try_get::<M>(*id)?
&& let Some(value) = row.with_value_untracked(|v| v.cloned())
{
rows.push(value);
}
}
}
Ok(rows)
}
pub fn ids(&self) -> Vec<ModelId<M>> {
self.refresh_if_stale();
let _ = self.state.version.get();
self.state
.set
.borrow()
.ids()
.iter()
.map(|h| ModelId::from_handle(*h))
.collect()
}
pub fn count(&self) -> usize {
self.refresh_if_stale();
let _ = self.state.version.get();
self.state.set.borrow().ids().len()
}
pub fn first(&self) -> Option<ModelId<M>> {
self.refresh_if_stale();
let _ = self.state.version.get();
self.state
.set
.borrow()
.ids()
.first()
.map(|h| ModelId::from_handle(*h))
}
pub fn contains(&self, id: impl Into<ModelId<M>>) -> bool {
self.refresh_if_stale();
let _ = self.state.version.get();
self.state.set.borrow().ids().contains(&id.into().handle())
}
pub fn ids_untracked(&self) -> Vec<ModelId<M>> {
self.refresh_if_stale();
self.state
.set
.borrow()
.ids()
.iter()
.map(|h| ModelId::from_handle(*h))
.collect()
}
pub fn materialize(&self, range: std::ops::Range<usize>) {
let keys: Vec<u64> = {
let set = self.state.set.borrow();
let ids = set.ids();
let end = range.end.min(ids.len());
let start = range.start.min(end);
ids[start..end].to_vec()
};
if let Err(e) = self.container.ensure_resident::<M>(&keys) {
self.container.inner.error.set(Some(e.to_string()));
}
}
pub fn set_fetch(&self, fetch: Fetch) {
if *self.state.set.borrow().fetch() == fetch {
return;
}
self.container.rewire_watches(&self.state, &fetch);
{
let mut set = self.state.set.borrow_mut();
*set = ResultSet::new(fetch);
}
self.state.needs_sql.set(true);
self.state
.pending_commit
.set(!self.container.inner.dirty.borrow().is_empty());
self.refresh_if_stale();
self.state.pending.borrow_mut().reload();
self.bump();
}
pub fn take_events(&self) -> QueryEvents {
self.refresh_if_stale();
std::mem::replace(&mut *self.state.pending.borrow_mut(), QueryEvents::None)
}
fn refresh_if_stale(&self) {
if !self.state.needs_sql.get() {
return;
}
if self.container.inner.autosave.get() {
let _ = self.container.save();
}
if self.state.needs_sql.replace(false) {
self.container.requery(&self.state);
}
}
fn bump(&self) {
self.state
.version
.set(self.state.version.get_untracked().wrapping_add(1));
}
}
pub struct QueryBuilder<'c, M: Model> {
container: &'c ModelContainer,
fetch: Fetch,
_p: std::marker::PhantomData<fn() -> M>,
}
impl<M: Model> QueryBuilder<'_, M> {
pub fn filter(mut self, p: Pred) -> Self {
self.fetch = self.fetch.filter(p);
self
}
pub fn sort(mut self, s: Sort) -> Self {
self.fetch = self.fetch.sort(s);
self
}
pub fn limit(mut self, n: usize) -> Self {
self.fetch = self.fetch.limit(n);
self
}
pub fn live(self) -> Query<M> {
self.container.install_query::<M>(self.fetch, None)
}
pub fn live_count(self) -> CountQuery<M> {
CountQuery {
state: self.container.install_state::<M>(self.fetch, None, true),
container: self.container.clone(),
_p: std::marker::PhantomData,
}
}
}
pub struct CountQuery<M: Model> {
state: Rc<QueryState>,
container: ModelContainer,
_p: std::marker::PhantomData<fn() -> M>,
}
impl<M: Model> Clone for CountQuery<M> {
fn clone(&self) -> Self {
CountQuery {
state: self.state.clone(),
container: self.container.clone(),
_p: std::marker::PhantomData,
}
}
}
impl<M: Model> CountQuery<M> {
pub fn error(&self) -> day_reactive::Signal<Option<DbError>> {
self.state.error
}
pub fn try_get(&self) -> Result<usize, DbError> {
let n = self.get();
match self.state.error.get() {
Some(e) => Err(e),
None => Ok(n),
}
}
pub fn refresh(&self) {
self.state.needs_sql.set(true);
self.refresh_if_stale();
}
pub fn get(&self) -> usize {
self.refresh_if_stale();
let _ = self.state.version.get();
self.state.count.get()
}
pub fn get_untracked(&self) -> usize {
self.refresh_if_stale();
self.state.count.get()
}
pub fn set_fetch(&self, fetch: Fetch) {
if *self.state.set.borrow().fetch() == fetch {
return;
}
self.container.rewire_watches(&self.state, &fetch);
*self.state.set.borrow_mut() = ResultSet::new(fetch);
self.state.needs_sql.set(true);
self.state
.pending_commit
.set(!self.container.inner.dirty.borrow().is_empty());
self.refresh_if_stale();
}
fn refresh_if_stale(&self) {
if !self.state.needs_sql.get() {
return;
}
if self.container.inner.autosave.get() {
let _ = self.container.save();
}
if self.state.needs_sql.replace(false) {
self.container.requery(&self.state);
}
}
}
struct FallbackRow<'a> {
cols: &'a [&'static str],
values: &'a [Value],
}
impl RowView for FallbackRow<'_> {
fn col(&self, c: &str) -> Option<Value> {
self.cols
.iter()
.position(|n| *n == c)
.and_then(|i| self.values.get(i + 1).cloned())
}
}
impl ModelContainer {
pub fn query<M: Model>(&self) -> QueryBuilder<'_, M> {
let _ = self.cache::<M>();
QueryBuilder {
container: self,
fetch: Fetch::new(),
_p: std::marker::PhantomData,
}
}
pub fn query_fn<M: Model>(&self, f: impl Fn() -> Fetch + 'static) -> Query<M> {
let q = self.install_query::<M>(f(), None);
let q2 = q.clone();
day_reactive::bind(f, move |fetch| q2.set_fetch(fetch.clone()));
q
}
pub fn count_fn<M: Model>(&self, f: impl Fn() -> Fetch + 'static) -> CountQuery<M> {
let q = CountQuery::<M> {
state: self.install_state::<M>(f(), None, true),
container: self.clone(),
_p: std::marker::PhantomData,
};
let q2 = q.clone();
day_reactive::bind(f, move |fetch| q2.set_fetch(fetch.clone()));
q
}
pub fn query_raw<M: Model>(
&self,
sql: impl Into<String>,
params: Vec<Value>,
tables: &[&str],
) -> Query<M> {
self.install_query::<M>(
Fetch::new().filter(Pred::Raw(String::new(), Vec::new())),
Some(RawQuery {
sql: sql.into(),
params,
tables: tables.iter().map(|s| s.to_string()).collect(),
}),
)
}
pub fn undo(&self, levels: usize) -> day_model::UndoStack {
let stack = day_model::UndoStack::new(levels);
for hooks in self.inner.tables.borrow().values() {
(hooks.watch_undo)(&stack);
}
stack
}
pub fn with_connection<R>(&self, f: impl FnOnce(&mut dyn SqliteConnection) -> R) -> R {
self.save().expect(
"pending persistence save failed; use try_with_connection to handle storage errors",
);
f(self.conn().as_mut())
}
pub fn try_with_connection<R>(
&self,
f: impl FnOnce(&mut dyn SqliteConnection) -> Result<R, DbError>,
) -> Result<R, DbError> {
self.save()?;
f(self.conn().as_mut())
}
pub fn rescan(&self) -> Result<(), DbError> {
self.save()?;
self.refresh_resident()?;
self.invalidate_relation_memos();
self.requery_all();
if self.inner.caps.external_changes {
self.inner.data_version.set(self.file_data_version()?);
}
Ok(())
}
pub fn attach_database(&self, alias: &str, path: &str, schema: Schema) -> Result<(), DbError> {
if alias.is_empty() || !alias.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
return Err(DbError::new(
DbErrorKind::Unsupported,
format!("attach_database: {alias:?} is not a plain SQL schema name"),
));
}
let held = self.inner.attached.borrow().iter().any(|a| a == alias);
if held {
self.detach_database(alias)?;
}
if !self.inner.dirty.borrow().is_empty() {
self.save()?;
}
let uri = if path.starts_with("file:") || path.contains("://") {
path.to_string()
} else {
format!("file:{}?mode=ro", path.replace('?', "%3F"))
};
let attach = |target: &str| -> Result<(), DbError> {
self.conn().execute(
&format!("ATTACH DATABASE ? AS {alias}"),
&[Value::Text(target.to_string())],
)?;
Ok(())
};
if let Err(uri_err) = attach(&uri) {
attach(path).map_err(|_| uri_err)?;
}
self.inner.attached.borrow_mut().push(alias.to_string());
let Schema {
installers, wirers, ..
} = schema;
for install in installers {
install(self)?;
}
for wire in wirers {
wire(self)?;
}
self.rescan()
}
pub fn detach_database(&self, alias: &str) -> Result<(), DbError> {
let position = self.inner.attached.borrow().iter().position(|a| a == alias);
let Some(position) = position else {
return Ok(());
};
if !self.inner.dirty.borrow().is_empty() {
self.save()?;
}
let prefix = format!("{alias}.");
type Doomed = Vec<(Rc<dyn Fn() -> Vec<u64>>, EvictFn)>;
let doomed: Doomed = self
.inner
.tables
.borrow()
.values()
.filter(|h| h.table.starts_with(&prefix))
.map(|h| (h.resident_keys.clone(), h.evict.clone()))
.collect();
for (keys, evict) in &doomed {
let resident = keys();
let _ = evict(&resident, resident.len());
}
self.conn()
.execute(&format!("DETACH DATABASE {alias}"), &[])?;
self.inner.attached.borrow_mut().remove(position);
self.rescan()
}
pub fn check_external(&self) -> Result<bool, DbError> {
if !self.inner.caps.external_changes {
return Ok(false);
}
let Some(current) = self.file_data_version()? else {
return Ok(false);
};
if self.inner.data_version.get() == Some(current) {
return Ok(false);
}
self.save()?;
self.refresh_resident()?;
self.invalidate_relation_memos();
self.requery_all();
self.inner.data_version.set(Some(current));
Ok(true)
}
fn file_data_version(&self) -> Result<Option<i64>, DbError> {
let mut v = None;
self.conn().query("PRAGMA data_version", &[], &mut |row| {
if let Ok(i) = row.get(0).as_int() {
v = Some(i);
}
})?;
Ok(v)
}
fn refresh_resident(&self) -> Result<bool, DbError> {
struct RefreshJob {
resident_keys: Rc<dyn Fn() -> Vec<u64>>,
refresh: RefreshFn,
columns: String,
table: &'static str,
keys_where: KeysWhere,
}
let jobs: Vec<RefreshJob> = {
let tables = self.inner.tables.borrow();
tables
.values()
.map(|h| RefreshJob {
resident_keys: h.resident_keys.clone(),
refresh: h.refresh.clone(),
columns: h.columns.join(", "),
table: h.table,
keys_where: keys_where_of(h),
})
.collect()
};
let mut changed = false;
for RefreshJob {
resident_keys,
refresh,
columns,
table,
keys_where,
} in jobs
{
let resident = resident_keys();
if resident.is_empty() {
continue;
}
let mut rows: Vec<Vec<Value>> = Vec::new();
for chunk in resident.chunks(MAX_BOUND_PARAMS / 2) {
let (clause, params) = keys_where(chunk);
self.conn().query(
&format!("SELECT {columns} FROM {table} WHERE {clause}"),
¶ms,
&mut |row| {
let n = Row::len(row);
rows.push((0..n).map(|i| row.get(i)).collect());
},
)?;
}
if refresh(rows)? {
changed = true;
}
}
Ok(changed)
}
fn install_state<M: Model>(
&self,
fetch: Fetch,
raw: Option<RawQuery>,
count_only: bool,
) -> Rc<QueryState> {
let store = self.cache::<M>();
let state = Rc::new(QueryState {
store_id: store.store_id(),
table: M::TABLE,
watches: RefCell::new(Vec::new()),
local_extra: RefCell::new(Vec::new()),
set: RefCell::new(ResultSet::new(fetch.clone())),
version: day_reactive::Scope::detached().enter(|| day_reactive::Signal::new(0)),
pending: RefCell::new(QueryEvents::None),
needs_sql: Cell::new(false),
pending_commit: Cell::new(!self.inner.dirty.borrow().is_empty()),
error: day_reactive::Scope::detached().enter(|| day_reactive::Signal::new(None)),
raw,
count_only,
count: Cell::new(0),
});
self.rewire_watches(&state, &fetch);
self.inner.queries.borrow_mut().push(Rc::downgrade(&state));
if self.inner.autosave.get() && !self.inner.dirty.borrow().is_empty() {
let _ = self.save();
}
if count_only {
state.count.set(self.answer_count(&state));
} else {
let ids = self.answer(&state);
state.set.borrow_mut().reset(ids);
}
*state.pending.borrow_mut() = QueryEvents::None;
state
}
fn install_query<M: Model>(&self, fetch: Fetch, raw: Option<RawQuery>) -> Query<M> {
Query {
state: self.install_state::<M>(fetch, raw, false),
container: self.clone(),
_p: std::marker::PhantomData,
}
}
fn rewire_watches(&self, state: &Rc<QueryState>, fetch: &Fetch) {
let deps = fetch.dependencies();
let mut watches: Vec<RelWatch> = Vec::new();
let mut local_extra: Vec<&'static str> = Vec::new();
let relations = self.inner.relations.borrow();
let joins = self.inner.joins.borrow();
for dep in &deps.related {
let mut resolved = false;
for r in relations.iter() {
if r.parent_table == dep.owner && r.parent_field == dep.field {
watches.push(RelWatch {
store: r.child_store,
join_store: None,
fields: {
let mut f = vec![r.fk_field];
if let Some(o) = r.ordered {
f.push(o);
}
f
},
columns: dep.columns.clone(),
});
resolved = true;
break;
}
if r.child_table == dep.owner && r.fk_field == dep.field {
if dep.owner == state.table && !local_extra.contains(&r.fk_col) {
local_extra.push(r.fk_col);
}
watches.push(RelWatch {
store: r.parent_store,
join_store: None,
fields: Vec::new(),
columns: dep.columns.clone(),
});
resolved = true;
break;
}
}
if resolved {
continue;
}
if let Some(l) = self
.inner
.links
.borrow()
.iter()
.find(|l| l.owner_table == dep.owner && l.field == dep.field)
.copied()
{
if dep.owner == state.table && !local_extra.contains(&l.local) {
local_extra.push(l.local);
}
let far = self
.inner
.tables
.borrow()
.iter()
.find(|(_, h)| h.table == l.target_table)
.map(|(id, _)| *id);
if let Some(store) = far {
let mut columns = dep.columns.clone();
if !columns.contains(&l.remote) {
columns.push(l.remote);
}
watches.push(RelWatch {
store,
join_store: None,
fields: Vec::new(),
columns,
});
}
continue;
}
for j in joins.iter() {
let side = if j.a_table == dep.owner && j.a_field == dep.field {
Some(j.b_store)
} else if j.b_table == dep.owner && j.b_field.get() == Some(dep.field) {
Some(j.a_store)
} else {
None
};
if let Some(other) = side {
watches.push(RelWatch {
store: other,
join_store: Some(j.join_store),
fields: Vec::new(),
columns: dep.columns.clone(),
});
break;
}
}
}
*state.watches.borrow_mut() = watches;
*state.local_extra.borrow_mut() = local_extra;
}
fn mark_queries_stale(&self, change: &day_model::Change) {
let Some(&store) = change.components.first() else {
return;
};
let states: Vec<Rc<QueryState>> = {
let mut queries = self.inner.queries.borrow_mut();
queries.retain(|w| w.strong_count() > 0);
queries.iter().filter_map(|w| w.upgrade()).collect()
};
if states.is_empty() {
return;
}
let key = change.components.get(1).copied();
if key == Some(day_model::STRUCTURE) {
return; }
let (column, changed_table): (Option<String>, Option<&'static str>) = {
let tables = self.inner.tables.borrow();
let hooks = tables.get(&store);
let column = if change.components.len() >= 3 {
hooks.map(|h| {
h.fields
.iter()
.position(|f| f == change.label)
.map(|i| h.columns[i].clone())
.unwrap_or_else(|| "\u{0}".to_string())
})
} else {
None
};
(column, hooks.map(|h| h.table))
};
let column = column.as_deref();
for state in &states {
if state.needs_sql.get() {
continue; }
if let Some(raw) = &state.raw {
if changed_table.is_some_and(|t| raw.tables.iter().any(|r| r == t)) {
state.needs_sql.set(true);
}
continue;
}
let own_stale = state.store_id == store
&& match (key, change.op) {
(None, _) => true, (Some(_), Op::Insert) | (Some(_), Op::Delete) => true,
(Some(_), Op::Set) => match column {
None => true,
Some(c) => {
let set = state.set.borrow();
set.deps().touches_local(c) || state.local_extra.borrow().contains(&c)
}
},
(Some(_), Op::Move) => false, };
let stale = own_stale || {
let watches = state.watches.borrow();
watches.iter().any(|w| {
if w.join_store == Some(store) {
return true; }
if w.store != store {
return false;
}
match (key, change.op) {
(None, _) => true,
(Some(_), Op::Insert) | (Some(_), Op::Delete) => true,
(Some(_), Op::Set) => {
w.fields.contains(&change.label)
|| column.is_some_and(|c| w.columns.contains(&c))
}
(Some(_), Op::Move) => false,
}
})
};
if stale {
state.needs_sql.set(true);
state.pending_commit.set(true);
}
}
}
fn run_deferred_requeries(&self, stmts: &[(String, Vec<Value>)]) {
let states: Vec<Rc<QueryState>> = self
.inner
.queries
.borrow()
.iter()
.filter_map(|w| w.upgrade())
.collect();
for state in states {
let mut due = state.needs_sql.replace(false) | state.pending_commit.replace(false);
if let Some(raw) = &state.raw
&& !due
{
due = stmts
.iter()
.any(|(sql, _)| raw.tables.iter().any(|t| statement_touches(sql, t)));
}
if due {
self.requery(&state);
}
}
}
fn requery_all(&self) {
let states: Vec<Rc<QueryState>> = self
.inner
.queries
.borrow()
.iter()
.filter_map(|w| w.upgrade())
.collect();
for state in states {
state.needs_sql.set(false);
self.requery(&state);
}
}
fn requery(&self, state: &Rc<QueryState>) {
if state.count_only {
let n = self.answer_count(state);
if n != state.count.get() {
state.count.set(n);
state
.version
.set(state.version.get_untracked().wrapping_add(1));
}
return;
}
let ids = self.answer(state);
let change = state.set.borrow_mut().adopt(ids);
match change {
SetChange::Same => {}
SetChange::Deltas(d) => {
state.pending.borrow_mut().push(&d);
state
.version
.set(state.version.get_untracked().wrapping_add(1));
}
SetChange::Reload => {
state.pending.borrow_mut().reload();
state
.version
.set(state.version.get_untracked().wrapping_add(1));
}
}
}
fn answer_count(&self, state: &Rc<QueryState>) -> usize {
let result = (|| {
if self.table_detached(state.table) {
return Ok(0);
}
if let Some(raw) = &state.raw {
return self
.select_ids_checked(&raw.sql, &raw.params)
.map(|ids| ids.len());
}
let fetch = state.set.borrow().fetch().clone();
let cap = fetch.limit.unwrap_or(usize::MAX);
let snapshot = self.sql_snapshot();
match compile_count(state.table, &fetch, &snapshot) {
Ok(q) => {
let mut n = Ok(0_i64);
self.conn().query(&q.sql, &q.params, &mut |row| {
n = row.get(0).as_int();
})?;
Ok((n?.max(0) as usize).min(cap))
}
Err(CompileErr::NeedsFold) => {
let (q, cols) = compile_fallback(state.table, &fetch, &snapshot)
.map_err(|e| DbError::new(DbErrorKind::Unsupported, e.message()))?;
let mut n = 0;
self.conn().query(&q.sql, &q.params, &mut |row| {
let values = (0..row.len()).map(|i| row.get(i)).collect::<Vec<_>>();
if let Some(h) = value_to_handle(&values[0])
&& fetch.pred.eval(
h,
&FallbackRow {
cols: &cols,
values: &values,
},
)
{
n += 1;
}
})?;
Ok(n.min(cap))
}
Err(e) => Err(DbError::new(DbErrorKind::Unsupported, e.message())),
}
})();
match result {
Ok(n) => {
state.error.set(None);
n
}
Err(error) => {
self.inner.error.set(Some(error.to_string()));
state.error.set(Some(error));
state.count.get()
}
}
}
fn answer(&self, state: &Rc<QueryState>) -> Vec<u64> {
match self.answer_checked(state) {
Ok(ids) => {
state.error.set(None);
ids
}
Err(error) => {
self.inner.error.set(Some(error.to_string()));
state.error.set(Some(error));
state.set.borrow().ids().to_vec()
}
}
}
fn table_detached(&self, table: &str) -> bool {
table.split_once('.').is_some_and(|(alias, _)| {
alias != "main"
&& alias != "temp"
&& !self.inner.attached.borrow().iter().any(|a| a == alias)
})
}
fn answer_checked(&self, state: &Rc<QueryState>) -> Result<Vec<u64>, DbError> {
if self.table_detached(state.table) {
return Ok(Vec::new());
}
if let Some(raw) = &state.raw {
return self.select_ids_checked(&raw.sql, &raw.params);
}
let fetch = state.set.borrow().fetch().clone();
let snapshot = self.sql_snapshot();
match compile_fetch(state.table, &fetch, &snapshot) {
Ok(q) => self.select_ids_checked(&q.sql, &q.params),
Err(CompileErr::NeedsFold) => {
let (q, cols) = compile_fallback(state.table, &fetch, &snapshot)
.map_err(|e| DbError::new(DbErrorKind::Unsupported, e.message()))?;
let mut ids = Vec::new();
self.conn().query(&q.sql, &q.params, &mut |row| {
let values = (0..row.len()).map(|i| row.get(i)).collect::<Vec<_>>();
if let Some(h) = value_to_handle(&values[0])
&& fetch.pred.eval(
h,
&FallbackRow {
cols: &cols,
values: &values,
},
)
{
ids.push(h);
}
})?;
if let Some(limit) = fetch.limit {
ids.truncate(limit);
}
Ok(ids)
}
Err(e) => Err(DbError::new(DbErrorKind::Unsupported, e.message())),
}
}
fn sql_snapshot(&self) -> SqlSnapshot {
let tables = self.inner.tables.borrow();
let relations = self.inner.relations.borrow();
let joins = self.inner.joins.borrow();
SqlSnapshot {
tables: tables
.values()
.map(|h| TableInfo {
table: h.table.to_string(),
key: h.key_cols[0].clone(),
fts: h.fts.clone(),
fts_key: h.fts_key,
spatial: h.spatial.clone(),
})
.collect(),
rels: relations
.iter()
.map(|r| RelInfo {
parent_table: r.parent_table,
parent_field: r.parent_field,
parent_key: r.parent_key_col,
child_table: r.child_table,
child_key: r.child_key_col,
fk_field: r.fk_field,
fk_col: r.fk_col,
})
.collect(),
joins: joins
.iter()
.map(|j| JoinInfo {
join_table: j.join_table,
a_table: j.a_table,
a_field: j.a_field,
a_key: j.a_key_col,
b_table: j.b_table,
b_field: j.b_field.get(),
b_key: j.b_key_col,
parent_col: j.parent_col,
child_col: j.child_col,
})
.collect(),
links: self.inner.links.borrow().clone(),
fold: self.inner.caps.unicode_fold,
}
}
}
struct TableInfo {
table: String,
key: String,
fts: Option<String>,
fts_key: &'static str,
spatial: Option<(String, String, String)>,
}
struct RelInfo {
parent_table: &'static str,
parent_field: &'static str,
parent_key: &'static str,
child_table: &'static str,
child_key: &'static str,
fk_field: &'static str,
fk_col: &'static str,
}
struct JoinInfo {
join_table: &'static str,
a_table: &'static str,
a_field: &'static str,
a_key: &'static str,
b_table: &'static str,
b_field: Option<&'static str>,
b_key: &'static str,
parent_col: &'static str,
child_col: &'static str,
}
struct SqlSnapshot {
tables: Vec<TableInfo>,
rels: Vec<RelInfo>,
joins: Vec<JoinInfo>,
links: Vec<LinkInfo>,
fold: bool,
}
impl SqlIndex for SqlSnapshot {
fn relation(&self, owner: &str, field: &str) -> Option<RelSql> {
for r in &self.rels {
if r.parent_table == owner && r.parent_field == field {
return Some(RelSql::Children {
target_key: r.child_key.to_string(),
fk_col: r.fk_col.to_string(),
owner_key: r.parent_key.to_string(),
});
}
if r.child_table == owner && r.fk_field == field {
return Some(RelSql::Referent {
target_key: r.parent_key.to_string(),
fk_col: r.fk_col.to_string(),
});
}
}
for j in &self.joins {
if j.a_table == owner && j.a_field == field {
return Some(RelSql::Join {
join_table: j.join_table.to_string(),
owner_col: j.parent_col.to_string(),
target_col: j.child_col.to_string(),
owner_key: j.a_key.to_string(),
target_key: j.b_key.to_string(),
});
}
if j.b_table == owner && j.b_field == Some(field) {
return Some(RelSql::Join {
join_table: j.join_table.to_string(),
owner_col: j.child_col.to_string(),
target_col: j.parent_col.to_string(),
owner_key: j.b_key.to_string(),
target_key: j.a_key.to_string(),
});
}
}
for l in &self.links {
if l.owner_table == owner && l.field == field {
return Some(RelSql::Linked {
local_col: l.local.to_string(),
remote_col: l.remote.to_string(),
target_key: self.key_of(l.target_table)?,
});
}
}
None
}
fn key_of(&self, table: &str) -> Option<String> {
self.tables
.iter()
.find(|t| t.table == table)
.map(|t| t.key.clone())
}
fn fts_of(&self, table: &str) -> Option<String> {
self.tables
.iter()
.find(|t| t.table == table)
.and_then(|t| t.fts.clone())
}
fn fts_key_of(&self, table: &str) -> &str {
self.tables
.iter()
.find(|t| t.table == table)
.map(|t| t.fts_key)
.unwrap_or("rowid")
}
fn geo_of(&self, table: &str, lat: &str, lon: &str) -> Option<String> {
self.tables
.iter()
.find(|t| t.table == table)
.and_then(|t| t.spatial.as_ref())
.filter(|(a, o, _)| a == lat && o == lon)
.map(|(_, _, g)| g.clone())
}
fn unicode_fold(&self) -> bool {
self.fold
}
}
fn statement_touches(sql: &str, table: &str) -> bool {
for prefix in [
"INSERT INTO ",
"INSERT OR REPLACE INTO ",
"UPDATE ",
"DELETE FROM ",
] {
if let Some(rest) = sql.strip_prefix(prefix) {
return rest.split([' ', '(']).next() == Some(table);
}
}
false
}
#[cfg(feature = "pieces")]
mod pieces_glue {
use std::cell::RefCell;
use day_model::{Elem, Keyed, Store};
use day_pieces::{ModelSlot, RowConn, RowSource};
use crate::{Delta, Model, Query, QueryEvents};
const FAULT_BEHIND: usize = 16;
const FAULT_AHEAD: usize = 64;
impl<M: Model> RowSource for Query<M> {
type Slot = ModelSlot<M>;
type Ref = Elem<M>;
type Conn = QueryConn<M>;
fn connect(self) -> QueryConn<M> {
let store = self.container.cache::<M>();
QueryConn {
query: self,
store,
keys: RefCell::new(Vec::new()),
}
}
}
pub struct QueryConn<M: Model> {
query: Query<M>,
store: Store<Keyed<M>>,
keys: RefCell<Vec<u64>>,
}
impl<M: Model> QueryConn<M> {
fn fault_window(&self, index: usize) {
let len = self.keys.borrow().len();
let start = index.saturating_sub(FAULT_BEHIND);
let end = (index + FAULT_AHEAD).min(len);
self.query.materialize(start..end);
}
}
impl<M: Model> RowConn for QueryConn<M> {
type Slot = ModelSlot<M>;
type Ref = Elem<M>;
fn refresh(&self) -> Vec<u64> {
let keys: Vec<u64> = self.query.ids().iter().map(|id| id.handle()).collect();
*self.keys.borrow_mut() = keys.clone();
keys
}
fn len(&self) -> usize {
self.keys.borrow().len()
}
fn token_at(&self, index: usize) -> u64 {
self.keys.borrow().get(index).copied().unwrap_or(0)
}
fn tokens_now(&self) -> Vec<u64> {
self.keys.borrow().clone()
}
fn slot_at(&self, index: usize) -> Option<ModelSlot<M>> {
let key = self.keys.borrow().get(index).copied()?;
if !self.store.with_untracked(|k| k.get(key).is_some()) {
self.fault_window(index);
}
Some(ModelSlot::for_key(self.store, key))
}
fn rebind(&self, slot: &ModelSlot<M>, index: usize) {
if let Some(key) = self.keys.borrow().get(index).copied() {
if !self.store.with_untracked(|k| k.get(key).is_some()) {
self.fault_window(index);
}
slot.rebind_key(key);
}
}
fn select_ref(&self, index: usize) -> Option<Elem<M>> {
let key = self.keys.borrow().get(index).copied()?;
if !self.store.with_untracked(|k| k.get(key).is_some()) {
self.fault_window(index);
}
Some(self.store.elem(key))
}
fn values_flow_by_reload(&self) -> bool {
false }
fn take_row_events(&self) -> Option<Vec<day_spec::props::RowDelta>> {
match self.query.take_events() {
QueryEvents::Deltas(d) => Some(
d.into_iter()
.map(|d| match d {
Delta::Insert(i, _) => day_spec::props::RowDelta::Insert(i),
Delta::Remove(i, _) => day_spec::props::RowDelta::Remove(i),
Delta::Move(from, to, _) => day_spec::props::RowDelta::Move(from, to),
})
.collect(),
),
QueryEvents::Reload | QueryEvents::None => None,
}
}
fn commit_move(&self, from: usize, to: usize) {
let mut keys = self.keys.borrow_mut();
if from < keys.len() && to < keys.len() {
let key = keys.remove(from);
keys.insert(to, key);
}
}
fn commit_delete(&self, index: usize) {
let mut keys = self.keys.borrow_mut();
if index < keys.len() {
keys.remove(index);
}
}
}
}
#[cfg(feature = "pieces")]
pub use pieces_glue::QueryConn;
#[cfg(feature = "datetime")]
pub mod datetime {
use crate::{ColumnValue, DbError, DbErrorKind, SqlType, Value, ValueCodec};
use day_spec::controls::datetime::{DayDate, DayTime};
impl ColumnValue for DayDate {
const SQL_TYPE: SqlType = SqlType::Integer;
fn to_sqlite_value(&self) -> Value {
Value::Int(self.to_epoch_days())
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
Ok(DayDate::from_epoch_days(v.as_int()?))
}
}
impl ColumnValue for DayTime {
const SQL_TYPE: SqlType = SqlType::Integer;
fn to_sqlite_value(&self) -> Value {
Value::Int(self.seconds_of_day())
}
fn from_sqlite_value(v: Value) -> Result<Self, DbError> {
Ok(DayTime::from_seconds_of_day(v.as_int()?))
}
}
pub struct Iso8601;
impl ValueCodec<DayDate> for Iso8601 {
const SQL_TYPE: SqlType = SqlType::Text;
fn to_sqlite_value(v: &DayDate) -> Value {
Value::Text(v.to_string())
}
fn from_sqlite_value(v: Value) -> Result<DayDate, DbError> {
let s = v.as_text()?;
DayDate::parse_iso(s).ok_or_else(|| {
DbError::new(DbErrorKind::Decode, format!("not an ISO-8601 date: {s:?}"))
})
}
}
impl ValueCodec<DayTime> for Iso8601 {
const SQL_TYPE: SqlType = SqlType::Text;
fn to_sqlite_value(v: &DayTime) -> Value {
Value::Text(v.to_string())
}
fn from_sqlite_value(v: Value) -> Result<DayTime, DbError> {
let s = v.as_text()?;
DayTime::parse_iso(s).ok_or_else(|| {
DbError::new(DbErrorKind::Decode, format!("not an ISO-8601 time: {s:?}"))
})
}
}
pub struct EpochSeconds;
impl ValueCodec<DayDate> for EpochSeconds {
const SQL_TYPE: SqlType = SqlType::Integer;
fn to_sqlite_value(v: &DayDate) -> Value {
Value::Int(v.to_epoch_days() * 86_400)
}
fn from_sqlite_value(v: Value) -> Result<DayDate, DbError> {
Ok(DayDate::from_epoch_days(v.as_int()?.div_euclid(86_400)))
}
}
pub struct EpochMillis;
impl ValueCodec<DayDate> for EpochMillis {
const SQL_TYPE: SqlType = SqlType::Integer;
fn to_sqlite_value(v: &DayDate) -> Value {
Value::Int(v.to_epoch_days() * 86_400_000)
}
fn from_sqlite_value(v: Value) -> Result<DayDate, DbError> {
Ok(DayDate::from_epoch_days(v.as_int()?.div_euclid(86_400_000)))
}
}
}
#[cfg(all(test, feature = "driver-rusqlite"))]
mod eviction_tests;