From ddde9204c72dd867f920f07f6483be03dda7cf68 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 28 Nov 2022 22:55:14 +0100 Subject: basically update impl; added "good" download; auto dl on new mod; db to 0.4; etc --- src/db.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 11 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 6b1e3ab..542c162 100644 --- a/src/db.rs +++ b/src/db.rs @@ -32,7 +32,7 @@ pub fn mods_get_all_ids(config: Cfg) -> Result, Box Result Result Result> { + let data = format!("{}/data.db", config.data); + let connection = Connection::open(data)?; + + let mut mod_name = String::new(); + let mut stmt = connection.prepare("SELECT name FROM mods WHERE id = ?")?; + let name_iter = stmt.query_map([id], |row| { + row.get::(0) + })?; + + for name in name_iter { + //println!("Found id {:?}", id.as_ref().unwrap()); + mod_name = name?; + }; + + match mod_name.is_empty() { + true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), + false => Ok(mod_name), + } +} + +pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> Result<(), Box> { + + println!("Updating versions for {} with \n {}", mod_id, versions); + + let data = format!("{}/data.db", config.data); + 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) -> Result<(), Box> { println!("Removing mod {} from database", id); @@ -95,14 +127,15 @@ pub fn mods_get_versions(config: Cfg, mods: Vec) -> Result = Vec::new(); - let mut stmt = connection.prepare(dbg!(format!("SELECT id, versions FROM mods {}", wherestr).as_str()))?; + let mut stmt = connection.prepare(format!("SELECT id, versions, name FROM mods {}", wherestr).as_str())?; let id_iter = stmt.query_map([], |row| { - Ok(vec![row.get::(0)?, row.get::(1)?]) + Ok(vec![row.get::(0)?, row.get::(1)?, row.get::(2)?]) })?; for ver in id_iter { let version = ver?; - println!("Found versions {} for mod {}", version[1], version[0]); + println!("Getting versions for {} from the database", String::from(&version[2])); + //println!("Found versions {} for mod {}", version[1], version[0]); versionmaps.push(DBModlistVersions { mod_id: String::from(&version[0]), versions: String::from(&version[1]) }) }; @@ -120,7 +153,7 @@ pub fn userlist_insert(config: Cfg, list_id: String, mod_id: String, current_ver let connection = Connection::open(data)?; - connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4)", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|"), current_link])?; + connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE')", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|"), current_link])?; Ok(()) } @@ -136,7 +169,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result, })?; for id in id_iter { - println!("Found id {:?}", id.as_ref().unwrap()); + //println!("Found id {:?}", id.as_ref().unwrap()); mod_ids.push(id?) }; @@ -167,7 +200,26 @@ pub fn userlist_get_applicable_versions(config: Cfg, list_id: String, mod_id: St })?; for ver in ver_iter { - println!("Found id {:?}", ver); + version = ver?; + }; + + match version.is_empty() { + true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), + false => Ok(version), + } +} + +pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result> { + let data = format!("{}/data.db", config.data); + let connection = Connection::open(data).unwrap(); + + let mut version: String = String::new(); + let mut stmt = connection.prepare(format!("SELECT current_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 { version = ver?; }; @@ -204,6 +256,40 @@ pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: S Ok(()) } +pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> Result<(), Box> { + let data = format!("{}/data.db", config.data); + let connection = Connection::open(data)?; + + let currently_disabled_versions = userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?; + let disabled_versions = match currently_disabled_versions == "NONE" { + true => disabled_version, + false => format!("{}|{}", currently_disabled_versions, disabled_version), + }; + + connection.execute(format!("UPDATE {} SET disabled_versions = ?1 WHERE mod_id = ?2", list_id).as_str(), [disabled_versions, mod_id])?; + Ok(()) +} + +pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> Result> { + let data = format!("{}/data.db", config.data); + let connection = Connection::open(data).unwrap(); + + let mut version: String = String::new(); + let mut stmt = connection.prepare(format!("SELECT disabled_versions 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 { + version = ver?; + }; + + match version.is_empty() { + true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), + false => Ok(version), + } +} + pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result, Box> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); @@ -233,7 +319,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.stringify(), download_folder])?; - connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT )", 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' )", id).as_str(), [])?; Ok(()) } @@ -360,11 +446,17 @@ pub fn s_config_get_version(config: Cfg) -> Result Result<(), Box> { +pub fn s_insert_column(config: Cfg, table: String, column: String, c_type: String, default: Option) -> Result<(), Box> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; - connection.execute(format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type).as_str(), ())?; + let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type); + + if default.is_some() { + sql = format!("{} DEFAULT {}", sql, default.unwrap()); + } + + connection.execute(sql.as_str(), ())?; Ok(()) } -- cgit v1.2.3