171 lines
3.9 KiB
C++
171 lines
3.9 KiB
C++
#include "entity.hpp"
|
|
|
|
#include <ikarus/errors.h>
|
|
#include <ikarus/errors.hpp>
|
|
#include <ikarus/objects/entity.h>
|
|
#include <ikarus/persistence/project.hpp>
|
|
|
|
IkarusEntity::IkarusEntity(
|
|
struct IkarusProject * project,
|
|
int64_t id,
|
|
std::string_view name
|
|
):
|
|
project{project},
|
|
id{id},
|
|
name{name} {}
|
|
|
|
IkarusEntity * ikarus_entity_create(
|
|
struct IkarusProject * project,
|
|
char const * name,
|
|
IkarusEntityCreateFlags flags,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(project, nullptr);
|
|
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
|
|
|
IKARUS_TRYRV_OR_FAIL(
|
|
nullptr,
|
|
"failed to create entity: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
project->db->execute("INSERT INTO `entities`(`name`) VALUES(?)", name)
|
|
);
|
|
|
|
auto const id = project->db->last_insert_rowid();
|
|
return new IkarusEntity{project, id, name};
|
|
}
|
|
|
|
void ikarus_entity_delete(
|
|
IkarusEntity * entity,
|
|
IkarusEntityDeleteFlags flags,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(entity, );
|
|
IKARUS_FAIL_IF_NOT_EXIST(entity, );
|
|
IKARUS_FAIL_IF_NULL(entity->project, );
|
|
|
|
IKARUS_TRYRV_OR_FAIL(
|
|
,
|
|
"failed to delete entity: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
entity->project->db
|
|
->execute("DELETE FROM `entities` WHERE `id` = ?", entity->id)
|
|
);
|
|
|
|
delete entity;
|
|
}
|
|
|
|
IkarusEntity * ikarus_entity_copy(
|
|
IkarusEntity * entity,
|
|
IkarusEntityCopyFlags flags,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
|
IKARUS_FAIL_IF_NOT_EXIST(entity, nullptr);
|
|
IKARUS_FAIL_IF_NULL(entity->project, nullptr);
|
|
|
|
IKARUS_VTRYRV_OR_FAIL(
|
|
auto id,
|
|
nullptr,
|
|
"failed to copy entity: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
entity->project->db->transact(
|
|
[entity](auto * db)
|
|
-> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
|
|
TRY(entity->project->db->execute(
|
|
"INSERT INTO `entities`(`name`) VALUES(?)",
|
|
entity->name.data()
|
|
));
|
|
|
|
TRY(entity->project->db->execute(
|
|
"INSERT INTO `entity_values`(`entity`, `name`, `value`)"
|
|
" SELECT ?1, `name`, `value` FROM `entity_values` WHERE "
|
|
"`entity` = ?1",
|
|
entity->id
|
|
))
|
|
|
|
TRY(entity->project->db->execute(
|
|
"INSERT INTO `entity_property_values`("
|
|
" `entity`, "
|
|
" `property`,"
|
|
" `value`"
|
|
") "
|
|
"SELECT ?1, `property`, `value` FROM "
|
|
"`entity_property_values` "
|
|
"WHERE `entity` = ?1",
|
|
entity->id
|
|
))
|
|
|
|
TRY(entity->project->db->execute(
|
|
"INSERT INTO `entity_blueprint_links`(`entity`, "
|
|
"`blueprint`)"
|
|
"SELECT ?1, `property`, `value` FROM "
|
|
"`entity_property_values` "
|
|
"WHERE `entity` = ?1",
|
|
entity->id
|
|
))
|
|
|
|
return cppbase::ok(entity->project->db->last_insert_rowid());
|
|
}
|
|
)
|
|
);
|
|
|
|
return new IkarusEntity{entity->project, id, entity->name};
|
|
}
|
|
|
|
IkarusProject *
|
|
ikarus_entity_get_project(IkarusEntity * entity, IkarusErrorData * error_out) {
|
|
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
|
IKARUS_FAIL_IF_NULL(entity->project, nullptr);
|
|
|
|
return entity->project;
|
|
}
|
|
|
|
char const *
|
|
ikarus_entity_get_name(IkarusEntity * entity, IkarusErrorData * error_out) {
|
|
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
|
|
|
return entity->name.data();
|
|
}
|
|
|
|
void ikarus_entity_set_name(
|
|
IkarusEntity * entity,
|
|
char const * name,
|
|
IkarusEntitySetNameFlags flags,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(entity, );
|
|
IKARUS_FAIL_IF_NAME_INVALID(name, );
|
|
|
|
IKARUS_TRYRV_OR_FAIL(
|
|
,
|
|
"failed to set name for entity: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
entity->project->db->execute(
|
|
"UPDATE `entities` SET `name` = ? WHERE `id` = ?",
|
|
name,
|
|
entity->id
|
|
)
|
|
);
|
|
|
|
entity->name = name;
|
|
}
|
|
|
|
char const * ikarus_entity_get_value(
|
|
IkarusEntity * entity,
|
|
char const * name,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
|
IKARUS_FAIL_IF_NULL(name, nullptr);
|
|
}
|
|
|
|
void ikarus_entity_set_value(
|
|
IkarusEntity * entity,
|
|
char const * name,
|
|
char const * value,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(entity, );
|
|
IKARUS_FAIL_IF_NULL(name, );
|
|
IKARUS_FAIL_IF_NULL(value, );
|
|
}
|