summaryrefslogtreecommitdiff
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs62
1 files changed, 29 insertions, 33 deletions
diff --git a/src/db.rs b/src/db.rs
index 49db2fd..6a3c9af 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -52,7 +52,7 @@ pub fn mods_get_all_ids(
52/// 52///
53///Will return `MLError` when no mod id is found 53///Will return `MLError` when no mod id is found
54pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { 54pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> {
55 let data = format!("{}/data.db", data); 55 let data = format!("{data}/data.db");
56 let connection = Connection::open(data)?; 56 let connection = Connection::open(data)?;
57 57
58 let mut mod_id = String::new(); 58 let mut mod_id = String::new();
@@ -157,14 +157,14 @@ pub fn mods_get_versions(
157 for (i, id) in mods.iter().enumerate() { 157 for (i, id) in mods.iter().enumerate() {
158 let mut or = " OR"; 158 let mut or = " OR";
159 if i == mods.len() - 1 { 159 if i == mods.len() - 1 {
160 or = "" 160 or = "";
161 }; 161 };
162 wherestr = format!("{} id = '{}'{}", wherestr, id, or); 162 wherestr = format!("{wherestr} id = '{id}'{or}");
163 } 163 }
164 164
165 let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); 165 let mut versionmaps: Vec<DBModlistVersions> = Vec::new();
166 let mut stmt = connection.prepare( 166 let mut stmt = connection.prepare(
167 format!("SELECT id, versions, title FROM mods {}", wherestr).as_str(), 167 format!("SELECT id, versions, title FROM mods {wherestr}").as_str(),
168 )?; 168 )?;
169 let id_iter = stmt.query_map([], |row| { 169 let id_iter = stmt.query_map([], |row| {
170 Ok(vec![ 170 Ok(vec![
@@ -179,7 +179,7 @@ pub fn mods_get_versions(
179 versionmaps.push(DBModlistVersions { 179 versionmaps.push(DBModlistVersions {
180 mod_id: String::from(&version[0]), 180 mod_id: String::from(&version[0]),
181 versions: String::from(&version[1]), 181 versions: String::from(&version[1]),
182 }) 182 });
183 } 183 }
184 184
185 match versionmaps.is_empty() { 185 match versionmaps.is_empty() {
@@ -208,8 +208,7 @@ pub fn userlist_insert(
208 208
209 connection.execute( 209 connection.execute(
210 format!( 210 format!(
211 "INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)", 211 "INSERT INTO {list_id} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)"
212 list_id
213 ) 212 )
214 .as_str(), 213 .as_str(),
215 [ 214 [
@@ -230,17 +229,17 @@ pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE<Vec<String>> {
230 229
231 let mut mod_ids: Vec<String> = Vec::new(); 230 let mut mod_ids: Vec<String> = Vec::new();
232 let mut stmt = connection 231 let mut stmt = connection
233 .prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?; 232 .prepare(format!("SELECT mod_id FROM {list_id}").as_str())?;
234 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; 233 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
235 234
236 for id in id_iter { 235 for id in id_iter {
237 mod_ids.push(id?) 236 mod_ids.push(id?);
238 } 237 }
239 238
240 match mod_ids.is_empty() { 239 match mod_ids.is_empty() {
241 true => Err(MLError::new( 240 true => Err(MLError::new(
242 ErrorType::DBError, 241 ErrorType::DBError,
243 &format!("NO_MODS_USERLIST{}", list_id), 242 &format!("NO_MODS_USERLIST{list_id}"),
244 )), 243 )),
245 false => Ok(mod_ids), 244 false => Ok(mod_ids),
246 } 245 }
@@ -251,7 +250,7 @@ pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> {
251 let connection = Connection::open(data)?; 250 let connection = Connection::open(data)?;
252 251
253 connection.execute( 252 connection.execute(
254 format!("DELETE FROM {} WHERE mod_id = ?", list_id).as_str(), 253 format!("DELETE FROM {list_id} WHERE mod_id = ?").as_str(),
255 [mod_id], 254 [mod_id],
256 )?; 255 )?;
257 Ok(()) 256 Ok(())
@@ -268,8 +267,7 @@ pub fn userlist_get_applicable_versions(
268 let mut version: String = String::new(); 267 let mut version: String = String::new();
269 let mut stmt = connection.prepare( 268 let mut stmt = connection.prepare(
270 format!( 269 format!(
271 "SELECT applicable_versions FROM {} WHERE mod_id = ?", 270 "SELECT applicable_versions FROM {list_id} WHERE mod_id = ?"
272 list_id
273 ) 271 )
274 .as_str(), 272 .as_str(),
275 )?; 273 )?;
@@ -295,7 +293,7 @@ pub fn userlist_get_all_applicable_versions_with_mods(
295 293
296 let mut versions: Vec<(String, String)> = Vec::new(); 294 let mut versions: Vec<(String, String)> = Vec::new();
297 let mut stmt = connection.prepare( 295 let mut stmt = connection.prepare(
298 format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str(), 296 format!("SELECT mod_id, applicable_versions FROM {list_id}").as_str(),
299 )?; 297 )?;
300 let id_iter = stmt.query_map([], |row| { 298 let id_iter = stmt.query_map([], |row| {
301 Ok(vec![ 299 Ok(vec![
@@ -306,7 +304,7 @@ pub fn userlist_get_all_applicable_versions_with_mods(
306 304
307 for ver in id_iter { 305 for ver in id_iter {
308 let out = ver?; 306 let out = ver?;
309 versions.push((out[0].to_owned(), out[1].to_owned())); 307 versions.push((out[0].clone(), out[1].clone()));
310 } 308 }
311 309
312 if versions.is_empty() { 310 if versions.is_empty() {
@@ -326,7 +324,7 @@ pub fn userlist_get_current_version(
326 324
327 let mut version: String = String::new(); 325 let mut version: String = String::new();
328 let mut stmt = connection.prepare( 326 let mut stmt = connection.prepare(
329 format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id) 327 format!("SELECT current_version FROM {list_id} WHERE mod_id = ?")
330 .as_str(), 328 .as_str(),
331 )?; 329 )?;
332 let ver_iter = 330 let ver_iter =
@@ -351,7 +349,7 @@ pub fn userlist_get_all_current_version_ids(
351 349
352 let mut versions: Vec<String> = Vec::new(); 350 let mut versions: Vec<String> = Vec::new();
353 let mut stmt = connection 351 let mut stmt = connection
354 .prepare(format!("SELECT current_version FROM {}", list_id).as_str())?; 352 .prepare(format!("SELECT current_version FROM {list_id}").as_str())?;
355 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; 353 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
356 354
357 for id in id_iter { 355 for id in id_iter {
@@ -374,7 +372,7 @@ pub fn userlist_get_all_current_versions_with_mods(
374 372
375 let mut versions: Vec<(String, String)> = Vec::new(); 373 let mut versions: Vec<(String, String)> = Vec::new();
376 let mut stmt = connection.prepare( 374 let mut stmt = connection.prepare(
377 format!("SELECT mod_id, current_version FROM {}", list_id).as_str(), 375 format!("SELECT mod_id, current_version FROM {list_id}").as_str(),
378 )?; 376 )?;
379 let id_iter = stmt.query_map([], |row| { 377 let id_iter = stmt.query_map([], |row| {
380 Ok(vec![ 378 Ok(vec![
@@ -385,7 +383,7 @@ pub fn userlist_get_all_current_versions_with_mods(
385 383
386 for ver in id_iter { 384 for ver in id_iter {
387 let out = ver?; 385 let out = ver?;
388 versions.push((out[0].to_owned(), out[1].to_owned())); 386 versions.push((out[0].clone(), out[1].clone()));
389 } 387 }
390 388
391 if versions.is_empty() { 389 if versions.is_empty() {
@@ -408,7 +406,7 @@ pub fn userlist_get_set_version(
408 406
409 let mut set_version: bool = false; 407 let mut set_version: bool = false;
410 let mut stmt = connection.prepare( 408 let mut stmt = connection.prepare(
411 format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id) 409 format!("SELECT set_version FROM {list_id} WHERE mod_id = ?")
412 .as_str(), 410 .as_str(),
413 )?; 411 )?;
414 let ver_iter = 412 let ver_iter =
@@ -432,7 +430,7 @@ pub fn userlist_change_versions(
432 let data = format!("{}/data.db", config.data); 430 let data = format!("{}/data.db", config.data);
433 let connection = Connection::open(data)?; 431 let connection = Connection::open(data)?;
434 432
435 connection.execute(format!("UPDATE {} SET current_version = ?1, applicable_versions = ?2, current_download = ?3 WHERE mod_id = ?4", list_id).as_str(), [current_version, versions, link, mod_id])?; 433 connection.execute(format!("UPDATE {list_id} SET current_version = ?1, applicable_versions = ?2, current_download = ?3 WHERE mod_id = ?4").as_str(), [current_version, versions, link, mod_id])?;
436 Ok(()) 434 Ok(())
437} 435}
438 436
@@ -453,14 +451,13 @@ pub fn userlist_add_disabled_versions(
453 let disabled_versions = match currently_disabled_versions == "NONE" { 451 let disabled_versions = match currently_disabled_versions == "NONE" {
454 true => disabled_version, 452 true => disabled_version,
455 false => { 453 false => {
456 format!("{}|{}", currently_disabled_versions, disabled_version) 454 format!("{currently_disabled_versions}|{disabled_version}")
457 } 455 }
458 }; 456 };
459 457
460 connection.execute( 458 connection.execute(
461 format!( 459 format!(
462 "UPDATE {} SET disabled_versions = ?1 WHERE mod_id = ?2", 460 "UPDATE {list_id} SET disabled_versions = ?1 WHERE mod_id = ?2"
463 list_id
464 ) 461 )
465 .as_str(), 462 .as_str(),
466 [disabled_versions, mod_id], 463 [disabled_versions, mod_id],
@@ -478,7 +475,7 @@ pub fn userlist_get_disabled_versions(
478 475
479 let mut version: String = String::new(); 476 let mut version: String = String::new();
480 let mut stmt = connection.prepare( 477 let mut stmt = connection.prepare(
481 format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id) 478 format!("SELECT disabled_versions FROM {list_id} WHERE mod_id = ?")
482 .as_str(), 479 .as_str(),
483 )?; 480 )?;
484 let ver_iter = 481 let ver_iter =
@@ -503,13 +500,13 @@ pub fn userlist_get_all_downloads(
503 500
504 let mut links: Vec<String> = Vec::new(); 501 let mut links: Vec<String> = Vec::new();
505 let mut stmt = connection.prepare( 502 let mut stmt = connection.prepare(
506 format!("SELECT current_download FROM {}", list_id).as_str(), 503 format!("SELECT current_download FROM {list_id}").as_str(),
507 )?; 504 )?;
508 let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; 505 let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
509 506
510 for link in link_iter { 507 for link in link_iter {
511 let l = link?; 508 let l = link?;
512 links.push(l) 509 links.push(l);
513 } 510 }
514 511
515 if links.is_empty() { 512 if links.is_empty() {
@@ -538,7 +535,7 @@ pub fn lists_insert(
538 "INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", 535 "INSERT INTO lists VALUES (?1, ?2, ?3, ?4)",
539 [id, mc_version, &mod_loader.to_string(), download_folder], 536 [id, mc_version, &mod_loader.to_string(), download_folder],
540 )?; 537 )?;
541 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(), [])?; 538 connection.execute(format!("CREATE TABLE {id}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT, 'disabled_versions' TEXT DEFAULT 'NONE', 'set_version' INTEGER, CONSTRAINT {id}_PK PRIMARY KEY (mod_id) )").as_str(), [])?;
542 539
543 Ok(()) 540 Ok(())
544} 541}
@@ -548,7 +545,7 @@ pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> {
548 let connection = Connection::open(data)?; 545 let connection = Connection::open(data)?;
549 546
550 connection.execute("DELETE FROM lists WHERE id = ?", [&id])?; 547 connection.execute("DELETE FROM lists WHERE id = ?", [&id])?;
551 connection.execute(format!("DROP TABLE {}", id).as_str(), [])?; 548 connection.execute(format!("DROP TABLE {id}").as_str(), [])?;
552 Ok(()) 549 Ok(())
553} 550}
554 551
@@ -611,7 +608,7 @@ pub fn lists_get_all_ids(config: &Cfg) -> MLE<Vec<String>> {
611 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; 608 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
612 609
613 for id in id_iter { 610 for id in id_iter {
614 list_ids.push(id?) 611 list_ids.push(id?);
615 } 612 }
616 613
617 match list_ids.is_empty() { 614 match list_ids.is_empty() {
@@ -664,8 +661,7 @@ pub fn s_userlist_update_download(
664 661
665 connection.execute( 662 connection.execute(
666 format!( 663 format!(
667 "UPDATE {} SET current_download = ?1 WHERE mod_id = ?2", 664 "UPDATE {list_id} SET current_download = ?1 WHERE mod_id = ?2"
668 list_id
669 ) 665 )
670 .as_str(), 666 .as_str(),
671 [link, mod_id], 667 [link, mod_id],
@@ -734,7 +730,7 @@ pub fn s_insert_column(
734 let data = format!("{}/data.db", config.data); 730 let data = format!("{}/data.db", config.data);
735 let connection = Connection::open(data)?; 731 let connection = Connection::open(data)?;
736 732
737 let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type); 733 let mut sql = format!("ALTER TABLE {table} ADD '{column}' {c_type}");
738 734
739 if default.is_some() { 735 if default.is_some() {
740 sql = format!("{} DEFAULT {}", sql, default.unwrap()); 736 sql = format!("{} DEFAULT {}", sql, default.unwrap());