2026-09-15 22:09:06 +03:00
|
|
|
use crate::data::Id;
|
2026-09-09 15:00:52 +03:00
|
|
|
use crate::data::entities::WordData;
|
2026-09-04 17:26:59 +03:00
|
|
|
use crate::data::repositories::{DataRepository, DataRepositoryInner};
|
2026-08-31 11:52:54 +03:00
|
|
|
use crate::data_provider::{SqliteDbLayer, TableAccessQuerySet};
|
2026-09-11 22:08:50 +03:00
|
|
|
use litemap::LiteMap;
|
2026-09-09 15:00:52 +03:00
|
|
|
use rusqlite::{Error, Params, Row};
|
2026-08-31 11:52:54 +03:00
|
|
|
|
2026-09-04 17:26:59 +03:00
|
|
|
pub type WordRepository = DataRepository<WordData, WordDataLayer>;
|
|
|
|
|
pub type WordRepositoryInner = DataRepositoryInner<WordData, WordDataLayer>;
|
2026-09-02 00:19:58 +03:00
|
|
|
pub type WordDataLayer = SqliteDbLayer<WordData, WordQuerySet>;
|
2026-08-31 12:12:14 +03:00
|
|
|
#[derive(Clone)]
|
2026-08-30 22:15:03 +03:00
|
|
|
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 {
|
2026-09-08 00:09:13 +03:00
|
|
|
"INSERT INTO words (key, value, tags, more, group_id) VALUES (?1, ?2, ?3, ?4, ?5)"
|
2026-08-30 22:15:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)?,
|
2026-09-11 13:56:26 +03:00
|
|
|
additional: additional_from_string(additional),
|
2026-08-30 22:15:03 +03:00
|
|
|
group_id: row.get(5)?,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn insert_params(entity: &WordData) -> impl Params {
|
|
|
|
|
(
|
|
|
|
|
&entity.key,
|
|
|
|
|
&entity.value,
|
|
|
|
|
&entity.tags,
|
2026-09-11 13:56:26 +03:00
|
|
|
additional_to_string(&entity.additional),
|
2026-08-30 22:15:03 +03:00
|
|
|
entity.group_id,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_params(entity: &WordData) -> impl Params {
|
|
|
|
|
(
|
|
|
|
|
&entity.key,
|
|
|
|
|
&entity.value,
|
|
|
|
|
&entity.tags,
|
2026-09-11 13:56:26 +03:00
|
|
|
additional_to_string(&entity.additional),
|
2026-08-30 22:15:03 +03:00
|
|
|
entity.id,
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-09-11 00:01:19 +03:00
|
|
|
}
|
2026-09-11 13:56:26 +03:00
|
|
|
|
|
|
|
|
fn additional_to_string(map: &LiteMap<Id, String>) -> String {
|
2026-09-11 22:08:50 +03:00
|
|
|
let mut parts = Vec::with_capacity(map.len() * 2);
|
2026-09-11 13:56:26 +03:00
|
|
|
for (id, value) in map {
|
|
|
|
|
parts.push(id.to_string());
|
|
|
|
|
parts.push(value.clone());
|
|
|
|
|
}
|
|
|
|
|
parts.join("")
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 22:08:50 +03:00
|
|
|
fn additional_from_string_temp(data: String) -> LiteMap<Id, String> {
|
2026-09-15 22:09:06 +03:00
|
|
|
let pre_map: LiteMap<String, String> = serde_json::from_str(data.as_str()).unwrap();
|
|
|
|
|
LiteMap::from(
|
|
|
|
|
pre_map
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(k, v)| {
|
|
|
|
|
(
|
|
|
|
|
match k.as_str() {
|
|
|
|
|
"reading" => Id(1),
|
|
|
|
|
"description" => Id(2),
|
|
|
|
|
"context" => Id(3),
|
|
|
|
|
&_ => Id(0),
|
|
|
|
|
},
|
|
|
|
|
v.to_string(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
2026-09-11 22:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-11 13:56:26 +03:00
|
|
|
fn additional_from_string(data: String) -> LiteMap<Id, String> {
|
2026-09-11 22:08:50 +03:00
|
|
|
if data.is_empty() {
|
2026-09-15 22:09:06 +03:00
|
|
|
return LiteMap::new();
|
2026-09-11 22:08:50 +03:00
|
|
|
}
|
2026-09-11 13:56:26 +03:00
|
|
|
let parts = data.split('').collect::<Vec<&str>>();
|
|
|
|
|
let chunks = parts.chunks(2).collect::<Vec<&[&str]>>();
|
|
|
|
|
let mut map = LiteMap::with_capacity(parts.len());
|
|
|
|
|
for part in &chunks {
|
2026-09-15 22:09:06 +03:00
|
|
|
let id: Id = part[0].parse().unwrap();
|
2026-09-11 13:56:26 +03:00
|
|
|
let value: String = part[1].to_string();
|
|
|
|
|
map.insert(id, value);
|
|
|
|
|
}
|
|
|
|
|
map
|
|
|
|
|
}
|