diff options
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 101 |
1 files changed, 66 insertions, 35 deletions
@@ -4,17 +4,17 @@ use rusqlite::Connection; | |||
4 | 4 | ||
5 | use crate::{Modloader, config::Cfg, List, devdir, error::{MLE, MLError, ErrorType}}; | 5 | use crate::{Modloader, config::Cfg, List, devdir, error::{MLE, MLError, ErrorType}}; |
6 | 6 | ||
7 | //mods | 7 | //MODS |
8 | pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec<String>) -> MLE<()> { | 8 | pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { |
9 | 9 | ||
10 | println!("Inserting mod {}({}) into database", name, id); | 10 | println!("\t └Save mod info"); |
11 | 11 | ||
12 | let data = devdir(format!("{}/data.db", config.data).as_str()); | 12 | let data = devdir(format!("{}/data.db", config.data).as_str()); |
13 | let connection = Connection::open(data)?; | 13 | let connection = Connection::open(data)?; |
14 | 14 | ||
15 | connection.execute( | 15 | connection.execute( |
16 | "INSERT INTO mods (id, name, versions) VALUES (?1, ?2, ?3)", | 16 | "INSERT INTO mods (id, slug, title) VALUES (?1, ?2, ?3)", |
17 | [id, name.replace('\'', ""), versions.join("|")] | 17 | [id, slug, name.replace('\'', "").as_str()] |
18 | )?; | 18 | )?; |
19 | 19 | ||
20 | Ok(()) | 20 | Ok(()) |
@@ -41,32 +41,53 @@ pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error:: | |||
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | pub fn mods_get_id(config: Cfg, name: String) -> MLE<String> { | 44 | ///Get mod id based on the slug or name |
45 | let data = devdir(format!("{}/data.db", config.data).as_str()); | 45 | ///# Arguments |
46 | /// | ||
47 | ///* `data` - file directory of the database | ||
48 | ///* `slug` - Slug or Name of a mod | ||
49 | /// | ||
50 | ///# Failure | ||
51 | /// | ||
52 | ///Will return `MLError` when no mod id is found | ||
53 | pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { | ||
54 | let data = devdir(format!("{}/data.db", data).as_str()); | ||
46 | let connection = Connection::open(data)?; | 55 | let connection = Connection::open(data)?; |
47 | 56 | ||
48 | let mut mod_id = String::new(); | 57 | let mut mod_id = String::new(); |
49 | let mut stmt = connection.prepare("SELECT id FROM mods WHERE name = ?")?; | 58 | |
50 | let id_iter = stmt.query_map([name], |row| { | 59 | //get from slug |
60 | let mut stmt = connection.prepare("SELECT id FROM mods WHERE slug = ?")?; | ||
61 | let id_iter = stmt.query_map([slug], |row| { | ||
51 | row.get::<usize, String>(0) | 62 | row.get::<usize, String>(0) |
52 | })?; | 63 | })?; |
53 | 64 | ||
54 | for id in id_iter { | 65 | for id in id_iter { |
55 | mod_id = id?; | 66 | mod_id = id?; |
56 | }; | 67 | }; |
57 | 68 | //get from title if no id found from slug | |
58 | match mod_id.is_empty() { | 69 | if mod_id.is_empty() { |
59 | true => Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND")), | 70 | let mut stmt = connection.prepare("SELECT id FROM mods WHERE title = ?")?; |
60 | false => Ok(mod_id), | 71 | let id_iter = stmt.query_map([slug], |row| { |
72 | row.get::<usize, String>(0) | ||
73 | })?; | ||
74 | |||
75 | for id in id_iter { | ||
76 | mod_id = id?; | ||
77 | }; | ||
61 | } | 78 | } |
79 | |||
80 | if mod_id.is_empty() { return Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND")) }; | ||
81 | |||
82 | Ok(mod_id) | ||
62 | } | 83 | } |
63 | 84 | ||
64 | pub fn mods_get_name(config: Cfg, id: &str) -> MLE<String> { | 85 | pub fn mods_get_title(config: Cfg, id: &str) -> MLE<String> { |
65 | let data = devdir(format!("{}/data.db", config.data).as_str()); | 86 | let data = devdir(format!("{}/data.db", config.data).as_str()); |
66 | let connection = Connection::open(data)?; | 87 | let connection = Connection::open(data)?; |
67 | 88 | ||
68 | let mut mod_name = String::new(); | 89 | let mut mod_name = String::new(); |
69 | let mut stmt = connection.prepare("SELECT name FROM mods WHERE id = ?")?; | 90 | let mut stmt = connection.prepare("SELECT title FROM mods WHERE id = ?")?; |
70 | let name_iter = stmt.query_map([id], |row| { | 91 | let name_iter = stmt.query_map([id], |row| { |
71 | row.get::<usize, String>(0) | 92 | row.get::<usize, String>(0) |
72 | })?; | 93 | })?; |
@@ -81,17 +102,6 @@ pub fn mods_get_name(config: Cfg, id: &str) -> MLE<String> { | |||
81 | } | 102 | } |
82 | } | 103 | } |
83 | 104 | ||
84 | pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> MLE<()> { | ||
85 | |||
86 | //println!("Updating versions for {} with \n {}", mod_id, versions); | ||
87 | |||
88 | let data = devdir(format!("{}/data.db", config.data).as_str()); | ||
89 | let connection = Connection::open(data)?; | ||
90 | |||
91 | connection.execute("UPDATE mods SET versions = ?1 WHERE id = ?2", [versions, mod_id])?; | ||
92 | Ok(()) | ||
93 | } | ||
94 | |||
95 | pub fn mods_remove(config: Cfg, id: String) -> MLE<()> { | 105 | pub fn mods_remove(config: Cfg, id: String) -> MLE<()> { |
96 | 106 | ||
97 | println!("Removing mod {} from database", id); | 107 | println!("Removing mod {} from database", id); |
@@ -124,7 +134,7 @@ pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVer | |||
124 | } | 134 | } |
125 | 135 | ||
126 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); | 136 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); |
127 | let mut stmt = connection.prepare(format!("SELECT id, versions, name FROM mods {}", wherestr).as_str())?; | 137 | let mut stmt = connection.prepare(format!("SELECT id, versions, title FROM mods {}", wherestr).as_str())?; |
128 | let id_iter = stmt.query_map([], |row| { | 138 | let id_iter = stmt.query_map([], |row| { |
129 | Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?, row.get::<usize, String>(2)?]) | 139 | Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?, row.get::<usize, String>(2)?]) |
130 | })?; | 140 | })?; |
@@ -143,14 +153,18 @@ pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVer | |||
143 | } | 153 | } |
144 | 154 | ||
145 | //userlist | 155 | //userlist |
146 | pub fn userlist_insert(config: Cfg, list_id: String, mod_id: String, current_version: String, applicable_versions: Vec<String>, current_link: String) -> MLE<()> { | 156 | pub 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<()> { |
147 | println!("Inserting {} into current list({})", mod_id, list_id); | 157 | println!("\t └Insert in list"); |
148 | 158 | ||
149 | let data = devdir(format!("{}/data.db", config.data).as_str()); | 159 | let data = devdir(format!("{}/data.db", config.data).as_str()); |
150 | let connection = Connection::open(data)?; | 160 | let connection = Connection::open(data)?; |
151 | |||
152 | 161 | ||
153 | connection.execute(format!("INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE')", list_id).as_str(), [mod_id, current_version, applicable_versions.join("|"), current_link])?; | 162 | let sv = match set_version { |
163 | true => "1", | ||
164 | false => "0", | ||
165 | }; | ||
166 | |||
167 | 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])?; | ||
154 | 168 | ||
155 | Ok(()) | 169 | Ok(()) |
156 | } | 170 | } |
@@ -285,6 +299,23 @@ pub fn userlist_get_all_current_versions_with_mods(config: Cfg, list_id: String) | |||
285 | Ok(versions) | 299 | Ok(versions) |
286 | } | 300 | } |
287 | 301 | ||
302 | pub fn userlist_get_set_version(config:Cfg, list_id: &str, mod_id: &str) -> MLE<bool> { | ||
303 | let data = devdir(format!("{}/data.db", config.data).as_str()); | ||
304 | let connection = Connection::open(data).unwrap(); | ||
305 | |||
306 | let mut set_version: bool = false; | ||
307 | let mut stmt = connection.prepare(format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id).as_str())?; | ||
308 | let ver_iter = stmt.query_map([&mod_id], |row| { | ||
309 | row.get::<usize, bool>(0) | ||
310 | })?; | ||
311 | |||
312 | for ver in ver_iter { | ||
313 | set_version = ver?; | ||
314 | }; | ||
315 | |||
316 | Ok(set_version) | ||
317 | } | ||
318 | |||
288 | pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> MLE<()> { | 319 | pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> MLE<()> { |
289 | let data = devdir(format!("{}/data.db", config.data).as_str()); | 320 | let data = devdir(format!("{}/data.db", config.data).as_str()); |
290 | let connection = Connection::open(data)?; | 321 | let connection = Connection::open(data)?; |
@@ -357,7 +388,7 @@ pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Mod | |||
357 | let connection = Connection::open(data)?; | 388 | let connection = Connection::open(data)?; |
358 | 389 | ||
359 | connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.to_string(), download_folder])?; | 390 | connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.to_string(), download_folder])?; |
360 | 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(), [])?; | 391 | 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(), [])?; |
361 | 392 | ||
362 | Ok(()) | 393 | Ok(()) |
363 | } | 394 | } |
@@ -505,7 +536,7 @@ pub fn s_insert_column(config: Cfg, table: String, column: String, c_type: Strin | |||
505 | Ok(()) | 536 | Ok(()) |
506 | } | 537 | } |
507 | 538 | ||
508 | pub fn db_setup(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | 539 | pub fn db_setup(config: Cfg) -> MLE<()> { |
509 | 540 | ||
510 | println!("Initiating database"); | 541 | println!("Initiating database"); |
511 | 542 | ||
@@ -514,9 +545,9 @@ pub fn db_setup(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | |||
514 | 545 | ||
515 | connection.execute_batch( | 546 | connection.execute_batch( |
516 | "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); | 547 | "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); |
517 | CREATE TABLE 'mods' ( 'id' TEXT, 'name' TEXT, 'versions' TEXT ); | 548 | CREATE TABLE 'mods' ( 'id' TEXT, 'slug' TEXT, 'title' TEXT, CONSTRAINT mods_PK PRIMARY KEY (id) ); |
518 | CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT, 'download_folder' TEXT ); | 549 | CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT, 'download_folder' TEXT ); |
519 | INSERT INTO 'user_config' VALUES ( 'db_version', '0.4' ); | 550 | INSERT INTO 'user_config' VALUES ( 'db_version', '0.5' ); |
520 | INSERT INTO 'user_config' VALUES ( 'current_list', '...' )", | 551 | INSERT INTO 'user_config' VALUES ( 'current_list', '...' )", |
521 | )?; | 552 | )?; |
522 | 553 | ||