diff options
author | fxqnlr <[email protected]> | 2023-05-29 18:01:12 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-05-29 18:01:12 +0200 |
commit | c7ecf3019a75dc0ab1a0aefeb9b880899fc8a231 (patch) | |
tree | 116075aaa57c35afca2749719d450c3cb473ab3e /src/db.rs | |
parent | 7755c9acf6b8a1d81c03ef1138e80a162f36e743 (diff) | |
download | modlist-c7ecf3019a75dc0ab1a0aefeb9b880899fc8a231.tar modlist-c7ecf3019a75dc0ab1a0aefeb9b880899fc8a231.tar.gz modlist-c7ecf3019a75dc0ab1a0aefeb9b880899fc8a231.zip |
cargo fmt and add fmt file
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 128 |
1 files changed, 88 insertions, 40 deletions
@@ -21,7 +21,9 @@ pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { | |||
21 | Ok(()) | 21 | Ok(()) |
22 | } | 22 | } |
23 | 23 | ||
24 | pub fn mods_get_all_ids(config: &Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> { | 24 | pub fn mods_get_all_ids( |
25 | config: &Cfg, | ||
26 | ) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
25 | let data = format!("{}/data.db", config.data); | 27 | let data = format!("{}/data.db", config.data); |
26 | let connection = Connection::open(data).unwrap(); | 28 | let connection = Connection::open(data).unwrap(); |
27 | 29 | ||
@@ -64,8 +66,10 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { | |||
64 | } | 66 | } |
65 | //get from id if no slug found | 67 | //get from id if no slug found |
66 | if mod_id.is_empty() { | 68 | if mod_id.is_empty() { |
67 | let mut stmt = connection.prepare("SELECT id FROM mods WHERE id = ?")?; | 69 | let mut stmt = |
68 | let id_iter = stmt.query_map([slug], |row| row.get::<usize, String>(0))?; | 70 | connection.prepare("SELECT id FROM mods WHERE id = ?")?; |
71 | let id_iter = | ||
72 | stmt.query_map([slug], |row| row.get::<usize, String>(0))?; | ||
69 | 73 | ||
70 | for id in id_iter { | 74 | for id in id_iter { |
71 | mod_id = id?; | 75 | mod_id = id?; |
@@ -73,8 +77,10 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { | |||
73 | } | 77 | } |
74 | //get from title if no id found from slug | 78 | //get from title if no id found from slug |
75 | if mod_id.is_empty() { | 79 | if mod_id.is_empty() { |
76 | let mut stmt = connection.prepare("SELECT id FROM mods WHERE title = ?")?; | 80 | let mut stmt = |
77 | let id_iter = stmt.query_map([slug], |row| row.get::<usize, String>(0))?; | 81 | connection.prepare("SELECT id FROM mods WHERE title = ?")?; |
82 | let id_iter = | ||
83 | stmt.query_map([slug], |row| row.get::<usize, String>(0))?; | ||
78 | 84 | ||
79 | for id in id_iter { | 85 | for id in id_iter { |
80 | mod_id = id?; | 86 | mod_id = id?; |
@@ -98,7 +104,8 @@ pub fn mods_get_info(config: &Cfg, id: &str) -> MLE<ModInfo> { | |||
98 | let connection = Connection::open(data)?; | 104 | let connection = Connection::open(data)?; |
99 | 105 | ||
100 | let mut mod_info: Option<ModInfo> = None; | 106 | let mut mod_info: Option<ModInfo> = None; |
101 | let mut stmt = connection.prepare("SELECT title, slug FROM mods WHERE id = ?")?; | 107 | let mut stmt = |
108 | connection.prepare("SELECT title, slug FROM mods WHERE id = ?")?; | ||
102 | let name_iter = stmt.query_map([id], |row| { | 109 | let name_iter = stmt.query_map([id], |row| { |
103 | Ok(vec![ | 110 | Ok(vec![ |
104 | row.get::<usize, String>(0)?, | 111 | row.get::<usize, String>(0)?, |
@@ -135,7 +142,10 @@ pub struct DBModlistVersions { | |||
135 | pub versions: String, | 142 | pub versions: String, |
136 | } | 143 | } |
137 | 144 | ||
138 | pub fn mods_get_versions(config: &Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVersions>> { | 145 | pub fn mods_get_versions( |
146 | config: &Cfg, | ||
147 | mods: Vec<String>, | ||
148 | ) -> MLE<Vec<DBModlistVersions>> { | ||
139 | let data = format!("{}/data.db", config.data); | 149 | let data = format!("{}/data.db", config.data); |
140 | let connection = Connection::open(data)?; | 150 | let connection = Connection::open(data)?; |
141 | 151 | ||
@@ -153,8 +163,9 @@ pub fn mods_get_versions(config: &Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVe | |||
153 | } | 163 | } |
154 | 164 | ||
155 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); | 165 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); |
156 | let mut stmt = connection | 166 | let mut stmt = connection.prepare( |
157 | .prepare(format!("SELECT id, versions, title FROM mods {}", wherestr).as_str())?; | 167 | format!("SELECT id, versions, title FROM mods {}", wherestr).as_str(), |
168 | )?; | ||
158 | let id_iter = stmt.query_map([], |row| { | 169 | let id_iter = stmt.query_map([], |row| { |
159 | Ok(vec![ | 170 | Ok(vec![ |
160 | row.get::<usize, String>(0)?, | 171 | row.get::<usize, String>(0)?, |
@@ -218,7 +229,8 @@ pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE<Vec<String>> { | |||
218 | let connection = Connection::open(data).unwrap(); | 229 | let connection = Connection::open(data).unwrap(); |
219 | 230 | ||
220 | let mut mod_ids: Vec<String> = Vec::new(); | 231 | let mut mod_ids: Vec<String> = Vec::new(); |
221 | let mut stmt = connection.prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?; | 232 | let mut stmt = connection |
233 | .prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?; | ||
222 | let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; | 234 | let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; |
223 | 235 | ||
224 | for id in id_iter { | 236 | for id in id_iter { |
@@ -261,7 +273,8 @@ pub fn userlist_get_applicable_versions( | |||
261 | ) | 273 | ) |
262 | .as_str(), | 274 | .as_str(), |
263 | )?; | 275 | )?; |
264 | let ver_iter = stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?; | 276 | let ver_iter = |
277 | stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?; | ||
265 | 278 | ||
266 | for ver in ver_iter { | 279 | for ver in ver_iter { |
267 | version = ver?; | 280 | version = ver?; |
@@ -281,8 +294,9 @@ pub fn userlist_get_all_applicable_versions_with_mods( | |||
281 | let connection = Connection::open(data)?; | 294 | let connection = Connection::open(data)?; |
282 | 295 | ||
283 | let mut versions: Vec<(String, String)> = Vec::new(); | 296 | let mut versions: Vec<(String, String)> = Vec::new(); |
284 | let mut stmt = connection | 297 | let mut stmt = connection.prepare( |
285 | .prepare(format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str())?; | 298 | format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str(), |
299 | )?; | ||
286 | let id_iter = stmt.query_map([], |row| { | 300 | let id_iter = stmt.query_map([], |row| { |
287 | Ok(vec![ | 301 | Ok(vec![ |
288 | row.get::<usize, String>(0)?, | 302 | row.get::<usize, String>(0)?, |
@@ -302,14 +316,21 @@ pub fn userlist_get_all_applicable_versions_with_mods( | |||
302 | Ok(versions) | 316 | Ok(versions) |
303 | } | 317 | } |
304 | 318 | ||
305 | pub fn userlist_get_current_version(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<String> { | 319 | pub fn userlist_get_current_version( |
320 | config: &Cfg, | ||
321 | list_id: &str, | ||
322 | mod_id: &str, | ||
323 | ) -> MLE<String> { | ||
306 | let data = format!("{}/data.db", config.data); | 324 | let data = format!("{}/data.db", config.data); |
307 | let connection = Connection::open(data).unwrap(); | 325 | let connection = Connection::open(data).unwrap(); |
308 | 326 | ||
309 | let mut version: String = String::new(); | 327 | let mut version: String = String::new(); |
310 | let mut stmt = connection | 328 | let mut stmt = connection.prepare( |
311 | .prepare(format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id).as_str())?; | 329 | format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id) |
312 | let ver_iter = stmt.query_map([&mod_id], |row| row.get::<usize, String>(0))?; | 330 | .as_str(), |
331 | )?; | ||
332 | let ver_iter = | ||
333 | stmt.query_map([&mod_id], |row| row.get::<usize, String>(0))?; | ||
313 | 334 | ||
314 | for ver in ver_iter { | 335 | for ver in ver_iter { |
315 | version = ver?; | 336 | version = ver?; |
@@ -321,13 +342,16 @@ pub fn userlist_get_current_version(config: &Cfg, list_id: &str, mod_id: &str) - | |||
321 | } | 342 | } |
322 | } | 343 | } |
323 | 344 | ||
324 | pub fn userlist_get_all_current_version_ids(config: &Cfg, list_id: String) -> MLE<Vec<String>> { | 345 | pub fn userlist_get_all_current_version_ids( |
346 | config: &Cfg, | ||
347 | list_id: String, | ||
348 | ) -> MLE<Vec<String>> { | ||
325 | let data = format!("{}/data.db", config.data); | 349 | let data = format!("{}/data.db", config.data); |
326 | let connection = Connection::open(data)?; | 350 | let connection = Connection::open(data)?; |
327 | 351 | ||
328 | let mut versions: Vec<String> = Vec::new(); | 352 | let mut versions: Vec<String> = Vec::new(); |
329 | let mut stmt = | 353 | let mut stmt = connection |
330 | connection.prepare(format!("SELECT current_version FROM {}", list_id).as_str())?; | 354 | .prepare(format!("SELECT current_version FROM {}", list_id).as_str())?; |
331 | let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; | 355 | let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; |
332 | 356 | ||
333 | for id in id_iter { | 357 | for id in id_iter { |
@@ -349,8 +373,9 @@ pub fn userlist_get_all_current_versions_with_mods( | |||
349 | let connection = Connection::open(data)?; | 373 | let connection = Connection::open(data)?; |
350 | 374 | ||
351 | let mut versions: Vec<(String, String)> = Vec::new(); | 375 | let mut versions: Vec<(String, String)> = Vec::new(); |
352 | let mut stmt = | 376 | let mut stmt = connection.prepare( |
353 | connection.prepare(format!("SELECT mod_id, current_version FROM {}", list_id).as_str())?; | 377 | format!("SELECT mod_id, current_version FROM {}", list_id).as_str(), |
378 | )?; | ||
354 | let id_iter = stmt.query_map([], |row| { | 379 | let id_iter = stmt.query_map([], |row| { |
355 | Ok(vec![ | 380 | Ok(vec![ |
356 | row.get::<usize, String>(0)?, | 381 | row.get::<usize, String>(0)?, |
@@ -373,14 +398,21 @@ pub fn userlist_get_all_current_versions_with_mods( | |||
373 | Ok(versions) | 398 | Ok(versions) |
374 | } | 399 | } |
375 | 400 | ||
376 | pub fn userlist_get_set_version(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<bool> { | 401 | pub fn userlist_get_set_version( |
402 | config: &Cfg, | ||
403 | list_id: &str, | ||
404 | mod_id: &str, | ||
405 | ) -> MLE<bool> { | ||
377 | let data = format!("{}/data.db", config.data); | 406 | let data = format!("{}/data.db", config.data); |
378 | let connection = Connection::open(data).unwrap(); | 407 | let connection = Connection::open(data).unwrap(); |
379 | 408 | ||
380 | let mut set_version: bool = false; | 409 | let mut set_version: bool = false; |
381 | let mut stmt = connection | 410 | let mut stmt = connection.prepare( |
382 | .prepare(format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id).as_str())?; | 411 | format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id) |
383 | let ver_iter = stmt.query_map([&mod_id], |row| row.get::<usize, bool>(0))?; | 412 | .as_str(), |
413 | )?; | ||
414 | let ver_iter = | ||
415 | stmt.query_map([&mod_id], |row| row.get::<usize, bool>(0))?; | ||
384 | 416 | ||
385 | for ver in ver_iter { | 417 | for ver in ver_iter { |
386 | set_version = ver?; | 418 | set_version = ver?; |
@@ -413,11 +445,16 @@ pub fn userlist_add_disabled_versions( | |||
413 | let data = format!("{}/data.db", config.data); | 445 | let data = format!("{}/data.db", config.data); |
414 | let connection = Connection::open(data)?; | 446 | let connection = Connection::open(data)?; |
415 | 447 | ||
416 | let currently_disabled_versions = | 448 | let currently_disabled_versions = userlist_get_disabled_versions( |
417 | userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?; | 449 | config, |
450 | String::from(&list_id), | ||
451 | String::from(&mod_id), | ||
452 | )?; | ||
418 | let disabled_versions = match currently_disabled_versions == "NONE" { | 453 | let disabled_versions = match currently_disabled_versions == "NONE" { |
419 | true => disabled_version, | 454 | true => disabled_version, |
420 | false => format!("{}|{}", currently_disabled_versions, disabled_version), | 455 | false => { |
456 | format!("{}|{}", currently_disabled_versions, disabled_version) | ||
457 | } | ||
421 | }; | 458 | }; |
422 | 459 | ||
423 | connection.execute( | 460 | connection.execute( |
@@ -440,9 +477,12 @@ pub fn userlist_get_disabled_versions( | |||
440 | let connection = Connection::open(data).unwrap(); | 477 | let connection = Connection::open(data).unwrap(); |
441 | 478 | ||
442 | let mut version: String = String::new(); | 479 | let mut version: String = String::new(); |
443 | let mut stmt = connection | 480 | let mut stmt = connection.prepare( |
444 | .prepare(format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id).as_str())?; | 481 | format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id) |
445 | let ver_iter = stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?; | 482 | .as_str(), |
483 | )?; | ||
484 | let ver_iter = | ||
485 | stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?; | ||
446 | 486 | ||
447 | for ver in ver_iter { | 487 | for ver in ver_iter { |
448 | version = ver?; | 488 | version = ver?; |
@@ -462,8 +502,9 @@ pub fn userlist_get_all_downloads( | |||
462 | let connection = Connection::open(data).unwrap(); | 502 | let connection = Connection::open(data).unwrap(); |
463 | 503 | ||
464 | let mut links: Vec<String> = Vec::new(); | 504 | let mut links: Vec<String> = Vec::new(); |
465 | let mut stmt = | 505 | let mut stmt = connection.prepare( |
466 | connection.prepare(format!("SELECT current_download FROM {}", list_id).as_str())?; | 506 | format!("SELECT current_download FROM {}", list_id).as_str(), |
507 | )?; | ||
467 | let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; | 508 | let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; |
468 | 509 | ||
469 | for link in link_iter { | 510 | for link in link_iter { |
@@ -521,8 +562,9 @@ pub fn lists_get(config: &Cfg, list_id: &str) -> MLE<List> { | |||
521 | modloader: Modloader::Fabric, | 562 | modloader: Modloader::Fabric, |
522 | download_folder: String::new(), | 563 | download_folder: String::new(), |
523 | }; | 564 | }; |
524 | let mut stmt = connection | 565 | let mut stmt = connection.prepare( |
525 | .prepare("SELECT mc_version, modloader, download_folder FROM lists WHERE id = ?")?; | 566 | "SELECT mc_version, modloader, download_folder FROM lists WHERE id = ?", |
567 | )?; | ||
526 | 568 | ||
527 | let list_iter = stmt.query_map([&list_id], |row| { | 569 | let list_iter = stmt.query_map([&list_id], |row| { |
528 | Ok(vec![ | 570 | Ok(vec![ |
@@ -595,7 +637,8 @@ pub fn config_get_current_list(config: &Cfg) -> MLE<String> { | |||
595 | let connection = Connection::open(data).unwrap(); | 637 | let connection = Connection::open(data).unwrap(); |
596 | 638 | ||
597 | let mut list_id = String::new(); | 639 | let mut list_id = String::new(); |
598 | let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'current_list'")?; | 640 | let mut stmt = connection |
641 | .prepare("SELECT value FROM user_config WHERE id = 'current_list'")?; | ||
599 | let list_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; | 642 | let list_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; |
600 | 643 | ||
601 | for list in list_iter { | 644 | for list in list_iter { |
@@ -630,7 +673,9 @@ pub fn s_userlist_update_download( | |||
630 | Ok(()) | 673 | Ok(()) |
631 | } | 674 | } |
632 | 675 | ||
633 | pub fn s_config_create_version(config: &Cfg) -> Result<(), Box<dyn std::error::Error>> { | 676 | pub fn s_config_create_version( |
677 | config: &Cfg, | ||
678 | ) -> Result<(), Box<dyn std::error::Error>> { | ||
634 | let data = format!("{}/data.db", config.data); | 679 | let data = format!("{}/data.db", config.data); |
635 | let connection = Connection::open(data)?; | 680 | let connection = Connection::open(data)?; |
636 | 681 | ||
@@ -655,12 +700,15 @@ pub fn s_config_update_version( | |||
655 | Ok(()) | 700 | Ok(()) |
656 | } | 701 | } |
657 | 702 | ||
658 | pub fn s_config_get_version(config: &Cfg) -> Result<String, Box<dyn std::error::Error>> { | 703 | pub fn s_config_get_version( |
704 | config: &Cfg, | ||
705 | ) -> Result<String, Box<dyn std::error::Error>> { | ||
659 | let data = format!("{}/data.db", config.data); | 706 | let data = format!("{}/data.db", config.data); |
660 | let connection = Connection::open(data)?; | 707 | let connection = Connection::open(data)?; |
661 | 708 | ||
662 | let mut version: String = String::new(); | 709 | let mut version: String = String::new(); |
663 | let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'db_version'")?; | 710 | let mut stmt = connection |
711 | .prepare("SELECT value FROM user_config WHERE id = 'db_version'")?; | ||
664 | let ver_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; | 712 | let ver_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?; |
665 | 713 | ||
666 | for ver in ver_iter { | 714 | for ver in ver_iter { |