65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
//
|
|
// Created by selim on 25.10.22.
|
|
//
|
|
|
|
#ifndef AUTOCAT_GNOME_STORAGE_H
|
|
#define AUTOCAT_GNOME_STORAGE_H
|
|
|
|
#include "../models/Vehicle.h"
|
|
#include "IDBEntity.h"
|
|
|
|
#include <utility>
|
|
#include <sqlite3.h>
|
|
|
|
class Storage {
|
|
private:
|
|
sqlite3* _sqlite;
|
|
|
|
private:
|
|
void executeQuery(const std::string& query);
|
|
|
|
public:
|
|
Storage();
|
|
~Storage();
|
|
static Storage instance();
|
|
void insert(IDBEntity* entity);
|
|
|
|
template<typename T>
|
|
std::vector<T> select() {
|
|
auto sql = T::selectQuery();
|
|
sqlite3_stmt* stmt = nullptr;
|
|
std::vector<T> rows;
|
|
|
|
int result = sqlite3_prepare_v2(_sqlite, sql.c_str(), sql.size(), &stmt, nullptr);
|
|
if(result != SQLITE_OK) {
|
|
throw std::runtime_error(sqlite3_errmsg(_sqlite));
|
|
}
|
|
|
|
do {
|
|
result = sqlite3_step(stmt);
|
|
switch(result) {
|
|
case SQLITE_BUSY:
|
|
// TODO: Rollback transaction
|
|
break;
|
|
case SQLITE_ERROR:
|
|
throw std::runtime_error(sqlite3_errmsg(_sqlite));
|
|
case SQLITE_ROW:
|
|
rows.emplace_back(stmt);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} while (result == SQLITE_ROW);
|
|
|
|
result = sqlite3_finalize(stmt);
|
|
if(result != SQLITE_OK) {
|
|
throw std::runtime_error(sqlite3_errmsg(_sqlite));
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
};
|
|
|
|
|
|
#endif //AUTOCAT_GNOME_STORAGE_H
|