updated build system

This commit is contained in:
2024-10-09 09:15:18 -05:00
parent 13a55bff61
commit 8bb291bbd9
14 changed files with 107 additions and 85 deletions

View File

@ -0,0 +1,227 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
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 REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_COMMON_HPP_
#define REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_COMMON_HPP_
#if defined(PROJECT_ENABLE_SQLITE)
namespace repertory::utils::db::sqlite {
using db_types_t = std::variant<std::int64_t, std::string>;
struct sqlite3_deleter {
void operator()(sqlite3 *db3) {
if (db3 != nullptr) {
sqlite3_close_v2(db3);
}
}
};
using db3_t = std::unique_ptr<sqlite3, sqlite3_deleter>;
struct sqlite3_statement_deleter {
void operator()(sqlite3_stmt *stmt) {
if (stmt != nullptr) {
sqlite3_finalize(stmt);
}
}
};
using db3_stmt_t = std::unique_ptr<sqlite3_stmt, sqlite3_statement_deleter>;
[[nodiscard]] auto execute_sql(sqlite3 &db3, const std::string &sql,
std::string &err) -> bool;
void set_journal_mode(sqlite3 &db3);
struct db_comp_data_t final {
std::string column_name;
std::string op_type;
};
struct db_context_t {
db_context_t(sqlite3 &db3_, std::string table_name_)
: db3(db3_), table_name(std::move(table_name_)) {}
sqlite3 &db3;
std::string table_name;
db3_stmt_t stmt{nullptr};
};
class db_column final {
public:
db_column() noexcept = default;
db_column(const db_column &) = default;
db_column(db_column &&column) noexcept = default;
~db_column() = default;
auto operator=(const db_column &) -> db_column & = default;
auto operator=(db_column &&) -> db_column & = default;
db_column(std::int32_t index, std::string name, db_types_t value) noexcept
: index_(index), name_(std::move(name)), value_(std::move(value)) {}
private:
std::int32_t index_{};
std::string name_;
db_types_t value_;
public:
[[nodiscard]] auto get_index() const -> std::int32_t { return index_; }
[[nodiscard]] auto get_name() const -> std::string { return name_; }
template <typename data_type>
[[nodiscard]] auto get_value() const -> data_type {
return std::visit(
overloaded{
[](const data_type &value) -> data_type { return value; },
[](auto &&) -> data_type {
throw std::runtime_error("data type not supported");
},
},
value_);
}
[[nodiscard]] auto get_value_as_json() const -> nlohmann::json {
return std::visit(
overloaded{
[this](std::int64_t value) -> auto {
return nlohmann::json({{name_, value}});
},
[](auto &&value) -> auto { return nlohmann::json::parse(value); },
},
value_);
}
};
template <typename ctx_t> class db_row final {
public:
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)};
auto column_type = sqlite3_column_type(context->stmt.get(), col);
db_types_t value;
switch (column_type) {
case SQLITE_INTEGER: {
value = sqlite3_column_int64(context->stmt.get(), col);
} break;
case SQLITE_TEXT: {
const auto *text = reinterpret_cast<const char *>(
sqlite3_column_text(context->stmt.get(), col));
value = std::string(text == nullptr ? "" : text);
} break;
default:
throw std::runtime_error("column type not implemented|" + name + '|' +
std::to_string(column_type));
}
columns_[name] = db_column{col, name, value};
}
}
private:
std::map<std::string, db_column> columns_;
public:
[[nodiscard]] auto get_columns() const -> std::vector<db_column> {
std::vector<db_column> ret;
for (const auto &item : columns_) {
ret.push_back(item.second);
}
return ret;
}
[[nodiscard]] auto get_column(std::int32_t index) const -> db_column {
auto iter = std::find_if(columns_.begin(), columns_.end(),
[&index](auto &&col) -> bool {
return col.second.get_index() == index;
});
if (iter == columns_.end()) {
throw std::out_of_range("");
}
return iter->second;
}
[[nodiscard]] auto get_column(std::string name) const -> db_column {
return columns_.at(name);
}
};
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__);
if (res == SQLITE_OK) {
set_res(sqlite3_step(context_->stmt.get()), function_name);
}
}
private:
std::shared_ptr<ctx_t> context_;
mutable std::int32_t res_;
private:
void set_res(std::int32_t res) const { res_ = res; }
public:
[[nodiscard]] auto ok() const -> bool {
return res_ == SQLITE_DONE || res_ == SQLITE_ROW;
}
[[nodiscard]] auto get_error() const -> std::int32_t { return res_; }
[[nodiscard]] auto get_error_str() const -> std::string {
return sqlite3_errstr(res_);
}
[[nodiscard]] auto get_row(std::optional<db_row<ctx_t>> &row) const -> bool {
row.reset();
if (not has_row()) {
return false;
}
row = db_row{context_};
set_res(sqlite3_step(context_->stmt.get()));
return true;
}
[[nodiscard]] auto has_row() const -> bool { return res_ == SQLITE_ROW; }
void next_row() const {
if (not has_row()) {
return;
}
set_res(sqlite3_step(context_->stmt.get()));
}
};
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)
#endif // REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_COMMON_HPP_

View File

@ -0,0 +1,66 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
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 REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_DELETE_HPP_
#define REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_DELETE_HPP_
#if defined(PROJECT_ENABLE_SQLITE)
#include "utils/db/sqlite/db_common.hpp"
#include "utils/db/sqlite/db_where_t.hpp"
namespace repertory::utils::db::sqlite {
class db_delete final {
public:
struct context final : db_context_t {
context(sqlite3 &db3_, std::string table_name_)
: db_context_t(db3_, table_name_) {}
using w_t = db_where_t<context, db_delete>;
std::optional<w_t> where;
std::vector<db_types_t> where_values;
};
using row = db_row<context>;
public:
db_delete(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(db3, table_name)) {}
db_delete(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
public:
[[nodiscard]] auto dump() const -> std::string;
[[nodiscard]] auto go() const -> db_result<context>;
[[nodiscard]] auto
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
};
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)
#endif // REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_DELETE_HPP_

View File

@ -0,0 +1,66 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
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 REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_INSERT_HPP_
#define REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_INSERT_HPP_
#if defined(PROJECT_ENABLE_SQLITE)
#include "utils/db/sqlite/db_common.hpp"
namespace repertory::utils::db::sqlite {
class db_insert final {
public:
struct context final : db_context_t {
context(sqlite3 &db3_, std::string table_name_)
: db_context_t(db3_, table_name_) {}
bool or_replace{false};
std::map<std::string, db_types_t> values{};
};
using row = db_row<context>;
public:
db_insert(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(db3, table_name)) {}
db_insert(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
public:
[[nodiscard]] auto or_replace() -> db_insert & {
context_->or_replace = true;
return *this;
}
[[nodiscard]] auto column_value(std::string column_name,
db_types_t value) -> db_insert &;
[[nodiscard]] auto dump() const -> std::string;
[[nodiscard]] auto go() const -> db_result<context>;
};
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)
#endif // REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_INSERT_HPP_

View File

@ -0,0 +1,80 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
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 REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_SELECT_HPP_
#define REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_SELECT_HPP_
#if defined(PROJECT_ENABLE_SQLITE)
#include "utils/db/sqlite/db_common.hpp"
#include "utils/db/sqlite/db_where_limit_t.hpp"
namespace repertory::utils::db::sqlite {
class db_select final {
public:
struct context final : db_context_t {
context(sqlite3 &db3_, std::string table_name_)
: db_context_t(db3_, table_name_) {}
using w_t = db_where_with_limit_t<context, db_select>;
std::vector<std::string> columns{};
std::map<std::string, std::string> count_columns{};
std::optional<std::int32_t> limit;
std::optional<std::pair<std::string, bool>> order_by;
std::optional<w_t> where;
std::vector<db_types_t> where_values;
};
using row = db_row<context>;
public:
db_select(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(db3, table_name)) {}
db_select(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
public:
[[nodiscard]] auto column(std::string column_name) -> db_select &;
[[nodiscard]] auto count(std::string column_name,
std::string as_column_name) -> db_select &;
[[nodiscard]] auto dump() const -> std::string;
[[nodiscard]] auto go() const -> db_result<context>;
[[nodiscard]] auto
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
[[nodiscard]] auto limit(std::int32_t value) -> db_select &;
[[nodiscard]] auto order_by(std::string column_name,
bool ascending) -> db_select &;
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
};
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)
#endif // REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_SELECT_HPP_

View File

@ -0,0 +1,77 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
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 REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_UPDATE_HPP_
#define REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_UPDATE_HPP_
#if defined(PROJECT_ENABLE_SQLITE)
#include "utils/db/sqlite/db_common.hpp"
#include "utils/db/sqlite/db_where_limit_t.hpp"
namespace repertory::utils::db::sqlite {
class db_update final {
public:
struct context final : db_context_t {
context(sqlite3 &db3_, std::string table_name_)
: db_context_t(db3_, table_name_) {}
using w_t = db_where_with_limit_t<context, db_update>;
std::map<std::string, db_types_t> column_values{};
std::optional<std::int32_t> limit;
std::optional<std::pair<std::string, bool>> order_by;
std::optional<w_t> where;
std::vector<db_types_t> where_values;
};
using row = db_row<context>;
public:
db_update(sqlite3 &db3, std::string table_name)
: context_(std::make_shared<context>(db3, table_name)) {}
db_update(std::shared_ptr<context> ctx) : context_(std::move(ctx)) {}
private:
std::shared_ptr<context> context_;
public:
[[nodiscard]] auto column_value(std::string column_name,
db_types_t value) -> db_update &;
[[nodiscard]] auto dump() const -> std::string;
[[nodiscard]] auto go() const -> db_result<context>;
[[nodiscard]] auto
group(context::w_t::group_func_t func) -> context::w_t::wn_t;
[[nodiscard]] auto limit(std::int32_t value) -> db_update &;
[[nodiscard]] auto order_by(std::string column_name,
bool ascending) -> db_update &;
[[nodiscard]] auto where(std::string column_name) const -> context::w_t::cn_t;
};
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)
#endif // REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_UPDATE_HPP_

View File

@ -0,0 +1,216 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
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 REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_WHERE_LIMIT_T_HPP_
#define REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_WHERE_LIMIT_T_HPP_
#if defined(PROJECT_ENABLE_SQLITE)
#include "utils/db/sqlite/db_common.hpp"
namespace repertory::utils::db::sqlite {
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 next{ctx};
using group_func_t = std::function<void(w_t &)>;
[[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 <typename cn_t, typename ctx_t, typename op_t, typename w_t>
struct db_where_next_limit_t final {
std::shared_ptr<ctx_t> ctx;
w_t *owner;
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{
ctx,
"AND",
});
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::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<n_t>(*std::prev(owner->actions.end()));
}
};
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, ctx_t, op_t, w_t>;
[[nodiscard]] auto create(std::string operation, db::db_types_t value) {
owner->actions.emplace_back(db_comp_data_t{
column_name,
operation,
});
ctx->where_values.push_back(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 <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<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 &)>;
using action_t = std::variant<db_comp_data_t, n_t, db_where_with_limit_t>;
std::vector<action_t> 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 db_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::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)
#endif // REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_WHERE_LIMIT_T_HPP_

View File

@ -0,0 +1,192 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
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 REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_WHERE_T_HPP_
#define REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_WHERE_T_HPP_
#if defined(PROJECT_ENABLE_SQLITE)
#include "utils/db/sqlite/db_common.hpp"
namespace repertory::utils::db::sqlite {
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 next{ctx};
using group_func_t = std::function<void(w_t &)>;
[[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 <typename cn_t, typename ctx_t, typename op_t, typename w_t>
struct db_where_next_t final {
std::shared_ptr<ctx_t> ctx;
w_t *owner;
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{
ctx,
"AND",
});
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::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<n_t>(*std::prev(owner->actions.end()));
}
};
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, ctx_t, op_t, w_t>;
[[nodiscard]] auto create(std::string operation, db::db_types_t value) {
owner->actions.emplace_back(db_comp_data_t{
column_name,
operation,
});
ctx->where_values.push_back(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 <typename ctx_t, typename op_t> struct db_where_t final {
std::shared_ptr<ctx_t> ctx;
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 &)>;
using action_t = std::variant<db_comp_data_t, n_t, db_where_t>;
std::vector<action_t> 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 db_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::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)
#endif // REPERTORY_INCLUDE_UTILS_DB_SQLITE_DB_WHERE_T_HPP_