simplify blueprint implementation with helpers
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
7acda2852f
commit
0496ea7259
6 changed files with 200 additions and 218 deletions
|
|
@ -1,20 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#include "util.hpp"
|
||||
|
||||
#include <concepts>
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/id.h>
|
||||
#include <ikarus/objects/object_type.h>
|
||||
|
||||
#include <persistence/function_context.hpp>
|
||||
|
||||
namespace util {
|
||||
template<std::invocable<sqlitecpp::Connection *, IkarusId> F>
|
||||
[[nodiscard]] cppbase::Result<IkarusId, sqlitecpp::TransactionError> insert_object(
|
||||
IkarusProject * project, FunctionContext * ctx, IkarusObjectType type, std::string_view name, F insert_function
|
||||
namespace ikarus::util {
|
||||
|
||||
struct EmptyNameError {};
|
||||
|
||||
COMPOUND_ERROR(InsertObjectError, EmptyNameError, sqlitecpp::TransactionError);
|
||||
|
||||
template<typename InsertFunction, typename ObjectFactory>
|
||||
[[nodiscard]] cppbase::Result<std::invoke_result_t<ObjectFactory, IkarusId>, InsertObjectError> insert_object(
|
||||
IkarusProject * project,
|
||||
IkarusObjectType type,
|
||||
std::string_view name,
|
||||
InsertFunction insert_function,
|
||||
ObjectFactory object_factory
|
||||
) {
|
||||
char const * object_type_str = ikarus_object_type_to_string(type);
|
||||
auto const * object_type_str = ikarus_object_type_to_string(type);
|
||||
|
||||
LOG_INFO("creating new {}", object_type_str);
|
||||
|
||||
LOG_DEBUG("project={}; name={}", project->get_path().c_str(), name);
|
||||
|
||||
auto * ctx = project->get_function_context();
|
||||
|
||||
if (cppbase::is_empty_or_blank(name)) {
|
||||
ctx->set_error("name is empty or blank", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
|
||||
return cppbase::err(EmptyNameError{});
|
||||
}
|
||||
|
||||
VTRY(
|
||||
auto const id,
|
||||
|
|
@ -42,17 +65,23 @@ template<std::invocable<sqlitecpp::Connection *, IkarusId> F>
|
|||
})
|
||||
);
|
||||
|
||||
LOG_VERBOSE("successfully created blueprint");
|
||||
LOG_VERBOSE("successfully created {}", object_type_str);
|
||||
|
||||
return cppbase::ok(id);
|
||||
return cppbase::ok(object_factory(id));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void delete_object(IkarusProject * project, FunctionContext * ctx, T * object) {
|
||||
auto id = object->id;
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(id));
|
||||
template<typename Object>
|
||||
requires std::derived_from<Object, IkarusObject>
|
||||
void delete_object(IkarusProject * project, Object * object) {
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||
|
||||
TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", id).on_error([&](auto const& err) {
|
||||
LOG_INFO("deleting {}", object_type_str);
|
||||
|
||||
LOG_DEBUG("project={}; {}={}", object_type_str, object->project->get_path().c_str(), object->id);
|
||||
|
||||
auto * ctx = object->project->get_function_context();
|
||||
|
||||
TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", object->id).on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("failed to delete {} from objects table: {}", object_type_str, err),
|
||||
true,
|
||||
|
|
@ -73,8 +102,9 @@ struct SingleQueryData {
|
|||
std::string_view select_field_name;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
cppbase::Result<T, sqlitecpp::SingleQueryError> fetch_single_field(IkarusObject * object, SingleQueryData const& query_data) {
|
||||
template<typename T, typename Object>
|
||||
requires std::derived_from<Object, IkarusObject>
|
||||
cppbase::Result<T, sqlitecpp::SingleQueryError> fetch_single_field(Object const * object, SingleQueryData const& query_data) {
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||
|
||||
LOG_VERBOSE("fetching property default value");
|
||||
|
|
@ -86,7 +116,7 @@ cppbase::Result<T, sqlitecpp::SingleQueryError> fetch_single_field(IkarusObject
|
|||
VTRY(
|
||||
T value,
|
||||
object->project->get_db()
|
||||
->query_one<T>(
|
||||
->template query_one<T>(
|
||||
fmt::format("SELECT `{}` FROM `{}` WHERE `id` = ?", query_data.select_field_name, query_data.table_name),
|
||||
object->id
|
||||
)
|
||||
|
|
@ -107,45 +137,105 @@ struct MultipleBufferQueryData {
|
|||
std::string_view table_name;
|
||||
std::string_view select_field_name;
|
||||
std::string_view where_field_name;
|
||||
std::string_view relation_desc;
|
||||
};
|
||||
|
||||
template<typename T, typename O>
|
||||
cppbase::Result<void, sqlitecpp::MultiQueryError> fetch_multiple_buffered(
|
||||
IkarusObject * object,
|
||||
MultipleBufferQueryData const& query_data,
|
||||
std::string_view relation_desc,
|
||||
T * buffer,
|
||||
size_t buffer_size
|
||||
) {
|
||||
template<typename Selected, typename Mapped, typename Object, typename F>
|
||||
requires std::derived_from<Object, IkarusObject>
|
||||
void fetch_multiple_buffered(
|
||||
Object const * object, MultipleBufferQueryData const& query_data, Mapped * mapped_buffer, size_t buffer_size, F transformer
|
||||
)
|
||||
requires cppbase::
|
||||
is_result_with_value_type_v<Mapped, std::invoke_result_t<F, IkarusProject *, IkarusFunctionContext *, Selected>>
|
||||
{
|
||||
auto * ctx = object->project->get_function_context();
|
||||
|
||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||
|
||||
LOG_VERBOSE("fetching {} {}", object_type_str, relation_desc);
|
||||
LOG_VERBOSE("fetching {} {}", object_type_str, query_data.relation_desc);
|
||||
|
||||
LOG_VERBOSE("project={};{}={}", object->project->get_path().c_str(), object_type_str, object->id);
|
||||
|
||||
auto * ctx = object->project->get_function_context();
|
||||
Selected select_buffer[buffer_size];
|
||||
|
||||
TRY(object->project->get_db()
|
||||
->query_many_buffered<T>(
|
||||
TRYRV(
|
||||
,
|
||||
object->project->get_db()
|
||||
->template query_many_buffered<Selected>(
|
||||
fmt::format(
|
||||
"SELECT `{}` FROM `{}` WHERE `{}` = ?",
|
||||
query_data.select_field_name,
|
||||
query_data.table_name,
|
||||
query_data.where_field_name
|
||||
),
|
||||
buffer,
|
||||
select_buffer,
|
||||
buffer_size,
|
||||
object->id
|
||||
)
|
||||
.on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("failed to fetch {} {} from database: {}", object_type_str, relation_desc, err),
|
||||
fmt::format("failed to fetch {} {} from database: {}", object_type_str, query_data.relation_desc, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
}));
|
||||
})
|
||||
);
|
||||
|
||||
return cppbase::ok();
|
||||
LOG_DEBUG(
|
||||
"{} {}: [{}]", object_type_str, query_data.relation_desc, fmt::join(select_buffer, select_buffer + buffer_size, ", ")
|
||||
);
|
||||
|
||||
for (size_t i = 0; i < buffer_size; ++i) {
|
||||
VTRYRV(mapped_buffer[i], , transformer(object->project, ctx, select_buffer[i]));
|
||||
}
|
||||
}
|
||||
|
||||
struct CountQueryData {
|
||||
std::string_view table_name;
|
||||
std::string_view select_field_name;
|
||||
std::string_view where_field_name;
|
||||
std::string_view relation_desc;
|
||||
};
|
||||
|
||||
template<typename Object>
|
||||
requires std::derived_from<Object, IkarusObject>
|
||||
cppbase::Result<cppbase::usize, sqlitecpp::QueryError> fetch_count(Object const * object, CountQueryData const& query_data) {
|
||||
auto * object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||
|
||||
LOG_VERBOSE("fetching {} {} count", object_type_str, query_data.relation_desc);
|
||||
|
||||
auto * ctx = object->project->get_function_context();
|
||||
|
||||
LOG_DEBUG("{}={}", object_type_str, object->id);
|
||||
|
||||
VTRY(
|
||||
auto count,
|
||||
object->project->get_db()
|
||||
->template query_one<int>(
|
||||
fmt::format(
|
||||
"SELECT COUNT(`{}`) FROM `{}` WHERE `{}` = ?;",
|
||||
query_data.select_field_name,
|
||||
query_data.table_name,
|
||||
query_data.where_field_name
|
||||
),
|
||||
object->id
|
||||
)
|
||||
.on_error([&](auto const& err) {
|
||||
ctx->set_error(
|
||||
fmt::format("failed to fetch {} {} count: {}", object_type_str, query_data.relation_desc, err),
|
||||
true,
|
||||
IkarusErrorInfo_Source_SubSystem,
|
||||
IkarusErrorInfo_Type_SubSystem_Database
|
||||
);
|
||||
})
|
||||
);
|
||||
|
||||
LOG_DEBUG("{} {} count: {}", object_type_str, query_data.relation_desc, count);
|
||||
|
||||
LOG_VERBOSE("successfully fetched {} {} count", object_type_str, query_data.relation_desc);
|
||||
|
||||
return cppbase::ok(static_cast<size_t>(count));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue