diff --git a/repertory/librepertory/include/database/db_common.hpp b/repertory/librepertory/include/database/db_common.hpp index 8f5fb86c..9e4ea2ea 100644 --- a/repertory/librepertory/include/database/db_common.hpp +++ b/repertory/librepertory/include/database/db_common.hpp @@ -23,7 +23,6 @@ #define INCLUDE_DATABASE_DB_COMMON_HPP_ #include "utils/error_utils.hpp" -#include namespace repertory::db { using db_types_t = std::variant; @@ -106,9 +105,9 @@ public: } }; -template class db_row final { +template class db_row final { public: - db_row(std::shared_ptr context) { + db_row(std::shared_ptr 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 struct db_result final { - db_result(std::shared_ptr context, std::int32_t res) +template struct db_result final { + db_result(std::shared_ptr context, std::int32_t res) : context_(std::move(context)), res_(res) { constexpr const auto *function_name = static_cast(__FUNCTION__); @@ -176,7 +175,7 @@ template struct db_result final { } private: - std::shared_ptr context_; + std::shared_ptr context_; mutable std::int32_t res_; private: @@ -200,8 +199,7 @@ public: return sqlite3_errstr(res_); } - [[nodiscard]] auto - get_row(std::optional> &row) const -> bool { + [[nodiscard]] auto get_row(std::optional> &row) const -> bool { constexpr const auto *function_name = static_cast(__FUNCTION__); @@ -226,11 +224,13 @@ public: } }; -template +template struct db_next_t final { + std::shared_ptr ctx; std::string action; - w_t &parent; - w_t next{parent.ctx, &parent}; + + w_t next{ctx}; using group_func_t = std::function; @@ -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 +template struct db_next_limit_t final { + std::shared_ptr ctx; std::string action; - w_t &parent; - w_t next{parent.ctx, &parent}; + + w_t next{ctx}; using group_func_t = std::function; @@ -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 +template struct db_where_next_t final { - w_t &owner; - w_t &parent; + std::shared_ptr ctx; + w_t *owner; - using n_t = db_next_t; + using n_t = db_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(*std::prev(owner.actions.end())); + return std::get(*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(*std::prev(owner.actions.end())); + return std::get(*std::prev(owner->actions.end())); } }; -template +template struct db_where_next_limit_t final { - w_t &owner; - w_t &parent; + std::shared_ptr ctx; + w_t *owner; - using n_t = db_next_limit_t; + using n_t = db_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(*std::prev(owner.actions.end())); + return std::get(*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(*std::prev(owner.actions.end())); + return std::get(*std::prev(owner->actions.end())); } }; -template struct db_comp_next_t final { - w_t &owner; - w_t &parent; +template +struct db_comp_next_t final { + std::shared_ptr ctx; + w_t *owner; std::string column_name; - using wn_t = db_where_next_t; + using wn_t = db_where_next_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 struct db_comp_next_t final { auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); }; }; -template struct db_comp_next_limit_t final { - w_t &owner; - w_t &parent; +template +struct db_comp_next_limit_t final { + std::shared_ptr ctx; + w_t *owner; std::string column_name; - using wn_t = db_where_next_limit_t; + using wn_t = db_where_next_limit_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 struct db_comp_next_limit_t final { auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); }; }; -template struct db_where_t final { - std::shared_ptr ctx; - db_where_t *parent{this}; +template struct db_where_t final { + std::shared_ptr ctx; - using cn_t = db_comp_next_t; - using wn_t = db_where_next_t; - using n_t = db_next_t; + using cn_t = db_comp_next_t; + using wn_t = db_where_next_t; + using n_t = db_next_t; using group_func_t = std::function; @@ -460,38 +471,38 @@ template 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 -struct db_where_with_limit_t final { - std::shared_ptr ctx; - db_where_with_limit_t *parent{this}; +template struct db_where_with_limit_t final { + std::shared_ptr ctx; - using cn_t = db_comp_next_limit_t; - using wn_t = db_where_next_limit_t; - using n_t = db_next_limit_t; + using cn_t = db_comp_next_limit_t; + using wn_t = db_where_next_limit_t; + using n_t = db_next_limit_t; using group_func_t = std::function; @@ -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, }; } diff --git a/repertory/repertory_test/src/database_test.cpp b/repertory/repertory_test/src/database_test.cpp index 9dcf796a..b248804e 100644 --- a/repertory/repertory_test/src/database_test.cpp +++ b/repertory/repertory_test/src/database_test.cpp @@ -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