added better database get api
This commit is contained in:
@@ -21,14 +21,27 @@ public:
|
||||
bool exec(const char* sqlQuery);
|
||||
bool exec(const std::string& sqlQuery);
|
||||
|
||||
/// returns true if the sql statment returns at least one row
|
||||
std::optional<int64_t> getInt(const char* sql);
|
||||
|
||||
std::optional<int64_t> insert(const char* sql);
|
||||
|
||||
std::set<std::string> getStrSet(const std::string& sql);
|
||||
|
||||
std::string getStr(const std::string sql, const QueryData& data);
|
||||
template <typename T>
|
||||
std::optional<T> get(const std::string sql, const QueryData& data){
|
||||
sqlite3_stmt* stmt = bind(sql, data);
|
||||
T ret;
|
||||
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));
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
sqlite3_stmt* bind(const std::string sql, const QueryData& data);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user