change error system & function signatures

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2024-01-02 15:14:39 +01:00 committed by Folling
parent 1367373819
commit e1bf97704a
No known key found for this signature in database
28 changed files with 633 additions and 651 deletions

View file

@ -1,7 +1,6 @@
#include "ikarus/objects/blueprint.h"
#include "objects/blueprint.hpp"
#include "util.hpp"
#include <cppbase/logger.hpp>
#include <cppbase/result.hpp>
@ -9,25 +8,44 @@
#include <objects/entity.hpp>
#include <objects/properties/property.hpp>
#include <objects/util.hpp>
#include <persistence/function_context.hpp>
#include <persistence/project.hpp>
IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id):
IkarusObject{project, id} {}
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name) {
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);
if (cppbase::is_empty_or_blank(name)) {
project->set_error("blueprint name must not be empty", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
project->db
->transact([name](auto * db) {
TRY(db->execute("INSERT INTO `objects`(`type`, `name`) VALUES(?, ?, ?)", IkarusObjectType_Blueprint, name));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
TRY(db->execute("INSERT INTO `blueprints`(`id`) VALUES(?)", id));
return cppbase::ok();
})
.on_error([project](auto const & err) {
project->set_error(
fmt::format("failed to create blueprint: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
});
}
void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
ikarus::util::delete_object(blueprint);
blueprint->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->id).on_error([blueprint](auto const & err) {
blueprint->project->set_error(
fmt::format("failed to delete blueprint from objects table: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
});
}
void ikarus_blueprint_get_properties(
@ -35,41 +53,34 @@ void ikarus_blueprint_get_properties(
struct IkarusProperty ** properties_out,
size_t properties_out_size
) {
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),
IkarusId ids[properties_out_size];
TRYRV(
,
blueprint->project->db
->query_many_buffered<IkarusId>("SELECT `id` FROM `properties` WHERE `source` = ?", ids, properties_out_size, blueprint->id)
.on_error([&](auto const & err) {
blueprint->project->set_error(
fmt::format("failed to fetch blueprint properties from database: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
}));
return cppbase::ok(project->get_property(id, type));
}
})
);
// not atomic, could be switched to two loops if necessary
for (size_t i = 0; i < properties_out_size; ++i) {
IkarusId id = ids[i];
VTRYRV(auto const type, , IkarusProperty::get_property_type(blueprint->project, id));
properties_out[i] = blueprint->project->get_property(id, type);
}
}
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
return ikarus::util::fetch_count(
blueprint,
ikarus::util::CountQueryData{
.table_name = "blueprint_properties",
.select_field_name = "property",
.where_field_name = "blueprint",
.relation_desc = "properties"
}
)
return blueprint->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `source` = ?", blueprint->id)
.unwrap_value_or(0);
}
@ -78,31 +89,34 @@ void ikarus_blueprint_get_linked_entities(
struct IkarusEntity ** entities_out,
size_t 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)); }
IkarusId ids[entities_out_size];
TRYRV(
,
blueprint->project->db
->query_many_buffered<IkarusId>(
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
ids,
entities_out_size,
blueprint->id
)
.on_error([&](auto const & err) {
blueprint->project->set_error(
fmt::format("failed to fetch linked entities from database: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
for (size_t i = 0; i < entities_out_size; ++i) {
entities_out[i] = blueprint->project->get_entity(ids[i]);
}
}
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) {
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"
}
)
return blueprint->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?", blueprint->id)
.unwrap_value_or(0);
}