73 lines
2 KiB
C++
73 lines
2 KiB
C++
#include "ikarus/objects/blueprint.h"
|
|
|
|
#include <ikarus/errors.hpp>
|
|
#include <ikarus/objects/blueprint.hpp>
|
|
#include <ikarus/objects/entity.h>
|
|
#include <ikarus/persistence/project.hpp>
|
|
|
|
IkarusBlueprint::IkarusBlueprint(struct IkarusProject * project, int64_t id):
|
|
project{project},
|
|
id{id} {}
|
|
|
|
IkarusBlueprint * ikarus_blueprint_create(
|
|
struct IkarusProject * project,
|
|
char const * name,
|
|
IkarusBlueprintCreateFlags flags,
|
|
struct 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 `blueprints`(`name`) VALUES(?)", name)
|
|
);
|
|
|
|
auto const id = project->db->last_insert_rowid();
|
|
return new IkarusBlueprint{project, id};
|
|
}
|
|
|
|
IkarusBlueprint * ikarus_blueprint_create_from_entity(
|
|
struct IkarusEntity * entity,
|
|
char const * name,
|
|
IkarusBlueprintCreateFromEntityFlags flags,
|
|
struct IkarusErrorData * error_out
|
|
) {
|
|
IKARUS_TRYRV_OR_FAIL(
|
|
nullptr,
|
|
"{}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
ikarus_libikarus_func_call_to_result(
|
|
error_out,
|
|
ikarus_must_return_true(ikarus_entity_exists, "entity doesn't exist", IkarusErrorInfo_Client_NonExistent),
|
|
entity
|
|
)
|
|
);
|
|
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
|
|
|
IKARUS_VTRYRV_OR_FAIL(
|
|
auto id,
|
|
nullptr,
|
|
"failed to create blueprint from entity: {}",
|
|
IkarusErrorInfo_Database_QueryFailed,
|
|
entity->project->db->transact([&](auto * db) -> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
|
|
CPPBASE_TRY(db->execute("INSERT INTO `blueprints`(`name`) VALUES(?)", name));
|
|
|
|
auto const id = db->last_insert_rowid();
|
|
|
|
CPPBASE_TRY(entity->project->db->execute(
|
|
"INSERT INTO `properties`(`blueprint`, `name`, `schema`) "
|
|
"SELECT ?, `name`, json_extract(`value`, '$.schema') FROM `entity_values` "
|
|
"WHERE `entity` = ?",
|
|
id,
|
|
entity->id
|
|
))
|
|
|
|
return cppbase::ok(entity->project->db->last_insert_rowid());
|
|
})
|
|
);
|
|
|
|
return new IkarusBlueprint{entity->project, id};
|
|
}
|