add flatbuffers support and initial rewrite

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
folling 2024-05-12 14:15:42 +02:00 committed by Folling
parent 5f7a62ecb7
commit 4d7bf09c4e
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
72 changed files with 3929 additions and 1403 deletions

View file

@ -10,22 +10,38 @@
#include <ikarus/persistence/project.hpp>
#include <ikarus/values/entity_property_value.hpp>
#include <ikarus/values/value.hpp>
#include <ikarus/values/value_type.h>
IkarusEntity * ikarus_entity_create(struct IkarusProject * project, char const * name, IkarusErrorData * error_out) {
IkarusEntity::IkarusEntity(IkarusProject * project, int64_t id):
IkarusObject{project, id} {}
std::string_view IkarusEntity::get_table_name() const noexcept {
return "entities";
}
IkarusEntity * ikarus_entity_create(
struct IkarusProject * project,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(project, nullptr);
IKARUS_FAIL_IF_NAME_INVALID_OR_DUPLICATE(name, project, static_cast<IkarusEntity const *>(nullptr), nullptr);
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
IKARUS_VTRYRV_OR_FAIL(
IkarusId const id,
int64_t 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`) VALUES(?)", IkarusObjectType_Entity));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Entity);
TRY(db->execute("INSERT INTO `entities`(`id`, `name`) VALUES(?, ?)", id, name));
return cppbase::ok(id);
})
project->db->transact(
[name](auto * db
) -> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
TRY(db->execute(
"INSERT INTO `entities`(`name`) VALUES(?, ?)",
name
));
return cppbase::ok(db->last_insert_rowid());
}
)
);
return project->get_entity(id);
@ -39,25 +55,37 @@ void ikarus_entity_delete(IkarusEntity * entity, IkarusErrorData * error_out) {
,
"unable to delete entity: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", entity->id)
entity->project->db
->execute("DELETE FROM `entities` WHERE `id` = ?", entity->id)
);
entity->project->uncache(entity);
}
IkarusId ikarus_entity_get_id(IkarusEntity const * entity, IkarusErrorData * error_out) {
int64_t
ikarus_entity_get_id(IkarusEntity const * entity, IkarusErrorData * error_out) {
return ikarus::util::object_get_id(entity, error_out);
}
IkarusProject * ikarus_entity_get_project(IkarusEntity const * entity, IkarusErrorData * error_out) {
IkarusProject * ikarus_entity_get_project(
IkarusEntity const * entity,
IkarusErrorData * error_out
) {
return ikarus::util::object_get_project(entity, error_out);
}
char const * ikarus_entity_get_name(IkarusEntity const * entity, IkarusErrorData * error_out) {
char const * ikarus_entity_get_name(
IkarusEntity const * entity,
IkarusErrorData * error_out
) {
return ikarus::util::object_get_name(entity, error_out);
}
void ikarus_entity_set_name(IkarusEntity * entity, char const * name, IkarusErrorData * error_out) {
void ikarus_entity_set_name(
IkarusEntity * entity,
char const * name,
IkarusErrorData * error_out
) {
ikarus::util::object_set_name(entity, name, error_out);
}
@ -77,7 +105,9 @@ bool ikarus_entity_is_linked_to_blueprint(
"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` = ?)",
"SELECT EXISTS(SELECT 1 FROM `entity_blueprint_links` WHERE "
"`entity` = ? AND "
"`blueprint` = ?)",
entity->id,
blueprint->id
)
@ -86,7 +116,11 @@ bool ikarus_entity_is_linked_to_blueprint(
return ret;
}
void ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
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, );
@ -97,14 +131,20 @@ void ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBluepri
"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",
"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) {
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, );
@ -114,9 +154,26 @@ void ikarus_entity_unlink_from_blueprint(IkarusEntity * entity, struct IkarusBlu
,
"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)
entity->project->db->execute(
"DELETE FROM `entity_blueprint_links` WHERE `entity` = ? AND "
"`blueprint` = ?",
entity->id,
blueprint->id
)
);
IKARUS_TRYRV_OR_FAIL(
,
"unable to remove entity property values: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"DELETE FROM `entity_property_values` WHERE `entity` = ? AND "
"`property` IN (SELECT "
"`id` FROM `properties` WHERE `blueprint` = ?)",
entity->id,
blueprint->id
)
)
}
void ikarus_entity_get_linked_blueprints(
@ -133,14 +190,15 @@ void ikarus_entity_get_linked_blueprints(
return;
}
IkarusId ids[blueprints_out_size];
int64_t 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` = ?",
entity->project->db->query_many_buffered<int64_t>(
"SELECT `blueprint` FROM `entity_blueprint_links` WHERE `entity` = "
"?",
ids,
blueprints_out_size,
entity->id
@ -152,7 +210,10 @@ void ikarus_entity_get_linked_blueprints(
}
}
size_t ikarus_entity_get_linked_blueprint_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
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);
@ -161,31 +222,144 @@ size_t ikarus_entity_get_linked_blueprint_count(IkarusEntity const * entity, Ika
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)
entity->project->db->query_one<int64_t>(
"SELECT COUNT(`blueprint`) 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) {
bool ikarus_entity_has_value(
IkarusEntity const * entity,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, false);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, false);
IKARUS_FAIL_IF_NAME_INVALID(name, false);
IKARUS_VTRYRV_OR_FAIL(
auto const has_value,
false,
"unable to check whether entity has value: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<bool>(
"SELECT EXISTS(SELECT 1 FROM `entity_values` WHERE `entity` = ? "
"AND `name` = ?)",
entity->id,
name
)
);
return has_value;
}
struct IkarusValue * ikarus_entity_get_value(
IkarusEntity const * entity,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, nullptr);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, nullptr);
IKARUS_FAIL_IF_VALUE_MISSING(entity, name, nullptr);
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
auto * value = fetch_value_from_db(
entity->project,
error_out,
"SELECT `value` FROM `entity_values` WHERE `entity` = ? AND `name` = ?",
entity->id,
name
);
IKARUS_FAIL_IF_ERROR(nullptr);
return value;
}
void ikarus_entity_set_value(
IkarusEntity * entity,
char const * name,
struct IkarusValue const * value,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_NAME_INVALID(name, );
IKARUS_FAIL_IF_NULL(value, );
IKARUS_TRYRV_OR_FAIL(
,
"unable to set entity value: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"INSERT INTO `entity_values`(`entity`, `name`, `value`) VALUES(?1, "
"?2, ?3) ON "
"CONFLICT(`entity`, `name`) DO UPDATE SET `value` = ?3",
entity->id,
name,
boost::json::serialize(value->to_json())
)
);
}
void ikarus_entity_delete_value(
IkarusEntity * entity,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_VALUE_MISSING(entity, name, );
IKARUS_FAIL_IF_NAME_INVALID(name, );
IKARUS_TRYRV_OR_FAIL(
,
"unable to delete entity value: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"DELETE FROM `entity_values` WHERE `entity` = ? AND `name` = ?",
entity->id,
name
)
);
}
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);
// given that values are loaded lazily we can't just check
// `entity_property_values` here
IKARUS_VTRYRV_OR_FAIL(
auto const ret,
auto const has_property,
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` = ?)",
"SELECT EXISTS(\n"
" SELECT 1\n"
" FROM `entity_blueprint_links`\n"
" JOIN `properties` ON `properties`.`blueprint` = "
"`entity_blueprint_links`.`blueprint`\n"
" WHERE `entity_blueprint_links`.`entity` = ? AND "
"`properties`.`id` = ?\n"
")",
entity->id,
property->id
)
)
return ret;
return has_property;
}
void ikarus_entity_get_properties(
@ -202,19 +376,25 @@ void ikarus_entity_get_properties(
return;
}
std::tuple<IkarusId, IkarusPropertyType> ids_and_types[properties_out_size];
std::tuple<int64_t, IkarusValueType> ids_and_types[properties_out_size];
// given that values are loaded lazily we can't just check
// `entity_property_values` here
IKARUS_TRYRV_OR_FAIL(
,
"unable to fetch entity properties from database: {}",
"unable to fetch properties from entity: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_many_buffered<IkarusId, IkarusPropertyType>(
"SELECT `property`, `type` FROM `properties` WHERE `source` = ?",
entity->project->db->query_many_buffered<int64_t, IkarusValueType>(
"SELECT `properties`.`id`, `properties`.`type`\n"
"FROM `entity_blueprint_links`\n"
"JOIN `properties` ON `properties`.`blueprint` = "
"`entity_blueprint_links`.`blueprint`\n"
"WHERE `entity_blueprint_links`.`entity` = ?\n",
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];
@ -222,23 +402,39 @@ void ikarus_entity_get_properties(
}
}
size_t ikarus_entity_get_property_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
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);
// given that values are loaded lazily we can't just check
// `entity_property_values` here
IKARUS_VTRYRV_OR_FAIL(
size_t const ret,
size_t const count,
0,
"unable to fetch entity property count from database: {}",
"unable to fetch property count from entity: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `source` = ?", entity->id)
entity->project->db->query_one<int64_t>(
"SELECT COUNT(`properties`.`id`)\n"
"FROM `entity_blueprint_links`\n"
"JOIN `properties` ON `properties`.`blueprint` = "
"`entity_blueprint_links`.`blueprint`\n"
"WHERE `entity_blueprint_links`.`entity` = ?\n"
")",
entity->id
)
);
return ret;
return count;
}
struct IkarusEntityPropertyValue *
ikarus_entity_get_value(IkarusEntity const * entity, struct IkarusProperty const * property, IkarusErrorData * error_out) {
struct IkarusValue * ikarus_entity_get_property_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);
@ -247,25 +443,27 @@ ikarus_entity_get_value(IkarusEntity const * entity, struct IkarusProperty const
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` = ?))",
"SELECT IFNULL(\n"
" (\n"
" SELECT `value`\n"
" FROM `entity_property_values`\n"
" WHERE `entity` = ?1 AND `property` = ?2\n"
" ),\n"
" (SELECT `default_value` FROM `properties` WHERE `id` = ?2)\n"
")",
entity->id,
property->id,
property->id
);
IKARUS_FAIL_IF_ERROR(nullptr);
return new IkarusEntityPropertyValue{
.entity = entity,
.property = property,
.value = value,
};
return value;
}
void ikarus_entity_set_value(
void ikarus_entity_set_property_value(
IkarusEntity * entity,
struct IkarusProperty const * property,
struct IkarusEntityPropertyValue * value,
struct IkarusValue * value,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
@ -274,18 +472,17 @@ void ikarus_entity_set_value(
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` = ?",
"INSERT INTO `entity_property_values`(`entity`, `property`, "
"`value`) VALUES(?1, ?2, "
"?3) ON CONFLICT(`entity`, `property`) DO UPDATE SET `value` = ?3",
entity->id,
property->id,
value_json_str,
value_json_str
boost::json::serialize(value->to_json())
)
);
}