summaryrefslogblamecommitdiff
path: root/src/db.rs
blob: bbbca87efe8f74989ef133c6ef98832f3c6bd739 (plain) (tree)
1
2
3
4
5




                                    










                                                                                                                                                                                 







                                                                                                                     
    








                                                                                                                                              

                           
 
 





























































                                                                                           
 
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)
}