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, | ||||
|     }; | ||||
|   } | ||||
|   | ||||
| @@ -141,8 +141,7 @@ TEST_F(database_test, db_delete_where_query) { | ||||
|                    .and_() | ||||
|                    .where("column2") | ||||
|                    .equals("test2"); | ||||
|   std::int64_t idx{}; | ||||
|   auto query_str = query.dump(idx); | ||||
|   auto query_str = query.dump(); | ||||
|   std::cout << query_str << std::endl; | ||||
|   EXPECT_STREQ(R"(DELETE FROM "table" WHERE ("column1"=?1 AND "column2"=?2);)", | ||||
|                query_str.c_str()); | ||||
| @@ -184,8 +183,7 @@ TEST_F(database_test, db_select_where_query) { | ||||
|                    .and_() | ||||
|                    .where("column2") | ||||
|                    .equals("test2"); | ||||
|   std::int64_t idx{}; | ||||
|   auto query_str = query.dump(idx); | ||||
|   auto query_str = query.dump(); | ||||
|   std::cout << query_str << std::endl; | ||||
|   EXPECT_STREQ( | ||||
|       R"(SELECT * FROM "table" WHERE ("column1"=?1 AND "column2"=?2);)", | ||||
| @@ -201,8 +199,7 @@ TEST_F(database_test, db_select_columns_query) { | ||||
|                    .and_() | ||||
|                    .where("column2") | ||||
|                    .equals("test2"); | ||||
|   std::int64_t idx{}; | ||||
|   auto query_str = query.dump(idx); | ||||
|   auto query_str = query.dump(); | ||||
|   std::cout << query_str << std::endl; | ||||
|   EXPECT_STREQ( | ||||
|       R"(SELECT column1, column2 FROM "table" WHERE ("column1"=?1 AND "column2"=?2);)", | ||||
| @@ -217,8 +214,7 @@ TEST_F(database_test, db_update_query) { | ||||
|                    .and_() | ||||
|                    .where("column2") | ||||
|                    .equals("test2"); | ||||
|   std::int64_t idx{}; | ||||
|   auto query_str = query.dump(idx); | ||||
|   auto query_str = query.dump(); | ||||
|   std::cout << query_str << std::endl; | ||||
|   EXPECT_STREQ( | ||||
|       R"(UPDATE "table" SET "column1"=?1 WHERE ("column1"=?2 AND "column2"=?3);)", | ||||
| @@ -241,8 +237,7 @@ TEST_F(database_test, insert_update_delete) { | ||||
|                      .column_value("column1", "moose") | ||||
|                      .where("column1") | ||||
|                      .equals("test0"); | ||||
|     std::int64_t idx{}; | ||||
|     std::cout << query.dump(idx) << std::endl; | ||||
|     std::cout << query.dump() << std::endl; | ||||
|     auto res = query.go(); | ||||
|     EXPECT_TRUE(res.ok()); | ||||
|   } | ||||
| @@ -269,26 +264,4 @@ TEST_F(database_test, insert_or_replace_and_delete) { | ||||
|  | ||||
|   common_delete(*db3.get()); | ||||
| } | ||||
|  | ||||
| TEST(database, groups) { | ||||
|   // // "s=a and r=t" | ||||
|   // std::int64_t idx{0U}; | ||||
|   // auto str = | ||||
|   //     db_where_t().column("s").equals("a").and_().column("r").equals("t").dump( | ||||
|   //         idx); | ||||
|   // std::cout << str << std::endl; | ||||
|   // | ||||
|   // // "(s=a and r=t) OR (s=c or s=x)" | ||||
|   // idx = 0U; | ||||
|   // str = db_where_t() | ||||
|   //           .group([](db_where_t &where) { | ||||
|   //             where.column("s").equals("a").and_().column("r").equals("t"); | ||||
|   //           }) | ||||
|   //           .or_() | ||||
|   //           .group([](db_where_t &where) { | ||||
|   //             where.column("s").equals("c").or_().column("s").equals("x"); | ||||
|   //           }) | ||||
|   //           .dump(idx); | ||||
|   // std::cout << str << std::endl; | ||||
| } | ||||
| } // namespace repertory | ||||
|   | ||||
		Reference in New Issue
	
	Block a user