164 lines
5.5 KiB
C++
164 lines
5.5 KiB
C++
#include "ikarus/objects/blueprint.h"
|
|
|
|
#include <cppbase/logger.hpp>
|
|
#include <cppbase/result.hpp>
|
|
#include <cppbase/strings.hpp>
|
|
|
|
#include <ikarus/errors.hpp>
|
|
#include <ikarus/objects/blueprint.hpp>
|
|
#include <ikarus/objects/entity.hpp>
|
|
#include <ikarus/objects/properties/property.h>
|
|
#include <ikarus/objects/properties/property.hpp>
|
|
#include <ikarus/objects/util.hpp>
|
|
#include <ikarus/persistence/project.hpp>
|
|
|
|
IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id):
|
|
IkarusObject{project, id} {}
|
|
|
|
IkarusBlueprint * ikarus_blueprint_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, nullptr, nullptr);
|
|
|
|
IKARUS_VTRYRV_OR_FAIL(
|
|
IkarusId const id,
|
|
nullptr,
|
|
"failed to create blueprint: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
project->db->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
|
|
TRY(db->execute("INSERT INTO `objects`(`type`, `name`, `information`) 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(id);
|
|
})
|
|
);
|
|
|
|
return project->get_blueprint(id);
|
|
}
|
|
|
|
void ikarus_blueprint_delete(IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
|
|
IKARUS_FAIL_IF_NULL(blueprint, );
|
|
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
|
|
|
IKARUS_TRYRV_OR_FAIL(
|
|
,
|
|
"unable to delete blueprint: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
blueprint->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->id)
|
|
);
|
|
|
|
blueprint->project->uncache(blueprint);
|
|
}
|
|
|
|
IkarusId ikarus_blueprint_get_id(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
|
return ikarus::util::object_get_id(blueprint, error_out);
|
|
}
|
|
|
|
IkarusProject * ikarus_blueprint_get_project(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
|
return ikarus::util::object_get_project(blueprint, error_out);
|
|
}
|
|
|
|
char const * ikarus_blueprint_get_name(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
|
return ikarus::util::object_get_name(blueprint, error_out);
|
|
}
|
|
|
|
void ikarus_blueprint_set_name(IkarusBlueprint * blueprint, char const * name, IkarusErrorData * error_out) {
|
|
ikarus::util::object_set_name(blueprint, name, error_out);
|
|
}
|
|
|
|
void ikarus_blueprint_get_properties(
|
|
IkarusBlueprint const * blueprint,
|
|
struct IkarusProperty ** properties_out,
|
|
size_t properties_out_size,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(blueprint, );
|
|
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
|
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 blueprint properties from database: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
blueprint->project->db->query_many_buffered<IkarusId, IkarusPropertyType>(
|
|
"SELECT `id` FROM `properties` WHERE `source` = ?",
|
|
ids_and_types,
|
|
properties_out_size,
|
|
blueprint->id
|
|
)
|
|
)
|
|
|
|
for (size_t i = 0; i < properties_out_size; ++i) {
|
|
auto [id, type] = ids_and_types[i];
|
|
|
|
properties_out[i] = blueprint->project->get_property(id, type);
|
|
}
|
|
}
|
|
|
|
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
|
IKARUS_FAIL_IF_NULL(blueprint, 0);
|
|
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, 0);
|
|
|
|
IKARUS_VTRYRV_OR_FAIL(
|
|
auto const ret,
|
|
0,
|
|
"unable to fetch blueprint property count from database: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
blueprint->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `source` = ?", blueprint->id)
|
|
);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void ikarus_blueprint_get_linked_entities(
|
|
IkarusBlueprint const * blueprint,
|
|
struct IkarusEntity ** entities_out,
|
|
size_t entities_out_size,
|
|
IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_FAIL_IF_NULL(blueprint, );
|
|
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
|
IKARUS_FAIL_IF_NULL(entities_out, );
|
|
|
|
if (entities_out_size == 0) {
|
|
return;
|
|
}
|
|
|
|
IkarusId ids[entities_out_size];
|
|
|
|
IKARUS_TRYRV_OR_FAIL(
|
|
,
|
|
"unable to fetch blueprint linked entities from database: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
blueprint->project->db->query_many_buffered<IkarusId>(
|
|
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
|
|
ids,
|
|
entities_out_size,
|
|
blueprint->id
|
|
)
|
|
)
|
|
|
|
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, IkarusErrorData * error_out) {
|
|
IKARUS_FAIL_IF_NULL(blueprint, 0);
|
|
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, 0);
|
|
|
|
IKARUS_VTRYRV_OR_FAIL(
|
|
auto const ret,
|
|
0,
|
|
"unable to fetch blueprint linked entity count from database: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
blueprint->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?", blueprint->id)
|
|
);
|
|
|
|
return ret;
|
|
}
|