From 9a3d6e725e80728a6c8bb7fef6e3c71cabb05fe3 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Tue, 8 Oct 2024 17:01:17 -0500 Subject: [PATCH] refactor --- .../include/database/db_common.hpp | 351 ------------------ .../include/database/db_delete.hpp | 3 +- .../include/database/db_insert.hpp | 5 +- .../include/database/db_select.hpp | 4 +- .../include/database/db_update.hpp | 2 + .../include/database/db_where_limit_t.hpp | 216 +++++++++++ .../include/database/db_where_t.hpp | 192 ++++++++++ 7 files changed, 416 insertions(+), 357 deletions(-) create mode 100644 repertory/librepertory/include/database/db_where_limit_t.hpp create mode 100644 repertory/librepertory/include/database/db_where_t.hpp diff --git a/repertory/librepertory/include/database/db_common.hpp b/repertory/librepertory/include/database/db_common.hpp index e2594379..0243ac73 100644 --- a/repertory/librepertory/include/database/db_common.hpp +++ b/repertory/librepertory/include/database/db_common.hpp @@ -223,356 +223,5 @@ public: } } }; - -template -struct db_next_t final { - std::shared_ptr ctx; - std::string action; - - w_t next{ctx}; - - using group_func_t = std::function; - - [[nodiscard]] auto where(std::string column_name) -> cn_t { - return next.where(column_name); - } - - [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } - - [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { - return ctx->where->dump(idx); - } - - [[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 -struct db_next_limit_t final { - std::shared_ptr ctx; - std::string action; - - w_t next{ctx}; - - using group_func_t = std::function; - - [[nodiscard]] auto where(std::string column_name) -> cn_t { - return next.where(column_name); - } - - [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } - - [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { - return ctx->where->dump(idx); - } - - [[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{ctx}.limit(value); - } - - [[nodiscard]] auto order_by(std::string column_name, bool ascending) -> op_t { - return op_t{ctx}.order_by(column_name, ascending); - } -}; - -template -struct db_where_next_t final { - std::shared_ptr ctx; - w_t *owner; - - using n_t = db_next_t; - - [[nodiscard]] auto and_() -> n_t & { - owner->actions.emplace_back(n_t{ - ctx, - "AND", - }); - - return std::get(*std::prev(owner->actions.end())); - } - - [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } - - [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { - return ctx->where->dump(idx); - } - - [[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); } - - [[nodiscard]] auto or_() -> n_t & { - owner->actions.emplace_back(n_t{ - ctx, - "OR", - }); - - return std::get(*std::prev(owner->actions.end())); - } -}; - -template -struct db_where_next_limit_t final { - std::shared_ptr ctx; - w_t *owner; - - using n_t = db_next_limit_t; - - [[nodiscard]] auto and_() -> n_t & { - owner->actions.emplace_back(n_t{ - ctx, - "AND", - }); - - return std::get(*std::prev(owner->actions.end())); - } - - [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } - - [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { - return ctx->where->dump(idx); - } - - [[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); } - - [[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) -> op_t { - return op_t{ctx}.order_by(column_name, ascending); - } - - [[nodiscard]] auto or_() -> n_t & { - owner->actions.emplace_back(n_t{ - ctx, - "OR", - }); - - return std::get(*std::prev(owner->actions.end())); - } -}; - -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; - - [[nodiscard]] auto create(std::string operation, db::db_types_t value) { - owner->actions.emplace_back(comp_data_t{ - column_name, - operation, - value, - }); - - ctx->where_values.push_back( - &std::get(*std::prev(owner->actions.end())).value); - - return wn_t{ - ctx, - owner, - }; - } - - auto equals(db::db_types_t value) -> wn_t { return create("=", value); }; - - auto gt(db::db_types_t value) -> wn_t { return create(">", value); } - - auto gte(db::db_types_t value) -> wn_t { return create(">=", value); } - - auto like(db::db_types_t value) -> wn_t { return create("LIKE", value); } - - auto lt(db::db_types_t value) -> wn_t { return create("<", value); } - - auto lte(db::db_types_t value) -> wn_t { return create("<=", value); } - - auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); }; -}; - -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; - - [[nodiscard]] auto create(std::string operation, db::db_types_t value) { - owner->actions.emplace_back(comp_data_t{ - column_name, - operation, - value, - }); - - ctx->where_values.push_back( - &std::get(*std::prev(owner->actions.end())).value); - - return wn_t{ - ctx, - owner, - }; - } - - auto equals(db::db_types_t value) -> wn_t { return create("=", value); }; - - auto gt(db::db_types_t value) -> wn_t { return create(">", value); } - - auto gte(db::db_types_t value) -> wn_t { return create(">=", value); } - - auto like(db::db_types_t value) -> wn_t { return create("LIKE", value); } - - auto lt(db::db_types_t value) -> wn_t { return create("<", value); } - - auto lte(db::db_types_t value) -> wn_t { return create("<=", value); } - - auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); }; -}; - -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 group_func_t = std::function; - - using action_t = std::variant; - - std::vector actions{}; - - [[nodiscard]] static auto dump(std::int32_t &idx, - auto &&data) -> std::string { - std::stringstream stream; - - for (auto &&action : data.actions) { - std::visit(overloaded{ - [&idx, &stream](const comp_data_t &comp) { - stream << '"' << comp.column_name << '"' << comp.op_type - << '?' + std::to_string(++idx); - }, - [&idx, &stream](const n_t &next) { - stream << ' ' << next.action << ' ' - << dump(idx, next.next); - }, - [&idx, &stream](const db_where_t &where) { - stream << '(' << dump(idx, where) << ')'; - }, - }, - action); - } - - return stream.str(); - } - - [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } - - [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { - return dump(idx, *this); - } - - [[nodiscard]] auto group(group_func_t func) -> wn_t { - db_where_t where{ctx}; - func(where); - - actions.emplace_back(std::move(where)); - return wn_t{ - ctx, - this, - }; - } - - [[nodiscard]] auto where(std::string column_name) -> cn_t { - return cn_t{ - ctx, - this, - column_name, - }; - } -}; - -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 group_func_t = std::function; - - using action_t = std::variant; - - std::vector actions{}; - - [[nodiscard]] static auto dump(std::int32_t &idx, - auto &&data) -> std::string { - std::stringstream stream; - - for (auto &&action : data.actions) { - std::visit(overloaded{ - [&idx, &stream](const comp_data_t &comp) { - stream << '"' << comp.column_name << '"' << comp.op_type - << '?' + std::to_string(++idx); - }, - [&idx, &stream](const n_t &next) { - stream << ' ' << next.action << ' ' - << dump(idx, next.next); - }, - [&idx, &stream](const db_where_with_limit_t &where) { - stream << '(' << dump(idx, where) << ')'; - }, - }, - action); - } - - return stream.str(); - } - - [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } - - [[nodiscard]] auto dump(std::int32_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}; - func(where); - - actions.emplace_back(std::move(where)); - return wn_t{ - ctx, - this, - }; - } - - [[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) -> op_t { - return op_t{ctx}.order_by(column_name, ascending); - } - - [[nodiscard]] auto where(std::string column_name) -> cn_t { - return cn_t{ - ctx, - this, - column_name, - }; - } -}; } // namespace repertory::db - #endif // INCLUDE_DATABASE_DB_COMMON_HPP_ diff --git a/repertory/librepertory/include/database/db_delete.hpp b/repertory/librepertory/include/database/db_delete.hpp index 434f6a42..2aa68024 100644 --- a/repertory/librepertory/include/database/db_delete.hpp +++ b/repertory/librepertory/include/database/db_delete.hpp @@ -23,7 +23,8 @@ #define INCLUDE_DATABASE_DB_DELETE_HPP_ #include "database/db_common.hpp" -#include "utils/error_utils.hpp" + +#include "database/db_where_t.hpp" namespace repertory::db { class db_delete final { diff --git a/repertory/librepertory/include/database/db_insert.hpp b/repertory/librepertory/include/database/db_insert.hpp index 64bd8547..387518a8 100644 --- a/repertory/librepertory/include/database/db_insert.hpp +++ b/repertory/librepertory/include/database/db_insert.hpp @@ -23,7 +23,6 @@ #define INCLUDE_DATABASE_DB_INSERT_HPP_ #include "database/db_common.hpp" -#include "utils/error_utils.hpp" namespace repertory::db { class db_insert final { @@ -57,8 +56,8 @@ 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; diff --git a/repertory/librepertory/include/database/db_select.hpp b/repertory/librepertory/include/database/db_select.hpp index b6cb5510..b5d9c27d 100644 --- a/repertory/librepertory/include/database/db_select.hpp +++ b/repertory/librepertory/include/database/db_select.hpp @@ -23,8 +23,8 @@ #define INCLUDE_DATABASE_DB_SELECT_HPP_ #include "database/db_common.hpp" -#include "db_common.hpp" -#include "utils/error_utils.hpp" + +#include "database/db_where_limit_t.hpp" namespace repertory::db { class db_select final { diff --git a/repertory/librepertory/include/database/db_update.hpp b/repertory/librepertory/include/database/db_update.hpp index c72dee99..55c38b31 100644 --- a/repertory/librepertory/include/database/db_update.hpp +++ b/repertory/librepertory/include/database/db_update.hpp @@ -24,6 +24,8 @@ #include "database/db_common.hpp" +#include "database/db_where_limit_t.hpp" + namespace repertory::db { class db_update final { public: diff --git a/repertory/librepertory/include/database/db_where_limit_t.hpp b/repertory/librepertory/include/database/db_where_limit_t.hpp new file mode 100644 index 00000000..a45a09c0 --- /dev/null +++ b/repertory/librepertory/include/database/db_where_limit_t.hpp @@ -0,0 +1,216 @@ +/* + Copyright <2018-2024> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef INCLUDE_DATABASE_DB_WHERE_LIMIT_T_HPP_ +#define INCLUDE_DATABASE_DB_WHERE_LIMIT_T_HPP_ + +#include "database/db_common.hpp" + +namespace repertory::db { +template +struct db_next_limit_t final { + std::shared_ptr ctx; + std::string action; + + w_t next{ctx}; + + using group_func_t = std::function; + + [[nodiscard]] auto where(std::string column_name) -> cn_t { + return next.where(column_name); + } + + [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } + + [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { + return ctx->where->dump(idx); + } + + [[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{ctx}.limit(value); + } + + [[nodiscard]] auto order_by(std::string column_name, bool ascending) -> op_t { + return op_t{ctx}.order_by(column_name, ascending); + } +}; + +template +struct db_where_next_limit_t final { + std::shared_ptr ctx; + w_t *owner; + + using n_t = db_next_limit_t; + + [[nodiscard]] auto and_() -> n_t & { + owner->actions.emplace_back(n_t{ + ctx, + "AND", + }); + + return std::get(*std::prev(owner->actions.end())); + } + + [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } + + [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { + return ctx->where->dump(idx); + } + + [[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); } + + [[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) -> op_t { + return op_t{ctx}.order_by(column_name, ascending); + } + + [[nodiscard]] auto or_() -> n_t & { + owner->actions.emplace_back(n_t{ + ctx, + "OR", + }); + + return std::get(*std::prev(owner->actions.end())); + } +}; + +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; + + [[nodiscard]] auto create(std::string operation, db::db_types_t value) { + owner->actions.emplace_back(comp_data_t{ + column_name, + operation, + value, + }); + + ctx->where_values.push_back( + &std::get(*std::prev(owner->actions.end())).value); + + return wn_t{ + ctx, + owner, + }; + } + + auto equals(db::db_types_t value) -> wn_t { return create("=", value); }; + + auto gt(db::db_types_t value) -> wn_t { return create(">", value); } + + auto gte(db::db_types_t value) -> wn_t { return create(">=", value); } + + auto like(db::db_types_t value) -> wn_t { return create("LIKE", value); } + + auto lt(db::db_types_t value) -> wn_t { return create("<", value); } + + auto lte(db::db_types_t value) -> wn_t { return create("<=", value); } + + auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); }; +}; + +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 group_func_t = std::function; + + using action_t = std::variant; + + std::vector actions{}; + + [[nodiscard]] static auto dump(std::int32_t &idx, + auto &&data) -> std::string { + std::stringstream stream; + + for (auto &&action : data.actions) { + std::visit(overloaded{ + [&idx, &stream](const comp_data_t &comp) { + stream << '"' << comp.column_name << '"' << comp.op_type + << '?' + std::to_string(++idx); + }, + [&idx, &stream](const n_t &next) { + stream << ' ' << next.action << ' ' + << dump(idx, next.next); + }, + [&idx, &stream](const db_where_with_limit_t &where) { + stream << '(' << dump(idx, where) << ')'; + }, + }, + action); + } + + return stream.str(); + } + + [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } + + [[nodiscard]] auto dump(std::int32_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}; + func(where); + + actions.emplace_back(std::move(where)); + return wn_t{ + ctx, + this, + }; + } + + [[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) -> op_t { + return op_t{ctx}.order_by(column_name, ascending); + } + + [[nodiscard]] auto where(std::string column_name) -> cn_t { + return cn_t{ + ctx, + this, + column_name, + }; + } +}; +} // namespace repertory::db + +#endif // INCLUDE_DATABASE_DB_WHERE_LIMIT_T_HPP_ diff --git a/repertory/librepertory/include/database/db_where_t.hpp b/repertory/librepertory/include/database/db_where_t.hpp new file mode 100644 index 00000000..51c38bd5 --- /dev/null +++ b/repertory/librepertory/include/database/db_where_t.hpp @@ -0,0 +1,192 @@ +/* + Copyright <2018-2024> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#ifndef INCLUDE_DATABASE_DB_WHERE_T_HPP_ +#define INCLUDE_DATABASE_DB_WHERE_T_HPP_ + +#include "database/db_common.hpp" + +namespace repertory::db { +template +struct db_next_t final { + std::shared_ptr ctx; + std::string action; + + w_t next{ctx}; + + using group_func_t = std::function; + + [[nodiscard]] auto where(std::string column_name) -> cn_t { + return next.where(column_name); + } + + [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } + + [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { + return ctx->where->dump(idx); + } + + [[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 +struct db_where_next_t final { + std::shared_ptr ctx; + w_t *owner; + + using n_t = db_next_t; + + [[nodiscard]] auto and_() -> n_t & { + owner->actions.emplace_back(n_t{ + ctx, + "AND", + }); + + return std::get(*std::prev(owner->actions.end())); + } + + [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } + + [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { + return ctx->where->dump(idx); + } + + [[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); } + + [[nodiscard]] auto or_() -> n_t & { + owner->actions.emplace_back(n_t{ + ctx, + "OR", + }); + + return std::get(*std::prev(owner->actions.end())); + } +}; + +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; + + [[nodiscard]] auto create(std::string operation, db::db_types_t value) { + owner->actions.emplace_back(comp_data_t{ + column_name, + operation, + value, + }); + + ctx->where_values.push_back( + &std::get(*std::prev(owner->actions.end())).value); + + return wn_t{ + ctx, + owner, + }; + } + + auto equals(db::db_types_t value) -> wn_t { return create("=", value); }; + + auto gt(db::db_types_t value) -> wn_t { return create(">", value); } + + auto gte(db::db_types_t value) -> wn_t { return create(">=", value); } + + auto like(db::db_types_t value) -> wn_t { return create("LIKE", value); } + + auto lt(db::db_types_t value) -> wn_t { return create("<", value); } + + auto lte(db::db_types_t value) -> wn_t { return create("<=", value); } + + auto not_equals(db::db_types_t value) -> wn_t { return create("!=", value); }; +}; + +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 group_func_t = std::function; + + using action_t = std::variant; + + std::vector actions{}; + + [[nodiscard]] static auto dump(std::int32_t &idx, + auto &&data) -> std::string { + std::stringstream stream; + + for (auto &&action : data.actions) { + std::visit(overloaded{ + [&idx, &stream](const comp_data_t &comp) { + stream << '"' << comp.column_name << '"' << comp.op_type + << '?' + std::to_string(++idx); + }, + [&idx, &stream](const n_t &next) { + stream << ' ' << next.action << ' ' + << dump(idx, next.next); + }, + [&idx, &stream](const db_where_t &where) { + stream << '(' << dump(idx, where) << ')'; + }, + }, + action); + } + + return stream.str(); + } + + [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } + + [[nodiscard]] auto dump(std::int32_t &idx) const -> std::string { + return dump(idx, *this); + } + + [[nodiscard]] auto group(group_func_t func) -> wn_t { + db_where_t where{ctx}; + func(where); + + actions.emplace_back(std::move(where)); + return wn_t{ + ctx, + this, + }; + } + + [[nodiscard]] auto where(std::string column_name) -> cn_t { + return cn_t{ + ctx, + this, + column_name, + }; + } +}; +} // namespace repertory::db + +#endif // INCLUDE_DATABASE_DB_WHERE_T_HPP_