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, FtsSql, RelSql, SqlIndex, compile_count, compile_fallback, compile_fetch,
};
mod blob;
pub use blob::{Blob, BlobField, BlobReader};
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,
CommittedRefreshFailed,
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,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Checkpoint {
pub busy: bool,
pub log_frames: i64,
pub checkpointed_frames: i64,
}
impl Checkpoint {
pub fn is_complete(&self) -> bool {
!self.busy
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ForeignKeyViolation {
pub table: String,
pub rowid: Option<i64>,
pub parent: String,
pub constraint: i64,
}
impl fmt::Display for ForeignKeyViolation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.rowid {
Some(rowid) => write!(
f,
"foreign key: {} row {rowid} references missing {}",
self.table, self.parent
),
None => write!(
f,
"foreign key: {} row references missing {}",
self.table, self.parent
),
}
}
}
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 = quoted_name(from)
.unwrap_or_else(|| from.split_whitespace().next().unwrap_or("").to_string());
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,
pub lazy: 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 UNIQUE_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 LAZY_FIELDS: &'static [&'static str] = &[];
fn settle_lazy(&mut self) -> Vec<(&'static str, std::sync::Arc<Vec<u8>>)> {
Vec::new()
}
const EXTERNAL: bool = false;
const SCHEMA: Option<&'static str> = None;
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 ident(name: &str) -> String {
let mut out = String::with_capacity(name.len() + 2);
out.push('"');
for ch in name.chars() {
if ch == '"' {
out.push('"');
}
out.push(ch);
}
out.push('"');
out
}
#[derive(Clone, Copy)]
pub(crate) struct TableName {
pub(crate) schema: Option<&'static str>,
pub(crate) bare: &'static str,
}
impl TableName {
pub(crate) fn of<M: Model>() -> Self {
let bare = match M::SCHEMA {
Some(schema) => M::TABLE
.strip_prefix(schema)
.and_then(|rest| rest.strip_prefix('.'))
.unwrap_or(M::TABLE),
None => M::TABLE,
};
TableName {
schema: M::SCHEMA,
bare,
}
}
pub(crate) fn plain(bare: &'static str) -> Self {
TableName { schema: None, bare }
}
pub(crate) fn sql(&self) -> String {
table_sql(self.schema, self.bare)
}
pub(crate) fn shadow(&self, suffix: &str) -> String {
table_sql(self.schema, &format!("{}{suffix}", self.bare))
}
}
pub(crate) fn table_sql(schema: Option<&str>, bare: &str) -> String {
match schema {
Some(schema) => format!("{}.{}", ident(schema), ident(bare)),
None => ident(bare),
}
}
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(b"ddl-v2|");
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 idx in M::UNIQUE_INDEXES {
eat(b"uniq:");
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 SettleFn = Rc<dyn Fn(&[u64]) -> Vec<(u64, &'static str, std::sync::Arc<Vec<u8>>)>>;
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, Debug)]
struct FkSpec {
child_table: &'static str,
child_field: &'static str,
parent_table: &'static str,
parent_sql: String,
parent_bare: &'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_sql: TableName::of::<M>().sql(),
parent_bare: TableName::of::<M>().bare,
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)>,
by_store: HashMap<u64, Vec<u64>>,
full: Vec<u64>,
authors: HashMap<(u64, u64), Vec<&'static str>>,
store_authors: HashMap<u64, Vec<&'static str>>,
}
fn add_author(tags: &mut Vec<&'static str>, author: Option<&'static str>) {
if let Some(author) = author
&& !tags.contains(&author)
{
tags.push(author);
}
}
impl DirtyState {
fn merge(&mut self, newer: Self) {
for store in newer.full {
if !self.full.contains(&store) {
self.full.push(store);
}
}
for (store, tags) in newer.store_authors {
let mine = self.store_authors.entry(store).or_default();
for tag in tags {
add_author(mine, Some(tag));
}
}
for (id, tags) in newer.authors {
let mine = self.authors.entry(id).or_default();
for tag in tags {
add_author(mine, Some(tag));
}
}
for id in newer.order {
let next = newer.rows[&id].clone();
if !self.rows.contains_key(&id) {
self.order.push(id);
self.by_store.entry(id.0).or_default().push(id.1);
}
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);
}
add_author(self.store_authors.entry(store).or_default(), change.author);
return;
};
if key == day_model::STRUCTURE {
return; }
let id = (store, key);
if !self.rows.contains_key(&id) {
self.order.push(id);
self.by_store.entry(store).or_default().push(key);
}
add_author(self.authors.entry(id).or_default(), change.author);
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) name: TableName,
pub(crate) key_cols: Vec<String>,
pub(crate) key_where: KeyWhere,
pub(crate) columns: Vec<String>,
pub(crate) column_names: Vec<&'static str>,
pub(crate) fields: Vec<String>,
pub(crate) lazy: Vec<bool>,
pub(crate) settle: SettleFn,
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)>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum WriteOrigin {
#[default]
Local,
RemoteImport,
Maintenance,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RowChange {
pub table: &'static str,
pub key: Vec<Value>,
pub kind: RowChangeKind,
pub authors: Vec<&'static str>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RowChangeKind {
Inserted,
Updated(Vec<&'static str>),
Deleted,
Rewritten,
}
impl RowChange {
fn absorb(&mut self, later: RowChange) {
for author in later.authors {
add_author(&mut self.authors, Some(author));
}
self.kind = match (
std::mem::replace(&mut self.kind, RowChangeKind::Deleted),
later.kind,
) {
(RowChangeKind::Updated(mut cols), RowChangeKind::Updated(more)) => {
for col in more {
if !cols.contains(&col) {
cols.push(col);
}
}
RowChangeKind::Updated(cols)
}
(RowChangeKind::Updated(_), RowChangeKind::Rewritten)
| (RowChangeKind::Rewritten, RowChangeKind::Updated(_) | RowChangeKind::Rewritten) => {
RowChangeKind::Rewritten
}
(
kept @ (RowChangeKind::Inserted | RowChangeKind::Deleted),
RowChangeKind::Updated(_) | RowChangeKind::Rewritten,
) => kept,
(_, next @ (RowChangeKind::Inserted | RowChangeKind::Deleted)) => next,
};
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub enum Refresh {
#[default]
None,
Tables(Vec<&'static str>),
All,
}
pub trait TransactionParticipant: 'static {
fn begin(
&self,
connection: &mut dyn SqliteConnection,
origin: WriteOrigin,
) -> Result<(), DbError>;
fn before_commit(
&self,
connection: &mut dyn SqliteConnection,
origin: WriteOrigin,
changes: &[RowChange],
) -> Result<Refresh, DbError>;
}
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>,
participant: RefCell<Option<Rc<dyn TransactionParticipant>>>,
transaction_changes: RefCell<Vec<RowChange>>,
refresh_scope: RefCell<Refresh>,
refresh_pending: Cell<bool>,
cache_limit: Cell<usize>,
eviction_retry: RefCell<HashMap<u64, usize>>,
blob_memo: RefCell<blob::BlobMemo>,
blob_cache_limit: Cell<usize>,
engine_cascades: RefCell<Vec<&'static str>>,
pub(crate) reconciling: Cell<bool>,
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;
pub const DEFAULT_BLOB_CACHE_LIMIT: usize = 32 * 1024 * 1024;
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),
refresh_pending: Cell::new(false),
participant: RefCell::new(None),
transaction_changes: RefCell::new(Vec::new()),
refresh_scope: RefCell::new(Refresh::None),
cache_limit: Cell::new(DEFAULT_CACHE_LIMIT),
eviction_retry: RefCell::new(HashMap::new()),
blob_memo: RefCell::new(blob::BlobMemo::default()),
blob_cache_limit: Cell::new(DEFAULT_BLOB_CACHE_LIMIT),
engine_cascades: RefCell::new(Vec::new()),
reconciling: Cell::new(false),
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 set_blob_cache_limit(&self, bytes: usize) {
self.inner.blob_cache_limit.set(bytes);
let mut memo = self.inner.blob_memo.borrow_mut();
if bytes == 0 {
memo.clear();
} else {
memo.insert(
(u64::MAX, u64::MAX, ""),
std::sync::Arc::new(Vec::new()),
bytes,
);
memo.forget_row(u64::MAX, u64::MAX);
}
}
pub fn blob_cache_bytes(&self) -> usize {
self.inner.blob_memo.borrow().bytes()
}
pub fn blob_cache_len(&self) -> usize {
self.inner.blob_memo.borrow().len()
}
pub(crate) fn engine_cascade_allowed(&self) -> bool {
!day_model::record_capture_active() && self.inner.participant.borrow().is_none()
}
pub(crate) fn note_engine_cascade(&self, child_table: &'static str) {
let mut tables = self.inner.engine_cascades.borrow_mut();
if !tables.contains(&child_table) {
tables.push(child_table);
}
}
fn reconcile_engine_cascades(&self) {
for _ in 0..16 {
let tables: Vec<&'static str> =
std::mem::take(&mut *self.inner.engine_cascades.borrow_mut());
if tables.is_empty() {
return;
}
self.inner.reconciling.set(true);
let refreshed = self.refresh_resident_in(Some(&tables));
self.inner.reconciling.set(false);
if let Err(error) = refreshed {
self.inner.error.set(Some(error.to_string()));
return;
}
self.invalidate_relation_memos_of(&tables);
self.requery_tables(&tables);
}
}
fn settle_blobs(&self, dirty: &DirtyState) {
let mut written: HashMap<u64, Vec<u64>> = HashMap::new();
let mut deleted: Vec<(u64, u64)> = Vec::new();
for id in &dirty.order {
match dirty.rows.get(id) {
Some(DirtyRow::Delete) => deleted.push(*id),
Some(_) => written.entry(id.0).or_default().push(id.1),
None => {}
}
}
for store in &dirty.full {
let keys = {
let tables = self.inner.tables.borrow();
tables
.get(store)
.map(|h| (h.resident_keys)())
.unwrap_or_default()
};
written.entry(*store).or_default().extend(keys);
}
let limit = self.inner.blob_cache_limit.get();
for (store, keys) in written {
let settle = {
let tables = self.inner.tables.borrow();
match tables.get(&store) {
Some(h) if h.lazy.iter().any(|l| *l) => h.settle.clone(),
_ => continue,
}
};
for (key, field, bytes) in settle(&keys) {
self.inner
.blob_memo
.borrow_mut()
.insert((store, key, field), bytes, limit);
}
}
if !deleted.is_empty() {
let mut memo = self.inner.blob_memo.borrow_mut();
for (store, key) in deleted {
memo.forget_row(store, key);
}
}
}
fn blob_target(
&self,
store: u64,
key: u64,
column: &str,
) -> Option<(String, String, Vec<Value>)> {
let tables = self.inner.tables.borrow();
let hooks = tables.get(&store)?;
let (clause, params) = (hooks.key_where)(key);
Some((
hooks.name.sql(),
format!("{} FROM {} WHERE {clause}", ident(column), hooks.name.sql()),
params,
))
}
pub(crate) fn blob_bytes(
&self,
store: u64,
key: u64,
column: &'static str,
) -> Result<std::sync::Arc<Vec<u8>>, DbError> {
if self.inner.autosave.get() && !self.inner.dirty.borrow().is_empty() {
self.save()?;
}
if let Some(hit) = self.inner.blob_memo.borrow_mut().get((store, key, column)) {
return Ok(hit);
}
let Some((_, tail, params)) = self.blob_target(store, key, column) else {
return Ok(std::sync::Arc::new(Vec::new()));
};
let mut bytes = Vec::new();
self.conn()
.query(&format!("SELECT {tail}"), ¶ms, &mut |row| {
bytes = row.get(0).as_blob().map(|b| b.to_vec()).unwrap_or_default();
})?;
let bytes = std::sync::Arc::new(bytes);
let limit = self.inner.blob_cache_limit.get();
self.inner
.blob_memo
.borrow_mut()
.insert((store, key, column), bytes.clone(), limit);
Ok(bytes)
}
pub(crate) fn blob_len(
&self,
store: u64,
key: u64,
column: &'static str,
) -> Result<usize, DbError> {
if self.inner.autosave.get() && !self.inner.dirty.borrow().is_empty() {
self.save()?;
}
if let Some(hit) = self.inner.blob_memo.borrow_mut().get((store, key, column)) {
return Ok(hit.len());
}
let Some((table, _, params)) = self.blob_target(store, key, column) else {
return Ok(0);
};
let clause = {
let tables = self.inner.tables.borrow();
tables
.get(&store)
.map(|h| (h.key_where)(key).0)
.unwrap_or_default()
};
let mut len = 0usize;
self.conn().query(
&format!(
"SELECT length({}) FROM {table} WHERE {clause}",
ident(column)
),
¶ms,
&mut |row| len = row.get(0).as_int().unwrap_or(0).max(0) as usize,
)?;
Ok(len)
}
pub(crate) fn blob_range(
&self,
store: u64,
key: u64,
column: &'static str,
range: std::ops::Range<usize>,
) -> Result<Vec<u8>, DbError> {
if self.inner.autosave.get() && !self.inner.dirty.borrow().is_empty() {
self.save()?;
}
if let Some(hit) = self.inner.blob_memo.borrow_mut().get((store, key, column)) {
let start = range.start.min(hit.len());
let end = range.end.clamp(start, hit.len());
return Ok(hit[start..end].to_vec());
}
if range.end <= range.start {
return Ok(Vec::new());
}
let Some((table, _, mut params)) = self.blob_target(store, key, column) else {
return Ok(Vec::new());
};
let clause = {
let tables = self.inner.tables.borrow();
tables
.get(&store)
.map(|h| (h.key_where)(key).0)
.unwrap_or_default()
};
let mut all = vec![
Value::Int(range.start as i64 + 1),
Value::Int((range.end - range.start) as i64),
];
all.append(&mut params);
let mut bytes = Vec::new();
self.conn().query(
&format!(
"SELECT substr({}, ?, ?) FROM {table} WHERE {clause}",
ident(column)
),
&all,
&mut |row| bytes = row.get(0).as_blob().map(|b| b.to_vec()).unwrap_or_default(),
)?;
Ok(bytes)
}
#[allow(dead_code)]
pub(crate) fn blob_forget(&self, store: u64, key: u64) {
self.inner.blob_memo.borrow_mut().forget_row(store, key);
}
pub(crate) fn blob_forget_field(&self, store: u64, key: u64, column: &'static str) {
self.inner
.blob_memo
.borrow_mut()
.forget_one((store, key, column));
}
pub fn install_transaction_participant(
&self,
participant: impl TransactionParticipant,
) -> Result<(), DbError> {
if self.inner.worker_transaction.get()
|| !self.inner.dirty.borrow().is_empty()
|| self.inner.participant.borrow().is_some()
{
return Err(DbError::new(
DbErrorKind::Deny,
"transaction participant must be installed on an idle, clean container",
));
}
*self.inner.participant.borrow_mut() = Some(Rc::new(participant));
Ok(())
}
fn participant_begin(
&self,
conn: &mut dyn SqliteConnection,
origin: WriteOrigin,
) -> Result<(), DbError> {
let participant = self.inner.participant.borrow().clone();
if let Some(p) = participant {
self.inner.transaction_changes.borrow_mut().clear();
*self.inner.refresh_scope.borrow_mut() = Refresh::None;
p.begin(conn, origin)?;
}
Ok(())
}
fn participant_commit(
&self,
conn: &mut dyn SqliteConnection,
origin: WriteOrigin,
) -> Result<(), DbError> {
let participant = self.inner.participant.borrow().clone();
if let Some(p) = participant {
let changes = std::mem::take(&mut *self.inner.transaction_changes.borrow_mut());
let scope = p.before_commit(conn, origin, &changes)?;
*self.inner.refresh_scope.borrow_mut() = scope;
}
Ok(())
}
fn participant_rollback(&self) {
self.inner.transaction_changes.borrow_mut().clear();
*self.inner.refresh_scope.borrow_mut() = Refresh::None;
}
fn record_changes(&self, changes: Vec<RowChange>) {
if changes.is_empty() {
return;
}
let mut set = self.inner.transaction_changes.borrow_mut();
for change in changes {
match set
.iter_mut()
.find(|c| c.table == change.table && c.key == change.key)
{
None => set.push(change),
Some(prior) => prior.absorb(change),
}
}
}
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());
}
if self.inner.refresh_pending.get() {
self.refresh_committed()?;
}
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 had_writes = !dirty.is_empty();
let result = self.flush(&dirty);
self.inner.flushing.set(false);
match &result {
Ok(stmts) => {
self.inner.eviction_retry.borrow_mut().clear();
let mut touched: Vec<u64> = dirty.order.iter().map(|(store, _)| *store).collect();
touched.extend(dirty.full.iter().copied());
touched.sort_unstable();
touched.dedup();
for store in touched {
self.enforce_cache_limit(store, &[], false);
}
self.settle_blobs(&dirty);
self.reconcile_engine_cascades();
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()));
}
}
if result.is_ok() && !self.inner.worker_transaction.get() && had_writes {
self.participant_refresh()?;
}
result
}
fn participant_refresh(&self) -> Result<(), DbError> {
let scope = std::mem::take(&mut *self.inner.refresh_scope.borrow_mut());
let tables = match scope {
Refresh::None => return Ok(()),
Refresh::Tables(tables) => Some(tables),
Refresh::All => None,
};
self.inner.refresh_pending.set(true);
self.refresh_committed_in(tables.as_deref()).map_err(|e| {
let error = DbError::new(DbErrorKind::CommittedRefreshFailed, e.to_string());
self.inner.error.set(Some(error.to_string()));
error
})
}
fn refresh_committed(&self) -> Result<(), DbError> {
self.refresh_committed_in(None)
}
fn refresh_committed_in(&self, tables: Option<&[&'static str]>) -> Result<(), DbError> {
if !self.inner.dirty.borrow().is_empty() {
return Err(DbError::new(
DbErrorKind::Deny,
"refresh committed state before making further model edits",
));
}
self.refresh_resident_in(tables)?;
match tables {
None => {
self.invalidate_relation_memos();
self.requery_all();
}
Some(tables) => {
self.invalidate_relation_memos_of(tables);
self.requery_tables(tables);
}
}
if self.inner.caps.external_changes {
self.inner.data_version.set(self.file_data_version()?);
}
self.inner.refresh_pending.set(false);
self.inner.error.set_if_changed(None);
Ok(())
}
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 key = ident(M::KEY);
let keys = self.select_id_column_checked(&format!(
"SELECT {key} FROM {} ORDER BY {key}",
TableName::of::<M>().sql()
))?;
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 {}", TableName::of::<M>().sql()),
&[],
&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(),
select_list(hooks),
hooks.name.sql(),
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);
if evicted > 0 {
let is_resident = {
let tables = self.inner.tables.borrow();
tables.get(&store_id).map(|h| h.is_resident.clone())
};
if let Some(is_resident) = is_resident {
let mut memo = self.inner.blob_memo.borrow_mut();
for key in candidates.iter().filter(|k| !is_resident(**k)) {
memo.forget_row(store_id, *key);
}
}
}
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)> {
let dirty = self.inner.dirty.borrow();
let Some(keys) = dirty.by_store.get(&store_id) else {
return Vec::new();
};
keys.iter()
.filter_map(|k| {
dirty
.rows
.get(&(store_id, *k))
.map(|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 write_db_version(conn: &mut dyn SqliteConnection, v: u32) -> Result<(), DbError> {
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 versioned_transaction(
&self,
version: u32,
f: impl FnOnce(&mut dyn SqliteConnection) -> Result<(), DbError>,
) -> Result<(), DbError> {
let mut conn = self.conn();
conn.begin()?;
let result = f(conn.as_mut())
.and_then(|()| Self::write_db_version(conn.as_mut(), version))
.and_then(|()| conn.commit());
if result.is_err() {
let _ = conn.rollback();
}
result
}
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
),
));
}
self.versioned_transaction(stage.to, stage.run)?;
current = stage.to;
}
if target > 0 && current != target {
self.versioned_transaction(target, |_| Ok(()))?;
}
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)>;
let settle = Rc::new(move |keys: &[u64]| {
let mut out = Vec::new();
if M::LAZY_FIELDS.is_empty() {
return out;
}
day_model::with_author(ModelContainer::EXTERNAL_AUTHOR, || {
for &key in keys {
let Some(mut row) = store.with_untracked(|k| k.get(key).cloned()) else {
continue;
};
let taken = row.settle_lazy();
if taken.is_empty() {
continue;
}
store.merge_row(key, row, M::LAZY_FIELDS);
out.extend(taken.into_iter().map(|(f, b)| (key, f, b)));
}
});
out
}) as SettleFn;
self.inner.tables.borrow_mut().insert(
store_id,
TableHooks {
table: M::TABLE,
name: TableName::of::<M>(),
key_cols: vec![M::KEY.to_string()],
key_where: Rc::new(|h| (format!("{} = ?", ident(M::KEY)), vec![key_param(h)])),
columns,
column_names: M::COLUMNS.iter().map(|c| c.name).collect(),
fields,
lazy: M::COLUMNS.iter().map(|c| c.lazy).collect(),
settle,
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()) {
self.conn().begin()?;
let result = self
.create_shadow_tables::<M>()
.and_then(|()| self.conn().commit());
if result.is_err() {
let _ = self.conn().rollback();
}
return result;
}
let name = TableName::of::<M>();
let delta = if self.table_exists(M::TABLE)? {
let file = self.file_schema(name)?;
Some((self.compare_schema::<M>(&file)?, file))
} else {
None
};
let rebuild = matches!(delta, Some((SchemaDelta::Rebuild, _)));
let fk_was_on = rebuild && self.foreign_keys_enabled()?;
if fk_was_on {
self.conn().execute("PRAGMA foreign_keys = OFF", &[])?;
}
let began = self.conn().begin();
let result = began.and_then(|()| {
let result = (|| {
let t = name.bare;
if self.table_exists(&format!("{t}_fts"))? {
for suffix in ["ai", "ad", "au"] {
self.conn().execute(
&format!(
"DROP TRIGGER IF EXISTS {}",
ident(&format!("{t}_fts_{suffix}"))
),
&[],
)?;
}
self.conn()
.execute(&format!("DROP TABLE {}", name.shadow("_fts")), &[])?;
self.conn().execute(
&format!("DROP TABLE IF EXISTS {}", name.shadow("_fts_keys")),
&[],
)?;
}
let mut unique_indexes: Vec<&'static str> = Vec::new();
match delta {
None => self.create_table_named::<M>(name.sql())?,
Some((SchemaDelta::Lightweight(plan), _)) => {
unique_indexes = plan.unique_indexes.clone();
self.lightweight_migrate::<M>(&plan)?;
}
Some((SchemaDelta::Rebuild, file)) => {
self.rebuild_table::<M>(&file)?;
}
}
self.create_indexes::<M>(&unique_indexes)?;
if rebuild {
let dangling = Self::foreign_key_check_on(self.conn().as_mut())?;
if let Some(first) = dangling.first() {
return Err(DbError::new(
DbErrorKind::Schema,
format!(
"rebuilding `{}` leaves {} row(s) with a dangling foreign \
key ({first}); the file is unchanged",
M::TABLE,
dangling.len()
),
));
}
}
self.create_shadow_tables::<M>()?;
self.store_fingerprint(M::TABLE, &fp)?;
self.conn().commit()
})();
if result.is_err() {
let _ = self.conn().rollback();
}
result
});
if fk_was_on {
let restored = self
.conn()
.execute("PRAGMA foreign_keys = ON", &[])
.map(|_| ());
return result.and(restored);
}
result
}
fn foreign_keys_enabled(&self) -> Result<bool, DbError> {
let mut on = false;
self.conn().query("PRAGMA foreign_keys", &[], &mut |row| {
on = row.get(0).as_int().unwrap_or(0) != 0;
})?;
Ok(on)
}
fn create_shadow_tables<M: Model>(&self) -> Result<(), DbError> {
let name = TableName::of::<M>();
let bare = name.bare;
let t = name.sql();
let key = ident(M::KEY);
let trigger = |suffix: &str| ident(&format!("{bare}{suffix}"));
if !M::FTS_COLUMNS.is_empty() {
if !self.inner.caps.full_text_search {
return Err(DbError::new(
DbErrorKind::Unsupported,
format!(
"`{bare}` declares fts(…) but this driver's engine has no FTS5 — the \
bundled and cipher engines compile it in"
),
));
}
let fts = name.shadow("_fts");
let fts_command = ident(&format!("{bare}_fts"));
let cols = column_list(M::FTS_COLUMNS);
let fresh = !self.table_exists(&format!("{bare}_fts"))?;
let tokenize = match M::FTS_TOKENIZE {
Some(tok) => format!(", tokenize='{}'", tok.replace('\'', "''")),
None => String::new(),
};
let new_cols = prefixed_list("new", M::FTS_COLUMNS);
let wide = M::COLUMNS
.iter()
.find(|c| c.name == M::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 == M::KEY)
.expect("key column")
.sql
.ddl();
let fts_keys = name.shadow("_fts_keys");
self.conn().execute(&format!("CREATE TABLE IF NOT EXISTS {fts_keys}(day_rowid INTEGER PRIMARY KEY, day_key {key_type} NOT NULL UNIQUE)"), &[])?;
self.conn().execute(&format!("CREATE VIRTUAL TABLE IF NOT EXISTS {fts} USING fts5(day_key UNINDEXED, {cols}{tokenize})"), &[])?;
let insert = format!(
"INSERT INTO {fts_keys}(day_key) VALUES (new.{key}); INSERT INTO {fts}(rowid, day_key, {cols}) VALUES ((SELECT day_rowid FROM {fts_keys} WHERE day_key = new.{key}), new.{key}, {new_cols});"
);
let delete = format!(
"DELETE FROM {fts} WHERE rowid = (SELECT day_rowid FROM {fts_keys} WHERE day_key = old.{key}); DELETE FROM {fts_keys} WHERE day_key = old.{key};"
);
let (update_of, changed) = fts_update_guard(M::KEY, M::FTS_COLUMNS);
for (suffix, event, body) in [
("_fts_ai", "INSERT".to_string(), insert.clone()),
("_fts_ad", "DELETE".to_string(), delete.clone()),
(
"_fts_au",
format!("UPDATE OF {update_of}"),
format!("{delete} {insert}"),
),
] {
let when = if suffix == "_fts_au" {
format!(" WHEN {changed}")
} else {
String::new()
};
self.ensure_trigger(
&format!("{bare}{suffix}"),
name.schema,
&format!(
"CREATE TRIGGER {} AFTER {event} ON {t}{when} BEGIN {body} END",
trigger(suffix)
),
)?;
}
if fresh {
self.conn().execute(
&format!("INSERT INTO {fts_keys}(day_key) SELECT {key} FROM {t}"),
&[],
)?;
let qualified = prefixed_list("b", M::FTS_COLUMNS);
self.conn().execute(&format!("INSERT INTO {fts}(rowid, day_key, {cols}) SELECT k.day_rowid, b.{key}, {qualified} FROM {t} b JOIN {fts_keys} k ON k.day_key = b.{key}"), &[])?;
}
} else {
self.conn().execute(
&format!(
"CREATE VIRTUAL TABLE IF NOT EXISTS {fts} USING fts5({cols}, \
content='{}', content_rowid='{}'{tokenize})",
bare.replace('\'', "''"),
M::KEY.replace('\'', "''")
),
&[],
)?;
let old_cols = prefixed_list("old", M::FTS_COLUMNS);
self.ensure_trigger(
&format!("{bare}_fts_ai"),
name.schema,
&format!(
"CREATE TRIGGER {} AFTER INSERT ON {t} BEGIN \
INSERT INTO {fts}(rowid, {cols}) VALUES (new.{key}, {new_cols}); END",
trigger("_fts_ai")
),
)?;
self.ensure_trigger(
&format!("{bare}_fts_ad"),
name.schema,
&format!(
"CREATE TRIGGER {} AFTER DELETE ON {t} BEGIN \
INSERT INTO {fts}({fts_command}, rowid, {cols}) VALUES ('delete', old.{key}, {old_cols}); END",
trigger("_fts_ad")
),
)?;
let (update_of, changed) = fts_update_guard(M::KEY, M::FTS_COLUMNS);
self.ensure_trigger(
&format!("{bare}_fts_au"),
name.schema,
&format!(
"CREATE TRIGGER {} AFTER UPDATE OF {update_of} ON {t} WHEN {changed} BEGIN \
INSERT INTO {fts}({fts_command}, rowid, {cols}) VALUES ('delete', old.{key}, {old_cols}); \
INSERT INTO {fts}(rowid, {cols}) VALUES (new.{key}, {new_cols}); END",
trigger("_fts_au")
),
)?;
if fresh {
self.conn().execute(
&format!("INSERT INTO {fts}({fts_command}) VALUES ('rebuild')"),
&[],
)?;
}
}
}
if let Some(s) = M::SPATIAL {
if !self.inner.caps.rtree {
return Err(DbError::new(
DbErrorKind::Unsupported,
format!(
"`{bare}` declares spatial(…) but this driver's engine has no R*Tree — \
the bundled and cipher engines compile it in"
),
));
}
let (lat, lon) = (ident(s.lat), ident(s.lon));
let geo = name.shadow("_geo");
let fresh = !self.table_exists(&format!("{bare}_geo"))?;
self.conn().execute(
&format!(
"CREATE VIRTUAL TABLE IF NOT EXISTS {geo} USING rtree({key}, \
min_lat, max_lat, min_lon, max_lon)"
),
&[],
)?;
self.ensure_trigger(
&format!("{bare}_geo_ai"),
name.schema,
&format!(
"CREATE TRIGGER {} AFTER INSERT ON {t} BEGIN \
INSERT OR REPLACE INTO {geo} VALUES (new.{key}, new.{lat}, new.{lat}, new.{lon}, new.{lon}); END",
trigger("_geo_ai")
),
)?;
self.ensure_trigger(
&format!("{bare}_geo_au"),
name.schema,
&format!(
"CREATE TRIGGER {} AFTER UPDATE OF {lat}, {lon} ON {t} BEGIN \
INSERT OR REPLACE INTO {geo} VALUES (new.{key}, new.{lat}, new.{lat}, new.{lon}, new.{lon}); END",
trigger("_geo_au")
),
)?;
self.ensure_trigger(
&format!("{bare}_geo_ad"),
name.schema,
&format!(
"CREATE TRIGGER {} AFTER DELETE ON {t} BEGIN \
DELETE FROM {geo} WHERE {key} = old.{key}; END",
trigger("_geo_ad")
),
)?;
if fresh {
self.conn().execute(
&format!(
"INSERT OR REPLACE INTO {geo} \
SELECT {key}, {lat}, {lat}, {lon}, {lon} FROM {t}"
),
&[],
)?;
}
}
Ok(())
}
fn ensure_trigger(
&self,
bare_name: &str,
schema: Option<&str>,
create_sql: &str,
) -> Result<(), DbError> {
let master = table_sql(schema, "sqlite_master");
let mut stored: Option<String> = None;
self.conn().query(
&format!("SELECT sql FROM {master} WHERE type = 'trigger' AND name = ?"),
&[Value::Text(bare_name.into())],
&mut |row| {
if let Ok(sql) = row.get(0).as_text() {
stored = Some(sql.to_string());
}
},
)?;
match stored {
Some(sql) if sql == create_sql => return Ok(()),
Some(_) => {
self.conn().execute(
&format!("DROP TRIGGER IF EXISTS {}", table_sql(schema, bare_name)),
&[],
)?;
}
None => {}
}
self.conn().execute(create_sql, &[])?;
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_named<M: Model>(&self, table_sql: String) -> Result<(), DbError> {
let cols: Vec<String> = self
.declared_columns::<M>()
.iter()
.map(|d| {
let c = &d.def;
let mut s = format!("{} {}", ident(c.name), c.sql.ddl());
if d.pk {
s.push_str(" PRIMARY KEY");
} else if c.not_null {
s.push_str(" NOT NULL");
}
if c.unique && !d.pk {
s.push_str(" UNIQUE");
}
if let Some(fk) = &d.fk {
s.push_str(&fk.clause());
}
s
})
.collect();
let body = format!("CREATE TABLE {table_sql} ({})", cols.join(", "));
if self.conn().execute(&format!("{body} STRICT"), &[]).is_err() {
self.conn().execute(&body, &[])?;
}
Ok(())
}
fn declared_indexes<M: Model>(&self, unique_indexes: &[&'static str]) -> Vec<DeclaredIndex> {
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();
let bare = TableName::of::<M>().bare;
let mut out: Vec<DeclaredIndex> = M::COLUMNS
.iter()
.filter(|c| c.indexed || (fk_cols.contains(&c.name) && c.name != M::KEY))
.map(|c| DeclaredIndex {
name: format!("day_idx_{bare}_{}", c.name),
columns: vec![c.name.to_string()],
unique: false,
})
.collect();
out.extend(M::COMPOSITE_INDEXES.iter().map(|idx| DeclaredIndex {
name: format!("day_idx_{bare}_{}", idx.join("_")),
columns: idx.iter().map(|c| c.to_string()).collect(),
unique: false,
}));
out.extend(unique_indexes.iter().map(|c| DeclaredIndex {
name: format!("day_uidx_{bare}_{c}"),
columns: vec![c.to_string()],
unique: true,
}));
out.extend(M::UNIQUE_INDEXES.iter().map(|idx| DeclaredIndex {
name: format!("day_uidx_{bare}_{}", idx.join("_")),
columns: idx.iter().map(|c| c.to_string()).collect(),
unique: true,
}));
out
}
fn create_indexes<M: Model>(&self, unique_indexes: &[&'static str]) -> Result<(), DbError> {
let name = TableName::of::<M>();
let t = name.sql();
for idx in self.declared_indexes::<M>(unique_indexes) {
self.conn().execute(
&format!(
"CREATE {}INDEX IF NOT EXISTS {} ON {t}({})",
if idx.unique { "UNIQUE " } else { "" },
table_sql(name.schema, &idx.name),
column_list(&idx.columns)
),
&[],
)?;
}
Ok(())
}
fn declared_columns<M: Model>(&self) -> Vec<DeclaredColumn> {
let specs = self.inner.fk_specs.borrow();
M::COLUMNS
.iter()
.enumerate()
.map(|(index, c)| DeclaredColumn {
def: *c,
index,
pk: c.name == M::KEY,
fk: specs
.iter()
.find(|f| f.child_table == M::TABLE && f.child_field == c.field)
.map(|s| DeclaredFk {
parent_sql: s.parent_sql.clone(),
parent_bare: s.parent_bare,
parent_key: s.parent_key,
rule: s.rule,
}),
})
.collect()
}
fn file_schema(&self, name: TableName) -> Result<FileSchema, DbError> {
let mut columns: Vec<FileColumn> = Vec::new();
self.conn().query(
&pragma_sql(name.schema, "table_info", name.bare),
&[],
&mut |row| {
columns.push(FileColumn {
name: row.get(1).as_text().unwrap_or("").to_string(),
decl_type: row.get(2).as_text().unwrap_or("").to_ascii_uppercase(),
not_null: row.get(3).as_int().unwrap_or(0) != 0,
pk: row.get(5).as_int().unwrap_or(0) != 0,
fk: None,
});
},
)?;
self.conn().query(
&pragma_sql(name.schema, "foreign_key_list", name.bare),
&[],
&mut |row| {
let from = row.get(3).as_text().unwrap_or("").to_string();
if let Some(col) = columns.iter_mut().find(|c| c.name == from) {
col.fk = Some(FileFk {
parent: row.get(2).as_text().unwrap_or("").to_string(),
parent_key: row.get(4).as_text().unwrap_or("").to_string(),
on_delete: row.get(6).as_text().unwrap_or("").to_ascii_uppercase(),
});
}
},
)?;
let mut indexes: Vec<FileIndex> = Vec::new();
self.conn().query(
&pragma_sql(name.schema, "index_list", name.bare),
&[],
&mut |row| {
indexes.push(FileIndex {
name: row.get(1).as_text().unwrap_or("").to_string(),
unique: row.get(2).as_int().unwrap_or(0) != 0,
origin: row.get(3).as_text().unwrap_or("").to_string(),
columns: Vec::new(),
});
},
)?;
for idx in &mut indexes {
let mut cols: Vec<(i64, String)> = Vec::new();
self.conn().query(
&pragma_sql(name.schema, "index_info", &idx.name),
&[],
&mut |row| {
cols.push((
row.get(0).as_int().unwrap_or(0),
row.get(2).as_text().unwrap_or("").to_string(),
));
},
)?;
cols.sort_by_key(|(seq, _)| *seq);
idx.columns = cols.into_iter().map(|(_, c)| c).collect();
}
Ok(FileSchema { columns, indexes })
}
fn compare_schema<M: Model>(&self, file: &FileSchema) -> Result<SchemaDelta, DbError> {
let declared = self.declared_columns::<M>();
let defaults = M::default_row();
let bare = TableName::of::<M>().bare;
let refuse = |column: &str, what: String| {
Err(DbError::new(
DbErrorKind::Schema,
format!(
"`{}`.`{column}`: {what}; this is not a change the container can make to \
an existing table, and it refuses to open rather than guess (a staged \
migration can make it)",
M::TABLE
),
))
};
let mut plan = LightweightPlan::default();
let mut rebuild = false;
let mut note = |_why: String| rebuild = true;
let inline_unique = |col: &str| {
file.indexes
.iter()
.any(|i| i.origin == "u" && i.columns.len() == 1 && i.columns[0] == col)
};
let unique_index_name = |col: &str| format!("day_uidx_{bare}_{col}");
let has_unique_index = |col: &str| {
let name = unique_index_name(col);
file.indexes
.iter()
.any(|i| i.name == name && i.unique && i.columns.len() == 1 && i.columns[0] == col)
};
for d in &declared {
let c = &d.def;
let default = Row::get(&defaults, d.index);
let required = c.not_null || d.pk;
match file.columns.iter().find(|f| f.name == c.name) {
None => {
if d.pk {
return refuse(c.name, "the declared key column is not in the file".into());
}
if required && default == Value::Null {
return refuse(
c.name,
"a required column whose Rust default stores NULL cannot be \
added to existing rows"
.into(),
);
}
if d.fk.is_some() && required {
note(format!("required foreign-key column `{}` added", c.name));
} else {
plan.add.push(d.index);
if c.unique {
plan.unique_indexes.push(c.name);
}
}
}
Some(f) => {
if f.decl_type != c.sql.ddl() {
if d.pk {
return refuse(
c.name,
format!(
"the key column is {} in the file and {} in the declaration",
f.decl_type,
c.sql.ddl()
),
);
}
note(format!(
"`{}` is {} in the file and {} in the declaration",
c.name,
f.decl_type,
c.sql.ddl()
));
}
if f.pk != d.pk {
return refuse(
c.name,
if d.pk {
"declared as the key but not the file's primary key".into()
} else {
"the file's primary key but not the declared key".into()
},
);
}
if !d.pk && f.not_null != c.not_null {
if c.not_null && default == Value::Null {
return refuse(
c.name,
"becomes required, but its Rust default stores NULL, so \
existing NULLs cannot be filled"
.into(),
);
}
note(format!(
"`{}` is {} in the file and {} in the declaration",
c.name,
if f.not_null { "NOT NULL" } else { "nullable" },
if c.not_null { "NOT NULL" } else { "nullable" }
));
}
let want_unique = c.unique && !d.pk;
match (want_unique, inline_unique(c.name), has_unique_index(c.name)) {
(true, false, _) => plan.unique_indexes.push(c.name),
(false, true, _) => note(format!(
"`{}` carries UNIQUE but is not declared unique",
c.name
)),
(false, false, true) => plan.drop_indexes.push(unique_index_name(c.name)),
_ => {}
}
let fk_matches = match (&d.fk, &f.fk) {
(None, None) => true,
(Some(want), Some(have)) => {
have.parent == want.parent_bare
&& have.parent_key == want.parent_key
&& have.on_delete == want.rule.sql()
}
_ => false,
};
if !fk_matches {
note(format!("`{}`'s foreign key differs", c.name));
}
}
}
}
for f in file
.columns
.iter()
.filter(|f| !declared.iter().any(|d| d.def.name == f.name))
{
if f.pk {
return refuse(&f.name, "the file's primary key is not declared".into());
}
let covered_by_foreign_index = file.indexes.iter().any(|i| {
i.origin == "c" && !is_day_index(&i.name, bare) && i.columns.contains(&f.name)
});
if f.fk.is_some() || inline_unique(&f.name) || covered_by_foreign_index {
note(format!("constrained column `{}` dropped", f.name));
} else {
plan.drop.push(f.name.clone());
}
}
if let Some(extra) = file
.indexes
.iter()
.find(|i| i.origin == "u" && i.columns.len() > 1)
{
note(format!(
"undeclared multi-column UNIQUE constraint over ({})",
extra.columns.join(", ")
));
}
let want = self.declared_indexes::<M>(&plan.unique_indexes);
for i in file
.indexes
.iter()
.filter(|i| i.origin == "c" && is_day_index(&i.name, bare))
{
let matches = want
.iter()
.any(|w| w.name == i.name && w.columns == i.columns && w.unique == i.unique);
if !matches && !plan.drop_indexes.contains(&i.name) {
plan.drop_indexes.push(i.name.clone());
}
}
Ok(if rebuild {
SchemaDelta::Rebuild
} else {
SchemaDelta::Lightweight(plan)
})
}
fn lightweight_migrate<M: Model>(&self, plan: &LightweightPlan) -> Result<(), DbError> {
let name = TableName::of::<M>();
let t = name.sql();
let declared = self.declared_columns::<M>();
let defaults = M::default_row();
for idx in &plan.drop_indexes {
self.conn().execute(
&format!("DROP INDEX IF EXISTS {}", table_sql(name.schema, idx)),
&[],
)?;
}
for &i in &plan.add {
let d = &declared[i];
let c = &d.def;
let default = Row::get(&defaults, i);
let mut ddl = format!(
"ALTER TABLE {t} ADD COLUMN {} {}",
ident(c.name),
c.sql.ddl()
);
if c.not_null {
ddl.push_str(&format!(" NOT NULL DEFAULT {}", sql_literal(&default)?));
}
if let Some(fk) = &d.fk {
ddl.push_str(&fk.clause());
}
self.conn().execute(&ddl, &[])?;
if !c.not_null && default != Value::Null {
self.conn()
.execute(&format!("UPDATE {t} SET {} = ?", ident(c.name)), &[default])?;
}
}
for gone in &plan.drop {
self.conn()
.execute(&format!("ALTER TABLE {t} DROP COLUMN {}", ident(gone)), &[])?;
}
Ok(())
}
fn rebuild_table<M: Model>(&self, file: &FileSchema) -> Result<(), DbError> {
let name = TableName::of::<M>();
let t = name.sql();
let bare = name.bare;
let tmp_bare = format!("{bare}_new");
let tmp = table_sql(name.schema, &tmp_bare);
let declared = self.declared_columns::<M>();
let defaults = M::default_row();
let master = table_sql(name.schema, "sqlite_master");
let mut foreign_indexes: Vec<String> = Vec::new();
self.conn().query(
&format!(
"SELECT name, sql FROM {master} WHERE type = 'index' AND tbl_name = ? \
AND sql IS NOT NULL"
),
&[Value::Text(bare.into())],
&mut |row| {
let idx_name = row.get(0).as_text().unwrap_or("").to_string();
if is_day_index(&idx_name, bare) {
return;
}
let survives = file
.indexes
.iter()
.find(|i| i.name == idx_name)
.is_some_and(|i| {
i.columns
.iter()
.all(|c| declared.iter().any(|d| d.def.name == c))
});
if survives && let Ok(sql) = row.get(1).as_text() {
foreign_indexes.push(sql.to_string());
}
},
)?;
let mut triggers: Vec<String> = Vec::new();
self.conn().query(
&format!(
"SELECT sql FROM {master} WHERE type = 'trigger' AND tbl_name = ? \
AND sql IS NOT NULL ORDER BY rowid"
),
&[Value::Text(bare.into())],
&mut |row| {
if let Ok(sql) = row.get(0).as_text() {
triggers.push(sql.to_string());
}
},
)?;
let wrap =
|e: DbError| DbError::new(e.kind, format!("rebuilding `{}`: {}", M::TABLE, e.message));
self.conn()
.execute(&format!("DROP TABLE IF EXISTS {tmp}"), &[])
.map_err(wrap)?;
self.create_table_named::<M>(tmp.clone()).map_err(wrap)?;
let targets = column_list(&declared.iter().map(|d| d.def.name).collect::<Vec<_>>());
let mut sources: Vec<String> = Vec::with_capacity(declared.len());
for d in &declared {
let c = &d.def;
let default = Row::get(&defaults, d.index);
let required = c.not_null || d.pk;
let shared = file.columns.iter().find(|f| f.name == c.name);
sources.push(match shared {
Some(f) if !required || f.not_null => ident(c.name),
Some(_) => format!(
"COALESCE({}, {})",
ident(c.name),
sql_literal(&default).map_err(wrap)?
),
None => sql_literal(&default).map_err(wrap)?,
});
}
self.conn()
.execute(
&format!(
"INSERT INTO {tmp} ({targets}) SELECT {} FROM {t}",
sources.join(", ")
),
&[],
)
.map_err(wrap)?;
self.conn()
.execute(&format!("DROP TABLE {t}"), &[])
.map_err(wrap)?;
self.conn()
.execute_batch("PRAGMA legacy_alter_table = ON")
.map_err(wrap)?;
let renamed = self
.conn()
.execute(&format!("ALTER TABLE {tmp} RENAME TO {}", ident(bare)), &[]);
self.conn()
.execute_batch("PRAGMA legacy_alter_table = OFF")
.map_err(wrap)?;
renamed.map_err(wrap)?;
for sql in foreign_indexes {
self.conn().execute(&sql, &[]).map_err(wrap)?;
}
for sql in triggers {
let _ = self.conn().execute(&sql, &[]);
}
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 capture = self.inner.participant.borrow().is_some();
let (stmts, changes) = self.fold(dirty, capture)?;
if stmts.is_empty() {
return Ok(stmts);
}
let mut conn = self.conn();
let outer = self.inner.worker_transaction.get();
if !outer {
conn.begin()?;
if let Err(error) = self.participant_begin(conn.as_mut(), WriteOrigin::Local) {
let _ = conn.rollback();
return Err(error);
}
}
self.record_changes(changes);
for (sql, params) in &stmts {
if let Err(e) = conn.execute(sql, params) {
if !outer {
let _ = conn.rollback();
self.participant_rollback();
}
return Err(e);
}
}
if !outer
&& let Err(error) = self
.participant_commit(conn.as_mut(), WriteOrigin::Local)
.and_then(|_| conn.commit())
{
let _ = conn.rollback();
self.participant_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)();
}
self.inner.participant.borrow_mut().take();
}
#[cfg(not(target_arch = "wasm32"))]
fn worker_write<R>(
&self,
write: impl FnOnce(&Self) -> Result<R, DbError>,
) -> Result<R, DbError> {
self.worker_write_origin(WriteOrigin::Local, write)
}
#[cfg(not(target_arch = "wasm32"))]
fn worker_write_origin<R>(
&self,
origin: WriteOrigin,
write: impl FnOnce(&Self) -> Result<R, DbError>,
) -> Result<R, DbError> {
self.save()?;
self.conn().begin()?;
let admission = self.participant_begin(self.conn().as_mut(), origin);
if let Err(error) = admission {
self.conn().rollback().map_err(|rollback| {
DbError::new(
DbErrorKind::Closed,
format!("transaction admission rollback failed: {rollback}"),
)
})?;
return Err(error);
}
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.participant_commit(self.conn().as_mut(), origin)?;
self.conn().commit()?;
Ok(value)
});
self.inner.worker_transaction.set(false);
if result.is_err() {
self.participant_rollback();
self.conn().rollback().map_err(|error| {
DbError::new(
DbErrorKind::Closed,
format!("transaction rollback failed: {error}"),
)
})?;
self.worker_discard_changes()?;
}
if result.is_ok() {
self.participant_refresh()?;
}
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();
self.participant_rollback();
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}"),
)
})
}
#[allow(clippy::type_complexity)]
fn fold(
&self,
dirty: &DirtyState,
capture: bool,
) -> Result<(Vec<(String, Vec<Value>)>, Vec<RowChange>), DbError> {
let tables = self.inner.tables.borrow();
let mut stmts: Vec<(String, Vec<Value>)> = Vec::new();
let mut changes: Vec<RowChange> = Vec::new();
let authors_of = |store_id: u64, key: u64| -> Vec<&'static str> {
dirty
.authors
.get(&(store_id, key))
.cloned()
.unwrap_or_default()
};
for store_id in &dirty.full {
let Some(hooks) = tables.get(store_id) else {
continue;
};
for (key, row) in (hooks.all_rows)() {
stmts.push(upsert_stmt(hooks, &row));
if capture {
let mut authors = dirty
.store_authors
.get(store_id)
.cloned()
.unwrap_or_default();
for tag in authors_of(*store_id, key) {
add_author(&mut authors, Some(tag));
}
changes.push(RowChange {
table: hooks.table,
key: (hooks.key_where)(key).1,
kind: RowChangeKind::Rewritten,
authors,
});
}
}
}
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();
let mut open_inserts: HashMap<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;
let mut capture_as = |kind: RowChangeKind| {
if capture {
changes.push(RowChange {
table: hooks.table,
key: (hooks.key_where)(key).1,
kind,
authors: authors_of(store_id, key),
});
}
};
match state {
DirtyRow::Delete => {
capture_as(RowChangeKind::Deleted);
if !batchable {
let (clause, params) = (hooks.key_where)(key);
deletes.push(Batch::Single(
format!("DELETE FROM {} WHERE {clause}", hooks.name.sql()),
params,
));
continue;
}
join_batch(&mut deletes, &mut open_deletes, store_id, None, key);
}
DirtyRow::Insert => {
if let Some(row) = (hooks.row_for)(key) {
capture_as(RowChangeKind::Inserted);
match open_inserts.get(&store_id) {
Some(&i) => {
if let Batch::Inserts { rows, .. } = &mut batches[i] {
rows.push(row);
}
}
None => {
open_inserts.insert(store_id, batches.len());
batches.push(Batch::Inserts {
store: store_id,
rows: vec![row],
});
}
}
}
}
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));
capture_as(RowChangeKind::Rewritten);
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; };
let value = row.get(i);
if hooks.lazy.get(i).copied().unwrap_or(false) && value == Value::Null {
continue; }
sets.push(format!("{} = ?", ident(&hooks.columns[i])));
set_params.push(value);
}
if set_params.is_empty() {
continue; }
if capture {
let written: Vec<&'static str> = cols
.iter()
.filter_map(|c| hooks.fields.iter().position(|n| n == c))
.map(|i| hooks.column_names[i])
.collect();
capture_as(RowChangeKind::Updated(written));
}
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.name.sql()
),
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::Inserts { store, rows } => {
let Some(hooks) = tables.get(&store) else {
continue;
};
insert_stmts(hooks, rows, &mut stmts);
}
Batch::Keyed { store, set, keys } => {
let Some(hooks) = tables.get(&store) else {
continue;
};
materialize(hooks, set, &keys, &mut stmts);
}
}
}
Ok((stmts, changes))
}
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());
}
})?;
for v in self.foreign_key_check()? {
findings.push(v.to_string());
}
Ok(findings)
}
pub fn foreign_key_check(&self) -> Result<Vec<ForeignKeyViolation>, DbError> {
Self::foreign_key_check_on(self.conn().as_mut())
}
fn foreign_key_check_on(
conn: &mut dyn SqliteConnection,
) -> Result<Vec<ForeignKeyViolation>, DbError> {
let mut violations = Vec::new();
conn.query("PRAGMA foreign_key_check", &[], &mut |row| {
violations.push(ForeignKeyViolation {
table: row.get(0).as_text().unwrap_or("").to_string(),
rowid: row.get(1).as_int().ok(),
parent: row.get(2).as_text().unwrap_or("").to_string(),
constraint: row.get(3).as_int().unwrap_or(0),
});
})?;
Ok(violations)
}
pub fn checkpoint(&self) -> Result<Checkpoint, DbError> {
let mut result = Checkpoint::default();
self.conn()
.query("PRAGMA wal_checkpoint(TRUNCATE)", &[], &mut |row| {
result = Checkpoint {
busy: row.get(0).as_int().unwrap_or(0) != 0,
log_frames: row.get(1).as_int().unwrap_or(-1),
checkpointed_frames: row.get(2).as_int().unwrap_or(-1),
};
})?;
Ok(result)
}
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)
}
}
pub(crate) fn column_list<S: AsRef<str>>(columns: &[S]) -> String {
columns
.iter()
.map(|c| ident(c.as_ref()))
.collect::<Vec<_>>()
.join(", ")
}
pub(crate) fn prefixed_list<S: AsRef<str>>(prefix: &str, columns: &[S]) -> String {
columns
.iter()
.map(|c| format!("{prefix}.{}", ident(c.as_ref())))
.collect::<Vec<_>>()
.join(", ")
}
fn fts_update_guard(key: &str, fts_columns: &[&str]) -> (String, String) {
let mut cols: Vec<&str> = vec![key];
cols.extend(fts_columns.iter().copied().filter(|c| *c != key));
let update_of = column_list(&cols);
let changed = cols
.iter()
.map(|c| {
let c = ident(c);
format!("new.{c} IS NOT old.{c}")
})
.collect::<Vec<_>>()
.join(" OR ");
(update_of, changed)
}
struct DeclaredColumn {
def: ColumnDef,
index: usize,
pk: bool,
fk: Option<DeclaredFk>,
}
struct DeclaredFk {
parent_sql: String,
parent_bare: &'static str,
parent_key: &'static str,
rule: DeleteRule,
}
impl DeclaredFk {
fn clause(&self) -> String {
format!(
" REFERENCES {}({}) ON DELETE {} DEFERRABLE INITIALLY DEFERRED",
self.parent_sql,
ident(self.parent_key),
self.rule.sql()
)
}
}
struct DeclaredIndex {
name: String,
columns: Vec<String>,
unique: bool,
}
struct FileColumn {
name: String,
decl_type: String,
not_null: bool,
pk: bool,
fk: Option<FileFk>,
}
struct FileFk {
parent: String,
parent_key: String,
on_delete: String,
}
struct FileIndex {
name: String,
unique: bool,
origin: String,
columns: Vec<String>,
}
struct FileSchema {
columns: Vec<FileColumn>,
indexes: Vec<FileIndex>,
}
#[derive(Default, Clone)]
struct LightweightPlan {
add: Vec<usize>,
drop: Vec<String>,
drop_indexes: Vec<String>,
unique_indexes: Vec<&'static str>,
}
enum SchemaDelta {
Lightweight(LightweightPlan),
Rebuild,
}
fn is_day_index(name: &str, table: &str) -> bool {
name.starts_with(&format!("day_idx_{table}_"))
|| name.starts_with(&format!("day_uidx_{table}_"))
}
fn pragma_sql(schema: Option<&str>, name: &str, arg: &str) -> String {
match schema {
Some(s) => format!("PRAGMA {}.{name}({})", ident(s), ident(arg)),
None => format!("PRAGMA {name}({})", ident(arg)),
}
}
fn sql_literal(v: &Value) -> Result<String, DbError> {
Ok(match v {
Value::Null => "NULL".into(),
Value::Int(i) => i.to_string(),
Value::Real(r) if r.is_finite() => format!("{r:?}"),
Value::Real(r) => {
return Err(DbError::new(
DbErrorKind::Schema,
format!("{r} has no SQL literal to use as a column default"),
));
}
Value::Text(t) => format!("'{}'", t.replace('\'', "''")),
Value::Blob(b) => {
let mut s = String::with_capacity(b.len() * 2 + 3);
s.push_str("X'");
for byte in b {
s.push_str(&format!("{byte:02x}"));
}
s.push('\'');
s
}
})
}
fn keys_where_of(hooks: &TableHooks) -> KeysWhere {
if hooks.key_cols.len() == 1 {
let col = ident(&hooks.key_cols[0]);
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>),
Inserts { store: u64, rows: Vec<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>)>,
) {
let table = hooks.name.sql();
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 {table} SET {clause} WHERE {where_clause}"),
params,
));
}
None => out.push((
format!("DELETE FROM {table} WHERE {where_clause}"),
where_params,
)),
}
return;
}
let key_col = ident(&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 {table} SET {clause} WHERE {key_col} IN ({marks})")
}
None => format!("DELETE FROM {table} WHERE {key_col} IN ({marks})"),
};
out.push((sql, params));
}
}
fn select_list(hooks: &TableHooks) -> String {
hooks
.columns
.iter()
.enumerate()
.map(|(i, c)| {
if hooks.lazy.get(i).copied().unwrap_or(false) {
"NULL".to_string()
} else {
ident(c)
}
})
.collect::<Vec<_>>()
.join(", ")
}
fn insert_stmts(hooks: &TableHooks, rows: Vec<Vec<Value>>, out: &mut Vec<(String, Vec<Value>)>) {
let per_row = hooks.columns.len().max(1);
let per_stmt = (MAX_BOUND_PARAMS / per_row).max(1);
for chunk in rows.chunks(per_stmt) {
let sql = upsert_sql(hooks, chunk.len());
let params: Vec<Value> = chunk.iter().flatten().cloned().collect();
out.push((sql, params));
}
}
fn upsert_stmt(hooks: &TableHooks, row: &[Value]) -> (String, Vec<Value>) {
(upsert_sql(hooks, 1), row.to_vec())
}
fn upsert_sql(hooks: &TableHooks, rows: usize) -> String {
let cols = column_list(&hooks.columns);
let one = format!("({})", vec!["?"; hooks.columns.len()].join(", "));
let marks = vec![one.as_str(); rows.max(1)].join(", ");
let sets = hooks
.columns
.iter()
.enumerate()
.filter(|(_, c)| !hooks.key_cols.contains(c))
.map(|(i, c)| {
if hooks.lazy.get(i).copied().unwrap_or(false) {
format!("{0} = COALESCE(excluded.{0}, {0})", ident(c))
} else {
format!("{0} = excluded.{0}", ident(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.name.sql(),
column_list(&hooks.key_cols)
)
}
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.inner.blob_memo.borrow_mut().clear();
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 {}", ident(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()?;
}
self.inner.blob_memo.borrow_mut().clear();
type Doomed = Vec<(Rc<dyn Fn() -> Vec<u64>>, EvictFn)>;
let doomed: Doomed = self
.inner
.tables
.borrow()
.values()
.filter(|h| h.name.schema == Some(alias))
.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 {}", ident(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> {
self.refresh_resident_in(None)
}
fn refresh_resident_in(&self, only: Option<&[&'static str]>) -> Result<bool, DbError> {
struct RefreshJob {
store: u64,
resident_keys: Rc<dyn Fn() -> Vec<u64>>,
refresh: RefreshFn,
columns: String,
table: String,
keys_where: KeysWhere,
}
let jobs: Vec<RefreshJob> = {
let tables = self.inner.tables.borrow();
tables
.iter()
.filter(|(_, h)| only.is_none_or(|t| t.contains(&h.table)))
.map(|(store, h)| RefreshJob {
store: *store,
resident_keys: h.resident_keys.clone(),
refresh: h.refresh.clone(),
columns: select_list(h),
table: h.name.sql(),
keys_where: keys_where_of(h),
})
.collect()
};
let mut changed = false;
for RefreshJob {
store,
resident_keys,
refresh,
columns,
table,
keys_where,
} in jobs
{
self.inner.blob_memo.borrow_mut().forget_store(store);
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_tables(&self, tables: &[&'static str]) {
let stores: Vec<u64> = self
.inner
.tables
.borrow()
.iter()
.filter(|(_, h)| tables.contains(&h.table))
.map(|(id, _)| *id)
.collect();
let states: Vec<Rc<QueryState>> = self
.inner
.queries
.borrow()
.iter()
.filter_map(|w| w.upgrade())
.collect();
for state in states {
let reads = match &state.raw {
Some(raw) => raw.tables.iter().any(|t| tables.contains(&t.as_str())),
None => {
stores.contains(&state.store_id)
|| state.watches.borrow().iter().any(|w| {
stores.contains(&w.store)
|| w.join_store.is_some_and(|j| stores.contains(&j))
})
}
};
if reads {
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 {
let tables = self.inner.tables.borrow();
let Some(alias) = tables
.values()
.find(|h| h.table == table)
.and_then(|h| h.name.schema)
else {
return false;
};
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(),
sql: h.name.sql(),
key: h.key_cols[0].clone(),
fts: h.fts.as_ref().map(|_| FtsSql {
from: h.name.shadow("_fts"),
match_col: ident(&format!("{}_fts", h.name.bare)),
}),
fts_key: h.fts_key,
spatial: h
.spatial
.as_ref()
.map(|(lat, lon, _)| (lat.clone(), lon.clone(), h.name.shadow("_geo"))),
})
.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,
sql: String,
key: String,
fts: Option<FtsSql>,
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 table_sql(&self, table: &str) -> String {
self.tables
.iter()
.find(|t| t.table == table)
.map(|t| t.sql.clone())
.unwrap_or_else(|| ident(table))
}
fn fts_of(&self, table: &str) -> Option<FtsSql> {
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 quoted_name(rest).as_deref() == Some(table);
}
}
false
}
fn quoted_name(sql: &str) -> Option<String> {
let mut chars = sql.strip_prefix('"')?.chars();
let mut name = String::new();
loop {
match chars.next()? {
'"' => match chars.next() {
Some('"') => name.push('"'),
_ => return Some(name),
},
ch => name.push(ch),
}
}
}
#[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;