refactor
This commit is contained in:
@ -66,8 +66,6 @@ struct db_context_t {
|
||||
|
||||
sqlite3 *db3{};
|
||||
std::string table_name;
|
||||
|
||||
db3_stmt_t stmt;
|
||||
};
|
||||
|
||||
class db_column final {
|
||||
@ -178,9 +176,20 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ctx_t> struct db_result final {
|
||||
db_result(std::shared_ptr<ctx_t> ctx, std::int32_t res)
|
||||
: ctx_(std::move(ctx)), res_(res) {
|
||||
struct db_result final {
|
||||
struct context final {
|
||||
db3_stmt_t stmt;
|
||||
};
|
||||
|
||||
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()));
|
||||
}
|
||||
@ -193,14 +202,8 @@ template <typename ctx_t> struct db_result final {
|
||||
auto operator=(const db_result &) -> db_result & = default;
|
||||
auto operator=(db_result &&) -> db_result & = default;
|
||||
|
||||
~db_result() {
|
||||
if (ctx_) {
|
||||
ctx_->clear();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<ctx_t> ctx_;
|
||||
std::shared_ptr<context> ctx_;
|
||||
mutable std::int32_t res_{};
|
||||
|
||||
private:
|
||||
@ -218,14 +221,14 @@ public:
|
||||
return err_msg == nullptr ? std::to_string(res_) : err_msg;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_row(std::optional<db_row<ctx_t>> &row) const -> bool {
|
||||
row.reset();
|
||||
[[nodiscard]] auto get_row(std::optional<row> &opt_row) const -> bool {
|
||||
opt_row.reset();
|
||||
|
||||
if (not has_row()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
row = db_row{ctx_};
|
||||
opt_row = db_row<context>{ctx_};
|
||||
set_res(sqlite3_step(ctx_->stmt.get()));
|
||||
return true;
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ public:
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
};
|
||||
|
||||
context(sqlite3 *db3_, std::string table_name_)
|
||||
@ -64,10 +64,10 @@ private:
|
||||
public:
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
|
||||
[[nodiscard]] auto
|
||||
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
|
||||
[[nodiscard]] auto group(context::w_t::group_func_t func)
|
||||
-> context::w_t::wn_t;
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
|
||||
};
|
||||
|
@ -55,12 +55,14 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto column_value(std::string column_name,
|
||||
db_types_t value) -> db_insert &;
|
||||
[[nodiscard]] auto column_value(std::string column_name, db_types_t value)
|
||||
|
||||
|
||||
-> db_insert &;
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
};
|
||||
} // namespace repertory::utils::db::sqlite
|
||||
|
||||
|
@ -36,7 +36,7 @@ public:
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
|
||||
[[nodiscard]] auto group_by(std::string column_name) -> db_select_op_t;
|
||||
|
||||
@ -44,8 +44,10 @@ public:
|
||||
|
||||
[[nodiscard]] auto offset(std::int32_t value) -> db_select_op_t;
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name,
|
||||
bool ascending) -> db_select_op_t;
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
|
||||
|
||||
|
||||
-> db_select_op_t;
|
||||
};
|
||||
|
||||
context(sqlite3 *db3_, std::string table_name_)
|
||||
@ -67,8 +69,6 @@ public:
|
||||
void clear();
|
||||
};
|
||||
|
||||
using row = db_row<context>;
|
||||
|
||||
public:
|
||||
db_select(sqlite3 &db3, std::string table_name)
|
||||
: ctx_(std::make_shared<context>(&db3, table_name)) {}
|
||||
@ -81,24 +81,30 @@ private:
|
||||
public:
|
||||
[[nodiscard]] auto column(std::string column_name) -> db_select &;
|
||||
|
||||
[[nodiscard]] auto count(std::string column_name,
|
||||
std::string as_column_name) -> db_select &;
|
||||
[[nodiscard]] auto count(std::string column_name, std::string as_column_name)
|
||||
|
||||
|
||||
-> db_select &;
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
|
||||
[[nodiscard]] auto group_by(std::string column_name) -> db_select &;
|
||||
|
||||
[[nodiscard]] auto
|
||||
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
|
||||
[[nodiscard]] auto group(context::w_t::group_func_t func)
|
||||
|
||||
|
||||
-> context::w_t::wn_t;
|
||||
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> db_select &;
|
||||
|
||||
[[nodiscard]] auto offset(std::int32_t value) -> db_select &;
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name,
|
||||
bool ascending) -> db_select &;
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
|
||||
|
||||
|
||||
-> db_select &;
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
|
||||
};
|
||||
|
@ -39,12 +39,14 @@ public:
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> db_update_op_t;
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name,
|
||||
bool ascending) -> db_update_op_t;
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
|
||||
|
||||
|
||||
-> db_update_op_t;
|
||||
};
|
||||
|
||||
using w_t = db_where_t<context, db_update_op_t>;
|
||||
@ -71,20 +73,26 @@ private:
|
||||
std::shared_ptr<context> ctx_;
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto column_value(std::string column_name,
|
||||
db_types_t value) -> db_update &;
|
||||
[[nodiscard]] auto column_value(std::string column_name, db_types_t value)
|
||||
|
||||
|
||||
-> db_update &;
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string;
|
||||
|
||||
[[nodiscard]] auto go() const -> db_result<context>;
|
||||
[[nodiscard]] auto go() const -> db_result;
|
||||
|
||||
[[nodiscard]] auto
|
||||
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
|
||||
[[nodiscard]] auto group(context::w_t::group_func_t func)
|
||||
|
||||
|
||||
-> context::w_t::wn_t;
|
||||
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> db_update &;
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name,
|
||||
bool ascending) -> db_update &;
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending)
|
||||
|
||||
|
||||
-> db_update &;
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
|
||||
};
|
||||
|
Reference in New Issue
Block a user