use crate::{
api::meta::MetaTable,
api::version_control::VersionControl,
db::{DbError, DbResult, DucConnection},
};
pub struct DucDocument {
conn: DucConnection,
}
impl DucDocument {
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
pub fn open(path: impl AsRef<std::path::Path>) -> DbResult<Self> {
let conn = crate::db::open_file(path)?;
Ok(Self { conn })
}
pub fn open_memory() -> DbResult<Self> {
let conn = crate::db::open_memory()?;
Ok(Self { conn })
}
#[cfg(all(target_family = "wasm", target_os = "unknown", feature = "opfs"))]
pub async fn open_opfs(name: &str) -> DbResult<Self> {
let conn = crate::db::open_file_opfs(name).await?;
Ok(Self { conn })
}
pub fn meta(&self) -> MetaTable<'_> {
MetaTable::new(&self.conn)
}
pub fn version_control(&self) -> VersionControl<'_> {
VersionControl::new(&self.conn)
}
pub fn schema_version(&self) -> DbResult<i64> {
self.conn
.with(|c| c.pragma_query_value(None, "user_version", |r| r.get(0)))
.map_err(DbError::from)
}
pub fn into_connection(self) -> DucConnection {
self.conn
}
}