implement remaining logic

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
folling 2024-01-03 17:14:26 +01:00 committed by Folling
parent e17e346768
commit bac85e87c8
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
41 changed files with 1393 additions and 408 deletions

View file

@ -1,36 +1,289 @@
#include "entity.hpp"
#include "values/value.hpp"
#include <cppbase/strings.hpp>
#include <errors.hpp>
#include <objects/blueprint.hpp>
#include <objects/util.hpp>
#include <objects/properties/property.hpp>
#include <persistence/project.hpp>
#include <values/entity_property_value.hpp>
IkarusEntity * ikarus_entity_create(struct IkarusProject * project, char const * name) {
return ikarus::util::insert_object(
project,
IkarusObjectType_Entity,
name,
[](auto * db, IkarusId id) { return db->execute("INSERT INTO `entities`(`id`) VALUES(?)", id); },
[project](IkarusId id) { return project->get_entity(id); }
).unwrap_value_or(nullptr);
IkarusEntity * ikarus_entity_create(struct IkarusProject * project, char const * name, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(project, nullptr);
IKARUS_FAIL_IF_NULL(name, nullptr);
IKARUS_FAIL_IF(cppbase::is_empty_or_blank(name), nullptr, "name must not be empty", IkarusErrorInfo_Client_InvalidInput);
IKARUS_VTRYRV_OR_FAIL(
IkarusId const id,
nullptr,
"failed to create entity: {}",
IkarusErrorInfo_Database_QueryFailed,
project->db->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
TRY(db->execute("INSERT INTO `objects`(`type`, `name`) VALUES(?, ?, ?)", IkarusObjectType_Entity, name));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Entity);
TRY(db->execute("INSERT INTO `entities`(`id`) VALUES(?)", id));
return cppbase::ok(id);
})
);
return project->get_entity(id);
}
void ikarus_entity_delete(IkarusEntity * entity) {
ikarus::util::delete_object(entity);
void ikarus_entity_delete(IkarusEntity * entity, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_TRYRV_OR_FAIL(
,
"unable to delete entity: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute("DELETE FROM `objects` WHERE `id` == ?", entity->id)
);
entity->project->uncache(entity);
}
bool ikarus_entity_is_linked_to_blueprint(IkarusEntity const * entity, struct IkarusBlueprint const * blueprint) {
return ikarus::util::check_exists(
entity,
ikarus::util::ExistsQueryData<IkarusId>{
.table_name = "entity_blueprint_links",
.where_field_name = "blueprint",
.where_field_value = blueprint->id,
.relation_desc = "linked blueprints"
}
bool ikarus_entity_is_linked_to_blueprint(
IkarusEntity const * entity,
struct IkarusBlueprint const * blueprint,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, false);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, false);
IKARUS_FAIL_IF_NULL(blueprint, false);
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, false);
IKARUS_VTRYRV_OR_FAIL(
auto const ret,
false,
"unable to check whether entity is linked to blueprint",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<bool>(
"SELECT EXISTS(SELECT 1 FROM `entity_blueprint_links` WHERE `entity` = ? AND `blueprint` = ?)",
entity->id,
blueprint->id
)
)
.unwrap_value_or(false);
return ret;
}
bool ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint) {}
void ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_NULL(blueprint, );
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
IKARUS_TRYRV_OR_FAIL(
,
"unable to link entity to blueprint: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"INSERT INTO `entity_blueprint_links`(`entity`, `blueprint`) VALUES(?, ?) ON CONFLICT(`entity`, `blueprint`) DO NOTHING",
entity->id,
blueprint->id
)
);
}
void ikarus_entity_unlink_from_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_NULL(blueprint, );
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
IKARUS_TRYRV_OR_FAIL(
,
"unable to unlink entity from blueprint: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db
->execute("DELETE FROM `entity_blueprint_links` WHERE `entity` = ? AND `blueprint` = ?", entity->id, blueprint->id)
);
}
void ikarus_entity_get_linked_blueprints(
IkarusEntity const * entity,
struct IkarusBlueprint ** blueprints_out,
size_t blueprints_out_size,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_NULL(blueprints_out, );
if (blueprints_out_size == 0) {
return;
}
IkarusId ids[blueprints_out_size];
IKARUS_TRYRV_OR_FAIL(
,
"unable to fetch entity linked blueprints from database: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_many_buffered<IkarusId>(
"SELECT `blueprint` FROM `entity_blueprint_links` WHERE `entity` = ?",
ids,
blueprints_out_size,
entity->id
)
)
for (size_t i = 0; i < blueprints_out_size; ++i) {
blueprints_out[i] = entity->project->get_blueprint(ids[i]);
}
}
size_t ikarus_entity_get_linked_blueprint_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, 0);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, 0);
IKARUS_VTRYRV_OR_FAIL(
auto const ret,
0,
"unable to fetch entity linked blueprint count from database: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `entity` = ?", entity->id)
);
return ret;
}
bool ikarus_entity_has_property(IkarusEntity const * entity, struct IkarusProperty const * property, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, false);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, false);
IKARUS_FAIL_IF_NULL(property, false);
IKARUS_FAIL_IF_OBJECT_MISSING(property, false);
IKARUS_VTRYRV_OR_FAIL(
auto const ret,
false,
"unable to check whether entity has property: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<bool>(
"SELECT EXISTS(SELECT 1 FROM `entity_properties` WHERE `entity` = ? AND `property` = ?)",
entity->id,
property->id
)
)
return ret;
}
void ikarus_entity_get_properties(
IkarusEntity const * entity,
struct IkarusProperty ** properties_out,
size_t properties_out_size,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_NULL(properties_out, );
if (properties_out_size == 0) {
return;
}
std::tuple<IkarusId, IkarusPropertyType> ids_and_types[properties_out_size];
IKARUS_TRYRV_OR_FAIL(
,
"unable to fetch entity properties from database: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_many_buffered<IkarusId, IkarusPropertyType>(
"SELECT `property`, `type` FROM `properties` WHERE `source` = ?",
ids_and_types,
properties_out_size,
entity->id
)
)
for (size_t i = 0; i < properties_out_size; ++i) {
auto [id, type] = ids_and_types[i];
properties_out[i] = entity->project->get_property(id, type);
}
}
size_t ikarus_entity_get_property_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, 0);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, 0);
IKARUS_VTRYRV_OR_FAIL(
size_t const ret,
0,
"unable to fetch entity property count from database: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `source` = ?", entity->id)
);
return ret;
}
struct IkarusEntityPropertyValue *
ikarus_entity_get_value(IkarusEntity const * entity, struct IkarusProperty const * property, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, nullptr);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, nullptr);
IKARUS_FAIL_IF_NULL(property, nullptr);
IKARUS_FAIL_IF_OBJECT_MISSING(property, nullptr);
auto * value = fetch_value_from_db(
entity->project,
error_out,
"SELECT IFNULL((SELECT `value` FROM `values` WHERE `entity` = ? AND `property` = ?), (SELECT `default_value` FROM `properties` WHERE `id` = ?))",
entity->id,
property->id,
property->id
);
IKARUS_FAIL_IF_ERROR(nullptr);
return new IkarusEntityPropertyValue{
.entity = entity,
.property = property,
.value = value,
};
}
void ikarus_entity_set_value(
IkarusEntity * entity,
struct IkarusProperty const * property,
struct IkarusEntityPropertyValue * value,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_NULL(property, );
IKARUS_FAIL_IF_OBJECT_MISSING(property, );
IKARUS_FAIL_IF_NULL(value, );
auto value_json_str = boost::json::serialize(value->value->to_json());
IKARUS_TRYRV_OR_FAIL(
,
"unable to set entity property value: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"INSERT INTO `values`(`entity`, `property`, `value`) VALUES(?, ?, ?) ON CONFLICT(`entity`, `property`) DO UPDATE SET `value` = ?",
entity->id,
property->id,
value_json_str,
value_json_str
)
);
}
// No existence checks here for performance reasons. All methods on IkarusObject perform this check anyway.
struct IkarusObject * ikarus_entity_to_object(IkarusEntity * entity, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, nullptr);
return static_cast<IkarusObject *>(entity);
}
struct IkarusObject const * ikarus_entity_to_object_const(IkarusEntity const * entity, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(entity, nullptr);
return static_cast<IkarusObject const *>(entity);
}