use std::path::Path;
mod auto_vacuum;
mod connect;
mod journal_mode;
mod locking_mode;
mod parse;
mod synchronous;
use crate::connection::LogSettings;
pub use auto_vacuum::SqliteAutoVacuum;
pub use journal_mode::SqliteJournalMode;
pub use locking_mode::SqliteLockingMode;
use std::cmp::Ordering;
use std::sync::Arc;
use std::{borrow::Cow, time::Duration};
pub use synchronous::SqliteSynchronous;
use crate::common::DebugFn;
use crate::sqlite::connection::collation::Collation;
use indexmap::IndexMap;
#[derive(Clone, Debug)]
pub struct SqliteConnectOptions {
pub(crate) filename: Cow<'static, Path>,
pub(crate) in_memory: bool,
pub(crate) read_only: bool,
pub(crate) create_if_missing: bool,
pub(crate) shared_cache: bool,
pub(crate) statement_cache_capacity: usize,
pub(crate) busy_timeout: Duration,
pub(crate) log_settings: LogSettings,
pub(crate) immutable: bool,
pub(crate) pragmas: IndexMap<Cow<'static, str>, Cow<'static, str>>,
pub(crate) command_channel_size: usize,
pub(crate) row_channel_size: usize,
pub(crate) collations: Vec<Collation>,
pub(crate) serialized: bool,
pub(crate) thread_name: Arc<DebugFn<dyn Fn(u64) -> String + Send + Sync + 'static>>,
}
impl Default for SqliteConnectOptions {
fn default() -> Self {
Self::new()
}
}
impl SqliteConnectOptions {
pub fn new() -> Self {
let mut pragmas: IndexMap<Cow<'static, str>, Cow<'static, str>> = IndexMap::new();
let locking_mode: SqliteLockingMode = Default::default();
let auto_vacuum: SqliteAutoVacuum = Default::default();
pragmas.insert("page_size".into(), "4096".into());
pragmas.insert("locking_mode".into(), locking_mode.as_str().into());
pragmas.insert(
"journal_mode".into(),
SqliteJournalMode::Wal.as_str().into(),
);
pragmas.insert("foreign_keys".into(), "ON".into());
pragmas.insert(
"synchronous".into(),
SqliteSynchronous::Full.as_str().into(),
);
pragmas.insert("auto_vacuum".into(), auto_vacuum.as_str().into());
Self {
filename: Cow::Borrowed(Path::new(":memory:")),
in_memory: false,
read_only: false,
create_if_missing: false,
shared_cache: false,
statement_cache_capacity: 100,
busy_timeout: Duration::from_secs(5),
log_settings: Default::default(),
immutable: false,
pragmas,
collations: Default::default(),
serialized: false,
thread_name: Arc::new(DebugFn(|id| format!("sqlx-sqlite-worker-{}", id))),
command_channel_size: 50,
row_channel_size: 50,
}
}
pub fn filename(mut self, filename: impl AsRef<Path>) -> Self {
self.filename = Cow::Owned(filename.as_ref().to_owned());
self
}
pub fn foreign_keys(mut self, on: bool) -> Self {
self.pragmas.insert(
"foreign_keys".into(),
(if on { "ON" } else { "OFF" }).into(),
);
self
}
pub fn shared_cache(mut self, on: bool) -> Self {
self.shared_cache = on;
self
}
pub fn journal_mode(mut self, mode: SqliteJournalMode) -> Self {
self.pragmas
.insert("journal_mode".into(), mode.as_str().into());
self
}
pub fn locking_mode(mut self, mode: SqliteLockingMode) -> Self {
self.pragmas
.insert("locking_mode".into(), mode.as_str().into());
self
}
pub fn read_only(mut self, read_only: bool) -> Self {
self.read_only = read_only;
self
}
pub fn create_if_missing(mut self, create: bool) -> Self {
self.create_if_missing = create;
self
}
pub fn statement_cache_capacity(mut self, capacity: usize) -> Self {
self.statement_cache_capacity = capacity;
self
}
pub fn busy_timeout(mut self, timeout: Duration) -> Self {
self.busy_timeout = timeout;
self
}
pub fn synchronous(mut self, synchronous: SqliteSynchronous) -> Self {
self.pragmas
.insert("synchronous".into(), synchronous.as_str().into());
self
}
pub fn auto_vacuum(mut self, auto_vacuum: SqliteAutoVacuum) -> Self {
self.pragmas
.insert("auto_vacuum".into(), auto_vacuum.as_str().into());
self
}
pub fn page_size(mut self, page_size: u32) -> Self {
self.pragmas
.insert("page_size".into(), page_size.to_string().into());
self
}
pub fn pragma<K, V>(mut self, key: K, value: V) -> Self
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
{
self.pragmas.insert(key.into(), value.into());
self
}
pub fn collation<N, F>(mut self, name: N, collate: F) -> Self
where
N: Into<Arc<str>>,
F: Fn(&str, &str) -> Ordering + Send + Sync + 'static,
{
self.collations.push(Collation::new(name, collate));
self
}
pub fn immutable(mut self, immutable: bool) -> Self {
self.immutable = immutable;
self
}
pub fn serialized(mut self, serialized: bool) -> Self {
self.serialized = serialized;
self
}
pub fn thread_name(
mut self,
generator: impl Fn(u64) -> String + Send + Sync + 'static,
) -> Self {
self.thread_name = Arc::new(DebugFn(generator));
self
}
pub fn command_buffer_size(mut self, size: usize) -> Self {
self.command_channel_size = size;
self
}
pub fn row_buffer_size(mut self, size: usize) -> Self {
self.row_channel_size = size;
self
}
}