fix
This commit is contained in:
@ -23,7 +23,6 @@
|
||||
#define INCLUDE_DATABASE_DB_COMMON_HPP_
|
||||
|
||||
#include "utils/error_utils.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace repertory::db {
|
||||
using db_types_t = std::variant<std::int64_t, std::string>;
|
||||
@ -106,9 +105,9 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename context_t> class db_row final {
|
||||
template <typename ctx_t> class db_row final {
|
||||
public:
|
||||
db_row(std::shared_ptr<context_t> context) {
|
||||
db_row(std::shared_ptr<ctx_t> context) {
|
||||
auto column_count = sqlite3_column_count(context->stmt.get());
|
||||
for (std::int32_t col = 0; col < column_count; col++) {
|
||||
std::string name{sqlite3_column_name(context->stmt.get(), col)};
|
||||
@ -164,8 +163,8 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename context_t> struct db_result final {
|
||||
db_result(std::shared_ptr<context_t> context, std::int32_t res)
|
||||
template <typename ctx_t> struct db_result final {
|
||||
db_result(std::shared_ptr<ctx_t> context, std::int32_t res)
|
||||
: context_(std::move(context)), res_(res) {
|
||||
constexpr const auto *function_name =
|
||||
static_cast<const char *>(__FUNCTION__);
|
||||
@ -176,7 +175,7 @@ template <typename context_t> struct db_result final {
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<context_t> context_;
|
||||
std::shared_ptr<ctx_t> context_;
|
||||
mutable std::int32_t res_;
|
||||
|
||||
private:
|
||||
@ -200,8 +199,7 @@ public:
|
||||
return sqlite3_errstr(res_);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto
|
||||
get_row(std::optional<db_row<context_t>> &row) const -> bool {
|
||||
[[nodiscard]] auto get_row(std::optional<db_row<ctx_t>> &row) const -> bool {
|
||||
constexpr const auto *function_name =
|
||||
static_cast<const char *>(__FUNCTION__);
|
||||
|
||||
@ -226,11 +224,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename cn_t, typename op_t, typename w_t, typename wn_t>
|
||||
template <typename cn_t, typename ctx_t, typename op_t, typename w_t,
|
||||
typename wn_t>
|
||||
struct db_next_t final {
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
std::string action;
|
||||
w_t &parent;
|
||||
w_t next{parent.ctx, &parent};
|
||||
|
||||
w_t next{ctx};
|
||||
|
||||
using group_func_t = std::function<void(w_t &)>;
|
||||
|
||||
@ -238,22 +238,26 @@ struct db_next_t final {
|
||||
return next.where(column_name);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); }
|
||||
|
||||
[[nodiscard]] auto dump(std::int64_t &idx) const -> std::string {
|
||||
return parent.dump(idx);
|
||||
return ctx->where->dump(idx);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{parent.ctx}.go(); }
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); }
|
||||
|
||||
[[nodiscard]] auto group(group_func_t func) -> wn_t {
|
||||
return next.group(std::move(func));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename cn_t, typename op_t, typename w_t, typename wn_t>
|
||||
template <typename cn_t, typename ctx_t, typename op_t, typename w_t,
|
||||
typename wn_t>
|
||||
struct db_next_limit_t final {
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
std::string action;
|
||||
w_t &parent;
|
||||
w_t next{parent.ctx, &parent};
|
||||
|
||||
w_t next{ctx};
|
||||
|
||||
using group_func_t = std::function<void(w_t &)>;
|
||||
|
||||
@ -261,114 +265,121 @@ struct db_next_limit_t final {
|
||||
return next.where(column_name);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); }
|
||||
|
||||
[[nodiscard]] auto dump(std::int64_t &idx) const -> std::string {
|
||||
return parent.dump(idx);
|
||||
return ctx->where->dump(idx);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{parent.ctx}.go(); }
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); }
|
||||
|
||||
[[nodiscard]] auto group(group_func_t func) -> wn_t {
|
||||
return next.group(std::move(func));
|
||||
}
|
||||
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> op_t {
|
||||
return op_t{parent.ctx}.limit(value);
|
||||
return op_t{ctx}.limit(value);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending) -> op_t {
|
||||
return op_t{parent.ctx}.order_by(column_name, ascending);
|
||||
return op_t{ctx}.order_by(column_name, ascending);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename cn_t, typename op_t, typename w_t>
|
||||
template <typename cn_t, typename ctx_t, typename op_t, typename w_t>
|
||||
struct db_where_next_t final {
|
||||
w_t &owner;
|
||||
w_t &parent;
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
w_t *owner;
|
||||
|
||||
using n_t = db_next_t<cn_t, op_t, w_t, db_where_next_t>;
|
||||
using n_t = db_next_t<cn_t, ctx_t, op_t, w_t, db_where_next_t>;
|
||||
|
||||
[[nodiscard]] auto and_() -> n_t & {
|
||||
owner.actions.emplace_back(n_t{
|
||||
owner->actions.emplace_back(n_t{
|
||||
ctx,
|
||||
"AND",
|
||||
parent,
|
||||
});
|
||||
|
||||
return std::get<n_t>(*std::prev(owner.actions.end()));
|
||||
return std::get<n_t>(*std::prev(owner->actions.end()));
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); }
|
||||
|
||||
[[nodiscard]] auto dump(std::int64_t &idx) const -> std::string {
|
||||
return parent.dump(idx);
|
||||
return ctx->where->dump(idx);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{parent.ctx}.go(); }
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); }
|
||||
|
||||
[[nodiscard]] auto or_() -> n_t & {
|
||||
owner.actions.emplace_back(n_t{
|
||||
owner->actions.emplace_back(n_t{
|
||||
ctx,
|
||||
"OR",
|
||||
parent,
|
||||
});
|
||||
|
||||
return std::get<n_t>(*std::prev(owner.actions.end()));
|
||||
return std::get<n_t>(*std::prev(owner->actions.end()));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename cn_t, typename op_t, typename w_t>
|
||||
template <typename cn_t, typename ctx_t, typename op_t, typename w_t>
|
||||
struct db_where_next_limit_t final {
|
||||
w_t &owner;
|
||||
w_t &parent;
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
w_t *owner;
|
||||
|
||||
using n_t = db_next_limit_t<cn_t, op_t, w_t, db_where_next_limit_t>;
|
||||
using n_t = db_next_limit_t<cn_t, ctx_t, op_t, w_t, db_where_next_limit_t>;
|
||||
|
||||
[[nodiscard]] auto and_() -> n_t & {
|
||||
owner.actions.emplace_back(n_t{
|
||||
owner->actions.emplace_back(n_t{
|
||||
ctx,
|
||||
"AND",
|
||||
parent,
|
||||
});
|
||||
|
||||
return std::get<n_t>(*std::prev(owner.actions.end()));
|
||||
return std::get<n_t>(*std::prev(owner->actions.end()));
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); }
|
||||
|
||||
[[nodiscard]] auto dump(std::int64_t &idx) const -> std::string {
|
||||
return parent.dump(idx);
|
||||
return ctx->where->dump(idx);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{parent.ctx}.go(); }
|
||||
[[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); }
|
||||
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> op_t {
|
||||
return op_t{parent.ctx}.limit(value);
|
||||
return op_t{ctx}.limit(value);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending) -> op_t {
|
||||
return op_t{parent.ctx}.order_by(column_name, ascending);
|
||||
return op_t{ctx}.order_by(column_name, ascending);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto or_() -> n_t & {
|
||||
owner.actions.emplace_back(n_t{
|
||||
owner->actions.emplace_back(n_t{
|
||||
ctx,
|
||||
"OR",
|
||||
parent,
|
||||
});
|
||||
|
||||
return std::get<n_t>(*std::prev(owner.actions.end()));
|
||||
return std::get<n_t>(*std::prev(owner->actions.end()));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename op_t, typename w_t> struct db_comp_next_t final {
|
||||
w_t &owner;
|
||||
w_t &parent;
|
||||
template <typename ctx_t, typename op_t, typename w_t>
|
||||
struct db_comp_next_t final {
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
w_t *owner;
|
||||
std::string column_name;
|
||||
|
||||
using wn_t = db_where_next_t<db_comp_next_t, op_t, w_t>;
|
||||
using wn_t = db_where_next_t<db_comp_next_t, ctx_t, op_t, w_t>;
|
||||
|
||||
[[nodiscard]] auto create(std::string operation, db::db_types_t value) {
|
||||
owner.actions.emplace_back(comp_data_t{
|
||||
owner->actions.emplace_back(comp_data_t{
|
||||
column_name,
|
||||
operation,
|
||||
value,
|
||||
});
|
||||
|
||||
return wn_t{
|
||||
ctx,
|
||||
owner,
|
||||
parent,
|
||||
};
|
||||
}
|
||||
|
||||
@ -387,23 +398,24 @@ template <typename op_t, typename w_t> struct db_comp_next_t final {
|
||||
auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); };
|
||||
};
|
||||
|
||||
template <typename op_t, typename w_t> struct db_comp_next_limit_t final {
|
||||
w_t &owner;
|
||||
w_t &parent;
|
||||
template <typename ctx_t, typename op_t, typename w_t>
|
||||
struct db_comp_next_limit_t final {
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
w_t *owner;
|
||||
std::string column_name;
|
||||
|
||||
using wn_t = db_where_next_limit_t<db_comp_next_limit_t, op_t, w_t>;
|
||||
using wn_t = db_where_next_limit_t<db_comp_next_limit_t, ctx_t, op_t, w_t>;
|
||||
|
||||
[[nodiscard]] auto create(std::string operation, db::db_types_t value) {
|
||||
owner.actions.emplace_back(comp_data_t{
|
||||
owner->actions.emplace_back(comp_data_t{
|
||||
column_name,
|
||||
operation,
|
||||
value,
|
||||
});
|
||||
|
||||
return wn_t{
|
||||
ctx,
|
||||
owner,
|
||||
parent,
|
||||
};
|
||||
}
|
||||
|
||||
@ -422,13 +434,12 @@ template <typename op_t, typename w_t> struct db_comp_next_limit_t final {
|
||||
auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); };
|
||||
};
|
||||
|
||||
template <typename context_t, typename operation_t> struct db_where_t final {
|
||||
std::shared_ptr<context_t> ctx;
|
||||
db_where_t *parent{this};
|
||||
template <typename ctx_t, typename op_t> struct db_where_t final {
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
|
||||
using cn_t = db_comp_next_t<operation_t, db_where_t>;
|
||||
using wn_t = db_where_next_t<cn_t, operation_t, db_where_t>;
|
||||
using n_t = db_next_t<cn_t, operation_t, db_where_t, wn_t>;
|
||||
using cn_t = db_comp_next_t<ctx_t, op_t, db_where_t>;
|
||||
using wn_t = db_where_next_t<cn_t, ctx_t, op_t, db_where_t>;
|
||||
using n_t = db_next_t<cn_t, ctx_t, op_t, db_where_t, wn_t>;
|
||||
|
||||
using group_func_t = std::function<void(db_where_t &)>;
|
||||
|
||||
@ -460,38 +471,38 @@ template <typename context_t, typename operation_t> struct db_where_t final {
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); }
|
||||
|
||||
[[nodiscard]] auto dump(std::int64_t &idx) const -> std::string {
|
||||
return dump(idx, *this);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto group(group_func_t func) -> wn_t {
|
||||
db_where_t where{ctx, parent};
|
||||
db_where_t where{ctx};
|
||||
func(where);
|
||||
|
||||
actions.emplace_back(std::move(where));
|
||||
return wn_t{
|
||||
*this,
|
||||
*parent,
|
||||
ctx,
|
||||
this,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) -> cn_t {
|
||||
return cn_t{
|
||||
*this,
|
||||
*parent,
|
||||
ctx,
|
||||
this,
|
||||
column_name,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
template <typename context_t, typename operation_t>
|
||||
struct db_where_with_limit_t final {
|
||||
std::shared_ptr<context_t> ctx;
|
||||
db_where_with_limit_t *parent{this};
|
||||
template <typename ctx_t, typename op_t> struct db_where_with_limit_t final {
|
||||
std::shared_ptr<ctx_t> ctx;
|
||||
|
||||
using cn_t = db_comp_next_limit_t<operation_t, db_where_with_limit_t>;
|
||||
using wn_t = db_where_next_limit_t<cn_t, operation_t, db_where_with_limit_t>;
|
||||
using n_t = db_next_limit_t<cn_t, operation_t, db_where_with_limit_t, wn_t>;
|
||||
using cn_t = db_comp_next_limit_t<ctx_t, op_t, db_where_with_limit_t>;
|
||||
using wn_t = db_where_next_limit_t<cn_t, ctx_t, op_t, db_where_with_limit_t>;
|
||||
using n_t = db_next_limit_t<cn_t, ctx_t, op_t, db_where_with_limit_t, wn_t>;
|
||||
|
||||
using group_func_t = std::function<void(db_where_with_limit_t &)>;
|
||||
|
||||
@ -523,34 +534,35 @@ struct db_where_with_limit_t final {
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); }
|
||||
|
||||
[[nodiscard]] auto dump(std::int64_t &idx) const -> std::string {
|
||||
return dump(idx, *this);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto group(group_func_t func) -> wn_t {
|
||||
db_where_with_limit_t where{ctx, parent};
|
||||
db_where_with_limit_t where{ctx};
|
||||
func(where);
|
||||
|
||||
actions.emplace_back(std::move(where));
|
||||
return wn_t{
|
||||
*this,
|
||||
*parent,
|
||||
ctx,
|
||||
this,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> operation_t {
|
||||
return operation_t{ctx}.limit(value);
|
||||
[[nodiscard]] auto limit(std::int32_t value) -> op_t {
|
||||
return op_t{ctx}.limit(value);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto order_by(std::string column_name,
|
||||
bool ascending) -> operation_t {
|
||||
return operation_t{ctx}.order_by(column_name, ascending);
|
||||
[[nodiscard]] auto order_by(std::string column_name, bool ascending) -> op_t {
|
||||
return op_t{ctx}.order_by(column_name, ascending);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto where(std::string column_name) -> cn_t {
|
||||
return cn_t{
|
||||
*this,
|
||||
*parent,
|
||||
ctx,
|
||||
this,
|
||||
column_name,
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user