From 96cc5257de09682df345e768dc2a91303f9b36c9 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 3 Nov 2022 21:34:04 +0100 Subject: added update beginnings; init of tests --- src/db.rs | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 166 insertions(+), 17 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index bbbca87..33d8344 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,36 +1,164 @@ use std::io::ErrorKind; -use crate::{Modloader, config::Cfg}; +use crate::{Modloader, config::Cfg, List, modrinth::Version, get_modloader}; //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> { +//MODS +pub fn insert_mod(config: Cfg, id: String, name: String, versions: Vec) -> Result<(), sqlite::Error> { - let connection = sqlite::open("./data.db").unwrap(); + println!("Inserting into modlist"); - let loader = match mod_loader { - Modloader::Fabric => "fabric", - Modloader::Forge => "forge", - }; + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}')", id, name, versions.join("|")); + + connection.execute(sql) +} + +pub fn insert_mod_in_list(config: Cfg, list: List, id: String, current_version: String, applicable_versions: Vec) -> Result<(), sqlite::Error> { + + println!("Inserting into current list"); + + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let mut applicable_versions_vec = vec![]; + + for ver in applicable_versions { + applicable_versions_vec.push(ver.id); + } + + let sql = format!("INSERT INTO {} VALUES ('{}', '{}', '{}')", list.id, id, current_version, applicable_versions_vec.join("|")); + + connection.execute(sql) +} + +pub fn remove_mod_from_list(config: Cfg, list: List, mod_id: String) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("DELETE FROM {} WHERE mod_id = '{}'", list.id, mod_id); - let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}', '{}', '{}', '{}')", id, name, current_version, old_versions.join("|"), loader, desired_mc_version); + dbg!(&sql); connection.execute(sql) } -//LIST -pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modloader) -> Result<(), sqlite::Error> { +pub fn get_mods(config: Cfg) -> Result, Box> { + let data = format!("{}/data.db", config.data); let connection = sqlite::open(data).unwrap(); + + let sql = "SELECT id FROM mods"; - //Setup list in table - let loader = match mod_loader { - Modloader::Fabric => "fabric", - Modloader::Forge => "forge", + let mut mods: Vec = Vec::new(); + //TODO catch sql errors better + connection.iterate(sql, |ids| { + if ids.is_empty() { return false; }; + for &(_column, value) in ids.iter() { + mods.push(String::from(value.unwrap())); + } + true + }).unwrap(); + match mods.is_empty() { + true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_MODS"))), + false => Ok(mods), + } +} + +pub fn get_mods_from_list(config: Cfg, list: List) -> Result, Box> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("SELECT mod_id FROM {}", list.id); + + let mut mods: Vec = Vec::new(); + //TODO catch sql errors better + connection.iterate(sql, |ids| { + if ids.is_empty() { return false; }; + for &(_column, value) in ids.iter() { + mods.push(String::from(value.unwrap())); + } + true + }).unwrap(); + match mods.is_empty() { + true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_MODS"))), + false => Ok(mods), + } +} + +pub fn get_mod_id(config: Cfg, name: String) -> Result> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("SELECT id FROM mods WHERE name = '{}'", name); + + dbg!(&sql); + + let mut modification = String::new(); + //TODO catch sql errors better + connection.iterate(sql, |id| { + if id.is_empty() { return false; }; + for &(_column, value) in id.iter() { + dbg!(&(_column, value)); + modification = String::from(value.unwrap()); + } + true + }).unwrap(); + + dbg!(&modification); + + if modification.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "MOD_NOT_IN_DATABASE"))) }; + + Ok(modification) +} + +#[derive(Debug, Clone)] +pub struct DBModlistVersions { + pub mod_id: String, + pub versions: String, +} + +pub fn get_versions(config: Cfg, mods: Vec) -> Result, Box> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let mut wherestr = String::from("WHERE"); + for (i, id) in mods.iter().enumerate() { + let mut or = " OR"; + if i == mods.len() - 1 { or = "" } + println!("Pushing {}({}) | OR: '{}'", id, i, or); + wherestr = format!("{} id = '{}'{}", wherestr, id, or); + } + + let sql = format!("SELECT id, versions FROM mods {}", wherestr); + + dbg!(&sql); + + let mut versionmaps: Vec = Vec::new(); + //TODO catch sql errors better + let mut cursor = connection.prepare(sql).unwrap().into_cursor(); + + while let Some(Ok(row)) = cursor.next() { + println!("{}: {}", row.get::(0), row.get::(1)); + versionmaps.push(DBModlistVersions { mod_id: row.get::(0), versions: row.get::(1) }) }; - 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); + if versionmaps.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; + + Ok(versionmaps) +} + + +//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(); + + let sql_list = format!("INSERT INTO lists VALUES ('{}', '{}', '{}')", id, mc_version, mod_loader.stringify()); + let sql_table = format!("CREATE TABLE '{}' ( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB)", id); let sql = format!("{};{};", sql_list, sql_table); connection.execute(sql) @@ -68,6 +196,27 @@ pub fn get_lists(config: Cfg) -> Result, Box> } } +pub fn get_list(config: Cfg, id: String) -> Result> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("SELECT mc_version, modloader FROM lists WHERE id = '{}'", id); + + let mut list = vec![]; + //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(); + + if list.len() != 2 { return Err(Box::new(std::io::Error::new(ErrorKind::InvalidData, "LIST_MISSING_DATA"))) }; + + Ok(List { id, mc_version: String::from(&list[0]), modloader: get_modloader(String::from(&list[1]))? }) +} + //config pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { let data = format!("{}/data.db", config.data); @@ -78,7 +227,7 @@ pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { connection.execute(sql) } -pub fn get_current_list(config: Cfg) -> Result> { +pub fn get_current_list_id(config: Cfg) -> Result> { let data = format!("{}/data.db", config.data); let connection = sqlite::open(data).unwrap(); -- cgit v1.2.3