summaryrefslogtreecommitdiff
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs423
1 files changed, 292 insertions, 131 deletions
diff --git a/src/db.rs b/src/db.rs
index 2c48cab..09d54c2 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -2,17 +2,21 @@ use std::io::{Error, ErrorKind};
2 2
3use rusqlite::Connection; 3use rusqlite::Connection;
4 4
5use crate::{Modloader, config::Cfg, List, devdir, error::{MLE, MLError, ErrorType}}; 5use crate::{
6 config::Cfg,
7 devdir,
8 error::{ErrorType, MLError, MLE},
9 List, Modloader,
10};
6 11
7//MODS 12//MODS
8pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { 13pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> {
9
10 let data = devdir(format!("{}/data.db", config.data).as_str()); 14 let data = devdir(format!("{}/data.db", config.data).as_str());
11 let connection = Connection::open(data)?; 15 let connection = Connection::open(data)?;
12 16
13 connection.execute( 17 connection.execute(
14 "INSERT INTO mods (id, slug, title) VALUES (?1, ?2, ?3)", 18 "INSERT INTO mods (id, slug, title) VALUES (?1, ?2, ?3)",
15 [id, slug, name.replace('\'', "").as_str()] 19 [id, slug, name.replace('\'', "").as_str()],
16 )?; 20 )?;
17 21
18 Ok(()) 22 Ok(())
@@ -21,13 +25,11 @@ pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> {
21pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> { 25pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> {
22 let data = devdir(format!("{}/data.db", config.data).as_str()); 26 let data = devdir(format!("{}/data.db", config.data).as_str());
23 let connection = Connection::open(data).unwrap(); 27 let connection = Connection::open(data).unwrap();
24 28
25 let mut mods: Vec<String> = Vec::new(); 29 let mut mods: Vec<String> = Vec::new();
26 30
27 let mut stmt = connection.prepare("SELECT id FROM mods")?; 31 let mut stmt = connection.prepare("SELECT id FROM mods")?;
28 let id_iter = stmt.query_map([], |row| { 32 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
29 row.get::<usize, String>(0)
30 })?;
31 33
32 for id in id_iter { 34 for id in id_iter {
33 mods.push(id?); 35 mods.push(id?);
@@ -49,36 +51,33 @@ pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::
49/// 51///
50///Will return `MLError` when no mod id is found 52///Will return `MLError` when no mod id is found
51pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { 53pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> {
52
53 //TODO check if "slug" is id 54 //TODO check if "slug" is id
54 55
55 let data = devdir(format!("{}/data.db", data).as_str()); 56 let data = devdir(format!("{}/data.db", data).as_str());
56 let connection = Connection::open(data)?; 57 let connection = Connection::open(data)?;
57 58
58 let mut mod_id = String::new(); 59 let mut mod_id = String::new();
59 60
60 //get from slug 61 //get from slug
61 let mut stmt = connection.prepare("SELECT id FROM mods WHERE slug = ?")?; 62 let mut stmt = connection.prepare("SELECT id FROM mods WHERE slug = ?")?;
62 let id_iter = stmt.query_map([slug], |row| { 63 let id_iter = stmt.query_map([slug], |row| row.get::<usize, String>(0))?;
63 row.get::<usize, String>(0)
64 })?;
65 64
66 for id in id_iter { 65 for id in id_iter {
67 mod_id = id?; 66 mod_id = id?;
68 }; 67 }
69 //get from title if no id found from slug 68 //get from title if no id found from slug
70 if mod_id.is_empty() { 69 if mod_id.is_empty() {
71 let mut stmt = connection.prepare("SELECT id FROM mods WHERE title = ?")?; 70 let mut stmt = connection.prepare("SELECT id FROM mods WHERE title = ?")?;
72 let id_iter = stmt.query_map([slug], |row| { 71 let id_iter = stmt.query_map([slug], |row| row.get::<usize, String>(0))?;
73 row.get::<usize, String>(0)
74 })?;
75 72
76 for id in id_iter { 73 for id in id_iter {
77 mod_id = id?; 74 mod_id = id?;
78 }; 75 }
79 } 76 }
80 77
81 if mod_id.is_empty() { return Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND")) }; 78 if mod_id.is_empty() {
79 return Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND"));
80 };
82 81
83 Ok(mod_id) 82 Ok(mod_id)
84} 83}
@@ -91,17 +90,23 @@ pub struct ModInfo {
91pub fn mods_get_info(config: Cfg, id: &str) -> MLE<ModInfo> { 90pub fn mods_get_info(config: Cfg, id: &str) -> MLE<ModInfo> {
92 let data = devdir(format!("{}/data.db", config.data).as_str()); 91 let data = devdir(format!("{}/data.db", config.data).as_str());
93 let connection = Connection::open(data)?; 92 let connection = Connection::open(data)?;
94 93
95 let mut mod_info: Option<ModInfo> = None; 94 let mut mod_info: Option<ModInfo> = None;
96 let mut stmt = connection.prepare("SELECT title, slug FROM mods WHERE id = ?")?; 95 let mut stmt = connection.prepare("SELECT title, slug FROM mods WHERE id = ?")?;
97 let name_iter = stmt.query_map([id], |row| { 96 let name_iter = stmt.query_map([id], |row| {
98 Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?]) 97 Ok(vec![
98 row.get::<usize, String>(0)?,
99 row.get::<usize, String>(1)?,
100 ])
99 })?; 101 })?;
100 102
101 for info in name_iter { 103 for info in name_iter {
102 let i = info?; 104 let i = info?;
103 mod_info = Some(ModInfo { title: String::from(&i[0]), slug: String::from(&i[1]) }); 105 mod_info = Some(ModInfo {
104 }; 106 title: String::from(&i[0]),
107 slug: String::from(&i[1]),
108 });
109 }
105 110
106 match mod_info.is_none() { 111 match mod_info.is_none() {
107 true => Err(MLError::new(ErrorType::DBError, "GN_MOD_NOT_FOUND")), 112 true => Err(MLError::new(ErrorType::DBError, "GN_MOD_NOT_FOUND")),
@@ -110,7 +115,6 @@ pub fn mods_get_info(config: Cfg, id: &str) -> MLE<ModInfo> {
110} 115}
111 116
112pub fn mods_remove(config: Cfg, id: String) -> MLE<()> { 117pub fn mods_remove(config: Cfg, id: String) -> MLE<()> {
113
114 println!("Removing mod {} from database", id); 118 println!("Removing mod {} from database", id);
115 119
116 let data = devdir(format!("{}/data.db", config.data).as_str()); 120 let data = devdir(format!("{}/data.db", config.data).as_str());
@@ -131,27 +135,42 @@ pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVer
131 let data = devdir(format!("{}/data.db", config.data).as_str()); 135 let data = devdir(format!("{}/data.db", config.data).as_str());
132 let connection = Connection::open(data)?; 136 let connection = Connection::open(data)?;
133 137
134 if mods.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_INPUT")); } 138 if mods.is_empty() {
139 return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_INPUT"));
140 }
135 141
136 let mut wherestr = String::from("WHERE"); 142 let mut wherestr = String::from("WHERE");
137 for (i, id) in mods.iter().enumerate() { 143 for (i, id) in mods.iter().enumerate() {
138 let mut or = " OR"; 144 let mut or = " OR";
139 if i == mods.len() - 1 { or = "" }; 145 if i == mods.len() - 1 {
146 or = ""
147 };
140 wherestr = format!("{} id = '{}'{}", wherestr, id, or); 148 wherestr = format!("{} id = '{}'{}", wherestr, id, or);
141 } 149 }
142 150
143 let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); 151 let mut versionmaps: Vec<DBModlistVersions> = Vec::new();
144 let mut stmt = connection.prepare(format!("SELECT id, versions, title FROM mods {}", wherestr).as_str())?; 152 let mut stmt = connection
153 .prepare(format!("SELECT id, versions, title FROM mods {}", wherestr).as_str())?;
145 let id_iter = stmt.query_map([], |row| { 154 let id_iter = stmt.query_map([], |row| {
146 Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?, row.get::<usize, String>(2)?]) 155 Ok(vec![
156 row.get::<usize, String>(0)?,
157 row.get::<usize, String>(1)?,
158 row.get::<usize, String>(2)?,
159 ])
147 })?; 160 })?;
148 161
149 for ver in id_iter { 162 for ver in id_iter {
150 let version = ver?; 163 let version = ver?;
151 println!("\t({}) Get versions from the database", String::from(&version[2])); 164 println!(
165 "\t({}) Get versions from the database",
166 String::from(&version[2])
167 );
152 //println!("Found versions {} for mod {}", version[1], version[0]); 168 //println!("Found versions {} for mod {}", version[1], version[0]);
153 versionmaps.push(DBModlistVersions { mod_id: String::from(&version[0]), versions: String::from(&version[1]) }) 169 versionmaps.push(DBModlistVersions {
154 }; 170 mod_id: String::from(&version[0]),
171 versions: String::from(&version[1]),
172 })
173 }
155 174
156 match versionmaps.is_empty() { 175 match versionmaps.is_empty() {
157 true => Err(MLError::new(ErrorType::DBError, "MODS_MODS_NOT_FOUND")), 176 true => Err(MLError::new(ErrorType::DBError, "MODS_MODS_NOT_FOUND")),
@@ -160,16 +179,37 @@ pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVer
160} 179}
161 180
162//userlist 181//userlist
163pub fn userlist_insert(config: Cfg, list_id: &str, mod_id: &str, current_version: &str, applicable_versions: Vec<String>, current_link: &str, set_version: bool) -> MLE<()> { 182pub fn userlist_insert(
183 config: Cfg,
184 list_id: &str,
185 mod_id: &str,
186 current_version: &str,
187 applicable_versions: Vec<String>,
188 current_link: &str,
189 set_version: bool,
190) -> MLE<()> {
164 let data = devdir(format!("{}/data.db", config.data).as_str()); 191 let data = devdir(format!("{}/data.db", config.data).as_str());
165 let connection = Connection::open(data)?; 192 let connection = Connection::open(data)?;
166 193
167 let sv = match set_version { 194 let sv = match set_version {
168 true => "1", 195 true => "1",
169 false => "0", 196 false => "0",
170 }; 197 };
171 198
172 connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|").as_str(), current_link, sv])?; 199 connection.execute(
200 format!(
201 "INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)",
202 list_id
203 )
204 .as_str(),
205 [
206 mod_id,
207 current_version,
208 applicable_versions.join("|").as_str(),
209 current_link,
210 sv,
211 ],
212 )?;
173 213
174 Ok(()) 214 Ok(())
175} 215}
@@ -180,14 +220,12 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE<Vec<String>> {
180 220
181 let mut mod_ids: Vec<String> = Vec::new(); 221 let mut mod_ids: Vec<String> = Vec::new();
182 let mut stmt = connection.prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?; 222 let mut stmt = connection.prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?;
183 let id_iter = stmt.query_map([], |row| { 223 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
184 row.get::<usize, String>(0)
185 })?;
186 224
187 for id in id_iter { 225 for id in id_iter {
188 //println!("Found id {:?}", id.as_ref().unwrap()); 226 //println!("Found id {:?}", id.as_ref().unwrap());
189 mod_ids.push(id?) 227 mod_ids.push(id?)
190 }; 228 }
191 229
192 match mod_ids.is_empty() { 230 match mod_ids.is_empty() {
193 true => Err(MLError::new(ErrorType::DBError, "NO_MODS")), 231 true => Err(MLError::new(ErrorType::DBError, "NO_MODS")),
@@ -199,24 +237,34 @@ pub fn userlist_remove(config: Cfg, list_id: &str, mod_id: &str) -> MLE<()> {
199 let data = devdir(format!("{}/data.db", config.data).as_str()); 237 let data = devdir(format!("{}/data.db", config.data).as_str());
200 let connection = Connection::open(data)?; 238 let connection = Connection::open(data)?;
201 239
202 connection.execute(format!("DELETE FROM {} WHERE mod_id = ?", list_id).as_str(), [mod_id])?; 240 connection.execute(
241 format!("DELETE FROM {} WHERE mod_id = ?", list_id).as_str(),
242 [mod_id],
243 )?;
203 Ok(()) 244 Ok(())
204} 245}
205 246
206 247pub fn userlist_get_applicable_versions(
207pub fn userlist_get_applicable_versions(config: Cfg, list_id: String, mod_id: String) -> MLE<String> { 248 config: Cfg,
249 list_id: String,
250 mod_id: String,
251) -> MLE<String> {
208 let data = devdir(format!("{}/data.db", config.data).as_str()); 252 let data = devdir(format!("{}/data.db", config.data).as_str());
209 let connection = Connection::open(data).unwrap(); 253 let connection = Connection::open(data).unwrap();
210 254
211 let mut version: String = String::new(); 255 let mut version: String = String::new();
212 let mut stmt = connection.prepare(format!("SELECT applicable_versions FROM {} WHERE mod_id = ?", list_id).as_str())?; 256 let mut stmt = connection.prepare(
213 let ver_iter = stmt.query_map([mod_id], |row| { 257 format!(
214 row.get::<usize, String>(0) 258 "SELECT applicable_versions FROM {} WHERE mod_id = ?",
215 })?; 259 list_id
260 )
261 .as_str(),
262 )?;
263 let ver_iter = stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?;
216 264
217 for ver in ver_iter { 265 for ver in ver_iter {
218 version = ver?; 266 version = ver?;
219 }; 267 }
220 268
221 match version.is_empty() { 269 match version.is_empty() {
222 true => Err(MLError::new(ErrorType::DBError, "GAV_MOD_NOT_FOUND")), 270 true => Err(MLError::new(ErrorType::DBError, "GAV_MOD_NOT_FOUND")),
@@ -224,22 +272,31 @@ pub fn userlist_get_applicable_versions(config: Cfg, list_id: String, mod_id: St
224 } 272 }
225} 273}
226 274
227pub fn userlist_get_all_applicable_versions_with_mods(config: Cfg, list_id: String) -> MLE<Vec<(String, String)>> { 275pub fn userlist_get_all_applicable_versions_with_mods(
276 config: Cfg,
277 list_id: String,
278) -> MLE<Vec<(String, String)>> {
228 let data = devdir(format!("{}/data.db", config.data).as_str()); 279 let data = devdir(format!("{}/data.db", config.data).as_str());
229 let connection = Connection::open(data)?; 280 let connection = Connection::open(data)?;
230 281
231 let mut versions: Vec<(String, String)> = Vec::new(); 282 let mut versions: Vec<(String, String)> = Vec::new();
232 let mut stmt = connection.prepare(format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str())?; 283 let mut stmt = connection
284 .prepare(format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str())?;
233 let id_iter = stmt.query_map([], |row| { 285 let id_iter = stmt.query_map([], |row| {
234 Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?]) 286 Ok(vec![
287 row.get::<usize, String>(0)?,
288 row.get::<usize, String>(1)?,
289 ])
235 })?; 290 })?;
236 291
237 for ver in id_iter { 292 for ver in id_iter {
238 let out = ver?; 293 let out = ver?;
239 versions.push((out[0].to_owned(), out[1].to_owned())); 294 versions.push((out[0].to_owned(), out[1].to_owned()));
240 }; 295 }
241 296
242 if versions.is_empty() { return Err(MLError::new(ErrorType::DBError, "NO_MODS_ON_LIST")); }; 297 if versions.is_empty() {
298 return Err(MLError::new(ErrorType::DBError, "NO_MODS_ON_LIST"));
299 };
243 300
244 Ok(versions) 301 Ok(versions)
245} 302}
@@ -249,14 +306,13 @@ pub fn userlist_get_current_version(config: Cfg, list_id: &str, mod_id: &str) ->
249 let connection = Connection::open(data).unwrap(); 306 let connection = Connection::open(data).unwrap();
250 307
251 let mut version: String = String::new(); 308 let mut version: String = String::new();
252 let mut stmt = connection.prepare(format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id).as_str())?; 309 let mut stmt = connection
253 let ver_iter = stmt.query_map([&mod_id], |row| { 310 .prepare(format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id).as_str())?;
254 row.get::<usize, String>(0) 311 let ver_iter = stmt.query_map([&mod_id], |row| row.get::<usize, String>(0))?;
255 })?;
256 312
257 for ver in ver_iter { 313 for ver in ver_iter {
258 version = ver?; 314 version = ver?;
259 }; 315 }
260 316
261 match version.is_empty() { 317 match version.is_empty() {
262 true => Err(MLError::new(ErrorType::DBError, "GCV_MOD_NOT_FOUND")), 318 true => Err(MLError::new(ErrorType::DBError, "GCV_MOD_NOT_FOUND")),
@@ -264,63 +320,88 @@ pub fn userlist_get_current_version(config: Cfg, list_id: &str, mod_id: &str) ->
264 } 320 }
265} 321}
266 322
267pub fn userlist_get_all_current_version_ids(config: Cfg, list_id: String) -> Result<Vec<String>, Box<dyn std::error::Error>> { 323pub fn userlist_get_all_current_version_ids(
324 config: Cfg,
325 list_id: String,
326) -> Result<Vec<String>, Box<dyn std::error::Error>> {
268 let data = devdir(format!("{}/data.db", config.data).as_str()); 327 let data = devdir(format!("{}/data.db", config.data).as_str());
269 let connection = Connection::open(data)?; 328 let connection = Connection::open(data)?;
270 329
271 let mut versions: Vec<String> = Vec::new(); 330 let mut versions: Vec<String> = Vec::new();
272 let mut stmt = connection.prepare(format!("SELECT current_version FROM {}", list_id).as_str())?; 331 let mut stmt =
273 let id_iter = stmt.query_map([], |row| { 332 connection.prepare(format!("SELECT current_version FROM {}", list_id).as_str())?;
274 row.get::<usize, String>(0) 333 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
275 })?;
276 334
277 for id in id_iter { 335 for id in id_iter {
278 versions.push(id?); 336 versions.push(id?);
279 }; 337 }
280 338
281 if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; 339 if versions.is_empty() {
340 return Err(Box::new(std::io::Error::new(
341 ErrorKind::Other,
342 "NO_MODS_ON_LIST",
343 )));
344 };
282 345
283 Ok(versions) 346 Ok(versions)
284} 347}
285 348
286pub fn userlist_get_all_current_versions_with_mods(config: Cfg, list_id: String) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> { 349pub fn userlist_get_all_current_versions_with_mods(
350 config: Cfg,
351 list_id: String,
352) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> {
287 let data = devdir(format!("{}/data.db", config.data).as_str()); 353 let data = devdir(format!("{}/data.db", config.data).as_str());
288 let connection = Connection::open(data)?; 354 let connection = Connection::open(data)?;
289 355
290 let mut versions: Vec<(String, String)> = Vec::new(); 356 let mut versions: Vec<(String, String)> = Vec::new();
291 let mut stmt = connection.prepare(format!("SELECT mod_id, current_version FROM {}", list_id).as_str())?; 357 let mut stmt =
358 connection.prepare(format!("SELECT mod_id, current_version FROM {}", list_id).as_str())?;
292 let id_iter = stmt.query_map([], |row| { 359 let id_iter = stmt.query_map([], |row| {
293 Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?]) 360 Ok(vec![
361 row.get::<usize, String>(0)?,
362 row.get::<usize, String>(1)?,
363 ])
294 })?; 364 })?;
295 365
296 for ver in id_iter { 366 for ver in id_iter {
297 let out = ver?; 367 let out = ver?;
298 versions.push((out[0].to_owned(), out[1].to_owned())); 368 versions.push((out[0].to_owned(), out[1].to_owned()));
299 }; 369 }
300 370
301 if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; 371 if versions.is_empty() {
372 return Err(Box::new(std::io::Error::new(
373 ErrorKind::Other,
374 "NO_MODS_ON_LIST",
375 )));
376 };
302 377
303 Ok(versions) 378 Ok(versions)
304} 379}
305 380
306pub fn userlist_get_set_version(config:Cfg, list_id: &str, mod_id: &str) -> MLE<bool> { 381pub fn userlist_get_set_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE<bool> {
307 let data = devdir(format!("{}/data.db", config.data).as_str()); 382 let data = devdir(format!("{}/data.db", config.data).as_str());
308 let connection = Connection::open(data).unwrap(); 383 let connection = Connection::open(data).unwrap();
309 384
310 let mut set_version: bool = false; 385 let mut set_version: bool = false;
311 let mut stmt = connection.prepare(format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id).as_str())?; 386 let mut stmt = connection
312 let ver_iter = stmt.query_map([&mod_id], |row| { 387 .prepare(format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id).as_str())?;
313 row.get::<usize, bool>(0) 388 let ver_iter = stmt.query_map([&mod_id], |row| row.get::<usize, bool>(0))?;
314 })?;
315 389
316 for ver in ver_iter { 390 for ver in ver_iter {
317 set_version = ver?; 391 set_version = ver?;
318 }; 392 }
319 393
320 Ok(set_version) 394 Ok(set_version)
321} 395}
322 396
323pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> MLE<()> { 397pub fn userlist_change_versions(
398 config: Cfg,
399 list_id: String,
400 current_version: String,
401 versions: String,
402 link: String,
403 mod_id: String,
404) -> MLE<()> {
324 let data = devdir(format!("{}/data.db", config.data).as_str()); 405 let data = devdir(format!("{}/data.db", config.data).as_str());
325 let connection = Connection::open(data)?; 406 let connection = Connection::open(data)?;
326 407
@@ -328,33 +409,45 @@ pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: S
328 Ok(()) 409 Ok(())
329} 410}
330 411
331pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> MLE<()> { 412pub fn userlist_add_disabled_versions(
413 config: Cfg,
414 list_id: String,
415 disabled_version: String,
416 mod_id: String,
417) -> MLE<()> {
332 let data = devdir(format!("{}/data.db", config.data).as_str()); 418 let data = devdir(format!("{}/data.db", config.data).as_str());
333 let connection = Connection::open(data)?; 419 let connection = Connection::open(data)?;
334 420
335 let currently_disabled_versions = userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?; 421 let currently_disabled_versions =
336 let disabled_versions = match currently_disabled_versions == "NONE" { 422 userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?;
423 let disabled_versions = match currently_disabled_versions == "NONE" {
337 true => disabled_version, 424 true => disabled_version,
338 false => format!("{}|{}", currently_disabled_versions, disabled_version), 425 false => format!("{}|{}", currently_disabled_versions, disabled_version),
339 }; 426 };
340 427
341 connection.execute(format!("UPDATE {} SET disabled_versions = ?1 WHERE mod_id = ?2", list_id).as_str(), [disabled_versions, mod_id])?; 428 connection.execute(
429 format!(
430 "UPDATE {} SET disabled_versions = ?1 WHERE mod_id = ?2",
431 list_id
432 )
433 .as_str(),
434 [disabled_versions, mod_id],
435 )?;
342 Ok(()) 436 Ok(())
343} 437}
344 438
345pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> MLE<String> { 439pub fn userlist_get_disabled_versions(config: Cfg, list_id: String, mod_id: String) -> MLE<String> {
346 let data = devdir(format!("{}/data.db", config.data).as_str()); 440 let data = devdir(format!("{}/data.db", config.data).as_str());
347 let connection = Connection::open(data).unwrap(); 441 let connection = Connection::open(data).unwrap();
348 442
349 let mut version: String = String::new(); 443 let mut version: String = String::new();
350 let mut stmt = connection.prepare(format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id).as_str())?; 444 let mut stmt = connection
351 let ver_iter = stmt.query_map([mod_id], |row| { 445 .prepare(format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id).as_str())?;
352 row.get::<usize, String>(0) 446 let ver_iter = stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?;
353 })?;
354 447
355 for ver in ver_iter { 448 for ver in ver_iter {
356 version = ver?; 449 version = ver?;
357 }; 450 }
358 451
359 match version.is_empty() { 452 match version.is_empty() {
360 true => Err(MLError::new(ErrorType::DBError, "GDV_MOD_NOT_FOUND")), 453 true => Err(MLError::new(ErrorType::DBError, "GDV_MOD_NOT_FOUND")),
@@ -362,36 +455,57 @@ pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: Strin
362 } 455 }
363} 456}
364 457
365pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result<Vec<String>, Box<dyn std::error::Error>> { 458pub fn userlist_get_all_downloads(
459 config: Cfg,
460 list_id: String,
461) -> Result<Vec<String>, Box<dyn std::error::Error>> {
366 let data = devdir(format!("{}/data.db", config.data).as_str()); 462 let data = devdir(format!("{}/data.db", config.data).as_str());
367 let connection = Connection::open(data).unwrap(); 463 let connection = Connection::open(data).unwrap();
368 464
369 let mut links: Vec<String> = Vec::new(); 465 let mut links: Vec<String> = Vec::new();
370 let mut stmt = connection.prepare(format!("SELECT current_download FROM {}", list_id).as_str())?; 466 let mut stmt =
371 let link_iter = stmt.query_map([], |row| { 467 connection.prepare(format!("SELECT current_download FROM {}", list_id).as_str())?;
372 row.get::<usize, String>(0) 468 let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
373 })?;
374 469
375 for link in link_iter { 470 for link in link_iter {
376 let l = link?; 471 let l = link?;
377 println!("Found link {}", String::from(&l)); 472 println!("Found link {}", String::from(&l));
378 links.push(l) 473 links.push(l)
379 }; 474 }
380 475
381 if links.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; 476 if links.is_empty() {
477 return Err(Box::new(std::io::Error::new(
478 ErrorKind::Other,
479 "NO_MODS_ON_LIST",
480 )));
481 };
382 482
383 Ok(links) 483 Ok(links)
384} 484}
385 485
386//lists 486//lists
387///Inserts into lists table and creates new table 487///Inserts into lists table and creates new table
388pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Modloader, download_folder: String) -> MLE<()> { 488pub fn lists_insert(
489 config: Cfg,
490 id: String,
491 mc_version: String,
492 mod_loader: Modloader,
493 download_folder: String,
494) -> MLE<()> {
389 println!("Creating list {}", id); 495 println!("Creating list {}", id);
390 496
391 let data = devdir(format!("{}/data.db", config.data).as_str()); 497 let data = devdir(format!("{}/data.db", config.data).as_str());
392 let connection = Connection::open(data)?; 498 let connection = Connection::open(data)?;
393 499
394 connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.to_string(), download_folder])?; 500 connection.execute(
501 "INSERT INTO lists VALUES (?1, ?2, ?3, ?4)",
502 [
503 id.clone(),
504 mc_version,
505 mod_loader.to_string(),
506 download_folder,
507 ],
508 )?;
395 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(), [])?; 509 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(), [])?;
396 510
397 Ok(()) 511 Ok(())
@@ -410,20 +524,37 @@ pub fn lists_get(config: Cfg, list_id: String) -> MLE<List> {
410 let data = devdir(format!("{}/data.db", config.data).as_str()); 524 let data = devdir(format!("{}/data.db", config.data).as_str());
411 let connection = Connection::open(data).unwrap(); 525 let connection = Connection::open(data).unwrap();
412 526
413 let mut list = List { id: String::new(), mc_version: String::new(), modloader: Modloader::Fabric, download_folder: String::new() }; 527 let mut list = List {
414 let mut stmt = connection.prepare("SELECT mc_version, modloader, download_folder FROM lists WHERE id = ?")?; 528 id: String::new(),
529 mc_version: String::new(),
530 modloader: Modloader::Fabric,
531 download_folder: String::new(),
532 };
533 let mut stmt = connection
534 .prepare("SELECT mc_version, modloader, download_folder FROM lists WHERE id = ?")?;
415 535
416 let list_iter = stmt.query_map([&list_id], |row| { 536 let list_iter = stmt.query_map([&list_id], |row| {
417 Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?, row.get::<usize, String>(2)?]) 537 Ok(vec![
538 row.get::<usize, String>(0)?,
539 row.get::<usize, String>(1)?,
540 row.get::<usize, String>(2)?,
541 ])
418 })?; 542 })?;
419 543
420 for l in list_iter { 544 for l in list_iter {
421 let li = l?; 545 let li = l?;
422 list = List { id: String::from(&list_id), mc_version: String::from(&li[0]), modloader: Modloader::from(&li[1])?, download_folder: String::from(&li[2]) }; 546 list = List {
423 }; 547 id: String::from(&list_id),
548 mc_version: String::from(&li[0]),
549 modloader: Modloader::from(&li[1])?,
550 download_folder: String::from(&li[2]),
551 };
552 }
553
554 if list.id.is_empty() {
555 return Err(MLError::new(ErrorType::DBError, "LIST_NOT_FOUND"));
556 }
424 557
425 if list.id.is_empty() { return Err(MLError::new(ErrorType::DBError, "LIST_NOT_FOUND")); }
426
427 Ok(list) 558 Ok(list)
428} 559}
429 560
@@ -431,23 +562,24 @@ pub fn lists_version(config: Cfg, list_id: &str, version: &str) -> MLE<()> {
431 let data = devdir(format!("{}/data.db", config.data).as_str()); 562 let data = devdir(format!("{}/data.db", config.data).as_str());
432 let connection = Connection::open(data).unwrap(); 563 let connection = Connection::open(data).unwrap();
433 564
434 connection.execute("UPDATE lists SET mc_version = ? WHERE id = ?", [version, list_id])?; 565 connection.execute(
566 "UPDATE lists SET mc_version = ? WHERE id = ?",
567 [version, list_id],
568 )?;
435 Ok(()) 569 Ok(())
436} 570}
437 571
438pub fn lists_get_all_ids(config: Cfg) -> MLE<Vec<String>> { 572pub fn lists_get_all_ids(config: Cfg) -> MLE<Vec<String>> {
439 let data = devdir(format!("{}/data.db", config.data).as_str()); 573 let data = devdir(format!("{}/data.db", config.data).as_str());
440 let connection = Connection::open(data).unwrap(); 574 let connection = Connection::open(data).unwrap();
441 575
442 let mut list_ids: Vec<String> = Vec::new(); 576 let mut list_ids: Vec<String> = Vec::new();
443 let mut stmt = connection.prepare("SELECT id FROM lists")?; 577 let mut stmt = connection.prepare("SELECT id FROM lists")?;
444 let id_iter = stmt.query_map([], |row| { 578 let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
445 row.get::<usize, String>(0)
446 })?;
447 579
448 for id in id_iter { 580 for id in id_iter {
449 list_ids.push(id?) 581 list_ids.push(id?)
450 }; 582 }
451 583
452 match list_ids.is_empty() { 584 match list_ids.is_empty() {
453 true => Err(MLError::new(ErrorType::DBError, "NO_LISTS")), 585 true => Err(MLError::new(ErrorType::DBError, "NO_LISTS")),
@@ -460,35 +592,50 @@ pub fn config_change_current_list(config: Cfg, id: String) -> MLE<()> {
460 let data = devdir(format!("{}/data.db", config.data).as_str()); 592 let data = devdir(format!("{}/data.db", config.data).as_str());
461 let connection = Connection::open(data)?; 593 let connection = Connection::open(data)?;
462 594
463 connection.execute("UPDATE user_config SET value = ? WHERE id = 'current_list'", [id])?; 595 connection.execute(
596 "UPDATE user_config SET value = ? WHERE id = 'current_list'",
597 [id],
598 )?;
464 Ok(()) 599 Ok(())
465} 600}
466 601
467pub fn config_get_current_list(config: Cfg) -> MLE<String> { 602pub fn config_get_current_list(config: Cfg) -> MLE<String> {
468 let data = devdir(format!("{}/data.db", config.data).as_str()); 603 let data = devdir(format!("{}/data.db", config.data).as_str());
469 let connection = Connection::open(data).unwrap(); 604 let connection = Connection::open(data).unwrap();
470 605
471 let mut list_id = String::new(); 606 let mut list_id = String::new();
472 let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'current_list'")?; 607 let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'current_list'")?;
473 let list_iter = stmt.query_map([], |row| { 608 let list_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
474 row.get::<usize, String>(0)
475 })?;
476 609
477 for list in list_iter { 610 for list in list_iter {
478 list_id = list?; 611 list_id = list?;
479 }; 612 }
613
614 if list_id.is_empty() {
615 return Err(MLError::new(ErrorType::DBError, "NO_CURRENT_LIST"));
616 }
480 617
481 if list_id.is_empty() { return Err(MLError::new(ErrorType::DBError, "NO_CURRENT_LIST")); }
482
483 Ok(list_id) 618 Ok(list_id)
484} 619}
485 620
486//SETUP(UPDATES) 621//SETUP(UPDATES)
487pub fn s_userlist_update_download(config: Cfg, list_id: String, mod_id: String, link: String) -> Result<(), Box<dyn std::error::Error>> { 622pub fn s_userlist_update_download(
623 config: Cfg,
624 list_id: String,
625 mod_id: String,
626 link: String,
627) -> Result<(), Box<dyn std::error::Error>> {
488 let data = devdir(format!("{}/data.db", config.data).as_str()); 628 let data = devdir(format!("{}/data.db", config.data).as_str());
489 let connection = Connection::open(data)?; 629 let connection = Connection::open(data)?;
490 630
491 connection.execute(format!("UPDATE {} SET current_download = ?1 WHERE mod_id = ?2", list_id).as_str(), [link, mod_id])?; 631 connection.execute(
632 format!(
633 "UPDATE {} SET current_download = ?1 WHERE mod_id = ?2",
634 list_id
635 )
636 .as_str(),
637 [link, mod_id],
638 )?;
492 Ok(()) 639 Ok(())
493} 640}
494 641
@@ -496,7 +643,10 @@ pub fn s_config_create_version(config: Cfg) -> Result<(), Box<dyn std::error::Er
496 let data = devdir(format!("{}/data.db", config.data).as_str()); 643 let data = devdir(format!("{}/data.db", config.data).as_str());
497 let connection = Connection::open(data)?; 644 let connection = Connection::open(data)?;
498 645
499 connection.execute("INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' )", ())?; 646 connection.execute(
647 "INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' )",
648 (),
649 )?;
500 Ok(()) 650 Ok(())
501} 651}
502 652
@@ -504,34 +654,46 @@ pub fn s_config_update_version(config: Cfg, ver: String) -> Result<(), Box<dyn s
504 let data = devdir(format!("{}/data.db", config.data).as_str()); 654 let data = devdir(format!("{}/data.db", config.data).as_str());
505 let connection = Connection::open(data)?; 655 let connection = Connection::open(data)?;
506 656
507 connection.execute("UPDATE user_config SET value = ? WHERE id = 'db_version'", [ver])?; 657 connection.execute(
658 "UPDATE user_config SET value = ? WHERE id = 'db_version'",
659 [ver],
660 )?;
508 Ok(()) 661 Ok(())
509} 662}
510 663
511pub fn s_config_get_version(config: Cfg) -> Result<String, Box<dyn std::error::Error>> { 664pub fn s_config_get_version(config: Cfg) -> Result<String, Box<dyn std::error::Error>> {
512 let data = devdir(format!("{}/data.db", config.data).as_str()); 665 let data = devdir(format!("{}/data.db", config.data).as_str());
513 let connection = Connection::open(data)?; 666 let connection = Connection::open(data)?;
514 667
515 let mut version: String = String::new(); 668 let mut version: String = String::new();
516 let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'db_version'")?; 669 let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'db_version'")?;
517 let ver_iter = stmt.query_map([], |row| { 670 let ver_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
518 row.get::<usize, String>(0)
519 })?;
520 671
521 for ver in ver_iter { 672 for ver in ver_iter {
522 version = ver?; 673 version = ver?;
523 }; 674 }
524 675
525 if version.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_DBVERSION"))); }; 676 if version.is_empty() {
677 return Err(Box::new(std::io::Error::new(
678 ErrorKind::Other,
679 "NO_DBVERSION",
680 )));
681 };
526 Ok(version) 682 Ok(version)
527} 683}
528 684
529pub fn s_insert_column(config: Cfg, table: String, column: String, c_type: String, default: Option<String>) -> Result<(), Box<dyn std::error::Error>> { 685pub fn s_insert_column(
686 config: Cfg,
687 table: String,
688 column: String,
689 c_type: String,
690 default: Option<String>,
691) -> Result<(), Box<dyn std::error::Error>> {
530 let data = devdir(format!("{}/data.db", config.data).as_str()); 692 let data = devdir(format!("{}/data.db", config.data).as_str());
531 let connection = Connection::open(data)?; 693 let connection = Connection::open(data)?;
532 694
533 let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type); 695 let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type);
534 696
535 if default.is_some() { 697 if default.is_some() {
536 sql = format!("{} DEFAULT {}", sql, default.unwrap()); 698 sql = format!("{} DEFAULT {}", sql, default.unwrap());
537 } 699 }
@@ -541,7 +703,6 @@ pub fn s_insert_column(config: Cfg, table: String, column: String, c_type: Strin
541} 703}
542 704
543pub fn db_setup(config: Cfg) -> MLE<()> { 705pub fn db_setup(config: Cfg) -> MLE<()> {
544
545 println!("Initiating database"); 706 println!("Initiating database");
546 707
547 let data = devdir(format!("{}/data.db", config.data).as_str()); 708 let data = devdir(format!("{}/data.db", config.data).as_str());
@@ -554,6 +715,6 @@ pub fn db_setup(config: Cfg) -> MLE<()> {
554 INSERT INTO 'user_config' VALUES ( 'db_version', '0.5' ); 715 INSERT INTO 'user_config' VALUES ( 'db_version', '0.5' );
555 INSERT INTO 'user_config' VALUES ( 'current_list', '...' )", 716 INSERT INTO 'user_config' VALUES ( 'current_list', '...' )",
556 )?; 717 )?;
557 718
558 Ok(()) 719 Ok(())
559} 720}