diff options
author | fxqnlr <[email protected]> | 2022-11-04 23:41:21 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-11-04 23:41:21 +0100 |
commit | 5d50f446a1a4612c0c931bdbc61f945760392f29 (patch) | |
tree | 44414994e19b41979a8a939b120ada0d1aa8a13a /src/db.rs | |
parent | 96cc5257de09682df345e768dc2a91303f9b36c9 (diff) | |
download | modlist-5d50f446a1a4612c0c931bdbc61f945760392f29.tar modlist-5d50f446a1a4612c0c931bdbc61f945760392f29.tar.gz modlist-5d50f446a1a4612c0c931bdbc61f945760392f29.zip |
"finished" update, added some tests
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 32 |
1 files changed, 31 insertions, 1 deletions
@@ -2,7 +2,7 @@ use std::io::ErrorKind; | |||
2 | 2 | ||
3 | use crate::{Modloader, config::Cfg, List, modrinth::Version, get_modloader}; | 3 | use crate::{Modloader, config::Cfg, List, modrinth::Version, get_modloader}; |
4 | 4 | ||
5 | //TODO use prepared statements | 5 | //TODO use prepared statements / change to rusqlite |
6 | 6 | ||
7 | //MODS | 7 | //MODS |
8 | pub fn insert_mod(config: Cfg, id: String, name: String, versions: Vec<String>) -> Result<(), sqlite::Error> { | 8 | pub fn insert_mod(config: Cfg, id: String, name: String, versions: Vec<String>) -> Result<(), sqlite::Error> { |
@@ -151,6 +151,27 @@ pub fn get_versions(config: Cfg, mods: Vec<String>) -> Result<Vec<DBModlistVersi | |||
151 | Ok(versionmaps) | 151 | Ok(versionmaps) |
152 | } | 152 | } |
153 | 153 | ||
154 | pub fn get_list_version(config: Cfg, list: List, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { | ||
155 | let data = format!("{}/data.db", config.data); | ||
156 | let connection = sqlite::open(data).unwrap(); | ||
157 | |||
158 | let sql = format!("SELECT applicable_versions FROM {} WHERE mod_id = '{}'", list.id, mod_id); | ||
159 | |||
160 | //TODO catch sql errors better | ||
161 | let mut version: String = String::new(); | ||
162 | connection.iterate(sql, |ver| { | ||
163 | if ver.is_empty() { return false; }; | ||
164 | for &(_column, value) in ver.iter() { | ||
165 | version = String::from(value.unwrap()); | ||
166 | } | ||
167 | true | ||
168 | }).unwrap(); | ||
169 | |||
170 | if version.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; | ||
171 | |||
172 | Ok(version) | ||
173 | } | ||
174 | |||
154 | 175 | ||
155 | //LIST | 176 | //LIST |
156 | pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modloader) -> Result<(), sqlite::Error> { | 177 | pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modloader) -> Result<(), sqlite::Error> { |
@@ -217,6 +238,15 @@ pub fn get_list(config: Cfg, id: String) -> Result<List, Box<dyn std::error::Err | |||
217 | Ok(List { id, mc_version: String::from(&list[0]), modloader: get_modloader(String::from(&list[1]))? }) | 238 | Ok(List { id, mc_version: String::from(&list[0]), modloader: get_modloader(String::from(&list[1]))? }) |
218 | } | 239 | } |
219 | 240 | ||
241 | pub fn change_list_versions(config: Cfg, list: List, current_version: String, versions: Vec<String>, mod_id: String) -> Result<(), sqlite::Error> { | ||
242 | let data = format!("{}/data.db", config.data); | ||
243 | let connection = sqlite::open(data).unwrap(); | ||
244 | |||
245 | let sql = format!("UPDATE {} SET current_version = '{}', applicable_versions = '{}' WHERE mod_id = '{}'", list.id, current_version, versions.join("|"), mod_id); | ||
246 | |||
247 | connection.execute(sql) | ||
248 | } | ||
249 | |||
220 | //config | 250 | //config |
221 | pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { | 251 | pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { |
222 | let data = format!("{}/data.db", config.data); | 252 | let data = format!("{}/data.db", config.data); |