mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-09 17:39:35 +00:00
consume core in Tauri
This commit is contained in:
1 parent
876b879a65
commit
59968e0d6c
14 files changed
+157
-75
No files matched your search
@@ -0,0 +1,63 @@
|
||||
use std::{path::Path, sync::Mutex};
|
||||
|
||||
use rusqlite::{Connection, Result};
|
||||
|
||||
use crate::models::Entry;
|
||||
|
||||
pub struct Database {
|
||||
conn: Mutex<Connection>,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let conn = Connection::open(path)?;
|
||||
Ok(Self {
|
||||
conn: Mutex::new(conn),
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user