AutoCatGnome/services/Storage.cpp

43 lines
945 B
C++

//
// Created by selim on 25.10.22.
//
#include "Storage.h"
#include "Settings.h"
#include <fmt/core.h>
Storage Storage::instance() {
static Storage instance;
return instance;
}
Storage::Storage(): _sqlite(nullptr) {
auto path = Settings::getDataPath() / "vehicles.sqlite";
int result = sqlite3_open(path.c_str(), &_sqlite);
if(result != SQLITE_OK) {
throw std::runtime_error(sqlite3_errmsg(_sqlite));
}
executeQuery(Vehicle::createTableQuery());
}
Storage::~Storage() {
if(_sqlite) {
sqlite3_close(_sqlite);
}
}
void Storage::executeQuery(const std::string& query) {
char* error = nullptr;
sqlite3_exec(_sqlite, query.c_str(), nullptr, nullptr, &error);
if(error) {
throw std::runtime_error(error);
// TODO: error buffer should be deallocated with sqlite3_free()
}
}
void Storage::insert(IDBEntity *entity) {
executeQuery(entity->insertQuery());
}