added better database lookup api
This commit is contained in:
parent
ac6e47d906
commit
4916b4f1c1
@ -25,22 +25,43 @@ public:
|
||||
|
||||
std::set<std::string> getStrSet(const std::string& sql);
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> getSqlData(sqlite3_stmt* stmt, int i)
|
||||
{
|
||||
if ((stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW))
|
||||
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);
|
||||
T ret;
|
||||
|
||||
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))
|
||||
return {};
|
||||
|
||||
if constexpr (std::is_same_v<T, int>) {
|
||||
ret = sqlite3_column_int64(stmt, 0);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, std::string>){
|
||||
ret = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||
}
|
||||
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 ret;
|
||||
return std::make_pair(v1.value(), v2.value());
|
||||
}
|
||||
|
||||
sqlite3_stmt* bind(const std::string sql, const QueryData& data);
|
||||
|
||||
@ -94,9 +94,9 @@ bool loginUser(const std::string& username, const std::string& password)
|
||||
if (!db.open())
|
||||
return false;
|
||||
|
||||
auto opt_str = db.get<std::string>(sql, {username});
|
||||
if (opt_str.has_value()) {
|
||||
return verifyHashWithPassword(opt_str.value(), password);
|
||||
auto opt_pair = db.get<int, std::string>(sql, {username});
|
||||
if (opt_pair.has_value()) {
|
||||
return verifyHashWithPassword(opt_pair.value().second, password);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user