simplify blueprint implementation with helpers
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
5efc0a6e86
commit
f63d2b2fe2
6 changed files with 200 additions and 218 deletions
|
|
@ -1,16 +1,15 @@
|
||||||
#include "ikarus/objects/blueprint.h"
|
#include "ikarus/objects/blueprint.h"
|
||||||
|
|
||||||
#include "objects/blueprint.hpp"
|
#include "objects/blueprint.hpp"
|
||||||
|
#include "util.hpp"
|
||||||
#include <iterator>
|
|
||||||
|
|
||||||
#include <cppbase/logger.hpp>
|
#include <cppbase/logger.hpp>
|
||||||
#include <cppbase/result.hpp>
|
#include <cppbase/result.hpp>
|
||||||
#include <cppbase/strings.hpp>
|
#include <cppbase/strings.hpp>
|
||||||
|
|
||||||
#include <objects/entity.hpp>
|
#include <objects/entity.hpp>
|
||||||
#include <objects/object_helper.hpp>
|
|
||||||
#include <objects/properties/property.hpp>
|
#include <objects/properties/property.hpp>
|
||||||
|
#include <objects/util.hpp>
|
||||||
#include <persistence/function_context.hpp>
|
#include <persistence/function_context.hpp>
|
||||||
#include <persistence/project.hpp>
|
#include <persistence/project.hpp>
|
||||||
|
|
||||||
|
|
@ -18,199 +17,92 @@ IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id):
|
||||||
IkarusObject{project, id} {}
|
IkarusObject{project, id} {}
|
||||||
|
|
||||||
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name) {
|
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name) {
|
||||||
LOG_INFO("creating new blueprint");
|
return ikarus::util::insert_object(
|
||||||
|
project,
|
||||||
LOG_DEBUG("project={}; name={}", project->get_path().c_str(), name);
|
IkarusObjectType_Blueprint,
|
||||||
|
name,
|
||||||
auto * ctx = project->get_function_context();
|
[](auto * db, IkarusId id) { return db->execute("INSERT INTO `blueprints`(`id`) VALUES(?)", id); },
|
||||||
|
[project](IkarusId id) { return project->get_blueprint(id); }
|
||||||
if (cppbase::is_empty_or_blank(name)) {
|
).unwrap_value_or(nullptr);
|
||||||
ctx->set_error("name is empty or blank", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
VTRYRV(auto const id, nullptr, insert_object(project, ctx, IkarusObjectType_Blueprint, name, [](auto * db, IkarusId id) {
|
|
||||||
return db->execute("INSERT INTO `blueprints`(`id`) VALUES(?)", id);
|
|
||||||
}));
|
|
||||||
|
|
||||||
return project->get_blueprint(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
|
void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
|
||||||
LOG_INFO("deleting blueprint");
|
ikarus::util::delete_object(blueprint->project, blueprint);
|
||||||
|
|
||||||
LOG_DEBUG("project={}; blueprint={}", blueprint->project->get_path().c_str(), blueprint->id);
|
|
||||||
|
|
||||||
auto * ctx = blueprint->project->get_function_context();
|
|
||||||
|
|
||||||
delete_object(blueprint->project, ctx, blueprint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ikarus_blueprint_get_properties(
|
void ikarus_blueprint_get_properties(
|
||||||
IkarusBlueprint const * blueprint, struct IkarusProperty ** properties_out, size_t properties_out_size
|
IkarusBlueprint const * blueprint, struct IkarusProperty ** properties_out, size_t properties_out_size
|
||||||
) {
|
) {
|
||||||
LOG_VERBOSE("fetching blueprint properties");
|
ikarus::util::fetch_multiple_buffered<IkarusId>(
|
||||||
|
blueprint,
|
||||||
|
ikarus::util::MultipleBufferQueryData{
|
||||||
|
.table_name = "properties",
|
||||||
|
.select_field_name = "id",
|
||||||
|
.where_field_name = "blueprint",
|
||||||
|
.relation_desc = "properties"
|
||||||
|
},
|
||||||
|
properties_out,
|
||||||
|
properties_out_size,
|
||||||
|
[&](IkarusProject * project, IkarusFunctionContext * ctx, IkarusId id
|
||||||
|
) -> cppbase::Result<IkarusProperty *, sqlitecpp::QueryError> {
|
||||||
|
VTRY(
|
||||||
|
auto const type, IkarusProperty::get_property_type(blueprint->project, id).on_error([ctx, id](auto const& err) {
|
||||||
|
ctx->set_error(
|
||||||
|
fmt::format("failed to fetch property {}'s type: {}", id, err),
|
||||||
|
true,
|
||||||
|
IkarusErrorInfo_Source_SubSystem,
|
||||||
|
IkarusErrorInfo_Type_SubSystem_Database
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
LOG_DEBUG(
|
return cppbase::ok(project->get_property(id, type));
|
||||||
"project={}; blueprint={}; properties_out_size={}",
|
}
|
||||||
blueprint->project->get_path().c_str(),
|
|
||||||
blueprint->id,
|
|
||||||
properties_out_size
|
|
||||||
);
|
);
|
||||||
|
|
||||||
auto * ctx = blueprint->project->get_function_context();
|
|
||||||
|
|
||||||
IkarusId ids[properties_out_size];
|
|
||||||
|
|
||||||
TRYRV(
|
|
||||||
,
|
|
||||||
blueprint->project
|
|
||||||
->get_db()
|
|
||||||
->query_many_buffered<IkarusId>(
|
|
||||||
"SELECT `id` FROM `properties` WHERE `source` = ?",
|
|
||||||
static_cast<IkarusId *>(ids),
|
|
||||||
properties_out_size,
|
|
||||||
blueprint->id
|
|
||||||
)
|
|
||||||
.on_error([ctx](auto const& err) {
|
|
||||||
ctx->set_error(
|
|
||||||
fmt::format("failed to fetch blueprint property ids: {}", err),
|
|
||||||
true,
|
|
||||||
IkarusErrorInfo_Source_SubSystem,
|
|
||||||
IkarusErrorInfo_Type_SubSystem_Database
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
LOG_DEBUG("blueprint properties: [{}]", fmt::join(ids, ids + properties_out_size, ", "));
|
|
||||||
|
|
||||||
for (size_t i = 0; i < properties_out_size; ++i) {
|
|
||||||
auto const id = ids[i];
|
|
||||||
|
|
||||||
VTRYRV(
|
|
||||||
auto const type,
|
|
||||||
,
|
|
||||||
IkarusProperty::get_property_type(blueprint->project, id).on_error([ctx, id](auto const& err) {
|
|
||||||
ctx->set_error(
|
|
||||||
fmt::format("failed to fetch property {}'s type: {}", id, err),
|
|
||||||
true,
|
|
||||||
IkarusErrorInfo_Source_SubSystem,
|
|
||||||
IkarusErrorInfo_Type_SubSystem_Database
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
|
||||||
properties_out[i] = blueprint->project->get_property(id, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_VERBOSE("successfully fetched blueprint properties");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
|
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
|
||||||
LOG_VERBOSE("fetching blueprint property count");
|
return ikarus::util::fetch_count(
|
||||||
|
blueprint,
|
||||||
auto * ctx = blueprint->project->get_function_context();
|
ikarus::util::CountQueryData{
|
||||||
|
.table_name = "blueprint_properties",
|
||||||
LOG_DEBUG("blueprint={}", blueprint->id);
|
.select_field_name = "property",
|
||||||
|
.where_field_name = "blueprint",
|
||||||
VTRYRV(
|
.relation_desc = "properties"
|
||||||
auto count,
|
}
|
||||||
0,
|
)
|
||||||
blueprint->project
|
.unwrap_value_or(0);
|
||||||
->get_db()
|
|
||||||
->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint` = ?;", blueprint->id)
|
|
||||||
.on_error([ctx](auto const& err) {
|
|
||||||
ctx->set_error(
|
|
||||||
fmt::format("failed to fetch blueprint property count: {}", err),
|
|
||||||
true,
|
|
||||||
IkarusErrorInfo_Source_SubSystem,
|
|
||||||
IkarusErrorInfo_Type_SubSystem_Database
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
LOG_DEBUG("blueprint property count: {}", count);
|
|
||||||
|
|
||||||
LOG_VERBOSE("successfully fetched blueprint property count");
|
|
||||||
|
|
||||||
return static_cast<size_t>(count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ikarus_blueprint_get_linked_entities(
|
void ikarus_blueprint_get_linked_entities(
|
||||||
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
|
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
|
||||||
) {
|
) {
|
||||||
LOG_VERBOSE("fetching blueprint linked entities");
|
ikarus::util::fetch_multiple_buffered<IkarusId>(
|
||||||
|
blueprint,
|
||||||
LOG_DEBUG(
|
ikarus::util::MultipleBufferQueryData{
|
||||||
"project={}; blueprint={}; entities_out_size={}",
|
.table_name = "entity_blueprint_links",
|
||||||
blueprint->project->get_path().c_str(),
|
.select_field_name = "entity",
|
||||||
blueprint->id,
|
.where_field_name = "blueprint",
|
||||||
entities_out_size
|
.relation_desc = "linked entities"
|
||||||
|
},
|
||||||
|
entities_out,
|
||||||
|
entities_out_size,
|
||||||
|
[&](IkarusProject * project, [[maybe_unused]] IkarusFunctionContext * ctx, IkarusId id
|
||||||
|
) -> cppbase::Result<IkarusEntity *, sqlitecpp::QueryError> { return cppbase::ok(project->get_entity(id)); }
|
||||||
);
|
);
|
||||||
|
|
||||||
auto * ctx = blueprint->project->get_function_context();
|
|
||||||
|
|
||||||
IkarusId ids[entities_out_size];
|
|
||||||
|
|
||||||
TRYRV(
|
|
||||||
,
|
|
||||||
blueprint->project
|
|
||||||
->get_db()
|
|
||||||
->query_many_buffered<IkarusId>(
|
|
||||||
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
|
|
||||||
static_cast<IkarusId *>(ids),
|
|
||||||
entities_out_size,
|
|
||||||
blueprint->id
|
|
||||||
)
|
|
||||||
.on_error([ctx](auto const& err) {
|
|
||||||
ctx->set_error(
|
|
||||||
fmt::format("failed to fetch blueprint linked entity ids: {}", err),
|
|
||||||
true,
|
|
||||||
IkarusErrorInfo_Source_SubSystem,
|
|
||||||
IkarusErrorInfo_Type_SubSystem_Database
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
LOG_DEBUG("blueprint linked entities: [{}]", fmt::join(ids, ids + entities_out_size, ", "));
|
|
||||||
|
|
||||||
for (size_t i = 0; i < entities_out_size; ++i) {
|
|
||||||
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
|
||||||
entities_out[i] = blueprint->project->get_entity(ids[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_VERBOSE("successfully fetched blueprint linked entities");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) {
|
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) {
|
||||||
LOG_VERBOSE("fetching blueprint linked entity count");
|
return ikarus::util::fetch_count(
|
||||||
|
blueprint,
|
||||||
LOG_DEBUG("project={}; blueprint={}", blueprint->project->get_path().c_str(), blueprint->id);
|
ikarus::util::CountQueryData{
|
||||||
|
.table_name = "entity_blueprint_links",
|
||||||
auto * ctx = blueprint->project->get_function_context();
|
.select_field_name = "entity",
|
||||||
|
.where_field_name = "blueprint",
|
||||||
VTRYRV(
|
.relation_desc = "linked entities"
|
||||||
auto count,
|
}
|
||||||
0,
|
)
|
||||||
blueprint->project
|
.unwrap_value_or(0);
|
||||||
->get_db()
|
|
||||||
->query_one<int>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->id)
|
|
||||||
.on_error([ctx](auto const& err) {
|
|
||||||
ctx->set_error(
|
|
||||||
fmt::format("failed to fetch blueprint linked entity count: {}", err),
|
|
||||||
true,
|
|
||||||
IkarusErrorInfo_Source_SubSystem,
|
|
||||||
IkarusErrorInfo_Type_SubSystem_Database
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
LOG_DEBUG("blueprint linked entity count: {}", count);
|
|
||||||
|
|
||||||
LOG_VERBOSE("successfully fetched blueprint linked entity count: {}", count);
|
|
||||||
|
|
||||||
return static_cast<size_t>(count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IkarusObject * ikarus_blueprint_to_object(IkarusBlueprint * blueprint) {
|
IkarusObject * ikarus_blueprint_to_object(IkarusBlueprint * blueprint) {
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,43 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "util.hpp"
|
||||||
|
|
||||||
#include <concepts>
|
#include <concepts>
|
||||||
|
|
||||||
#include <cppbase/result.hpp>
|
#include <cppbase/result.hpp>
|
||||||
|
#include <cppbase/strings.hpp>
|
||||||
|
|
||||||
#include <ikarus/id.h>
|
#include <ikarus/id.h>
|
||||||
#include <ikarus/objects/object_type.h>
|
#include <ikarus/objects/object_type.h>
|
||||||
|
|
||||||
#include <persistence/function_context.hpp>
|
#include <persistence/function_context.hpp>
|
||||||
|
|
||||||
namespace util {
|
namespace ikarus::util {
|
||||||
template<std::invocable<sqlitecpp::Connection *, IkarusId> F>
|
|
||||||
[[nodiscard]] cppbase::Result<IkarusId, sqlitecpp::TransactionError> insert_object(
|
struct EmptyNameError {};
|
||||||
IkarusProject * project, FunctionContext * ctx, IkarusObjectType type, std::string_view name, F insert_function
|
|
||||||
|
COMPOUND_ERROR(InsertObjectError, EmptyNameError, sqlitecpp::TransactionError);
|
||||||
|
|
||||||
|
template<typename InsertFunction, typename ObjectFactory>
|
||||||
|
[[nodiscard]] cppbase::Result<std::invoke_result_t<ObjectFactory, IkarusId>, InsertObjectError> insert_object(
|
||||||
|
IkarusProject * project,
|
||||||
|
IkarusObjectType type,
|
||||||
|
std::string_view name,
|
||||||
|
InsertFunction insert_function,
|
||||||
|
ObjectFactory object_factory
|
||||||
) {
|
) {
|
||||||
char const * object_type_str = ikarus_object_type_to_string(type);
|
auto const * object_type_str = ikarus_object_type_to_string(type);
|
||||||
|
|
||||||
|
LOG_INFO("creating new {}", object_type_str);
|
||||||
|
|
||||||
|
LOG_DEBUG("project={}; name={}", project->get_path().c_str(), name);
|
||||||
|
|
||||||
|
auto * ctx = project->get_function_context();
|
||||||
|
|
||||||
|
if (cppbase::is_empty_or_blank(name)) {
|
||||||
|
ctx->set_error("name is empty or blank", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
|
||||||
|
return cppbase::err(EmptyNameError{});
|
||||||
|
}
|
||||||
|
|
||||||
VTRY(
|
VTRY(
|
||||||
auto const id,
|
auto const id,
|
||||||
|
|
@ -42,17 +65,23 @@ template<std::invocable<sqlitecpp::Connection *, IkarusId> F>
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
LOG_VERBOSE("successfully created blueprint");
|
LOG_VERBOSE("successfully created {}", object_type_str);
|
||||||
|
|
||||||
return cppbase::ok(id);
|
return cppbase::ok(object_factory(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename Object>
|
||||||
void delete_object(IkarusProject * project, FunctionContext * ctx, T * object) {
|
requires std::derived_from<Object, IkarusObject>
|
||||||
auto id = object->id;
|
void delete_object(IkarusProject * project, Object * object) {
|
||||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(id));
|
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||||
|
|
||||||
TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", id).on_error([&](auto const& err) {
|
LOG_INFO("deleting {}", object_type_str);
|
||||||
|
|
||||||
|
LOG_DEBUG("project={}; {}={}", object_type_str, object->project->get_path().c_str(), object->id);
|
||||||
|
|
||||||
|
auto * ctx = object->project->get_function_context();
|
||||||
|
|
||||||
|
TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", object->id).on_error([&](auto const& err) {
|
||||||
ctx->set_error(
|
ctx->set_error(
|
||||||
fmt::format("failed to delete {} from objects table: {}", object_type_str, err),
|
fmt::format("failed to delete {} from objects table: {}", object_type_str, err),
|
||||||
true,
|
true,
|
||||||
|
|
@ -73,8 +102,9 @@ struct SingleQueryData {
|
||||||
std::string_view select_field_name;
|
std::string_view select_field_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T, typename Object>
|
||||||
cppbase::Result<T, sqlitecpp::SingleQueryError> fetch_single_field(IkarusObject * object, SingleQueryData const& query_data) {
|
requires std::derived_from<Object, IkarusObject>
|
||||||
|
cppbase::Result<T, sqlitecpp::SingleQueryError> fetch_single_field(Object const * object, SingleQueryData const& query_data) {
|
||||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||||
|
|
||||||
LOG_VERBOSE("fetching property default value");
|
LOG_VERBOSE("fetching property default value");
|
||||||
|
|
@ -86,7 +116,7 @@ cppbase::Result<T, sqlitecpp::SingleQueryError> fetch_single_field(IkarusObject
|
||||||
VTRY(
|
VTRY(
|
||||||
T value,
|
T value,
|
||||||
object->project->get_db()
|
object->project->get_db()
|
||||||
->query_one<T>(
|
->template query_one<T>(
|
||||||
fmt::format("SELECT `{}` FROM `{}` WHERE `id` = ?", query_data.select_field_name, query_data.table_name),
|
fmt::format("SELECT `{}` FROM `{}` WHERE `id` = ?", query_data.select_field_name, query_data.table_name),
|
||||||
object->id
|
object->id
|
||||||
)
|
)
|
||||||
|
|
@ -107,45 +137,105 @@ struct MultipleBufferQueryData {
|
||||||
std::string_view table_name;
|
std::string_view table_name;
|
||||||
std::string_view select_field_name;
|
std::string_view select_field_name;
|
||||||
std::string_view where_field_name;
|
std::string_view where_field_name;
|
||||||
|
std::string_view relation_desc;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename O>
|
template<typename Selected, typename Mapped, typename Object, typename F>
|
||||||
cppbase::Result<void, sqlitecpp::MultiQueryError> fetch_multiple_buffered(
|
requires std::derived_from<Object, IkarusObject>
|
||||||
IkarusObject * object,
|
void fetch_multiple_buffered(
|
||||||
MultipleBufferQueryData const& query_data,
|
Object const * object, MultipleBufferQueryData const& query_data, Mapped * mapped_buffer, size_t buffer_size, F transformer
|
||||||
std::string_view relation_desc,
|
)
|
||||||
T * buffer,
|
requires cppbase::
|
||||||
size_t buffer_size
|
is_result_with_value_type_v<Mapped, std::invoke_result_t<F, IkarusProject *, IkarusFunctionContext *, Selected>>
|
||||||
) {
|
{
|
||||||
|
auto * ctx = object->project->get_function_context();
|
||||||
|
|
||||||
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||||
|
|
||||||
LOG_VERBOSE("fetching {} {}", object_type_str, relation_desc);
|
LOG_VERBOSE("fetching {} {}", object_type_str, query_data.relation_desc);
|
||||||
|
|
||||||
LOG_VERBOSE("project={};{}={}", object->project->get_path().c_str(), object_type_str, object->id);
|
LOG_VERBOSE("project={};{}={}", object->project->get_path().c_str(), object_type_str, object->id);
|
||||||
|
|
||||||
auto * ctx = object->project->get_function_context();
|
Selected select_buffer[buffer_size];
|
||||||
|
|
||||||
TRY(object->project->get_db()
|
TRYRV(
|
||||||
->query_many_buffered<T>(
|
,
|
||||||
|
object->project->get_db()
|
||||||
|
->template query_many_buffered<Selected>(
|
||||||
fmt::format(
|
fmt::format(
|
||||||
"SELECT `{}` FROM `{}` WHERE `{}` = ?",
|
"SELECT `{}` FROM `{}` WHERE `{}` = ?",
|
||||||
query_data.select_field_name,
|
query_data.select_field_name,
|
||||||
query_data.table_name,
|
query_data.table_name,
|
||||||
query_data.where_field_name
|
query_data.where_field_name
|
||||||
),
|
),
|
||||||
buffer,
|
select_buffer,
|
||||||
buffer_size,
|
buffer_size,
|
||||||
object->id
|
object->id
|
||||||
)
|
)
|
||||||
.on_error([&](auto const& err) {
|
.on_error([&](auto const& err) {
|
||||||
ctx->set_error(
|
ctx->set_error(
|
||||||
fmt::format("failed to fetch {} {} from database: {}", object_type_str, relation_desc, err),
|
fmt::format("failed to fetch {} {} from database: {}", object_type_str, query_data.relation_desc, err),
|
||||||
true,
|
true,
|
||||||
IkarusErrorInfo_Source_SubSystem,
|
IkarusErrorInfo_Source_SubSystem,
|
||||||
IkarusErrorInfo_Type_SubSystem_Database
|
IkarusErrorInfo_Type_SubSystem_Database
|
||||||
);
|
);
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
|
|
||||||
return cppbase::ok();
|
LOG_DEBUG(
|
||||||
|
"{} {}: [{}]", object_type_str, query_data.relation_desc, fmt::join(select_buffer, select_buffer + buffer_size, ", ")
|
||||||
|
);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < buffer_size; ++i) {
|
||||||
|
VTRYRV(mapped_buffer[i], , transformer(object->project, ctx, select_buffer[i]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct CountQueryData {
|
||||||
|
std::string_view table_name;
|
||||||
|
std::string_view select_field_name;
|
||||||
|
std::string_view where_field_name;
|
||||||
|
std::string_view relation_desc;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Object>
|
||||||
|
requires std::derived_from<Object, IkarusObject>
|
||||||
|
cppbase::Result<cppbase::usize, sqlitecpp::QueryError> fetch_count(Object const * object, CountQueryData const& query_data) {
|
||||||
|
auto * object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id));
|
||||||
|
|
||||||
|
LOG_VERBOSE("fetching {} {} count", object_type_str, query_data.relation_desc);
|
||||||
|
|
||||||
|
auto * ctx = object->project->get_function_context();
|
||||||
|
|
||||||
|
LOG_DEBUG("{}={}", object_type_str, object->id);
|
||||||
|
|
||||||
|
VTRY(
|
||||||
|
auto count,
|
||||||
|
object->project->get_db()
|
||||||
|
->template query_one<int>(
|
||||||
|
fmt::format(
|
||||||
|
"SELECT COUNT(`{}`) FROM `{}` WHERE `{}` = ?;",
|
||||||
|
query_data.select_field_name,
|
||||||
|
query_data.table_name,
|
||||||
|
query_data.where_field_name
|
||||||
|
),
|
||||||
|
object->id
|
||||||
|
)
|
||||||
|
.on_error([&](auto const& err) {
|
||||||
|
ctx->set_error(
|
||||||
|
fmt::format("failed to fetch {} {} count: {}", object_type_str, query_data.relation_desc, err),
|
||||||
|
true,
|
||||||
|
IkarusErrorInfo_Source_SubSystem,
|
||||||
|
IkarusErrorInfo_Type_SubSystem_Database
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
LOG_DEBUG("{} {} count: {}", object_type_str, query_data.relation_desc, count);
|
||||||
|
|
||||||
|
LOG_VERBOSE("successfully fetched {} {} count", object_type_str, query_data.relation_desc);
|
||||||
|
|
||||||
|
return cppbase::ok(static_cast<size_t>(count));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
#include "function_context.hpp"
|
#include "function_context.hpp"
|
||||||
|
|
||||||
FunctionContext::FunctionContext(IkarusProject * project):
|
IkarusFunctionContext::IkarusFunctionContext(IkarusProject * project):
|
||||||
_project{project} {}
|
_project{project} {}
|
||||||
|
|
||||||
FunctionContext::~FunctionContext() {
|
IkarusFunctionContext::~IkarusFunctionContext() {
|
||||||
if (_project->_function_contexts.size() == 1) {
|
if (_project->_function_contexts.size() == 1) {
|
||||||
if (_project->error_message_buffer.empty()) {
|
if (_project->error_message_buffer.empty()) {
|
||||||
_project->error_message_buffer.push_back('\0');
|
_project->error_message_buffer.push_back('\0');
|
||||||
|
|
|
||||||
|
|
@ -12,17 +12,17 @@
|
||||||
|
|
||||||
#include <persistence/project.hpp>
|
#include <persistence/project.hpp>
|
||||||
|
|
||||||
struct FunctionContext {
|
struct IkarusFunctionContext {
|
||||||
public:
|
public:
|
||||||
explicit FunctionContext(struct IkarusProject * project);
|
explicit IkarusFunctionContext(struct IkarusProject * project);
|
||||||
|
|
||||||
FunctionContext(FunctionContext const&) noexcept = default;
|
IkarusFunctionContext(IkarusFunctionContext const&) noexcept = default;
|
||||||
FunctionContext(FunctionContext&&) noexcept = default;
|
IkarusFunctionContext(IkarusFunctionContext&&) noexcept = default;
|
||||||
|
|
||||||
auto operator=(FunctionContext const&) noexcept -> FunctionContext& = default;
|
auto operator=(IkarusFunctionContext const&) noexcept -> IkarusFunctionContext& = default;
|
||||||
auto operator=(FunctionContext&&) noexcept -> FunctionContext& = default;
|
auto operator=(IkarusFunctionContext&&) noexcept -> IkarusFunctionContext& = default;
|
||||||
|
|
||||||
~FunctionContext();
|
~IkarusFunctionContext();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename... Infos>
|
template<typename... Infos>
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ auto IkarusProject::get_db() const -> sqlitecpp::Connection const * {
|
||||||
return _db.get();
|
return _db.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto IkarusProject::get_function_context() -> FunctionContext * {
|
auto IkarusProject::get_function_context() -> IkarusFunctionContext * {
|
||||||
return &_function_contexts.emplace_back(this);
|
return &_function_contexts.emplace_back(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ public:
|
||||||
[[nodiscard]] auto get_db() const -> sqlitecpp::Connection const *;
|
[[nodiscard]] auto get_db() const -> sqlitecpp::Connection const *;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] auto get_function_context() -> struct FunctionContext *;
|
[[nodiscard]] auto get_function_context() -> struct IkarusFunctionContext *;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] auto get_blueprint(IkarusId id) -> struct IkarusBlueprint *;
|
[[nodiscard]] auto get_blueprint(IkarusId id) -> struct IkarusBlueprint *;
|
||||||
|
|
@ -54,7 +54,7 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend struct FunctionContext;
|
friend struct IkarusFunctionContext;
|
||||||
|
|
||||||
std::string _name;
|
std::string _name;
|
||||||
std::filesystem::path _path;
|
std::filesystem::path _path;
|
||||||
|
|
@ -67,5 +67,5 @@ private:
|
||||||
std::unordered_map<IkarusId, std::unique_ptr<IkarusProperty>> _properties;
|
std::unordered_map<IkarusId, std::unique_ptr<IkarusProperty>> _properties;
|
||||||
std::unordered_map<IkarusId, std::unique_ptr<IkarusEntity>> _entities;
|
std::unordered_map<IkarusId, std::unique_ptr<IkarusEntity>> _entities;
|
||||||
|
|
||||||
std::vector<FunctionContext> _function_contexts;
|
std::vector<IkarusFunctionContext> _function_contexts;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue