From 5d50f446a1a4612c0c931bdbc61f945760392f29 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 4 Nov 2022 23:41:21 +0100 Subject: "finished" update, added some tests --- src/db.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 33d8344..497cc15 100644 --- a/src/db.rs +++ b/src/db.rs @@ -2,7 +2,7 @@ use std::io::ErrorKind; use crate::{Modloader, config::Cfg, List, modrinth::Version, get_modloader}; -//TODO use prepared statements +//TODO use prepared statements / change to rusqlite //MODS pub fn insert_mod(config: Cfg, id: String, name: String, versions: Vec) -> Result<(), sqlite::Error> { @@ -151,6 +151,27 @@ pub fn get_versions(config: Cfg, mods: Vec) -> Result Result> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("SELECT applicable_versions FROM {} WHERE mod_id = '{}'", list.id, mod_id); + + //TODO catch sql errors better + let mut version: String = String::new(); + connection.iterate(sql, |ver| { + if ver.is_empty() { return false; }; + for &(_column, value) in ver.iter() { + version = String::from(value.unwrap()); + } + true + }).unwrap(); + + if version.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; + + Ok(version) +} + //LIST pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modloader) -> Result<(), sqlite::Error> { @@ -217,6 +238,15 @@ pub fn get_list(config: Cfg, id: String) -> Result, mod_id: String) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("UPDATE {} SET current_version = '{}', applicable_versions = '{}' WHERE mod_id = '{}'", list.id, current_version, versions.join("|"), mod_id); + + connection.execute(sql) +} + //config pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { let data = format!("{}/data.db", config.data); -- cgit v1.2.3