|
|
|
@ -1,17 +1,36 @@
|
|
|
|
|
#include "database.h"
|
|
|
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
Database::Database(const std::string& dbname) : db_(nullptr)
|
|
|
|
|
Database::Database(const std::string& dbname)
|
|
|
|
|
{
|
|
|
|
|
dbname_ = dbname;
|
|
|
|
|
const int errCode = sqlite3_open(dbname_.c_str(), &db_);
|
|
|
|
|
if (errCode) {
|
|
|
|
|
throw std::runtime_error("Could not open database file.");
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Database::Database()
|
|
|
|
|
{
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
fs::path dbpath = fs::path(std::getenv("HOME")) / ".local/share/kima2-cpp";
|
|
|
|
|
#elif defined(__WIN64) || defined(__WIN32)
|
|
|
|
|
fs::path dbpath = fs::path(std::getenv("LOCALAPPDATA") / "/kima2-cpp";
|
|
|
|
|
#else
|
|
|
|
|
throw std::runtime_error("Platform not supported.");
|
|
|
|
|
#endif
|
|
|
|
|
if (!fs::exists(dbpath)) {
|
|
|
|
|
try {
|
|
|
|
|
fs::create_directories(dbpath);
|
|
|
|
|
} catch (fs::filesystem_error& err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY);
|
|
|
|
|
dbpath /= "kima2.db";
|
|
|
|
|
dbname_ = dbpath.string();
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Database::~Database() { sqlite3_close(db_); }
|
|
|
|
@ -82,6 +101,12 @@ void Database::createNew()
|
|
|
|
|
|
|
|
|
|
void Database::init()
|
|
|
|
|
{
|
|
|
|
|
const int errCode = sqlite3_open(dbname_.c_str(), &db_);
|
|
|
|
|
if (errCode) {
|
|
|
|
|
throw std::runtime_error("Could not open database file.");
|
|
|
|
|
}
|
|
|
|
|
sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY);
|
|
|
|
|
|
|
|
|
|
int version = getVersion();
|
|
|
|
|
|
|
|
|
|
switch (version) {
|
|
|
|
@ -253,7 +278,7 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
|
|
|
|
|
|
|
|
|
|
endTransaction();
|
|
|
|
|
|
|
|
|
|
// Everything went fine, so we can now update our objects
|
|
|
|
|
// Everything went fine, so we can now update our objects
|
|
|
|
|
sellers.erase(std::remove_if(sellers.begin(), sellers.end(),
|
|
|
|
|
[](const std::unique_ptr<Seller>& seller) {
|
|
|
|
|
return (seller->getState() == Seller::State::DELETE);
|
|
|
|
|