fix
This commit is contained in:
parent
08e381a307
commit
1d7f5b7ef1
@ -183,17 +183,7 @@ struct db_result final {
|
|||||||
|
|
||||||
using row = db_row<context>;
|
using row = db_row<context>;
|
||||||
|
|
||||||
db_result(sqlite3_stmt *stmt, std::int32_t res)
|
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() = default;
|
db_result() = default;
|
||||||
db_result(const db_result &) = default;
|
db_result(const db_result &) = default;
|
||||||
@ -210,38 +200,17 @@ private:
|
|||||||
void set_res(std::int32_t res) const { res_ = res; }
|
void set_res(std::int32_t res) const { res_ = res; }
|
||||||
|
|
||||||
public:
|
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() const -> std::int32_t { return res_; }
|
||||||
|
|
||||||
[[nodiscard]] auto get_error_str() const -> std::string {
|
[[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_row(std::optional<row> &opt_row) const -> bool {
|
[[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 has_row() const -> bool { return res_ == SQLITE_ROW; }
|
[[nodiscard]] auto has_row() const -> bool { return res_ == SQLITE_ROW; }
|
||||||
|
|
||||||
void next_row() const {
|
void next_row() const;
|
||||||
if (not has_row()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
set_res(sqlite3_step(ctx_->stmt.get()));
|
[[nodiscard]] auto ok() const -> bool;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
} // namespace repertory::utils::db::sqlite
|
} // namespace repertory::utils::db::sqlite
|
||||||
|
|
||||||
|
@ -76,6 +76,47 @@ auto db_column::get_value_as_json() const -> nlohmann::json {
|
|||||||
}
|
}
|
||||||
#endif // defined(PROJECT_ENABLE_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,
|
auto create_db(std::string db_path,
|
||||||
const std::map<std::string, std::string> &sql_create_tables)
|
const std::map<std::string, std::string> &sql_create_tables)
|
||||||
-> db3_t {
|
-> db3_t {
|
||||||
@ -115,8 +156,8 @@ auto create_db(std::string db_path,
|
|||||||
return db3;
|
return db3;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto execute_sql(sqlite3 &db3, const std::string &sql,
|
auto execute_sql(sqlite3 &db3, const std::string &sql, std::string &err)
|
||||||
std::string &err) -> bool {
|
-> bool {
|
||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
char *err_msg{nullptr};
|
char *err_msg{nullptr};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user