diff options
author | fxqnlr <[email protected]> | 2022-11-28 22:55:14 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-11-28 22:55:14 +0100 |
commit | ddde9204c72dd867f920f07f6483be03dda7cf68 (patch) | |
tree | 51ee1140311be4a82a7832bcef2772db7fd4e639 /src/db.rs | |
parent | d8cb7bc5f9c2e01c82f954427a60da6eaf0610ca (diff) | |
download | modlist-ddde9204c72dd867f920f07f6483be03dda7cf68.tar modlist-ddde9204c72dd867f920f07f6483be03dda7cf68.tar.gz modlist-ddde9204c72dd867f920f07f6483be03dda7cf68.zip |
basically update impl; added "good" download;
auto dl on new mod; db to 0.4; etc
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 114 |
1 files changed, 103 insertions, 11 deletions
@@ -32,7 +32,7 @@ pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error:: | |||
32 | })?; | 32 | })?; |
33 | 33 | ||
34 | for id in id_iter { | 34 | for id in id_iter { |
35 | println!("Found id {:?}", id.as_ref().unwrap()); | 35 | //println!("Found id {:?}", id.as_ref().unwrap()); |
36 | mods.push(id?); | 36 | mods.push(id?); |
37 | } | 37 | } |
38 | 38 | ||
@@ -53,7 +53,7 @@ pub fn mods_get_id(config: Cfg, name: String) -> Result<String, Box<dyn std::err | |||
53 | })?; | 53 | })?; |
54 | 54 | ||
55 | for id in id_iter { | 55 | for id in id_iter { |
56 | println!("Found id {:?}", id.as_ref().unwrap()); | 56 | //println!("Found id {:?}", id.as_ref().unwrap()); |
57 | mod_id = id?; | 57 | mod_id = id?; |
58 | }; | 58 | }; |
59 | 59 | ||
@@ -63,6 +63,38 @@ pub fn mods_get_id(config: Cfg, name: String) -> Result<String, Box<dyn std::err | |||
63 | } | 63 | } |
64 | } | 64 | } |
65 | 65 | ||
66 | pub fn mods_get_name(config: Cfg, id: String) -> Result<String, Box<dyn std::error::Error>> { | ||
67 | let data = format!("{}/data.db", config.data); | ||
68 | let connection = Connection::open(data)?; | ||
69 | |||
70 | let mut mod_name = String::new(); | ||
71 | let mut stmt = connection.prepare("SELECT name FROM mods WHERE id = ?")?; | ||
72 | let name_iter = stmt.query_map([id], |row| { | ||
73 | row.get::<usize, String>(0) | ||
74 | })?; | ||
75 | |||
76 | for name in name_iter { | ||
77 | //println!("Found id {:?}", id.as_ref().unwrap()); | ||
78 | mod_name = name?; | ||
79 | }; | ||
80 | |||
81 | match mod_name.is_empty() { | ||
82 | true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), | ||
83 | false => Ok(mod_name), | ||
84 | } | ||
85 | } | ||
86 | |||
87 | pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { | ||
88 | |||
89 | println!("Updating versions for {} with \n {}", mod_id, versions); | ||
90 | |||
91 | let data = format!("{}/data.db", config.data); | ||
92 | let connection = Connection::open(data)?; | ||
93 | |||
94 | connection.execute("UPDATE mods SET versions = ?1 WHERE id = ?2", [versions, mod_id])?; | ||
95 | Ok(()) | ||
96 | } | ||
97 | |||
66 | pub fn mods_remove(config: Cfg, id: String) -> Result<(), Box<dyn std::error::Error>> { | 98 | pub fn mods_remove(config: Cfg, id: String) -> Result<(), Box<dyn std::error::Error>> { |
67 | 99 | ||
68 | println!("Removing mod {} from database", id); | 100 | println!("Removing mod {} from database", id); |
@@ -95,14 +127,15 @@ pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> Result<Vec<DBModlist | |||
95 | } | 127 | } |
96 | 128 | ||
97 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); | 129 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); |
98 | let mut stmt = connection.prepare(dbg!(format!("SELECT id, versions FROM mods {}", wherestr).as_str()))?; | 130 | let mut stmt = connection.prepare(format!("SELECT id, versions, name FROM mods {}", wherestr).as_str())?; |
99 | let id_iter = stmt.query_map([], |row| { | 131 | let id_iter = stmt.query_map([], |row| { |
100 | Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?]) | 132 | Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?, row.get::<usize, String>(2)?]) |
101 | })?; | 133 | })?; |
102 | 134 | ||
103 | for ver in id_iter { | 135 | for ver in id_iter { |
104 | let version = ver?; | 136 | let version = ver?; |
105 | println!("Found versions {} for mod {}", version[1], version[0]); | 137 | println!("Getting versions for {} from the database", String::from(&version[2])); |
138 | //println!("Found versions {} for mod {}", version[1], version[0]); | ||
106 | versionmaps.push(DBModlistVersions { mod_id: String::from(&version[0]), versions: String::from(&version[1]) }) | 139 | versionmaps.push(DBModlistVersions { mod_id: String::from(&version[0]), versions: String::from(&version[1]) }) |
107 | }; | 140 | }; |
108 | 141 | ||
@@ -120,7 +153,7 @@ pub fn userlist_insert(config: Cfg, list_id: String, mod_id: String, current_ver | |||
120 | let connection = Connection::open(data)?; | 153 | let connection = Connection::open(data)?; |
121 | 154 | ||
122 | 155 | ||
123 | connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4)", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|"), current_link])?; | 156 | connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE')", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|"), current_link])?; |
124 | 157 | ||
125 | Ok(()) | 158 | Ok(()) |
126 | } | 159 | } |
@@ -136,7 +169,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result<Vec<String>, | |||
136 | })?; | 169 | })?; |
137 | 170 | ||
138 | for id in id_iter { | 171 | for id in id_iter { |
139 | println!("Found id {:?}", id.as_ref().unwrap()); | 172 | //println!("Found id {:?}", id.as_ref().unwrap()); |
140 | mod_ids.push(id?) | 173 | mod_ids.push(id?) |
141 | }; | 174 | }; |
142 | 175 | ||
@@ -167,7 +200,26 @@ pub fn userlist_get_applicable_versions(config: Cfg, list_id: String, mod_id: St | |||
167 | })?; | 200 | })?; |
168 | 201 | ||
169 | for ver in ver_iter { | 202 | for ver in ver_iter { |
170 | println!("Found id {:?}", ver); | 203 | version = ver?; |
204 | }; | ||
205 | |||
206 | match version.is_empty() { | ||
207 | true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), | ||
208 | false => Ok(version), | ||
209 | } | ||
210 | } | ||
211 | |||
212 | pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { | ||
213 | let data = format!("{}/data.db", config.data); | ||
214 | let connection = Connection::open(data).unwrap(); | ||
215 | |||
216 | let mut version: String = String::new(); | ||
217 | let mut stmt = connection.prepare(format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id).as_str())?; | ||
218 | let ver_iter = stmt.query_map([&mod_id], |row| { | ||
219 | row.get::<usize, String>(0) | ||
220 | })?; | ||
221 | |||
222 | for ver in ver_iter { | ||
171 | version = ver?; | 223 | version = ver?; |
172 | }; | 224 | }; |
173 | 225 | ||
@@ -204,6 +256,40 @@ pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: S | |||
204 | Ok(()) | 256 | Ok(()) |
205 | } | 257 | } |
206 | 258 | ||
259 | pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { | ||
260 | let data = format!("{}/data.db", config.data); | ||
261 | let connection = Connection::open(data)?; | ||
262 | |||
263 | let currently_disabled_versions = userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?; | ||
264 | let disabled_versions = match currently_disabled_versions == "NONE" { | ||
265 | true => disabled_version, | ||
266 | false => format!("{}|{}", currently_disabled_versions, disabled_version), | ||
267 | }; | ||
268 | |||
269 | connection.execute(format!("UPDATE {} SET disabled_versions = ?1 WHERE mod_id = ?2", list_id).as_str(), [disabled_versions, mod_id])?; | ||
270 | Ok(()) | ||
271 | } | ||
272 | |||
273 | pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { | ||
274 | let data = format!("{}/data.db", config.data); | ||
275 | let connection = Connection::open(data).unwrap(); | ||
276 | |||
277 | let mut version: String = String::new(); | ||
278 | let mut stmt = connection.prepare(format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id).as_str())?; | ||
279 | let ver_iter = stmt.query_map([mod_id], |row| { | ||
280 | row.get::<usize, String>(0) | ||
281 | })?; | ||
282 | |||
283 | for ver in ver_iter { | ||
284 | version = ver?; | ||
285 | }; | ||
286 | |||
287 | match version.is_empty() { | ||
288 | true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), | ||
289 | false => Ok(version), | ||
290 | } | ||
291 | } | ||
292 | |||
207 | pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result<Vec<String>, Box<dyn std::error::Error>> { | 293 | pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result<Vec<String>, Box<dyn std::error::Error>> { |
208 | let data = format!("{}/data.db", config.data); | 294 | let data = format!("{}/data.db", config.data); |
209 | let connection = Connection::open(data).unwrap(); | 295 | let connection = Connection::open(data).unwrap(); |
@@ -233,7 +319,7 @@ pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Mod | |||
233 | let connection = Connection::open(data)?; | 319 | let connection = Connection::open(data)?; |
234 | 320 | ||
235 | connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.stringify(), download_folder])?; | 321 | connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.stringify(), download_folder])?; |
236 | connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT )", id).as_str(), [])?; | 322 | 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(), [])?; |
237 | 323 | ||
238 | Ok(()) | 324 | Ok(()) |
239 | } | 325 | } |
@@ -360,11 +446,17 @@ pub fn s_config_get_version(config: Cfg) -> Result<String, Box<dyn std::error::E | |||
360 | Ok(version) | 446 | Ok(version) |
361 | } | 447 | } |
362 | 448 | ||
363 | pub fn s_insert_column(config: Cfg, table: String, column: String, c_type: String) -> Result<(), Box<dyn std::error::Error>> { | 449 | pub fn s_insert_column(config: Cfg, table: String, column: String, c_type: String, default: Option<String>) -> Result<(), Box<dyn std::error::Error>> { |
364 | let data = format!("{}/data.db", config.data); | 450 | let data = format!("{}/data.db", config.data); |
365 | let connection = Connection::open(data)?; | 451 | let connection = Connection::open(data)?; |
366 | 452 | ||
367 | connection.execute(format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type).as_str(), ())?; | 453 | let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type); |
454 | |||
455 | if default.is_some() { | ||
456 | sql = format!("{} DEFAULT {}", sql, default.unwrap()); | ||
457 | } | ||
458 | |||
459 | connection.execute(sql.as_str(), ())?; | ||
368 | Ok(()) | 460 | Ok(()) |
369 | } | 461 | } |
370 | 462 | ||