fixed linux build for relation completer

This commit is contained in:
Sascha Kühl 2025-07-07 20:51:59 +02:00
parent e85543719c
commit 3374aba5ab
11 changed files with 72 additions and 59 deletions

View File

@ -17,7 +17,7 @@ struct ingredient
{
unsigned int id{};
std::string name;
matador::object::collection<matador::object::object_ptr<recipe>> recipes{};
matador::object::collection<matador::object::object_ptr<demo::recipe>> recipes{};
ingredient()= default;
ingredient(const unsigned int id, std::string name)
@ -36,7 +36,7 @@ struct recipe
{
unsigned int id{};
std::string name;
matador::object::collection<matador::object::object_ptr<ingredient>> ingredients{};
matador::object::collection<matador::object::object_ptr<demo::ingredient>> ingredients{};
recipe()= default;
recipe(const unsigned int id, std::string name)

View File

@ -74,7 +74,7 @@ struct profile {
unsigned int id{};
std::string first_name;
std::string last_name;
matador::object::object_ptr<user> user;
matador::object::object_ptr<demo::user> user;
template<typename Operator>
void process(Operator &op) {
@ -88,7 +88,7 @@ struct profile {
struct user {
unsigned int id{};
std::string username;
matador::object::object_ptr<profile> profile;
matador::object::object_ptr<demo::profile> profile;
template<typename Operator>
void process(Operator &op) {
@ -137,7 +137,7 @@ int main() {
// has_many_to_many (with join columns first)
object::schema schema;
auto result = schema.attach<ingredient>("ingredients")
auto result = schema.attach<demo::ingredient>("ingredients")
.and_then([&schema] { return schema.attach<recipe>("recipes"); });
schema.dump(std::cout);
@ -146,7 +146,7 @@ int main() {
// has_many_to_many (with join columns last)
object::schema schema;
auto result = schema.attach<recipe>("recipes")
auto result = schema.attach<demo::recipe>("recipes")
.and_then([&schema] { return schema.attach<ingredient>("ingredients"); });
schema.dump(std::cout);

View File

@ -25,20 +25,20 @@
template <> struct matador::utils::data_type_traits<work::core::timestamp, void> {
static basic_type type(std::size_t /*size*/) { return basic_type::type_uint64; }
static void read_value(attribute_reader &reader, const char *id, size_t index, nullptr_t &/*value*/) {
static void read_value(attribute_reader &/*reader*/, const char * /*id*/, size_t /*index*/, nullptr_t &/*value*/) {
}
static void bind_value(attribute_writer &binder, size_t index, nullptr_t &/*value*/) {
static void bind_value(attribute_writer &/*binder*/, size_t /*index*/, nullptr_t &/*value*/) {
}
};
template <> struct matador::utils::data_type_traits<work::core::UserInfo, void> {
static basic_type type(std::size_t /*size*/) { return basic_type::type_uint64; }
static void read_value(attribute_reader &reader, const char *id, size_t index, nullptr_t &/*value*/) {
static void read_value(attribute_reader &/*reader*/, const char * /*id*/, size_t /*index*/, nullptr_t &/*value*/) {
}
static void bind_value(attribute_writer &binder, size_t index, nullptr_t &/*value*/) {
static void bind_value(attribute_writer &/*binder*/, size_t /*index*/, nullptr_t &/*value*/) {
}
};

View File

@ -3,10 +3,12 @@
#include "matador/utils/access.hpp"
#include <cstdint>
namespace work::core {
struct Model {
unsigned long long id;
unsigned long long version;
uint64_t id;
uint64_t version;
template<typename Operator>
void process( Operator& op ) {

View File

@ -9,12 +9,12 @@
namespace work::models::jobs {
struct IdListPayload : Payload {
matador::object::collection<unsigned long long> ids;
matador::object::collection<uint64_t> payload_ids;
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::access;
field::process( op, *matador::base_class<Payload>( this ) );
field::has_many( op, "payload_ids", ids, "payload_id", matador::utils::default_foreign_attributes );
field::has_many( op, "payload_ids", payload_ids, "payload_id", matador::utils::default_foreign_attributes );
}
};
}

View File

@ -5,12 +5,12 @@
namespace work::models::jobs {
struct IdPayload : Payload {
unsigned long long id;
uint64_t payload_id{};
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::access;
field::process( op, *matador::base_class<Payload>( this ) );
field::attribute( op, "id", id );
field::attribute( op, "payload_id", payload_id );
}
};
}

View File

@ -20,7 +20,7 @@ struct Task : core::Model {
JobMode job_mode;
core::timestamp start_delay;
core::timestamp interval;
unsigned long long user_session_id;
uint64_t user_session_id;
template<typename Operator>
void process( Operator& op ) {

View File

@ -1,13 +1,12 @@
#ifndef QUERY_COLUMN_DEFINITION_HPP
#define QUERY_COLUMN_DEFINITION_HPP
#include <memory>
#include "matador/utils/basic_types.hpp"
#include "matador/utils/default_type_traits.hpp"
#include "matador/utils/field_attributes.hpp"
#include "matador/utils/value.hpp"
#include <memory>
#include <vector>
namespace matador::object {

View File

@ -9,6 +9,7 @@
#include "matador/utils/field_attributes.hpp"
#include "matador/utils/foreign_attributes.hpp"
#include <memory>
#include <typeindex>
#include <vector>
@ -32,7 +33,7 @@ public:
type_ = utils::data_type_traits<ValueType>::type(0);
}
void on_primary_key(const char * /*id*/, std::string &/*pk*/, size_t size);
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template < class Type >
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
static void on_attribute(const char * /*id*/, char * /*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}

View File

@ -5,7 +5,6 @@
#include "matador/object/join_columns_collector.hpp"
#include "matador/object/many_to_many_relation.hpp"
#include "matador/object/primary_key_resolver.hpp"
#include "matador/object/error_code.hpp"
#include "matador/object/schema_node.hpp"
#include "matador/object/schema_node_iterator.hpp"
@ -121,6 +120,7 @@ private:
const endpoint_ptr &other_endpoint);
static void link_relation_endpoints(const endpoint_ptr &endpoint,
const endpoint_ptr &other_endpoint);
private:
std::shared_ptr<schema_node> node_;
schema &schema_;
@ -143,7 +143,7 @@ public:
template<typename Type>
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
// if (has_node(name)) {
// return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
// return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + name + "' already exists"));
// }
auto node = schema_node::make_node<Type>(*this, name);
relation_completer<Type>::complete(node);
@ -167,7 +167,7 @@ public:
}
[[nodiscard]] utils::result<void, utils::error> detach(const node_ptr& node);
[[nodiscard]] utils::result<void, utils::error> detach(const node_ptr &node);
// [[nodiscard]] utils::result<void, utils::error> detach(const std::string& name);
/**
@ -275,8 +275,7 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
// Type was not found.
// Create and attach the relation node.
const std::type_index ti = typeid(many_to_many_relation<value_type, Type>);
const auto endpoint = node_->info().find_relation_endpoint(ti);
if (endpoint == node_->info().endpoint_end()) {
if (const auto endpoint = node_->info().find_relation_endpoint(ti); endpoint == node_->info().endpoint_end()) {
// Endpoint was not found
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", node_->name().c_str(), id, join_column);
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type> >(
@ -293,13 +292,13 @@ void relation_completer<Type>::on_has_many(const char *id, CollectionType &,
node_->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
foreign_endpoint->node_->info_->register_relation_endpoint(node_->type_index(), foreign_endpoint);
link_relation_endpoints(local_endpoint, foreign_endpoint);
}
} else {
// Type was found.
// Check if the relation node is already attached.
const auto &foreign_node = result.value();
if (const auto rit = foreign_node->info_->find_relation_endpoint(node_->type_index()); rit != foreign_node->info().endpoint_end()) {
if (const auto rit = foreign_node->info_->find_relation_endpoint(node_->type_index());
rit != foreign_node->info().endpoint_end()) {
if (rit->second->is_belongs_to()) {
rit->second->node_ = foreign_node;
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
@ -318,7 +317,8 @@ template<typename Type>
template<class CollectionType>
void relation_completer<Type>::on_has_many(const char *id, CollectionType &, const char *join_column,
const utils::foreign_attributes &,
std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>* /*unused*/) {
std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>
* /*unused*/) {
using value_type = typename CollectionType::value_type;
using relation_value_type = many_to_relation<Type, value_type>;
@ -348,9 +348,9 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
const utils::foreign_attributes &/*attr*/) {
auto result = schema_.find_node(id);
if (result) {
const auto foreign_node = result.value();
const auto &foreign_node = result.value();
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
const auto it = foreign_node->info_->find_relation_endpoint(node_->type_index());
if (it == foreign_node->info().endpoint_end()) {
// Todo: Throw error
@ -367,11 +367,13 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, node);
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BELONGS_TO, node);
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BELONGS_TO,
node);
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
node->info_->register_relation_endpoint(node_->type_index(), join_endpoint);
link_relation_endpoints(local_endpoint, join_endpoint);
node->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), inverse_join_endpoint);
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type),
inverse_join_endpoint);
result = schema_.attach_node(node, "");
if (!result) {
// Todo: throw internal error
@ -390,27 +392,28 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
const auto join_columns = join_columns_collector_.collect<typename CollectionType::value_type::value_type>();
using relation_value_type = many_to_many_relation<typename CollectionType::value_type::value_type, Type>;
auto creator = [&join_columns] {
return std::make_unique<relation_value_type>(join_columns.inverse_join_column, join_columns.join_column);
return std::make_unique<relation_value_type>(join_columns.inverse_join_column, join_columns.join_column);
};
auto node = schema_node::make_relation_node<relation_value_type>(schema_, id, std::move(creator));
auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
auto join_endpoint = std::make_shared<relation_endpoint>(join_columns.join_column, relation_type::BELONGS_TO, node);
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(join_columns.inverse_join_column, relation_type::BELONGS_TO, node);
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
auto inverse_join_endpoint = std::make_shared<relation_endpoint>(join_columns.inverse_join_column,
relation_type::BELONGS_TO, node);
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
node->info_->register_relation_endpoint(node_->type_index(), inverse_join_endpoint);
link_relation_endpoints(local_endpoint, inverse_join_endpoint);
node->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), join_endpoint);
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), join_endpoint);
result = schema_.attach_node(node, "");
if (!result) {
// Todo: throw internal error
return;
}
} else {
const auto foreign_node = result.value();
const auto &foreign_node = result.value();
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
node_->info_->register_relation_endpoint(typeid(CollectionType::value_type::value_type), local_endpoint);
node_->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
const auto it = foreign_node->info_->find_relation_endpoint(node_->type_index());
if (it == foreign_node->info().endpoint_end()) {
// Todo: Throw internal error
@ -425,20 +428,22 @@ template<class ForeignPointerType>
void relation_completer<Type>::on_has_one(const char *id,
ForeignPointerType &/*obj*/,
const utils::foreign_attributes &/*attr*/) {
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
const auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
if (const auto result = schema_.find_node(ti); !result.is_ok()) {
// Node was not found
// Create node without the foreign relation endpoint
log_.debug("node '%s' has foreign key '%s' has one '%s'", node_->name().c_str(), id, ti.name());
node_->info_->register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, node_));
// Node was not found
// Create node without the foreign relation endpoint
log_.debug("node '%s' has foreign key '%s' has one '%s'", node_->name().c_str(), id, ti.name());
node_->info_->
register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, node_));
} else {
const auto &foreign_node = result.value();
if (const auto it = foreign_node->info().find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
if (const auto it = foreign_node->info().find_relation_endpoint(typeid(Type));
it != foreign_node->info().endpoint_end()) {
if (it->second->is_belongs_to()) {
it->second->node_ = foreign_node;
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, node_);
node_->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
it->second->node_ = foreign_node;
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_ONE, node_);
node_->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else {
// Todo: Throw internal error
return;
@ -458,18 +463,21 @@ void relation_completer<Type>::on_belongs_to(const char *id,
// Type was not found
// Create node without the foreign relation endpoint
log_.debug("node '%s' has foreign key '%s' belongs to '%s'", node_->name().c_str(), id, ti.name());
node_->info_->register_relation_endpoint(ti, std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, schema::node_ptr{}));
node_->info_->register_relation_endpoint(
ti, std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, schema::node_ptr{}));
} else {
// Type was found
const auto& foreign_node = result.value();
const auto &foreign_node = result.value();
// Check foreign node and relation endpoint
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type));
it != foreign_node->info().endpoint_end()) {
// Found corresponding relation endpoint in the foreign node
if (it->second->is_has_one()) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, node_);
node_->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, node_);
node_->info_->register_relation_endpoint(ti, endpoint);
link_relation_endpoints(endpoint, it->second);
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type,
value_type>)) {
// Endpoint is a "many_to_many_relation". This means there
// is a "many_to_many_relation" node attached. Because of being a
// "belongs_to"-relation the "many_to_many_relation" can be removed
@ -490,14 +498,16 @@ void relation_completer<Type>::on_belongs_to(const char *id,
}
template<typename Type>
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint,
const endpoint_ptr &other_endpoint) {
endpoint->node_->info_->register_relation_endpoint(other_endpoint->node_->type_index(), endpoint);
other_endpoint->node_->info_->register_relation_endpoint(endpoint->node_->type_index(), other_endpoint);
link_relation_endpoints(endpoint, other_endpoint);
}
template<typename Type>
void relation_completer<Type>::link_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
void relation_completer<
Type>::link_relation_endpoints(const endpoint_ptr &endpoint, const endpoint_ptr &other_endpoint) {
endpoint->link_foreign_endpoint(other_endpoint);
other_endpoint->link_foreign_endpoint(endpoint);
}

View File

@ -1,6 +1,7 @@
#ifndef OOS_ACCESS_HPP
#define OOS_ACCESS_HPP
#include <cstdint>
#include <string>
#include <optional>
@ -38,7 +39,7 @@ void primary_key(Operator &op, const char *id, std::string &value, size_t size )
}
template<class Operator>
void revision(Operator &op, const char *id, unsigned long long &value) {
void revision(Operator &op, const char *id, uint64_t &value) {
op.on_revision(id, value);
}