simplify blueprint implementation with helpers

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2023-12-27 12:52:27 +01:00 committed by Folling
parent 7acda2852f
commit 0496ea7259
No known key found for this signature in database
6 changed files with 200 additions and 218 deletions

View file

@ -1,16 +1,15 @@
#include "ikarus/objects/blueprint.h"
#include "objects/blueprint.hpp"
#include <iterator>
#include "util.hpp"
#include <cppbase/logger.hpp>
#include <cppbase/result.hpp>
#include <cppbase/strings.hpp>
#include <objects/entity.hpp>
#include <objects/object_helper.hpp>
#include <objects/properties/property.hpp>
#include <objects/util.hpp>
#include <persistence/function_context.hpp>
#include <persistence/project.hpp>
@ -18,199 +17,92 @@ IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id):
IkarusObject{project, id} {}
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name) {
LOG_INFO("creating new blueprint");
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 nullptr;
}
VTRYRV(auto const id, nullptr, insert_object(project, ctx, IkarusObjectType_Blueprint, name, [](auto * db, IkarusId id) {
return db->execute("INSERT INTO `blueprints`(`id`) VALUES(?)", id);
}));
return project->get_blueprint(id);
return ikarus::util::insert_object(
project,
IkarusObjectType_Blueprint,
name,
[](auto * db, IkarusId id) { return db->execute("INSERT INTO `blueprints`(`id`) VALUES(?)", id); },
[project](IkarusId id) { return project->get_blueprint(id); }
).unwrap_value_or(nullptr);
}
void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
LOG_INFO("deleting blueprint");
LOG_DEBUG("project={}; blueprint={}", blueprint->project->get_path().c_str(), blueprint->id);
auto * ctx = blueprint->project->get_function_context();
delete_object(blueprint->project, ctx, blueprint);
ikarus::util::delete_object(blueprint->project, blueprint);
}
void ikarus_blueprint_get_properties(
IkarusBlueprint const * blueprint, struct IkarusProperty ** properties_out, size_t properties_out_size
) {
LOG_VERBOSE("fetching blueprint properties");
ikarus::util::fetch_multiple_buffered<IkarusId>(
blueprint,
ikarus::util::MultipleBufferQueryData{
.table_name = "properties",
.select_field_name = "id",
.where_field_name = "blueprint",
.relation_desc = "properties"
},
properties_out,
properties_out_size,
[&](IkarusProject * project, IkarusFunctionContext * ctx, IkarusId id
) -> cppbase::Result<IkarusProperty *, sqlitecpp::QueryError> {
VTRY(
auto const type, IkarusProperty::get_property_type(blueprint->project, id).on_error([ctx, id](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch property {}'s type: {}", id, err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG(
"project={}; blueprint={}; properties_out_size={}",
blueprint->project->get_path().c_str(),
blueprint->id,
properties_out_size
return cppbase::ok(project->get_property(id, type));
}
);
auto * ctx = blueprint->project->get_function_context();
IkarusId ids[properties_out_size];
TRYRV(
,
blueprint->project
->get_db()
->query_many_buffered<IkarusId>(
"SELECT `id` FROM `properties` WHERE `source` = ?",
static_cast<IkarusId *>(ids),
properties_out_size,
blueprint->id
)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint property ids: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint properties: [{}]", fmt::join(ids, ids + properties_out_size, ", "));
for (size_t i = 0; i < properties_out_size; ++i) {
auto const id = ids[i];
VTRYRV(
auto const type,
,
IkarusProperty::get_property_type(blueprint->project, id).on_error([ctx, id](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch property {}'s type: {}", id, err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
properties_out[i] = blueprint->project->get_property(id, type);
}
LOG_VERBOSE("successfully fetched blueprint properties");
}
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
LOG_VERBOSE("fetching blueprint property count");
auto * ctx = blueprint->project->get_function_context();
LOG_DEBUG("blueprint={}", blueprint->id);
VTRYRV(
auto count,
0,
blueprint->project
->get_db()
->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint` = ?;", blueprint->id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint property count: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint property count: {}", count);
LOG_VERBOSE("successfully fetched blueprint property count");
return static_cast<size_t>(count);
return ikarus::util::fetch_count(
blueprint,
ikarus::util::CountQueryData{
.table_name = "blueprint_properties",
.select_field_name = "property",
.where_field_name = "blueprint",
.relation_desc = "properties"
}
)
.unwrap_value_or(0);
}
void ikarus_blueprint_get_linked_entities(
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
) {
LOG_VERBOSE("fetching blueprint linked entities");
LOG_DEBUG(
"project={}; blueprint={}; entities_out_size={}",
blueprint->project->get_path().c_str(),
blueprint->id,
entities_out_size
ikarus::util::fetch_multiple_buffered<IkarusId>(
blueprint,
ikarus::util::MultipleBufferQueryData{
.table_name = "entity_blueprint_links",
.select_field_name = "entity",
.where_field_name = "blueprint",
.relation_desc = "linked entities"
},
entities_out,
entities_out_size,
[&](IkarusProject * project, [[maybe_unused]] IkarusFunctionContext * ctx, IkarusId id
) -> cppbase::Result<IkarusEntity *, sqlitecpp::QueryError> { return cppbase::ok(project->get_entity(id)); }
);
auto * ctx = blueprint->project->get_function_context();
IkarusId ids[entities_out_size];
TRYRV(
,
blueprint->project
->get_db()
->query_many_buffered<IkarusId>(
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
static_cast<IkarusId *>(ids),
entities_out_size,
blueprint->id
)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint linked entity ids: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint linked entities: [{}]", fmt::join(ids, ids + entities_out_size, ", "));
for (size_t i = 0; i < entities_out_size; ++i) {
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
entities_out[i] = blueprint->project->get_entity(ids[i]);
}
LOG_VERBOSE("successfully fetched blueprint linked entities");
}
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) {
LOG_VERBOSE("fetching blueprint linked entity count");
LOG_DEBUG("project={}; blueprint={}", blueprint->project->get_path().c_str(), blueprint->id);
auto * ctx = blueprint->project->get_function_context();
VTRYRV(
auto count,
0,
blueprint->project
->get_db()
->query_one<int>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint linked entity count: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint linked entity count: {}", count);
LOG_VERBOSE("successfully fetched blueprint linked entity count: {}", count);
return static_cast<size_t>(count);
return ikarus::util::fetch_count(
blueprint,
ikarus::util::CountQueryData{
.table_name = "entity_blueprint_links",
.select_field_name = "entity",
.where_field_name = "blueprint",
.relation_desc = "linked entities"
}
)
.unwrap_value_or(0);
}
IkarusObject * ikarus_blueprint_to_object(IkarusBlueprint * blueprint) {