diff options
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 87 |
1 files changed, 85 insertions, 2 deletions
@@ -1,4 +1,8 @@ | |||
1 | use crate::Modloader; | 1 | use std::io::ErrorKind; |
2 | |||
3 | use crate::{Modloader, config::Cfg}; | ||
4 | |||
5 | //TODO use prepared statements | ||
2 | 6 | ||
3 | pub fn insert_mod(id: String, name: String, current_version: String, old_versions: Vec<String>, mod_loader: Modloader, desired_mc_version: String) -> Result<(), sqlite::Error> { | 7 | pub fn insert_mod(id: String, name: String, current_version: String, old_versions: Vec<String>, mod_loader: Modloader, desired_mc_version: String) -> Result<(), sqlite::Error> { |
4 | 8 | ||
@@ -10,9 +14,88 @@ pub fn insert_mod(id: String, name: String, current_version: String, old_version | |||
10 | }; | 14 | }; |
11 | 15 | ||
12 | let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}', '{}', '{}', '{}')", id, name, current_version, old_versions.join("|"), loader, desired_mc_version); | 16 | let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}', '{}', '{}', '{}')", id, name, current_version, old_versions.join("|"), loader, desired_mc_version); |
17 | |||
18 | connection.execute(sql) | ||
19 | } | ||
20 | |||
21 | //LIST | ||
22 | pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modloader) -> Result<(), sqlite::Error> { | ||
23 | let data = format!("{}/data.db", config.data); | ||
24 | let connection = sqlite::open(data).unwrap(); | ||
13 | 25 | ||
14 | dbg!(&sql); | 26 | //Setup list in table |
27 | let loader = match mod_loader { | ||
28 | Modloader::Fabric => "fabric", | ||
29 | Modloader::Forge => "forge", | ||
30 | }; | ||
31 | |||
32 | let sql_list = format!("INSERT INTO lists VALUES ('{}', '{}', '{}')", id, mc_version, loader); | ||
33 | let sql_table = format!("CREATE TABLE '{}' ( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'mod_loader' TEXT )", id); | ||
34 | let sql = format!("{};{};", sql_list, sql_table); | ||
15 | 35 | ||
16 | connection.execute(sql) | 36 | connection.execute(sql) |
37 | } | ||
17 | 38 | ||
39 | pub fn remove_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { | ||
40 | let data = format!("{}/data.db", config.data); | ||
41 | let connection = sqlite::open(data).unwrap(); | ||
42 | |||
43 | let sql_list = format!("DELETE FROM lists WHERE id = '{}'", id); | ||
44 | let sql_table = format!("DROP TABLE '{}'", id); | ||
45 | let sql = format!("{};{};", sql_list, sql_table); | ||
46 | |||
47 | connection.execute(sql) | ||
48 | } | ||
49 | |||
50 | pub fn get_lists(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
51 | let data = format!("{}/data.db", config.data); | ||
52 | let connection = sqlite::open(data).unwrap(); | ||
53 | |||
54 | let sql = "SELECT id FROM lists"; | ||
55 | |||
56 | let mut list: Vec<String> = Vec::new(); | ||
57 | //TODO catch sql errors better | ||
58 | connection.iterate(sql, |ids| { | ||
59 | if ids.is_empty() { return false; }; | ||
60 | for &(_column, value) in ids.iter() { | ||
61 | list.push(String::from(value.unwrap())); | ||
62 | } | ||
63 | true | ||
64 | }).unwrap(); | ||
65 | match list.is_empty() { | ||
66 | true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_LISTS"))), | ||
67 | false => Ok(list), | ||
68 | } | ||
69 | } | ||
70 | |||
71 | //config | ||
72 | pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { | ||
73 | let data = format!("{}/data.db", config.data); | ||
74 | let connection = sqlite::open(data).unwrap(); | ||
75 | |||
76 | let sql = format!("UPDATE user_config SET value = '{}' WHERE id = 'current_list'", id); | ||
77 | |||
78 | connection.execute(sql) | ||
79 | } | ||
80 | |||
81 | pub fn get_current_list(config: Cfg) -> Result<String, Box<dyn std::error::Error>> { | ||
82 | let data = format!("{}/data.db", config.data); | ||
83 | let connection = sqlite::open(data).unwrap(); | ||
84 | |||
85 | let sql = "SELECT id FROM lists"; | ||
86 | |||
87 | let mut list: String = String::new(); | ||
88 | //TODO catch sql errors better | ||
89 | connection.iterate(sql, |ids| { | ||
90 | if ids.is_empty() { return false; }; | ||
91 | for &(_column, value) in ids.iter() { | ||
92 | list = String::from(value.unwrap()); | ||
93 | } | ||
94 | true | ||
95 | }).unwrap(); | ||
96 | if list.is_empty() { | ||
97 | get_lists(config)?; | ||
98 | panic!("current list field should never be empty if there are other lists"); | ||
99 | }; | ||
100 | Ok(list) | ||
18 | } | 101 | } |