Possible to load basic data from the database
This commit is contained in:
110
src/shadowrun/ShadowrunDb.cpp
Normal file
110
src/shadowrun/ShadowrunDb.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include <format>
|
||||
#include "database.hpp"
|
||||
#include "crow.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace shadowrun {
|
||||
|
||||
bool initDb() {
|
||||
auto db = Database();
|
||||
|
||||
if (!db.open()){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a tables
|
||||
const char* create_sql_chars = "CREATE TABLE IF NOT EXISTS shadowrun_characters ("
|
||||
"id INTEGER PRIMARY KEY,"
|
||||
"name TEXT NOT NULL,"
|
||||
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP);";
|
||||
|
||||
if (!db.exec(create_sql_chars)){
|
||||
CROW_LOG_ERROR << "Failed to create shadowrun_characters table";
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* create_sql_data = "CREATE TABLE IF NOT EXISTS shadowrun_data ("
|
||||
"id INTEGER PRIMARY KEY,"
|
||||
"character_id INTEGER NOT NULL,"
|
||||
"name TEXT NOT NULL,"
|
||||
"value TEXT,"
|
||||
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
"updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
"FOREIGN KEY (character_id) REFERENCES characters(id) ON DELETE CASCADE);";
|
||||
|
||||
if (!db.exec(create_sql_data)){
|
||||
CROW_LOG_ERROR << "Failed to create shadowrun_data table";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t getKeyOfCharacter(const string& name){
|
||||
auto sql = format("SELECT 1 FROM shadowrun_characters WHERE name = '{}' LIMIT 1;", name);
|
||||
auto db = Database();
|
||||
|
||||
if (!db.open())
|
||||
return -1;
|
||||
|
||||
auto opt_int = db.getInt(sql.c_str());
|
||||
if (opt_int.has_value()) {
|
||||
return opt_int.value();
|
||||
} else {
|
||||
sql = format("INSERT INTO shadowrun_characters (name) VALUES ('{}');", name);
|
||||
auto key = db.insert(sql.c_str());
|
||||
|
||||
if(key.has_value()){
|
||||
return key.value();
|
||||
} else {
|
||||
CROW_LOG_ERROR << "Failed to insert character " << name;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool storeCharacterData(int64_t characterKey, vector<pair<const string&, const string&>>& idValues){
|
||||
auto sql = format("SELECT name FROM shadowrun_data WHERE character_id = {};", characterKey);
|
||||
auto db = Database();
|
||||
if (!db.open())
|
||||
return false;
|
||||
|
||||
auto set = db.getStrSet(sql);
|
||||
|
||||
for (auto& idValue : idValues) {
|
||||
// update if already exist
|
||||
if(set.contains(idValue.first)){
|
||||
auto sql = format("UPDATE shadowrun_data SET value = {}, updated_at = CURRENT_TIMESTAMP WHERE name = {} AND character_id = {}", idValue.second, idValue.first, characterKey);
|
||||
if (!db.exec(sql)){
|
||||
CROW_LOG_WARNING << "Failed to update " << idValue.first << " with " << idValue.second;
|
||||
}
|
||||
} else {
|
||||
auto sql = format("INSERT INTO shadowrun_data (character_id, name, value)"
|
||||
"VALUES ({}, {}, {})", characterKey, idValue.first, idValue.second);
|
||||
if (!db.exec(sql)){
|
||||
CROW_LOG_WARNING << "Failed to insert " << idValue.first << " with " << idValue.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::set<std::string> getCharacters(){
|
||||
string sql = "SELECT name FROM shadowrun_characters;";
|
||||
auto db = Database();
|
||||
if (!db.open())
|
||||
return std::set<std::string>();
|
||||
|
||||
return db.getStrSet(sql);
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> getCharacterData(int64_t characterKey) {
|
||||
auto sql = format("SELECT name, value FROM shadowrun_data WHERE character_id = {};", characterKey);
|
||||
auto db = Database();
|
||||
if (!db.open())
|
||||
return std::map<std::string, std::string>();
|
||||
|
||||
return db.getStrMap(sql);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user