is init yet from Rust

This commit is contained in:
polv committed 2026-02-24 19:25:37 +07:00
1 parent 9b5ae1cd8b
commit dcfe074535
9 files changed
+38 -91

No files matched your search

+9 -49
View File
@@ -10,8 +10,6 @@ use std::{
use anyhow::Result;
use rusqlite::Connection;
use crate::models::Entry;
mod yomitan;
pub use yomitan::YomitanRow;
@@ -25,10 +23,13 @@ pub(crate) struct Database {
yomitan_attached: Arc<AtomicBool>,
}
const USER_DBFILE: &str = "user.db";
pub(crate) const YOMITAN_DBFILE: &str = "yomitan.db";
impl Database {
pub fn new(db_dir: impl AsRef<Path>) -> Result<Self> {
let dir = db_dir.as_ref().to_path_buf();
let conn = Connection::open(dir.join("user.db"))?;
let conn = Connection::open(dir.join(USER_DBFILE))?;
conn.execute_batch(
r"
@@ -45,6 +46,10 @@ impl Database {
})
}
pub fn is_yomitan_dbfile_exists(&self) -> bool {
self.dir.join(YOMITAN_DBFILE).exists()
}
pub fn yomitan(&self) -> Result<yomitan::YomitanDatabase> {
self.ensure_yomitan_attached()?;
Ok(yomitan::YomitanDatabase::new(self.clone()))
@@ -64,7 +69,7 @@ impl Database {
current_dir()?.join(dir)
};
let path = path
.join("yomitan.db")
.join(YOMITAN_DBFILE)
.to_string_lossy()
.replace(r"\", r"/");
@@ -83,49 +88,4 @@ impl Database {
Ok(())
}
pub fn init(&self) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute_batch(
"
CREATE TABLE IF NOT EXISTS entries (
id INTEGER PRIMARY KEY,
word TEXT NOT NULL,
definition TEXT NOT NULL
)",
)?;
Ok(())
}
pub fn insert_entry(&self, word: &str, definition: &str) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"INSERT INTO entries (word, definition) VALUES (?, ?)",
[word, definition],
)?;
Ok(())
}
pub fn fetch_all_entries(&self) -> Result<Vec<Entry>> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn.prepare("SELECT id, word, definition FROM entries")?;
let rows = stmt.query_map([], |row| {
Ok(Entry {
id: row.get(0)?,
word: row.get(1)?,
definition: row.get(2)?,
})
})?;
let mut entries = Vec::new();
for entry in rows {
entries.push(entry?);
}
Ok(entries)
}
}