diff --git a/src/database/database.hpp b/src/database/database.hpp index 7a5ea23..3a15dc7 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -25,22 +25,43 @@ public: std::set getStrSet(const std::string& sql); + template + std::optional getSqlData(sqlite3_stmt* stmt, int i) + { + if ((stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW)) + return {}; + + if constexpr (std::is_same_v) { + return sqlite3_column_int64(stmt, i); + } + else if constexpr (std::is_same_v){ + return reinterpret_cast(sqlite3_column_text(stmt, i)); + } + } + template std::optional get(const std::string sql, const QueryData& data){ sqlite3_stmt* stmt = bind(sql, data); - T ret; + + std::optional ret = getSqlData(stmt, 0); + sqlite3_finalize(stmt); + return ret; + } + + template + std::optional> get(const std::string sql, const QueryData& data){ + sqlite3_stmt* stmt = bind(sql, data); if ((stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW)) return {}; - if constexpr (std::is_same_v) { - ret = sqlite3_column_int64(stmt, 0); - } - else if constexpr (std::is_same_v){ - ret = reinterpret_cast(sqlite3_column_text(stmt, 0)); - } + std::optional v1 = getSqlData(stmt, 0); + std::optional v2 = getSqlData(stmt, 1); + + if (!v1.has_value() || !v2.has_value()) + return {}; sqlite3_finalize(stmt); - return ret; + return std::make_pair(v1.value(), v2.value()); } sqlite3_stmt* bind(const std::string sql, const QueryData& data); diff --git a/src/login/login.cpp b/src/login/login.cpp index ef2f525..167d520 100644 --- a/src/login/login.cpp +++ b/src/login/login.cpp @@ -94,9 +94,9 @@ bool loginUser(const std::string& username, const std::string& password) if (!db.open()) return false; - auto opt_str = db.get(sql, {username}); - if (opt_str.has_value()) { - return verifyHashWithPassword(opt_str.value(), password); + auto opt_pair = db.get(sql, {username}); + if (opt_pair.has_value()) { + return verifyHashWithPassword(opt_pair.value().second, password); } else { return false; }