From b125dfd03084fff47ab8e90d002c6699b762d998 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Tue, 1 Nov 2022 23:00:45 +0100 Subject: added list stuff + beginnings of mods --- src/db.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 3d50b0f..bbbca87 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,4 +1,8 @@ -use crate::Modloader; +use std::io::ErrorKind; + +use crate::{Modloader, config::Cfg}; + +//TODO use prepared statements pub fn insert_mod(id: String, name: String, current_version: String, old_versions: Vec, mod_loader: Modloader, desired_mc_version: String) -> Result<(), sqlite::Error> { @@ -10,9 +14,88 @@ pub fn insert_mod(id: String, name: String, current_version: String, old_version }; let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}', '{}', '{}', '{}')", id, name, current_version, old_versions.join("|"), loader, desired_mc_version); + + connection.execute(sql) +} + +//LIST +pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modloader) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); - dbg!(&sql); + //Setup list in table + let loader = match mod_loader { + Modloader::Fabric => "fabric", + Modloader::Forge => "forge", + }; + + let sql_list = format!("INSERT INTO lists VALUES ('{}', '{}', '{}')", id, mc_version, loader); + let sql_table = format!("CREATE TABLE '{}' ( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'mod_loader' TEXT )", id); + let sql = format!("{};{};", sql_list, sql_table); connection.execute(sql) +} +pub fn remove_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql_list = format!("DELETE FROM lists WHERE id = '{}'", id); + let sql_table = format!("DROP TABLE '{}'", id); + let sql = format!("{};{};", sql_list, sql_table); + + connection.execute(sql) +} + +pub fn get_lists(config: Cfg) -> Result, Box> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = "SELECT id FROM lists"; + + let mut list: Vec = Vec::new(); + //TODO catch sql errors better + connection.iterate(sql, |ids| { + if ids.is_empty() { return false; }; + for &(_column, value) in ids.iter() { + list.push(String::from(value.unwrap())); + } + true + }).unwrap(); + match list.is_empty() { + true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_LISTS"))), + false => Ok(list), + } +} + +//config +pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("UPDATE user_config SET value = '{}' WHERE id = 'current_list'", id); + + connection.execute(sql) +} + +pub fn get_current_list(config: Cfg) -> Result> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = "SELECT id FROM lists"; + + let mut list: String = String::new(); + //TODO catch sql errors better + connection.iterate(sql, |ids| { + if ids.is_empty() { return false; }; + for &(_column, value) in ids.iter() { + list = String::from(value.unwrap()); + } + true + }).unwrap(); + if list.is_empty() { + get_lists(config)?; + panic!("current list field should never be empty if there are other lists"); + }; + Ok(list) } -- cgit v1.2.3