mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-08 17:35:18 +00:00
is init yet from Rust
This commit is contained in:
1 parent
9b5ae1cd8b
commit
dcfe074535
9 files changed
+38
-91
No files matched your search
@@ -1 +0,0 @@
|
||||
|
||||
+9
-49
@@ -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)
|
||||
}
|
||||
}
|
||||
+3
-5
@@ -1,11 +1,9 @@
|
||||
mod api;
|
||||
pub mod db;
|
||||
mod db;
|
||||
mod error;
|
||||
mod models;
|
||||
pub mod service;
|
||||
mod service;
|
||||
|
||||
pub use db::{YomitanRow, YomitanZipImportProgress, YomitanZipImportResult};
|
||||
pub use error::CJDicError;
|
||||
pub use models::*;
|
||||
pub use service::AppService;
|
||||
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Entry {
|
||||
pub id: i64,
|
||||
pub word: String,
|
||||
pub definition: String,
|
||||
}
|
||||
+7
-11
@@ -3,9 +3,11 @@ use std::path::{Path, PathBuf};
|
||||
use rusqlite::Connection;
|
||||
|
||||
use crate::{
|
||||
db::{Database, YomitanRow, YomitanWriter, YomitanZipImportProgress, YomitanZipImportResult},
|
||||
db::{
|
||||
Database, YOMITAN_DBFILE, YomitanRow, YomitanWriter, YomitanZipImportProgress,
|
||||
YomitanZipImportResult,
|
||||
},
|
||||
error::CJDicError,
|
||||
models::Entry,
|
||||
};
|
||||
|
||||
pub struct AppService {
|
||||
@@ -15,17 +17,11 @@ pub struct AppService {
|
||||
impl AppService {
|
||||
pub fn new<P: AsRef<Path>>(db_dir: P) -> Result<Self, CJDicError> {
|
||||
let db = Database::new(db_dir)?;
|
||||
db.init()?;
|
||||
Ok(Self { db })
|
||||
}
|
||||
|
||||
pub fn add_entry(&self, word: &str, definition: &str) -> Result<(), CJDicError> {
|
||||
self.db.insert_entry(word, definition)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn list_entries(&self) -> Result<Vec<Entry>, CJDicError> {
|
||||
Ok(self.db.fetch_all_entries()?)
|
||||
pub fn is_yomitan_setup_yet(&self) -> bool {
|
||||
self.db.is_yomitan_dbfile_exists()
|
||||
}
|
||||
|
||||
pub fn search_yomitan(
|
||||
@@ -42,7 +38,7 @@ impl AppService {
|
||||
}
|
||||
|
||||
pub fn get_yomitan_writer(&self) -> Result<YomitanWriter, CJDicError> {
|
||||
let conn = Connection::open(self.db.dir.join("yomitan.db"))?;
|
||||
let conn = Connection::open(self.db.dir.join(YOMITAN_DBFILE))?;
|
||||
let mut writer = YomitanWriter::new(conn)?;
|
||||
writer.create_schema()?;
|
||||
Ok(writer)
|
||||
|
||||
Reference in new issue
Block a user