This commit is contained in:
Scott E. Graves 2024-10-22 15:04:38 -05:00
parent 08e381a307
commit 1d7f5b7ef1
2 changed files with 48 additions and 38 deletions

View File

@ -183,17 +183,7 @@ struct db_result final {
using row = db_row<context>;
db_result(sqlite3_stmt *stmt, std::int32_t res)
: ctx_(std::make_shared<context>()), 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<row> &opt_row) const -> bool {
opt_row.reset();
if (not has_row()) {
return false;
}
opt_row = db_row<context>{ctx_};
set_res(sqlite3_step(ctx_->stmt.get()));
return true;
}
[[nodiscard]] auto get_row(std::optional<row> &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

View File

@ -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<context>()), 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<row> &opt_row) const -> bool {
opt_row.reset();
if (not has_row()) {
return false;
}
opt_row = db_row<context>{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<std::string, std::string> &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};