diff --git a/src/data_provider/custom_fields.rs b/src/data_provider/custom_fields.rs new file mode 100644 index 0000000..c39a659 --- /dev/null +++ b/src/data_provider/custom_fields.rs @@ -0,0 +1,37 @@ +use rusqlite::{Error, Params, Row}; +use crate::data::entities::WordGroup; +use crate::data_provider::TableAccessQuerySet; + +pub struct CustomFieldsQuerySet; +impl TableAccessQuerySet for CustomFieldsQuerySet { + fn load() -> &'static str { + "SELECT id, name FROM custom_fields" + } + + fn save() -> &'static str { + "UPDATE custom_fields SET name = ?1 WHERE id = ?2" + } + + fn add() -> &'static str { + "INSERT INTO word_groups (name) VALUES (?1)" + } + + fn name() -> &'static str { + "custom_fields" + } + + fn from_sql(row: &Row) -> Result { + 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) + } +} \ No newline at end of file diff --git a/src/data_provider/mod.rs b/src/data_provider/mod.rs index 203b334..a7979f6 100644 --- a/src/data_provider/mod.rs +++ b/src/data_provider/mod.rs @@ -17,6 +17,8 @@ pub(crate) mod sqlite; pub(crate) mod voice; pub(crate) mod web_api; pub(crate) mod words; +pub mod word_groups; +pub mod custom_fields; pub trait TableAccessQuerySet { fn load() -> &'static str; diff --git a/src/data_provider/word_groups.rs b/src/data_provider/word_groups.rs new file mode 100644 index 0000000..ea3817f --- /dev/null +++ b/src/data_provider/word_groups.rs @@ -0,0 +1,37 @@ +use rusqlite::{Error, Params, Row}; +use crate::data::entities::WordGroup; +use crate::data_provider::TableAccessQuerySet; + +pub struct WordGroupQuerySet; +impl TableAccessQuerySet 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 { + 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) + } +} \ No newline at end of file diff --git a/src/data_provider/words.rs b/src/data_provider/words.rs index 75587ed..53658df 100644 --- a/src/data_provider/words.rs +++ b/src/data_provider/words.rs @@ -58,40 +58,6 @@ impl TableAccessQuerySet for WordQuerySet { } } -pub struct WordGroupQuerySet; -impl TableAccessQuerySet 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 { - 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 .query_row(