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