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