use crate::types::PdfEntry;
use rusqlite::{params, Connection, Result};
pub struct PdfDatabase {
conn: Connection,
}
impl PdfDatabase {
pub fn new(path: &std::path::Path) -> Result<Self> {
let conn = Connection::open(path)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS pdfs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL UNIQUE,
filename TEXT NOT NULL,
size INTEGER NOT NULL,
modified DATETIME NOT NULL,
indexed_at DATETIME NOT NULL
)",
[],
)?;
Ok(PdfDatabase { conn })
}
pub fn insert_pdf(&self, pdf: &PdfEntry) -> Result<()> {
self.conn.execute(
"INSERT OR REPLACE INTO pdfs (path, filename, size, modified, indexed_at)
VALUES (?, ?, ?, ?, ?)",
params![
pdf.path,
pdf.filename,
pdf.size as i64,
pdf.modified.to_rfc3339(),
pdf.indexed_at.unwrap().to_rfc3339(),
],
)?;
Ok(())
}
pub fn count_pdfs(&self) -> Result<i64> {
let count = self
.conn
.query_row("SELECT COUNT(*) FROM pdfs", [], |row| row.get(0))?;
Ok(count)
}
}