cleaned the code
This commit is contained in:
@@ -13,75 +13,9 @@
|
||||
#include "ShadowrunDb.hpp"
|
||||
#include "loginDb.hpp"
|
||||
|
||||
class Database {
|
||||
|
||||
typedef std::vector<std::variant<int64_t, std::string>> QueryData;
|
||||
|
||||
public:
|
||||
namespace Database {
|
||||
static constexpr std::string dbFile = "test.db";
|
||||
|
||||
Database();
|
||||
~Database();
|
||||
|
||||
bool open();
|
||||
bool exec(const char* sqlQuery);
|
||||
bool exec(const std::string& sqlQuery);
|
||||
|
||||
std::optional<int64_t> insert(const std::string& sql);
|
||||
|
||||
std::set<std::string> getStrSet(const std::string& sql);
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> getSqlData(sqlite3_stmt* stmt, int i)
|
||||
{
|
||||
if (stmt == nullptr)
|
||||
return {};
|
||||
|
||||
if constexpr (std::is_same_v<T, int>) {
|
||||
return sqlite3_column_int64(stmt, i);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, std::string>){
|
||||
return reinterpret_cast<const char*>(sqlite3_column_text(stmt, i));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> get(const std::string sql, const QueryData& data){
|
||||
sqlite3_stmt* stmt = bind(sql, data);
|
||||
|
||||
std::optional<T> ret = getSqlData<T>(stmt, 0);
|
||||
sqlite3_finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
std::optional<std::pair<T1, T2>> get(const std::string sql, const QueryData& data){
|
||||
sqlite3_stmt* stmt = bind(sql, data);
|
||||
if ( (stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW ) ) {
|
||||
CROW_LOG_ERROR << "Failed to run statement: " << sqlite3_errmsg(m_db);
|
||||
return {};
|
||||
}
|
||||
std::optional<T1> v1 = getSqlData<T1>(stmt, 0);
|
||||
std::optional<T2> v2 = getSqlData<T2>(stmt, 1);
|
||||
|
||||
if (!v1.has_value() || !v2.has_value())
|
||||
return {};
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return std::make_pair(v1.value(), v2.value());
|
||||
}
|
||||
|
||||
sqlite3_stmt* bind(const std::string sql, const QueryData& data);
|
||||
|
||||
std::map<std::string, std::string> getStrMap(const std::string sql, const QueryData& data);
|
||||
|
||||
private:
|
||||
sqlite3_stmt* prepareStmt(const std::string& sql);
|
||||
|
||||
sqlite3* m_db;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
inline auto make_database() {
|
||||
auto storage = sqlite_orm::make_storage(Database::dbFile,
|
||||
|
||||
Reference in New Issue
Block a user