use std::path::Path;
use std::time::Duration;
use rusqlite::OpenFlags;
use crate::{GeoPackage, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum JournalMode {
Delete,
Wal,
}
impl JournalMode {
pub(crate) fn keyword(self) -> &'static str {
match self {
Self::Delete => "DELETE",
Self::Wal => "WAL",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Synchronous {
Off,
Normal,
Full,
Extra,
}
impl Synchronous {
pub(crate) fn code(self) -> i32 {
match self {
Self::Off => 0,
Self::Normal => 1,
Self::Full => 2,
Self::Extra => 3,
}
}
}
pub const DEFAULT_BUSY_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct OpenOptions {
pub(crate) journal_mode: Option<JournalMode>,
pub(crate) synchronous: Option<Synchronous>,
pub(crate) busy_timeout: Option<Duration>,
pub(crate) allow_unsupported_extension_writes: bool,
pub(crate) enforce_column_constraints: bool,
pub(crate) lenient: bool,
}
impl OpenOptions {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn journal_mode(mut self, mode: JournalMode) -> Self {
self.journal_mode = Some(mode);
self
}
#[must_use]
pub fn lenient(mut self, lenient: bool) -> Self {
self.lenient = lenient;
self
}
#[must_use]
pub fn synchronous(mut self, synchronous: Synchronous) -> Self {
self.synchronous = Some(synchronous);
self
}
#[must_use]
pub fn busy_timeout(mut self, timeout: Duration) -> Self {
self.busy_timeout = Some(timeout);
self
}
#[must_use]
pub fn allow_unsupported_extension_writes(mut self, allow: bool) -> Self {
self.allow_unsupported_extension_writes = allow;
self
}
#[must_use]
pub fn enforce_column_constraints(mut self, enforce: bool) -> Self {
self.enforce_column_constraints = enforce;
self
}
pub(crate) fn with_default_busy_timeout(mut self) -> Self {
if self.busy_timeout.is_none() {
self.busy_timeout = Some(DEFAULT_BUSY_TIMEOUT);
}
self
}
pub fn create<P: AsRef<Path>>(self, path: P) -> Result<GeoPackage> {
GeoPackage::create_configured(path.as_ref(), self)
}
pub fn open<P: AsRef<Path>>(self, path: P) -> Result<GeoPackage> {
GeoPackage::open_configured(path.as_ref(), OpenFlags::SQLITE_OPEN_READ_WRITE, self)
}
pub fn open_read_only<P: AsRef<Path>>(self, path: P) -> Result<GeoPackage> {
GeoPackage::open_configured(path.as_ref(), OpenFlags::SQLITE_OPEN_READ_ONLY, self)
}
pub fn from_connection(self, conn: rusqlite::Connection) -> Result<GeoPackage> {
GeoPackage::from_connection_configured(conn, self, true)
}
}