Write TableAccessQuerySet for other tables. Create Id macro

This commit is contained in:
micialware committed 2026-08-30 22:15:03 +03:00
1 parent 9a5b64a4fe
commit d9bbf9e705
8 files changed
+184 -57

No files matched your search

+7 -2
View File
@@ -65,8 +65,13 @@ pub fn default_connection() -> Connection {
.execute("PRAGMA foreign_keys = ON;", [])
.expect("Failed to set PRAGMA");
connection
.execute("PRAGMA journal_mode = WAL;", [])
.expect("Failed to set PRAGMA");
.query_one("PRAGMA journal_mode = WAL;", [], |row| {
println!("{:?}", row);
Ok(())
})
.unwrap_or_else(|e| {
println!("{}", e);
});
connection
.execute("PRAGMA synchronous = NORMAL;", [])
.expect("Failed to set PRAGMA");
+90 -2
View File
@@ -1,8 +1,96 @@
use crate::data::entities::{WordData, WordGroup};
use crate::data_provider::TableAccessQuerySet;
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use crate::notification::{MaybeError, NotificationData};
use rusqlite::{Connection, params};
use rusqlite::{Connection, Error, Params, Row, params};
use std::collections::HashMap;
use litemap::LiteMap;
pub struct WordQuerySet;
impl TableAccessQuerySet<WordData> for WordQuerySet {
fn load() -> &'static str {
"SELECT id, key, value, tags, more, group_id FROM words"
}
fn save() -> &'static str {
"UPDATE words SET key = ?1, value = ?2, tags = ?3, more = ?4 WHERE id = ?5"
}
fn add() -> &'static str {
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)"
}
fn name() -> &'static str {
"words"
}
fn from_sql(row: &Row) -> Result<WordData, Error> {
let additional: String = row.get(4)?;
Ok(WordData {
id: row.get(0)?,
key: row.get(1)?,
value: row.get(2)?,
tags: row.get(3)?,
additional: serde_json::from_str(&additional)
.unwrap_or_default(),
group_id: row.get(5)?,
})
}
fn insert_params(entity: &WordData) -> impl Params {
(
&entity.key,
&entity.value,
&entity.tags,
serde_json::to_string(&entity.additional).unwrap_or_default(),
entity.group_id,
)
}
fn update_params(entity: &WordData) -> impl Params {
(
&entity.key,
&entity.value,
&entity.tags,
serde_json::to_string(&entity.additional).unwrap_or_default(),
entity.id,
)
}
}
pub struct WordGroupQuerySet;
impl TableAccessQuerySet<WordGroup> for WordGroupQuerySet {
fn load() -> &'static str {
"SELECT id, name FROM word_groups"
}
fn save() -> &'static str {
"UPDATE word_groups SET name = ?1 WHERE id = ?2"
}
fn add() -> &'static str {
"INSERT INTO word_groups (name) VALUES (?1)"
}
fn name() -> &'static str {
"word_groups"
}
fn from_sql(row: &Row) -> Result<WordGroup, Error> {
Ok(WordGroup {
id: row.get(0)?,
name: row.get(1)?,
})
}
fn insert_params(entity: &WordGroup) -> impl Params {
(&entity.name,)
}
fn update_params(entity: &WordGroup) -> impl Params {
(&entity.name, entity.id)
}
}
pub fn add_word(word: &mut WordData, connection: &Connection) -> MaybeError {
let index : u32 = connection
@@ -99,7 +187,7 @@ pub fn load_words(connection: &Connection) -> Result<Vec<WordData>, Notification
key: row.get(1)?,
value: row.get(2)?,
tags: row.get(3)?,
additional: serde_json::from_str::<HashMap<String, String>>(&additional)
additional: serde_json::from_str(&additional)
.unwrap_or_default(),
group_id: row.get(5)?,
})