diff options
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 171 |
1 files changed, 144 insertions, 27 deletions
@@ -12,12 +12,14 @@ pub fn insert_mod(config: Cfg, id: String, name: String, versions: Vec<String>) | |||
12 | let data = format!("{}/data.db", config.data); | 12 | let data = format!("{}/data.db", config.data); |
13 | let connection = sqlite::open(data).unwrap(); | 13 | let connection = sqlite::open(data).unwrap(); |
14 | 14 | ||
15 | let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}')", id, name, versions.join("|")); | 15 | let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}')", id, name.replace('\'', ""), versions.join("|")); |
16 | |||
17 | dbg!(&sql); | ||
16 | 18 | ||
17 | connection.execute(sql) | 19 | connection.execute(sql) |
18 | } | 20 | } |
19 | 21 | ||
20 | pub fn insert_mod_in_list(config: Cfg, list: List, id: String, current_version: String, applicable_versions: Vec<Version>) -> Result<(), sqlite::Error> { | 22 | pub fn insert_mod_in_list(config: Cfg, list: List, id: String, current_version: String, applicable_versions: Vec<Version>, current_link: String) -> Result<(), sqlite::Error> { |
21 | 23 | ||
22 | println!("Inserting into current list"); | 24 | println!("Inserting into current list"); |
23 | 25 | ||
@@ -30,8 +32,8 @@ pub fn insert_mod_in_list(config: Cfg, list: List, id: String, current_version: | |||
30 | applicable_versions_vec.push(ver.id); | 32 | applicable_versions_vec.push(ver.id); |
31 | } | 33 | } |
32 | 34 | ||
33 | let sql = format!("INSERT INTO {} VALUES ('{}', '{}', '{}')", list.id, id, current_version, applicable_versions_vec.join("|")); | 35 | let sql = format!("INSERT INTO {} VALUES ('{}', '{}', '{}', '{}')", list.id, id, current_version, applicable_versions_vec.join("|"), current_link); |
34 | 36 | ||
35 | connection.execute(sql) | 37 | connection.execute(sql) |
36 | } | 38 | } |
37 | 39 | ||
@@ -122,33 +124,33 @@ pub struct DBModlistVersions { | |||
122 | } | 124 | } |
123 | 125 | ||
124 | pub fn get_versions(config: Cfg, mods: Vec<String>) -> Result<Vec<DBModlistVersions>, Box<dyn std::error::Error>> { | 126 | pub fn get_versions(config: Cfg, mods: Vec<String>) -> Result<Vec<DBModlistVersions>, Box<dyn std::error::Error>> { |
125 | let data = format!("{}/data.db", config.data); | 127 | let data = format!("{}/data.db", config.data); |
126 | let connection = sqlite::open(data).unwrap(); | 128 | let connection = sqlite::open(data).unwrap(); |
127 | 129 | ||
128 | let mut wherestr = String::from("WHERE"); | 130 | let mut wherestr = String::from("WHERE"); |
129 | for (i, id) in mods.iter().enumerate() { | 131 | for (i, id) in mods.iter().enumerate() { |
130 | let mut or = " OR"; | 132 | let mut or = " OR"; |
131 | if i == mods.len() - 1 { or = "" } | 133 | if i == mods.len() - 1 { or = "" } |
132 | println!("Pushing {}({}) | OR: '{}'", id, i, or); | 134 | println!("Pushing {}({}) | OR: '{}'", id, i, or); |
133 | wherestr = format!("{} id = '{}'{}", wherestr, id, or); | 135 | wherestr = format!("{} id = '{}'{}", wherestr, id, or); |
134 | } | 136 | } |
135 | 137 | ||
136 | let sql = format!("SELECT id, versions FROM mods {}", wherestr); | 138 | let sql = format!("SELECT id, versions FROM mods {}", wherestr); |
137 | |||
138 | dbg!(&sql); | ||
139 | 139 | ||
140 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); | 140 | dbg!(&sql); |
141 | //TODO catch sql errors better | ||
142 | let mut cursor = connection.prepare(sql).unwrap().into_cursor(); | ||
143 | 141 | ||
144 | while let Some(Ok(row)) = cursor.next() { | 142 | let mut versionmaps: Vec<DBModlistVersions> = Vec::new(); |
145 | println!("{}: {}", row.get::<String, _>(0), row.get::<String, _>(1)); | 143 | //TODO catch sql errors better |
146 | versionmaps.push(DBModlistVersions { mod_id: row.get::<String, _>(0), versions: row.get::<String, _>(1) }) | 144 | let mut cursor = connection.prepare(sql).unwrap().into_cursor(); |
147 | }; | ||
148 | 145 | ||
149 | if versionmaps.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; | 146 | while let Some(Ok(row)) = cursor.next() { |
147 | println!("{}: {}", row.get::<String, _>(0), row.get::<String, _>(1)); | ||
148 | versionmaps.push(DBModlistVersions { mod_id: row.get::<String, _>(0), versions: row.get::<String, _>(1) }) | ||
149 | }; | ||
150 | 150 | ||
151 | Ok(versionmaps) | 151 | if versionmaps.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; |
152 | |||
153 | Ok(versionmaps) | ||
152 | } | 154 | } |
153 | 155 | ||
154 | pub fn get_list_version(config: Cfg, list: List, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { | 156 | pub fn get_list_version(config: Cfg, list: List, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { |
@@ -179,7 +181,7 @@ pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modl | |||
179 | let connection = sqlite::open(data).unwrap(); | 181 | let connection = sqlite::open(data).unwrap(); |
180 | 182 | ||
181 | let sql_list = format!("INSERT INTO lists VALUES ('{}', '{}', '{}')", id, mc_version, mod_loader.stringify()); | 183 | let sql_list = format!("INSERT INTO lists VALUES ('{}', '{}', '{}')", id, mc_version, mod_loader.stringify()); |
182 | let sql_table = format!("CREATE TABLE '{}' ( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB)", id); | 184 | let sql_table = format!("CREATE TABLE '{}' ( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT)", id); |
183 | let sql = format!("{};{};", sql_list, sql_table); | 185 | let sql = format!("{};{};", sql_list, sql_table); |
184 | 186 | ||
185 | connection.execute(sql) | 187 | connection.execute(sql) |
@@ -217,6 +219,27 @@ pub fn get_lists(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> | |||
217 | } | 219 | } |
218 | } | 220 | } |
219 | 221 | ||
222 | pub fn get_current_versions(config: Cfg, list: List) -> Result<Vec<String>, Box<std::io::Error>> { | ||
223 | let data = format!("{}/data.db", config.data); | ||
224 | let connection = sqlite::open(data).unwrap(); | ||
225 | |||
226 | let sql = format!("SELECT current_version FROM {}", list.id); | ||
227 | |||
228 | dbg!(&sql); | ||
229 | |||
230 | let mut versions: Vec<String> = Vec::new(); | ||
231 | //TODO catch sql errors better | ||
232 | let mut cursor = connection.prepare(sql).unwrap().into_cursor(); | ||
233 | |||
234 | while let Some(Ok(row)) = cursor.next() { | ||
235 | versions.push(row.get::<String, _>(0)); | ||
236 | }; | ||
237 | |||
238 | if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; | ||
239 | |||
240 | Ok(versions) | ||
241 | } | ||
242 | |||
220 | pub fn get_list(config: Cfg, id: String) -> Result<List, Box<dyn std::error::Error>> { | 243 | pub fn get_list(config: Cfg, id: String) -> Result<List, Box<dyn std::error::Error>> { |
221 | let data = format!("{}/data.db", config.data); | 244 | let data = format!("{}/data.db", config.data); |
222 | let connection = sqlite::open(data).unwrap(); | 245 | let connection = sqlite::open(data).unwrap(); |
@@ -247,6 +270,38 @@ pub fn change_list_versions(config: Cfg, list: List, current_version: String, ve | |||
247 | connection.execute(sql) | 270 | connection.execute(sql) |
248 | } | 271 | } |
249 | 272 | ||
273 | //DOWNLOAD | ||
274 | |||
275 | pub fn insert_dl_link(config: Cfg, list: List, mod_id: String, link: String) -> Result<(), sqlite::Error> { | ||
276 | let data = format!("{}/data.db", config.data); | ||
277 | let connection = sqlite::open(data).unwrap(); | ||
278 | |||
279 | let sql = format!("UPDATE {} SET current_download = '{}' WHERE mod_id = '{}'", list.id, link, mod_id); | ||
280 | |||
281 | connection.execute(sql) | ||
282 | } | ||
283 | |||
284 | pub fn get_dl_links(config: Cfg, list: List) -> Result<Vec<String>, Box<std::io::Error>> { | ||
285 | let data = format!("{}/data.db", config.data); | ||
286 | let connection = sqlite::open(data).unwrap(); | ||
287 | |||
288 | let sql = format!("SELECT current_download FROM {}", list.id); | ||
289 | |||
290 | dbg!(&sql); | ||
291 | |||
292 | let mut links: Vec<String> = Vec::new(); | ||
293 | //TODO catch sql errors better | ||
294 | let mut cursor = connection.prepare(sql).unwrap().into_cursor(); | ||
295 | |||
296 | while let Some(Ok(row)) = cursor.next() { | ||
297 | links.push(row.get::<String, _>(0)); | ||
298 | }; | ||
299 | |||
300 | if links.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; | ||
301 | |||
302 | Ok(links) | ||
303 | } | ||
304 | |||
250 | //config | 305 | //config |
251 | pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { | 306 | pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> { |
252 | let data = format!("{}/data.db", config.data); | 307 | let data = format!("{}/data.db", config.data); |
@@ -278,3 +333,65 @@ pub fn get_current_list_id(config: Cfg) -> Result<String, Box<dyn std::error::Er | |||
278 | }; | 333 | }; |
279 | Ok(list) | 334 | Ok(list) |
280 | } | 335 | } |
336 | |||
337 | pub fn update_dbversion(config: Cfg, ver: String) -> Result<(), sqlite::Error> { | ||
338 | let data = format!("{}/data.db", config.data); | ||
339 | let connection = sqlite::open(data).unwrap(); | ||
340 | |||
341 | let sql = format!("UPDATE user_config SET value = '{}' WHERE id = 'db_version'", ver); | ||
342 | |||
343 | connection.execute(sql) | ||
344 | } | ||
345 | |||
346 | pub fn create_dbversion(config: Cfg) -> Result<(), sqlite::Error> { | ||
347 | let data = format!("{}/data.db", config.data); | ||
348 | let connection = sqlite::open(data).unwrap(); | ||
349 | let sql = "INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' );"; | ||
350 | connection.execute(sql) | ||
351 | } | ||
352 | |||
353 | pub fn get_dbversion(config: Cfg) -> Result<String, Box<dyn std::error::Error>> { | ||
354 | let data = format!("{}/data.db", config.data); | ||
355 | let connection = sqlite::open(data).unwrap(); | ||
356 | |||
357 | let sql = "SELECT db_version FROM user_config"; | ||
358 | |||
359 | let mut ver: String = String::new(); | ||
360 | //TODO catch sql errors better | ||
361 | connection.iterate(sql, |ids| { | ||
362 | if ids.is_empty() { return false; }; | ||
363 | for &(_column, value) in ids.iter() { | ||
364 | ver = String::from(value.unwrap()); | ||
365 | } | ||
366 | true | ||
367 | })?; | ||
368 | if ver.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_DBVERSION"))); }; | ||
369 | Ok(ver) | ||
370 | } | ||
371 | |||
372 | pub fn db_setup(config: Cfg) -> Result<(), sqlite::Error> { | ||
373 | println!("Initiating database"); | ||
374 | |||
375 | let data = format!("{}/data.db", config.data); | ||
376 | let connection = sqlite::open(data).unwrap(); | ||
377 | |||
378 | let sql = "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); CREATE TABLE 'mods' ( 'id' TEXT, 'name' TEXT, 'versions' TEXT ); CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT ); INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' ); INSERT INTO 'user_config' VALUES ( 'current_list', '...' )"; | ||
379 | |||
380 | connection.execute(sql) | ||
381 | } | ||
382 | |||
383 | pub fn insert_column(config: Cfg, table: String, column: String, c_type: sqlite::Type) -> Result<(), sqlite::Error> { | ||
384 | let data = format!("{}/data.db", config.data); | ||
385 | let connection = sqlite::open(data).unwrap(); | ||
386 | |||
387 | let ct = match c_type { | ||
388 | sqlite::Type::Null => "NULL", | ||
389 | sqlite::Type::Float => "FLOAT", | ||
390 | sqlite::Type::Binary => "BINARY", | ||
391 | sqlite::Type::String => "TEXT", | ||
392 | sqlite::Type::Integer => "INT", | ||
393 | }; | ||
394 | |||
395 | let sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, ct); | ||
396 | connection.execute(sql) | ||
397 | } | ||