From 0f5223d3d3f6aeb6bb1a0b09ad3d4ef5731774dd Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 5 Nov 2022 21:53:24 +0100 Subject: added setup & download; direct input --- src/db.rs | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 144 insertions(+), 27 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 497cc15..88a104d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -12,12 +12,14 @@ pub fn insert_mod(config: Cfg, id: String, name: String, versions: Vec) let data = format!("{}/data.db", config.data); let connection = sqlite::open(data).unwrap(); - let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}')", id, name, versions.join("|")); + let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}')", id, name.replace('\'', ""), versions.join("|")); + + dbg!(&sql); connection.execute(sql) } -pub fn insert_mod_in_list(config: Cfg, list: List, id: String, current_version: String, applicable_versions: Vec) -> Result<(), sqlite::Error> { +pub fn insert_mod_in_list(config: Cfg, list: List, id: String, current_version: String, applicable_versions: Vec, current_link: String) -> Result<(), sqlite::Error> { println!("Inserting into current list"); @@ -30,8 +32,8 @@ pub fn insert_mod_in_list(config: Cfg, list: List, id: String, current_version: applicable_versions_vec.push(ver.id); } - let sql = format!("INSERT INTO {} VALUES ('{}', '{}', '{}')", list.id, id, current_version, applicable_versions_vec.join("|")); - + let sql = format!("INSERT INTO {} VALUES ('{}', '{}', '{}', '{}')", list.id, id, current_version, applicable_versions_vec.join("|"), current_link); + connection.execute(sql) } @@ -122,33 +124,33 @@ pub struct DBModlistVersions { } 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 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 sql = format!("SELECT id, versions FROM mods {}", wherestr); - let mut versionmaps: Vec = Vec::new(); - //TODO catch sql errors better - let mut cursor = connection.prepare(sql).unwrap().into_cursor(); +dbg!(&sql); - 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 mut versionmaps: Vec = Vec::new(); +//TODO catch sql errors better +let mut cursor = connection.prepare(sql).unwrap().into_cursor(); - if versionmaps.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; +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) }) +}; - Ok(versionmaps) +if versionmaps.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; + +Ok(versionmaps) } pub fn get_list_version(config: Cfg, list: List, mod_id: String) -> Result> { @@ -179,7 +181,7 @@ pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modl 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_table = format!("CREATE TABLE '{}' ( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT)", id); let sql = format!("{};{};", sql_list, sql_table); connection.execute(sql) @@ -217,6 +219,27 @@ pub fn get_lists(config: Cfg) -> Result, Box> } } +pub fn get_current_versions(config: Cfg, list: List) -> Result, Box> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("SELECT current_version FROM {}", list.id); + + dbg!(&sql); + + let mut versions: Vec = Vec::new(); + //TODO catch sql errors better + let mut cursor = connection.prepare(sql).unwrap().into_cursor(); + + while let Some(Ok(row)) = cursor.next() { + versions.push(row.get::(0)); + }; + + if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; + + Ok(versions) +} + pub fn get_list(config: Cfg, id: String) -> Result> { let data = format!("{}/data.db", config.data); let connection = sqlite::open(data).unwrap(); @@ -247,6 +270,38 @@ pub fn change_list_versions(config: Cfg, list: List, current_version: String, ve connection.execute(sql) } +//DOWNLOAD + +pub fn insert_dl_link(config: Cfg, list: List, mod_id: String, link: String) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("UPDATE {} SET current_download = '{}' WHERE mod_id = '{}'", list.id, link, mod_id); + + connection.execute(sql) +} + +pub fn get_dl_links(config: Cfg, list: List) -> Result, Box> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = format!("SELECT current_download FROM {}", list.id); + + dbg!(&sql); + + let mut links: Vec = Vec::new(); + //TODO catch sql errors better + let mut cursor = connection.prepare(sql).unwrap().into_cursor(); + + while let Some(Ok(row)) = cursor.next() { + links.push(row.get::(0)); + }; + + if links.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; + + Ok(links) +} + //config pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { let data = format!("{}/data.db", config.data); @@ -278,3 +333,65 @@ pub fn get_current_list_id(config: Cfg) -> Result 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 = 'db_version'", ver); + + connection.execute(sql) +} + +pub fn create_dbversion(config: Cfg) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + let sql = "INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' );"; + connection.execute(sql) +} + +pub fn get_dbversion(config: Cfg) -> Result> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = "SELECT db_version FROM user_config"; + + let mut ver: String = String::new(); + //TODO catch sql errors better + connection.iterate(sql, |ids| { + if ids.is_empty() { return false; }; + for &(_column, value) in ids.iter() { + ver = String::from(value.unwrap()); + } + true + })?; + if ver.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_DBVERSION"))); }; + Ok(ver) +} + +pub fn db_setup(config: Cfg) -> Result<(), sqlite::Error> { + println!("Initiating database"); + + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let sql = "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); CREATE TABLE 'mods' ( 'id' TEXT, 'name' TEXT, 'versions' TEXT ); CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT ); INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' ); INSERT INTO 'user_config' VALUES ( 'current_list', '...' )"; + + connection.execute(sql) +} + +pub fn insert_column(config: Cfg, table: String, column: String, c_type: sqlite::Type) -> Result<(), sqlite::Error> { + let data = format!("{}/data.db", config.data); + let connection = sqlite::open(data).unwrap(); + + let ct = match c_type { + sqlite::Type::Null => "NULL", + sqlite::Type::Float => "FLOAT", + sqlite::Type::Binary => "BINARY", + sqlite::Type::String => "TEXT", + sqlite::Type::Integer => "INT", + }; + + let sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, ct); + connection.execute(sql) +} -- cgit v1.2.3