finalize schema/data setup
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
98cb7a44ef
commit
a49912337d
89 changed files with 2324 additions and 6271 deletions
|
|
@ -1,168 +1,5 @@
|
|||
#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>
|
||||
#include <ikarus/values/value_type.h>
|
||||
|
||||
IkarusBlueprint::IkarusBlueprint(IkarusProject * project, int64_t id):
|
||||
IkarusObject{project, id} {}
|
||||
|
||||
std::string_view IkarusBlueprint::get_table_name() const noexcept {
|
||||
return "blueprints";
|
||||
}
|
||||
|
||||
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
int64_t const id,
|
||||
nullptr,
|
||||
"failed to create blueprint: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->transact([name](auto * db) -> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
|
||||
TRY(db->execute("INSERT INTO `blueprints`(`name`) VALUES(?)", name));
|
||||
return cppbase::ok(db->last_insert_rowid());
|
||||
})
|
||||
);
|
||||
|
||||
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 `blueprints` WHERE `id` = ?", blueprint->id)
|
||||
);
|
||||
|
||||
blueprint->project->uncache(blueprint);
|
||||
}
|
||||
|
||||
int64_t 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<int64_t, IkarusValueType> 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<int64_t, IkarusValueType>(
|
||||
"SELECT `id` FROM `properties` WHERE `blueprint` = ?",
|
||||
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 `blueprint` = ?", 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;
|
||||
}
|
||||
|
||||
int64_t 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<int64_t>(
|
||||
"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(`entity`) FROM `entity_blueprint_links` WHERE `blueprint` = ?", blueprint->id)
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue