use rusqlite::Connection;
use std::path::Path;
use crate::database::Database;
use crate::database::DatabaseError;
use crate::database::DatabaseOpeningError;
use crate::database::DatabaseTransaction;
pub struct CrawlerDatabase {
base_db: Database,
}
pub struct CrawlerDatabaseTransaction<'a> {
pub base_transaction: DatabaseTransaction<'a>
}
impl CrawlerDatabase {
pub fn initalize_from_base(db: Database) -> Result<Self, DatabaseOpeningError> {
let crawler_db = Self {
base_db: db,
};
crawler_db.check_crawler_schema()?;
crawler_db.initalize_crawler_database()
.map_err(|e| e.while_initlizing(crawler_db.base_db.path()))?;
Ok(crawler_db)
}
pub fn open_writable(path: impl AsRef<Path>) -> Result<Self, DatabaseOpeningError> {
let database = Database::open_writable(&path)?;
Self::initalize_from_base(database)
}
pub fn open_read_only(path: impl AsRef<Path>) -> Result<Self, DatabaseOpeningError> {
let database = Self {
base_db: Database::open_read_only(&path)?
};
database.check_crawler_schema()?;
Ok(database)
}
pub fn base(&self) -> &Database {
&self.base_db
}
pub fn base_mut(&mut self) -> &mut Database {
&mut self.base_db
}
pub fn start_transaction(
&mut self
) -> Result<CrawlerDatabaseTransaction, DatabaseError> {
return Ok(CrawlerDatabaseTransaction{
base_transaction: self.base_db.start_transaction()?,
})
}
pub fn connection(&self) -> &Connection {
&self.base_db.connection
}
}
impl<'a> CrawlerDatabaseTransaction<'a> {
pub fn connection(&self) -> &Connection {
&self.base_transaction.transaction
}
pub fn commit(self) -> Result<(), DatabaseError> {
return self.base_transaction.commit();
}
}