cartel-sqlite 0.27.2

SQLite driver for the dope runtime
Documentation
use crate::options::{Checkpoint, Journal, OpenOptions};
use crate::{api, database, open::journal};
use api::work;
use work::call::Call;

impl OpenOptions {
    pub(crate) fn set_journal(&self, call: &Call<'_, '_, '_>) -> Result<(), crate::Error> {
        let (sql, expected) = match self.journal {
            Journal::Rollback => ("PRAGMA journal_mode=DELETE", journal::Mode::Delete),
            Journal::Wal(_) => ("PRAGMA journal_mode=WAL", journal::Mode::Wal),
        };
        let actual = call.journal(sql)?;
        if actual != expected {
            return Err(crate::Error::Journal { expected, actual });
        }
        Ok(())
    }

    pub(crate) fn retain(&self, call: &Call<'_, '_, '_>) -> Result<(), crate::Error> {
        if let Journal::Wal(wal) = self.journal
            && let Some(value) = wal.retained_bytes
        {
            if value > i64::MAX as u64 {
                return Err(crate::Error::JournalBytes { value });
            }
            let bytes = value as i64;
            call.pragma("journal_size_limit", bytes)?;
        }
        Ok(())
    }

    pub(crate) fn set_mapped(
        &self,
        call: &Call<'_, '_, '_>,
        kind: database::Kind,
    ) -> Result<(), crate::Error> {
        call.pragma("mmap_size", self.mapped(kind)?)
    }

    pub(crate) fn sync(&self, call: &Call<'_, '_, '_>) -> Result<(), crate::Error> {
        call.pragma("synchronous", i64::from(self.synchronous.value()))
    }

    pub(crate) fn checkpoint(&self, call: &Call<'_, '_, '_>) -> Result<(), crate::Error> {
        if let Journal::Wal(wal) = self.journal {
            let pages = match wal.checkpoint {
                Checkpoint::Auto => 1_000,
                Checkpoint::Pages(pages) => pages.get(),
                Checkpoint::Manual => 0,
            };
            if pages > i32::MAX as u32 {
                return Err(crate::Error::CheckpointPages { value: pages });
            }
            call.inner().autocheckpoint(pages as i32);
        }
        Ok(())
    }

    pub(crate) fn mapped(&self, kind: database::Kind) -> Result<i64, crate::Error> {
        match kind {
            database::Kind::Memory => Ok(0),
            database::Kind::File => {
                let value = self.mapped_bytes;
                if value > i64::MAX as u64 {
                    return Err(crate::Error::MappedBytes { value });
                }
                Ok(value as i64)
            }
        }
    }
}