diff --git a/support/include/utils/db/sqlite/db_where_limit_t.hpp b/support/include/utils/db/sqlite/db_where_limit_t.hpp index 809f8395..1d5c6ac0 100644 --- a/support/include/utils/db/sqlite/db_where_limit_t.hpp +++ b/support/include/utils/db/sqlite/db_where_limit_t.hpp @@ -30,14 +30,14 @@ template struct db_next_limit_t final { std::shared_ptr ctx; - std::string action; + w_t *owner; - w_t next{ctx}; + std::string action; using group_func_t = std::function; [[nodiscard]] auto where(std::string column_name) -> cn_t { - return next.where(column_name); + return get_next().where(column_name); } [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } @@ -46,10 +46,18 @@ struct db_next_limit_t final { return ctx->where->dump(idx); } + [[nodiscard]] auto get_next() -> wn_t & { + return std::get(std::prev(owner->sub_actions->end())); + } + + [[nodiscard]] auto get_next() const -> const wn_t & { + return std::get(std::prev(owner->sub_actions->end())); + } + [[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)); + return get_next().group(std::move(func)); } [[nodiscard]] auto limit(std::int32_t value) -> op_t { @@ -68,12 +76,17 @@ struct db_where_next_limit_t final { using n_t = db_next_limit_t; - [[nodiscard]] auto and_() -> n_t & { + [[nodiscard]] auto and_() -> n_t { owner->actions.emplace_back(n_t{ ctx, + owner, "AND", }); + owner->sub_actions.emplace_back(w_t{ + ctx, + }); + return std::get(*std::prev(owner->actions.end())); } @@ -93,12 +106,17 @@ struct db_where_next_limit_t final { return op_t{ctx}.order_by(column_name, ascending); } - [[nodiscard]] auto or_() -> n_t & { + [[nodiscard]] auto or_() -> n_t { owner->actions.emplace_back(n_t{ ctx, + owner, "OR", }); + owner->sub_actions.emplace_back(w_t{ + ctx, + }); + return std::get(*std::prev(owner->actions.end())); } }; @@ -152,6 +170,7 @@ template struct db_where_with_limit_t final { using action_t = std::variant; std::vector actions{}; + std::vector sub_actions{}; [[nodiscard]] static auto dump(std::int32_t &idx, auto &&data) -> std::string { @@ -165,7 +184,7 @@ template struct db_where_with_limit_t final { }, [&idx, &stream](const n_t &next) { stream << ' ' << next.action << ' ' - << dump(idx, next.next); + << dump(idx, next.get_next()); }, [&idx, &stream](const db_where_with_limit_t &where) { stream << '(' << dump(idx, where) << ')'; diff --git a/support/include/utils/db/sqlite/db_where_t.hpp b/support/include/utils/db/sqlite/db_where_t.hpp index c1fb65d9..7e668746 100644 --- a/support/include/utils/db/sqlite/db_where_t.hpp +++ b/support/include/utils/db/sqlite/db_where_t.hpp @@ -30,14 +30,13 @@ template struct db_next_t final { std::shared_ptr ctx; + w_t *owner; 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); + return get_next().where(column_name); } [[nodiscard]] auto dump() const -> std::string { return op_t{ctx}.dump(); } @@ -46,10 +45,18 @@ struct db_next_t final { return ctx->where->dump(idx); } + [[nodiscard]] auto get_next() -> wn_t & { + return std::get(std::prev(owner->sub_actions->end())); + } + + [[nodiscard]] auto get_next() const -> const wn_t & { + return std::get(std::prev(owner->sub_actions->end())); + } + [[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)); + return get_next().group(std::move(func)); } }; @@ -60,12 +67,17 @@ struct db_where_next_t final { using n_t = db_next_t; - [[nodiscard]] auto and_() -> n_t & { + [[nodiscard]] auto and_() -> n_t { owner->actions.emplace_back(n_t{ ctx, + owner, "AND", }); + owner->sub_actions.emplace_back(w_t{ + ctx, + }); + return std::get(*std::prev(owner->actions.end())); } @@ -77,12 +89,17 @@ struct db_where_next_t final { [[nodiscard]] auto go() const -> auto { return op_t{ctx}.go(); } - [[nodiscard]] auto or_() -> n_t & { + [[nodiscard]] auto or_() -> n_t { owner->actions.emplace_back(n_t{ ctx, + owner, "OR", }); + owner->sub_actions.emplace_back(w_t{ + ctx, + }); + return std::get(*std::prev(owner->actions.end())); } }; @@ -136,6 +153,7 @@ template struct db_where_t final { using action_t = std::variant; std::vector actions{}; + std::vector sub_actions{}; [[nodiscard]] static auto dump(std::int32_t &idx, auto &&data) -> std::string { @@ -149,7 +167,7 @@ template struct db_where_t final { }, [&idx, &stream](const n_t &next) { stream << ' ' << next.action << ' ' - << dump(idx, next.next); + << dump(idx, next.get_next()); }, [&idx, &stream](const db_where_t &where) { stream << '(' << dump(idx, where) << ')'; diff --git a/support/src/utils/db/sqlite/db_common.cpp b/support/src/utils/db/sqlite/db_common.cpp index dc7d8eee..8fc9257a 100644 --- a/support/src/utils/db/sqlite/db_common.cpp +++ b/support/src/utils/db/sqlite/db_common.cpp @@ -44,7 +44,9 @@ auto execute_sql(sqlite3 &db3, const std::string &sql, } void set_journal_mode(sqlite3 &db3) { - sqlite3_exec(&db3, "PRAGMA journal_mode = WAL;PRAGMA synchronous = NORMAL;", + sqlite3_exec(&db3, + "PRAGMA journal_mode = WAL;PRAGMA synchronous = NORMAL;PRAGMA " + "auto_vacuum = FULL;", nullptr, nullptr, nullptr); } } // namespace repertory::utils::db::sqlite