use std::path::Path;
use super::error::{DbError, DbResult};
use super::ffi::{self, RawDb};
use super::statement::{Row, Statement, StepResult};
use super::transaction::Transaction;
use super::value::Value;
pub struct Connection {
db: RawDb,
}
impl Connection {
pub fn open(path: &Path, read_only: bool) -> DbResult<Self> {
let path_str = path.to_string_lossy();
let flags = if read_only {
ffi::SQLITE_OPEN_READONLY | ffi::SQLITE_OPEN_FULLMUTEX
} else {
ffi::SQLITE_OPEN_READWRITE
| ffi::SQLITE_OPEN_CREATE
| ffi::SQLITE_OPEN_FULLMUTEX
};
let db = RawDb::open(&path_str, flags)?;
Ok(Self { db })
}
pub fn execute_batch(&self, sql: &str) -> DbResult<()> {
self.db.exec(sql)
}
pub fn execute_batch_zeroized(&self, sql: &str) -> DbResult<()> {
self.db.exec_zeroized(sql)
}
pub fn prepare(&self, sql: &str) -> DbResult<Statement<'_>> {
let raw_stmt = self.db.prepare(sql)?;
Ok(Statement::new(raw_stmt))
}
pub fn execute(&self, sql: &str, params: &[Value]) -> DbResult<usize> {
let mut stmt = self.prepare(sql)?;
stmt.bind_values(params)?;
stmt.step()?;
Ok(usize::try_from(self.db.changes()).unwrap_or(0))
}
pub fn query_row<T>(
&self,
sql: &str,
params: &[Value],
mapper: impl FnOnce(&Row<'_, '_>) -> DbResult<T>,
) -> DbResult<T> {
let mut stmt = self.prepare(sql)?;
stmt.bind_values(params)?;
match stmt.step()? {
StepResult::Row(row) => mapper(&row),
StepResult::Done => {
Err(DbError::new(ffi::SQLITE_DONE, "query returned no rows"))
}
}
}
pub fn query_row_optional<T>(
&self,
sql: &str,
params: &[Value],
mapper: impl FnOnce(&Row<'_, '_>) -> DbResult<T>,
) -> DbResult<Option<T>> {
let mut stmt = self.prepare(sql)?;
stmt.bind_values(params)?;
match stmt.step()? {
StepResult::Row(row) => mapper(&row).map(Some),
StepResult::Done => Ok(None),
}
}
pub fn transaction(&self) -> DbResult<Transaction<'_>> {
Transaction::begin(self, false)
}
pub fn transaction_immediate(&self) -> DbResult<Transaction<'_>> {
Transaction::begin(self, true)
}
#[allow(dead_code)]
#[must_use]
pub fn last_insert_rowid(&self) -> i64 {
self.db.last_insert_rowid()
}
#[allow(dead_code)]
#[must_use]
pub fn changes(&self) -> usize {
usize::try_from(self.db.changes()).unwrap_or(0)
}
}
impl std::fmt::Debug for Connection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Connection").finish_non_exhaustive()
}
}
#[cfg(test)]
impl Connection {
pub fn open_in_memory() -> DbResult<Self> {
Self::open(Path::new(":memory:"), false)
}
}