From ff23a11e632812b685f594324e6004c6da81cd4d Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 5 Feb 2023 09:23:29 +0100 Subject: Fixed update shit not correctly updating --- src/db.rs | 101 ++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 35 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index f47bda6..ecc6854 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,17 +4,17 @@ use rusqlite::Connection; use crate::{Modloader, config::Cfg, List, devdir, error::{MLE, MLError, ErrorType}}; -//mods -pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec) -> MLE<()> { +//MODS +pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { - println!("Inserting mod {}({}) into database", name, id); + println!("\t └Save mod info"); let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute( - "INSERT INTO mods (id, name, versions) VALUES (?1, ?2, ?3)", - [id, name.replace('\'', ""), versions.join("|")] + "INSERT INTO mods (id, slug, title) VALUES (?1, ?2, ?3)", + [id, slug, name.replace('\'', "").as_str()] )?; Ok(()) @@ -41,32 +41,53 @@ pub fn mods_get_all_ids(config: Cfg) -> Result, Box MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); +///Get mod id based on the slug or name +///# Arguments +/// +///* `data` - file directory of the database +///* `slug` - Slug or Name of a mod +/// +///# Failure +/// +///Will return `MLError` when no mod id is found +pub fn mods_get_id(data: &str, slug: &str) -> MLE { + let data = devdir(format!("{}/data.db", data).as_str()); let connection = Connection::open(data)?; let mut mod_id = String::new(); - let mut stmt = connection.prepare("SELECT id FROM mods WHERE name = ?")?; - let id_iter = stmt.query_map([name], |row| { + + //get from slug + let mut stmt = connection.prepare("SELECT id FROM mods WHERE slug = ?")?; + let id_iter = stmt.query_map([slug], |row| { row.get::(0) })?; for id in id_iter { mod_id = id?; }; - - match mod_id.is_empty() { - true => Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND")), - false => Ok(mod_id), + //get from title if no id found from slug + if mod_id.is_empty() { + let mut stmt = connection.prepare("SELECT id FROM mods WHERE title = ?")?; + let id_iter = stmt.query_map([slug], |row| { + row.get::(0) + })?; + + for id in id_iter { + mod_id = id?; + }; } + + if mod_id.is_empty() { return Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND")) }; + + Ok(mod_id) } -pub fn mods_get_name(config: Cfg, id: &str) -> MLE { +pub fn mods_get_title(config: Cfg, id: &str) -> MLE { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut mod_name = String::new(); - let mut stmt = connection.prepare("SELECT name FROM mods WHERE id = ?")?; + let mut stmt = connection.prepare("SELECT title FROM mods WHERE id = ?")?; let name_iter = stmt.query_map([id], |row| { row.get::(0) })?; @@ -81,17 +102,6 @@ pub fn mods_get_name(config: Cfg, id: &str) -> MLE { } } -pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> MLE<()> { - - //println!("Updating versions for {} with \n {}", mod_id, versions); - - let data = devdir(format!("{}/data.db", config.data).as_str()); - let connection = Connection::open(data)?; - - connection.execute("UPDATE mods SET versions = ?1 WHERE id = ?2", [versions, mod_id])?; - Ok(()) -} - pub fn mods_remove(config: Cfg, id: String) -> MLE<()> { println!("Removing mod {} from database", id); @@ -124,7 +134,7 @@ pub fn mods_get_versions(config: Cfg, mods: Vec) -> MLE = Vec::new(); - let mut stmt = connection.prepare(format!("SELECT id, versions, name FROM mods {}", wherestr).as_str())?; + let mut stmt = connection.prepare(format!("SELECT id, versions, title FROM mods {}", wherestr).as_str())?; let id_iter = stmt.query_map([], |row| { Ok(vec![row.get::(0)?, row.get::(1)?, row.get::(2)?]) })?; @@ -143,14 +153,18 @@ pub fn mods_get_versions(config: Cfg, mods: Vec) -> MLE, current_link: String) -> MLE<()> { - println!("Inserting {} into current list({})", mod_id, list_id); +pub fn userlist_insert(config: Cfg, list_id: &str, mod_id: &str, current_version: &str, applicable_versions: Vec, current_link: &str, set_version: bool) -> MLE<()> { + println!("\t └Insert in list"); let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; - - connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE')", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|"), current_link])?; + let sv = match set_version { + true => "1", + false => "0", + }; + + connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|").as_str(), current_link, sv])?; Ok(()) } @@ -285,6 +299,23 @@ pub fn userlist_get_all_current_versions_with_mods(config: Cfg, list_id: String) Ok(versions) } +pub fn userlist_get_set_version(config:Cfg, list_id: &str, mod_id: &str) -> MLE { + let data = devdir(format!("{}/data.db", config.data).as_str()); + let connection = Connection::open(data).unwrap(); + + let mut set_version: bool = false; + let mut stmt = connection.prepare(format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id).as_str())?; + let ver_iter = stmt.query_map([&mod_id], |row| { + row.get::(0) + })?; + + for ver in ver_iter { + set_version = ver?; + }; + + Ok(set_version) +} + pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> MLE<()> { let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; @@ -357,7 +388,7 @@ pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Mod let connection = Connection::open(data)?; connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.to_string(), download_folder])?; - connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT, 'disabled_versions' TEXT DEFAULT 'NONE' )", id).as_str(), [])?; + connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT, 'disabled_versions' TEXT DEFAULT 'NONE', 'set_version' INTEGER, CONSTRAINT {}_PK PRIMARY KEY (mod_id) )", id, id).as_str(), [])?; Ok(()) } @@ -505,7 +536,7 @@ pub fn s_insert_column(config: Cfg, table: String, column: String, c_type: Strin Ok(()) } -pub fn db_setup(config: Cfg) -> Result<(), Box> { +pub fn db_setup(config: Cfg) -> MLE<()> { println!("Initiating database"); @@ -514,9 +545,9 @@ pub fn db_setup(config: Cfg) -> Result<(), Box> { connection.execute_batch( "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); - CREATE TABLE 'mods' ( 'id' TEXT, 'name' TEXT, 'versions' TEXT ); + CREATE TABLE 'mods' ( 'id' TEXT, 'slug' TEXT, 'title' TEXT, CONSTRAINT mods_PK PRIMARY KEY (id) ); CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT, 'download_folder' TEXT ); - INSERT INTO 'user_config' VALUES ( 'db_version', '0.4' ); + INSERT INTO 'user_config' VALUES ( 'db_version', '0.5' ); INSERT INTO 'user_config' VALUES ( 'current_list', '...' )", )?; -- cgit v1.2.3