Add word group to database and model
This commit is contained in:
+76
-59
@@ -1,5 +1,5 @@
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::lang::DictionaryElement;
|
||||
use crate::lang::{WordData, WordGroup};
|
||||
use rusqlite::Connection;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -10,13 +10,61 @@ pub fn create_db() {
|
||||
std::fs::File::create(&db_file).unwrap();
|
||||
let connection = Connection::open(&db_file).unwrap();
|
||||
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
||||
create_words_table(&connection);
|
||||
create_card_stat_table(&connection);
|
||||
create_card_set_table(&connection);
|
||||
|
||||
create_tables(&connection);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_word(word: &mut DictionaryElement, connection: &Connection) {
|
||||
fn create_tables(conn: &Connection) {
|
||||
conn.execute(
|
||||
"
|
||||
CREATE TABLE word_group (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
||||
insert into word_group (name)
|
||||
values (\"Слова\");
|
||||
|
||||
CREATE TABLE words (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
key TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
tags TEXT NOT NULL,
|
||||
more TEXT,
|
||||
group_id INTEGER NOT NULL DEFAULT 1,
|
||||
FOREIGN KEY(group_id) REFERENCES word_group(id)
|
||||
);
|
||||
|
||||
CREATE TABLE card_stats (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
word_id INTEGER NOT NULL,
|
||||
set_id TEXT NOT NULL,
|
||||
score INTEGER NOT NULL DEFAULT 1,
|
||||
last_opened INTEGER NOT NULL,
|
||||
FOREIGN KEY (word_id) REFERENCES words (id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (set_id) REFERENCES card_set (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
|
||||
|
||||
CREATE TABLE card_set (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
forward TEXT NOT NULL,
|
||||
backward TEXT NOT NULL,
|
||||
filter TEXT NOT NULL
|
||||
);
|
||||
",
|
||||
(),
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("{}", e);
|
||||
0
|
||||
});
|
||||
}
|
||||
|
||||
pub fn add_word(word: &mut WordData, connection: &Connection) {
|
||||
let index = connection
|
||||
.query_row(
|
||||
"INSERT INTO words (key, value, tags, more) VALUES (?1, ?2, ?3, ?4) RETURNING id",
|
||||
@@ -36,7 +84,7 @@ pub fn add_word(word: &mut DictionaryElement, connection: &Connection) {
|
||||
word.id = index;
|
||||
}
|
||||
|
||||
pub fn update_word(word: &mut DictionaryElement, connection: &Connection) {
|
||||
pub fn update_word(word: &mut WordData, connection: &Connection) {
|
||||
if word.id == 0 {
|
||||
add_word(word, &connection);
|
||||
} else {
|
||||
@@ -58,7 +106,7 @@ pub fn update_word(word: &mut DictionaryElement, connection: &Connection) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_word(word: &DictionaryElement, connection: &Connection) {
|
||||
pub fn delete_word(word: &WordData, connection: &Connection) {
|
||||
if word.id == 0 {
|
||||
return;
|
||||
}
|
||||
@@ -70,19 +118,20 @@ pub fn delete_word(word: &DictionaryElement, connection: &Connection) {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn load_words(connection: &Connection) -> Vec<DictionaryElement> {
|
||||
pub fn load_words(connection: &Connection) -> Vec<WordData> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id, key, value, tags, more FROM words")
|
||||
.prepare("SELECT id, key, value, tags, more, group_id FROM words")
|
||||
.unwrap();
|
||||
let word_iter = stmt
|
||||
.query_map([], |row| {
|
||||
let addinionals: String = row.get(4)?;
|
||||
Ok(DictionaryElement {
|
||||
Ok(WordData {
|
||||
id: row.get(0)?,
|
||||
key: row.get(1)?,
|
||||
value: row.get(2)?,
|
||||
tags: row.get(3)?,
|
||||
additional: serde_json::from_str::<HashMap<String, String>>(&addinionals).unwrap(),
|
||||
group_id: row.get(5)?,
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
@@ -95,55 +144,23 @@ pub fn load_words(connection: &Connection) -> Vec<DictionaryElement> {
|
||||
buffer
|
||||
}
|
||||
|
||||
fn create_words_table(conn: &Connection) {
|
||||
conn.execute(
|
||||
"CREATE TABLE words (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
key TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
tags TEXT NOT NULL,
|
||||
more TEXT
|
||||
)",
|
||||
(),
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("{}", e);
|
||||
0
|
||||
});
|
||||
}
|
||||
pub fn load_word_groups(connection: &Connection) -> Vec<WordGroup> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id, name FROM word_group")
|
||||
.unwrap();
|
||||
let group_iter = stmt
|
||||
.query_map([], |row| {
|
||||
Ok(WordGroup {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
fn create_card_stat_table(conn: &Connection) {
|
||||
conn.execute(
|
||||
"CREATE TABLE card_stats (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
word_id INTEGER NOT NULL,
|
||||
set_id TEXT NOT NULL,
|
||||
score INTEGER NOT NULL DEFAULT 1,
|
||||
last_opened INTEGER NOT NULL,
|
||||
FOREIGN KEY (word_id) REFERENCES words (id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (set_id) REFERENCES card_set (id) ON DELETE CASCADE
|
||||
)",
|
||||
(),
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("{}", e);
|
||||
0
|
||||
});
|
||||
}
|
||||
let mut buffer = vec![];
|
||||
for group in group_iter {
|
||||
buffer.push(group.unwrap());
|
||||
}
|
||||
|
||||
fn create_card_set_table(conn: &Connection) {
|
||||
conn.execute(
|
||||
"CREATE TABLE card_set (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
forward TEXT NOT NULL,
|
||||
backward TEXT NOT NULL,
|
||||
filter TEXT NOT NULL
|
||||
)",
|
||||
(),
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
println!("{}", e);
|
||||
0
|
||||
});
|
||||
buffer
|
||||
}
|
||||
Reference in New Issue
Block a user