summaryrefslogtreecommitdiff
path: root/src/db.rs
blob: bbbca87efe8f74989ef133c6ef98832f3c6bd739 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::io::ErrorKind;

use crate::{Modloader, config::Cfg};

//TODO use prepared statements

pub fn insert_mod(id: String, name: String, current_version: String, old_versions: Vec<String>, mod_loader: Modloader, desired_mc_version: String) -> Result<(), sqlite::Error> {

    let connection = sqlite::open("./data.db").unwrap();

    let loader = match mod_loader {
        Modloader::Fabric => "fabric",
        Modloader::Forge => "forge",
    };
    
    let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}', '{}', '{}', '{}')", id, name, current_version, old_versions.join("|"), loader, desired_mc_version);

    connection.execute(sql)
}

//LIST
pub fn insert_list(config: Cfg, id: String, mc_version: String, mod_loader: Modloader) -> Result<(), sqlite::Error> {
    let data = format!("{}/data.db", config.data);
    let connection = sqlite::open(data).unwrap();
    
    //Setup list in table
    let loader = match mod_loader {
        Modloader::Fabric => "fabric",
        Modloader::Forge => "forge",
    };

    let sql_list = format!("INSERT INTO lists VALUES ('{}', '{}', '{}')", id, mc_version, loader);
    let sql_table = format!("CREATE TABLE '{}' ( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'mod_loader' TEXT )", id);
    let sql = format!("{};{};", sql_list, sql_table);

    connection.execute(sql)
}

pub fn remove_list(config: Cfg, id: String) -> Result<(), sqlite::Error> {
    let data = format!("{}/data.db", config.data);
    let connection = sqlite::open(data).unwrap();

    let sql_list = format!("DELETE FROM lists WHERE id = '{}'", id);
    let sql_table = format!("DROP TABLE '{}'", id);
    let sql = format!("{};{};", sql_list, sql_table);

    connection.execute(sql)
}

pub fn get_lists(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> {
    let data = format!("{}/data.db", config.data);
    let connection = sqlite::open(data).unwrap();

    let sql = "SELECT id FROM lists";
    
    let mut list: Vec<String> = Vec::new();
    //TODO catch sql errors better
    connection.iterate(sql, |ids| {
        if ids.is_empty() { return false; };
        for &(_column, value) in ids.iter() {
            list.push(String::from(value.unwrap()));
        }
        true
    }).unwrap();
    match list.is_empty() {
        true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_LISTS"))),
        false => Ok(list),
    }
}

//config
pub fn change_list(config: Cfg, id: String) -> Result<(), sqlite::Error> {
    let data = format!("{}/data.db", config.data);
    let connection = sqlite::open(data).unwrap();

    let sql = format!("UPDATE user_config SET value = '{}' WHERE id = 'current_list'", id);

    connection.execute(sql)
}

pub fn get_current_list(config: Cfg) -> Result<String, Box<dyn std::error::Error>> {
    let data = format!("{}/data.db", config.data);
    let connection = sqlite::open(data).unwrap();

    let sql = "SELECT id FROM lists";
    
    let mut list: String = String::new();
    //TODO catch sql errors better
    connection.iterate(sql, |ids| {
        if ids.is_empty() { return false; };
        for &(_column, value) in ids.iter() {
            list = String::from(value.unwrap());
        }
        true
    }).unwrap();
    if list.is_empty() {
        get_lists(config)?;
        panic!("current list field should never be empty if there are other lists");
    };
    Ok(list)
}