diff --git a/support/include/utils/db/sqlite/db_common.hpp b/support/include/utils/db/sqlite/db_common.hpp index 9910453b..d293e46e 100644 --- a/support/include/utils/db/sqlite/db_common.hpp +++ b/support/include/utils/db/sqlite/db_common.hpp @@ -183,17 +183,7 @@ struct db_result final { using row = db_row; - db_result(sqlite3_stmt *stmt, std::int32_t res) - : ctx_(std::make_shared()), res_(res) { - ctx_->stmt = db3_stmt_t{ - stmt, - sqlite3_statement_deleter(), - }; - - if (res == SQLITE_OK) { - set_res(sqlite3_step(ctx_->stmt.get())); - } - } + db_result(sqlite3_stmt *stmt, std::int32_t res); db_result() = default; db_result(const db_result &) = default; @@ -210,38 +200,17 @@ private: void set_res(std::int32_t res) const { res_ = res; } public: - [[nodiscard]] auto ok() const -> bool { - return res_ == SQLITE_DONE || res_ == SQLITE_ROW; - } - [[nodiscard]] auto get_error() const -> std::int32_t { return res_; } - [[nodiscard]] auto get_error_str() const -> std::string { - auto &&err_msg = sqlite3_errstr(res_); - return err_msg == nullptr ? std::to_string(res_) : err_msg; - } + [[nodiscard]] auto get_error_str() const -> std::string; - [[nodiscard]] auto get_row(std::optional &opt_row) const -> bool { - opt_row.reset(); - - if (not has_row()) { - return false; - } - - opt_row = db_row{ctx_}; - set_res(sqlite3_step(ctx_->stmt.get())); - return true; - } + [[nodiscard]] auto get_row(std::optional &opt_row) const -> bool; [[nodiscard]] auto has_row() const -> bool { return res_ == SQLITE_ROW; } - void next_row() const { - if (not has_row()) { - return; - } + void next_row() const; - set_res(sqlite3_step(ctx_->stmt.get())); - } + [[nodiscard]] auto ok() const -> bool; }; } // namespace repertory::utils::db::sqlite diff --git a/support/src/utils/db/sqlite/db_common.cpp b/support/src/utils/db/sqlite/db_common.cpp index 62897102..e6089820 100644 --- a/support/src/utils/db/sqlite/db_common.cpp +++ b/support/src/utils/db/sqlite/db_common.cpp @@ -76,6 +76,47 @@ auto db_column::get_value_as_json() const -> nlohmann::json { } #endif // defined(PROJECT_ENABLE_JSON) +db_result::db_result(sqlite3_stmt *stmt, std::int32_t res) + : ctx_(std::make_shared()), res_(res) { + ctx_->stmt = db3_stmt_t{ + stmt, + sqlite3_statement_deleter(), + }; + + if (res == SQLITE_OK) { + set_res(sqlite3_step(ctx_->stmt.get())); + } +} + +auto db_result::get_error_str() const -> std::string { + auto &&err_msg = sqlite3_errstr(res_); + return err_msg == nullptr ? std::to_string(res_) : err_msg; +} + +auto db_result::get_row(std::optional &opt_row) const -> bool { + opt_row.reset(); + + if (not has_row()) { + return false; + } + + opt_row = db_row{ctx_}; + set_res(sqlite3_step(ctx_->stmt.get())); + return true; +} + +void db_result::next_row() const { + if (not has_row()) { + return; + } + + set_res(sqlite3_step(ctx_->stmt.get())); +} + +auto db_result::ok() const -> bool { + return res_ == SQLITE_DONE || res_ == SQLITE_ROW; +} + auto create_db(std::string db_path, const std::map &sql_create_tables) -> db3_t { @@ -115,8 +156,8 @@ auto create_db(std::string db_path, return db3; } -auto execute_sql(sqlite3 &db3, const std::string &sql, - std::string &err) -> bool { +auto execute_sql(sqlite3 &db3, const std::string &sql, std::string &err) + -> bool { REPERTORY_USES_FUNCTION_NAME(); char *err_msg{nullptr};