finished relation completer
This commit is contained in:
parent
c974628bee
commit
e85543719c
|
|
@ -13,3 +13,11 @@ target_link_libraries(sandbox PRIVATE
|
||||||
${CMAKE_DL_LIBS}
|
${CMAKE_DL_LIBS}
|
||||||
${SQLite3_LIBRARIES}
|
${SQLite3_LIBRARIES}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(work work.cpp)
|
||||||
|
target_link_libraries(work PRIVATE
|
||||||
|
matador-core
|
||||||
|
matador-orm
|
||||||
|
${CMAKE_DL_LIBS}
|
||||||
|
${SQLite3_LIBRARIES}
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
#ifndef QUERY_RECIPE_HPP
|
||||||
|
#define QUERY_RECIPE_HPP
|
||||||
|
|
||||||
|
#include "matador/utils/access.hpp"
|
||||||
|
#include "matador/utils/foreign_attributes.hpp"
|
||||||
|
|
||||||
|
#include "matador/object/collection.hpp"
|
||||||
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
#include "matador/object/many_to_many_relation.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace demo {
|
||||||
|
|
||||||
|
struct recipe;
|
||||||
|
struct ingredient
|
||||||
|
{
|
||||||
|
unsigned int id{};
|
||||||
|
std::string name;
|
||||||
|
matador::object::collection<matador::object::object_ptr<recipe>> recipes{};
|
||||||
|
|
||||||
|
ingredient()= default;
|
||||||
|
ingredient(const unsigned int id, std::string name)
|
||||||
|
: id(id), name(std::move(name)) {}
|
||||||
|
|
||||||
|
template<class Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::access;
|
||||||
|
field::primary_key(op, "id", id);
|
||||||
|
field::attribute(op, "name", name, 255);
|
||||||
|
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", matador::utils::fetch_type::EAGER);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct recipe
|
||||||
|
{
|
||||||
|
unsigned int id{};
|
||||||
|
std::string name;
|
||||||
|
matador::object::collection<matador::object::object_ptr<ingredient>> ingredients{};
|
||||||
|
|
||||||
|
recipe()= default;
|
||||||
|
recipe(const unsigned int id, std::string name)
|
||||||
|
: id(id), name(std::move(name)) {}
|
||||||
|
|
||||||
|
template<class Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::access;
|
||||||
|
field::primary_key(op, "id", id);
|
||||||
|
field::attribute(op, "name", name, 255);
|
||||||
|
field::has_many_to_many(op, "recipe_ingredients", ingredients, matador::utils::fetch_type::LAZY);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class recipe_ingredient : public matador::object::many_to_many_relation<recipe, ingredient> {
|
||||||
|
public:
|
||||||
|
recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //QUERY_RECIPE_HPP
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include "author.hpp"
|
#include "author.hpp"
|
||||||
#include "book.hpp"
|
#include "book.hpp"
|
||||||
|
#include "recipe.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
@ -54,6 +55,51 @@
|
||||||
* - set foreign endpoint of (2) to endpoint (1)
|
* - set foreign endpoint of (2) to endpoint (1)
|
||||||
* - check relation endpoints...
|
* - check relation endpoints...
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace demo {
|
||||||
|
struct names {
|
||||||
|
unsigned int id{};
|
||||||
|
std::vector<std::string> names_list;
|
||||||
|
|
||||||
|
template<typename Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::access;
|
||||||
|
field::primary_key( op, "id", id );
|
||||||
|
field::has_many(op, "name_list", names_list, "names_id", matador::utils::fetch_type::EAGER);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct user;
|
||||||
|
struct profile {
|
||||||
|
unsigned int id{};
|
||||||
|
std::string first_name;
|
||||||
|
std::string last_name;
|
||||||
|
matador::object::object_ptr<user> user;
|
||||||
|
|
||||||
|
template<typename Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::access;
|
||||||
|
field::primary_key( op, "id", id );
|
||||||
|
field::attribute( op, "first_name", first_name, 255 );
|
||||||
|
field::attribute( op, "last_name", last_name, 255 );
|
||||||
|
field::belongs_to( op, "user_id", user, matador::utils::default_foreign_attributes );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
struct user {
|
||||||
|
unsigned int id{};
|
||||||
|
std::string username;
|
||||||
|
matador::object::object_ptr<profile> profile;
|
||||||
|
|
||||||
|
template<typename Operator>
|
||||||
|
void process(Operator &op) {
|
||||||
|
namespace field = matador::access;
|
||||||
|
field::primary_key( op, "id", id );
|
||||||
|
field::attribute( op, "username", username, 255 );
|
||||||
|
field::has_one(op, "profile_id", profile, matador::utils::default_foreign_attributes );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
using namespace demo;
|
using namespace demo;
|
||||||
using namespace matador;
|
using namespace matador;
|
||||||
|
|
||||||
|
|
@ -62,6 +108,15 @@ int main() {
|
||||||
logger::add_log_sink(logger::create_stdout_sink());
|
logger::add_log_sink(logger::create_stdout_sink());
|
||||||
|
|
||||||
{
|
{
|
||||||
|
// has_many with builtin-type
|
||||||
|
object::schema schema;
|
||||||
|
|
||||||
|
auto result = schema.attach<names>("names");
|
||||||
|
|
||||||
|
schema.dump(std::cout);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// has_many to belongs_to
|
||||||
object::schema schema;
|
object::schema schema;
|
||||||
|
|
||||||
auto result = schema.attach<author>("authors")
|
auto result = schema.attach<author>("authors")
|
||||||
|
|
@ -70,6 +125,7 @@ int main() {
|
||||||
schema.dump(std::cout);
|
schema.dump(std::cout);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
// belongs_to to has_many
|
||||||
object::schema schema;
|
object::schema schema;
|
||||||
|
|
||||||
auto result = schema.attach<book>("books")
|
auto result = schema.attach<book>("books")
|
||||||
|
|
@ -77,4 +133,40 @@ int main() {
|
||||||
|
|
||||||
schema.dump(std::cout);
|
schema.dump(std::cout);
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
// has_many_to_many (with join columns first)
|
||||||
|
object::schema schema;
|
||||||
|
|
||||||
|
auto result = schema.attach<ingredient>("ingredients")
|
||||||
|
.and_then([&schema] { return schema.attach<recipe>("recipes"); });
|
||||||
|
|
||||||
|
schema.dump(std::cout);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// has_many_to_many (with join columns last)
|
||||||
|
object::schema schema;
|
||||||
|
|
||||||
|
auto result = schema.attach<recipe>("recipes")
|
||||||
|
.and_then([&schema] { return schema.attach<ingredient>("ingredients"); });
|
||||||
|
|
||||||
|
schema.dump(std::cout);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// belongs_to to has_one
|
||||||
|
object::schema schema;
|
||||||
|
|
||||||
|
auto result = schema.attach<profile>("profiles")
|
||||||
|
.and_then([&schema] { return schema.attach<user>("users"); });
|
||||||
|
|
||||||
|
schema.dump(std::cout);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// has_one to belongs_to
|
||||||
|
object::schema schema;
|
||||||
|
|
||||||
|
auto result = schema.attach<user>("users")
|
||||||
|
.and_then([&schema] { return schema.attach<profile>("profiles"); });
|
||||||
|
|
||||||
|
schema.dump(std::cout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,17 +11,38 @@
|
||||||
#include "work/admin/UserSession.hpp"
|
#include "work/admin/UserSession.hpp"
|
||||||
|
|
||||||
#include "work/jobs/Job.hpp"
|
#include "work/jobs/Job.hpp"
|
||||||
#include "work/jobs/Task.hpp""
|
#include "work/jobs/Task.hpp"
|
||||||
#include "work/jobs/Payload.hpp"
|
#include "work/jobs/Payload.hpp"
|
||||||
#include "work/jobs/IdPayload.hpp"
|
#include "work/jobs/IdPayload.hpp"
|
||||||
#include "work/jobs/IdListPayload.hpp"
|
#include "work/jobs/IdListPayload.hpp"
|
||||||
|
|
||||||
|
#include "matador/utils/default_type_traits.hpp"
|
||||||
#include "matador/object/schema.hpp"
|
#include "matador/object/schema.hpp"
|
||||||
|
|
||||||
#include "matador/sql/connection.hpp"
|
#include "matador/sql/connection.hpp"
|
||||||
|
|
||||||
#include "matador/orm/session.hpp"
|
#include "matador/orm/session.hpp"
|
||||||
|
|
||||||
|
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 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 bind_value(attribute_writer &binder, size_t index, nullptr_t &/*value*/) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
using namespace matador;
|
using namespace matador;
|
||||||
using namespace work::models;
|
using namespace work::models;
|
||||||
|
|
||||||
|
|
@ -51,36 +72,38 @@ using namespace work::models;
|
||||||
int main() {
|
int main() {
|
||||||
object::schema schema("Administration");
|
object::schema schema("Administration");
|
||||||
|
|
||||||
auto result = schema.attach<admin::CollectionCenter>("collection_center")
|
auto result = schema.attach<admin::CollectionCenter>("collection_centers")
|
||||||
.and_then([&schema] { return schema.attach<admin::UserDirectory>("user_directories"); })
|
.and_then([&schema] { return schema.attach<admin::UserDirectory>("user_directories"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::LdapGroupSchemaSettings>("ldap_group_schema_settings"); })
|
.and_then([&schema] { return schema.attach<admin::LdapGroupSchemaSettings>("ldap_group_schema_settings"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::LdapImportSettings>("ldap_import_settings"); })
|
.and_then([&schema] { return schema.attach<admin::LdapImportSettings>("ldap_import_settings"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::LdapUserSchemaSettings>("ldap_user_schema_settings"); })
|
.and_then([&schema] { return schema.attach<admin::LdapUserSchemaSettings>("ldap_user_schema_settings"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::UserDirectory, admin::InternalUserDirectory>("internal_user_directories"); })
|
.and_then([&schema] { return schema.attach<admin::InternalUserDirectory, admin::UserDirectory>("internal_user_directories"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::UserDirectory, admin::LdapUserDirectory>("ldap_user_directories"); } )
|
.and_then([&schema] { return schema.attach<admin::LdapUserDirectory, admin::UserDirectory>("ldap_user_directories"); } )
|
||||||
.and_then([&schema] { return schema.attach<admin::LoginHistory>("login_histories"); })
|
.and_then([&schema] { return schema.attach<admin::LoginHistory>("login_histories"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::Scenario>("scenarios"); })
|
.and_then([&schema] { return schema.attach<admin::Scenario>("scenarios"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::User>("users"); })
|
.and_then([&schema] { return schema.attach<admin::User>("users"); })
|
||||||
.and_then([&schema] { return schema.attach<admin::UserSession>("user_sessions"); })
|
.and_then([&schema] { return schema.attach<admin::UserSession>("user_sessions"); })
|
||||||
.and_then([&schema] { return schema.attach<jobs::Job>("jobs"); })
|
.and_then([&schema] { return schema.attach<jobs::Job>("jobs"); })
|
||||||
.and_then([&schema] { return schema.attach<jobs::Payload>("payloads"); })
|
.and_then([&schema] { return schema.attach<jobs::Payload>("payloads"); })
|
||||||
.and_then([&schema] { return schema.attach<jobs::Payload, jobs::IdPayload>("id_list_payloads"); })
|
.and_then([&schema] { return schema.attach<jobs::IdPayload, jobs::Payload>("id_list_payloads"); })
|
||||||
.and_then([&schema] { return schema.attach<jobs::Payload, jobs::IdListPayload>("id_payloads"); })
|
.and_then([&schema] { return schema.attach<jobs::IdListPayload, jobs::Payload>("id_payloads"); })
|
||||||
.and_then([&schema] { return schema.attach<jobs::Task>("tasks"); });
|
.and_then([&schema] { return schema.attach<jobs::Task>("tasks"); });
|
||||||
|
|
||||||
if (!result.is_ok()) {
|
if (!result.is_ok()) {
|
||||||
|
std::cout << "error: " << result.err().message() << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string dns{"sqlite://demo.db"};
|
schema.dump(std::cout);
|
||||||
sql::connection c(dns);
|
// const std::string dns{"sqlite://demo.db"};
|
||||||
|
// sql::connection c(dns);
|
||||||
result = c.open();
|
//
|
||||||
|
// result = c.open();
|
||||||
// orm::session s()
|
//
|
||||||
if (!result.is_ok()) {
|
// // orm::session s()
|
||||||
return 0;
|
// if (!result.is_ok()) {
|
||||||
}
|
// return 0;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include "../core/Model.hpp"
|
#include "../core/Model.hpp"
|
||||||
|
|
||||||
#include "matador/object/collection.hpp"
|
#include "matador/object/collection.hpp"
|
||||||
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
||||||
#include "matador/utils/base_class.hpp"
|
#include "matador/utils/base_class.hpp"
|
||||||
#include "matador/utils/enum_mapper.hpp"
|
#include "matador/utils/enum_mapper.hpp"
|
||||||
|
|
@ -29,7 +30,7 @@ struct CollectionCenter : core::Model {
|
||||||
std::string name;
|
std::string name;
|
||||||
matador::utils::blob symbol;
|
matador::utils::blob symbol;
|
||||||
CollectionCenterType type{CollectionCenterType::NoType};
|
CollectionCenterType type{CollectionCenterType::NoType};
|
||||||
matador::object::collection<User> users;
|
matador::object::collection<matador::object::object_ptr<User>> users;
|
||||||
|
|
||||||
template<typename Operator>
|
template<typename Operator>
|
||||||
void process( Operator& op ) {
|
void process( Operator& op ) {
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
#include "../core/Model.hpp"
|
#include "../core/Model.hpp"
|
||||||
|
|
||||||
#include "matador/object/object_ptr.hpp""
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
||||||
#include "matador/utils/base_class.hpp"
|
#include "matador/utils/base_class.hpp"
|
||||||
#include "matador/utils/enum_mapper.hpp"
|
#include "matador/utils/enum_mapper.hpp"
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ struct Scenario : core::Model {
|
||||||
std::string description;
|
std::string description;
|
||||||
matador::utils::blob symbol;
|
matador::utils::blob symbol;
|
||||||
ScenarioState state{ScenarioState::InvalidState};
|
ScenarioState state{ScenarioState::InvalidState};
|
||||||
matador::object::collection<CollectionCenter> collection_center;
|
matador::object::collection<matador::object::object_ptr<CollectionCenter>> collection_center;
|
||||||
|
|
||||||
template<typename Operator>
|
template<typename Operator>
|
||||||
void process( Operator& op ) {
|
void process( Operator& op ) {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
#include "../core/Model.hpp"
|
#include "../core/Model.hpp"
|
||||||
#include "../core/Types.hpp"
|
#include "../core/Types.hpp"
|
||||||
|
|
||||||
#include "matador/object/object_ptr.hpp""
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
||||||
#include "matador/utils/base_class.hpp"
|
#include "matador/utils/base_class.hpp"
|
||||||
#include "matador/utils/types.hpp"
|
#include "matador/utils/types.hpp"
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
#include "../core/Model.hpp"
|
#include "../core/Model.hpp"
|
||||||
#include "../core/Types.hpp"
|
#include "../core/Types.hpp"
|
||||||
|
|
||||||
#include "matador/object/object_ptr.hpp""
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
||||||
#include "matador/utils/base_class.hpp"
|
#include "matador/utils/base_class.hpp"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
#include "matador/object/collection.hpp"
|
#include "matador/object/collection.hpp"
|
||||||
|
|
||||||
|
#include "matador/utils/foreign_attributes.hpp"
|
||||||
|
|
||||||
namespace work::models::jobs {
|
namespace work::models::jobs {
|
||||||
struct IdListPayload : Payload {
|
struct IdListPayload : Payload {
|
||||||
matador::object::collection<unsigned long long> ids;
|
matador::object::collection<unsigned long long> ids;
|
||||||
|
|
@ -12,7 +14,7 @@ struct IdListPayload : Payload {
|
||||||
void process( Operator& op ) {
|
void process( Operator& op ) {
|
||||||
namespace field = matador::access;
|
namespace field = matador::access;
|
||||||
field::process( op, *matador::base_class<Payload>( this ) );
|
field::process( op, *matador::base_class<Payload>( this ) );
|
||||||
field::has_many( op, "payload_ids", ids );
|
field::has_many( op, "payload_ids", ids, "payload_id", matador::utils::default_foreign_attributes );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ struct Job : core::Model {
|
||||||
field::attribute( op, "state", state );
|
field::attribute( op, "state", state );
|
||||||
field::attribute( op, "mode", mode );
|
field::attribute( op, "mode", mode );
|
||||||
field::attribute( op, "created_at", created_at );
|
field::attribute( op, "created_at", created_at );
|
||||||
field::belongs_to( op, "payload", payload, matador::utils::default_foreign_attributes );
|
field::has_one(op, "payload", payload, matador::utils::default_foreign_attributes );
|
||||||
field::belongs_to( op, "task", task, matador::utils::default_foreign_attributes );
|
field::belongs_to( op, "task", task, matador::utils::default_foreign_attributes );
|
||||||
field::attribute( op, "user_info", user_info );
|
field::attribute( op, "user_info", user_info );
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,17 @@
|
||||||
#include "matador/utils/base_class.hpp"
|
#include "matador/utils/base_class.hpp"
|
||||||
|
|
||||||
namespace work::models::jobs {
|
namespace work::models::jobs {
|
||||||
|
struct Job;
|
||||||
struct Payload : core::Model {
|
struct Payload : core::Model {
|
||||||
std::string type;
|
std::string type;
|
||||||
|
matador::object::object_ptr<Job> job;
|
||||||
|
|
||||||
template<typename Operator>
|
template<typename Operator>
|
||||||
void process( Operator& op ) {
|
void process( Operator& op ) {
|
||||||
namespace field = matador::access;
|
namespace field = matador::access;
|
||||||
field::process( op, *matador::base_class<Model>( this ) );
|
field::process( op, *matador::base_class<Model>( this ) );
|
||||||
field::attribute( op, "type", type, 255 );
|
field::attribute( op, "type", type, 255 );
|
||||||
|
field::belongs_to( op, "job", job, matador::utils::default_foreign_attributes );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
#ifndef JOIN_COLUMNS_COLLECTOR_HPP
|
||||||
|
#define JOIN_COLUMNS_COLLECTOR_HPP
|
||||||
|
|
||||||
|
#include "matador/utils/access.hpp"
|
||||||
|
#include "matador/utils/field_attributes.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace matador::object {
|
||||||
|
struct join_columns {
|
||||||
|
std::string join_column;
|
||||||
|
std::string inverse_join_column;
|
||||||
|
};
|
||||||
|
|
||||||
|
class join_columns_collector final {
|
||||||
|
public:
|
||||||
|
template<class Type>
|
||||||
|
join_columns collect() {
|
||||||
|
join_columns_ = {};
|
||||||
|
Type obj;
|
||||||
|
|
||||||
|
access::process(*this, obj);
|
||||||
|
|
||||||
|
return join_columns_;
|
||||||
|
}
|
||||||
|
template < class V >
|
||||||
|
void on_primary_key(const char * /*id*/, V &, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr) {}
|
||||||
|
static void on_primary_key(const char * /*id*/, std::string &, size_t) {}
|
||||||
|
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
||||||
|
template<typename Type>
|
||||||
|
static void on_attribute(const char * /*id*/, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
||||||
|
template<class Pointer>
|
||||||
|
static void on_belongs_to(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
|
||||||
|
template<class Pointer>
|
||||||
|
static void on_has_one(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
|
||||||
|
template<class ContainerType>
|
||||||
|
static void on_has_many(ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {}
|
||||||
|
template<class ContainerType>
|
||||||
|
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
|
||||||
|
join_columns_.join_column = join_column;
|
||||||
|
join_columns_.inverse_join_column = inverse_join_column;
|
||||||
|
}
|
||||||
|
template<class ContainerType>
|
||||||
|
static void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
join_columns join_columns_;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //JOIN_COLUMNS_COLLECTOR_HPP
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
#define SCHEMA_HPP
|
#define SCHEMA_HPP
|
||||||
|
|
||||||
#include "matador/logger/log_manager.hpp"
|
#include "matador/logger/log_manager.hpp"
|
||||||
|
|
||||||
|
#include "matador/object/join_columns_collector.hpp"
|
||||||
#include "matador/object/many_to_many_relation.hpp"
|
#include "matador/object/many_to_many_relation.hpp"
|
||||||
#include "matador/object/primary_key_resolver.hpp"
|
#include "matador/object/primary_key_resolver.hpp"
|
||||||
#include "matador/object/error_code.hpp"
|
#include "matador/object/error_code.hpp"
|
||||||
|
|
@ -105,8 +107,8 @@ public:
|
||||||
template<class CollectionType>
|
template<class CollectionType>
|
||||||
void on_has_many_to_many(const char *id, CollectionType &collection, const char *join_column,
|
void on_has_many_to_many(const char *id, CollectionType &collection, const char *join_column,
|
||||||
const char *inverse_join_column, const utils::foreign_attributes &attr);
|
const char *inverse_join_column, const utils::foreign_attributes &attr);
|
||||||
template<class ContainerType>
|
template<class CollectionType>
|
||||||
void on_has_many_to_many(const char *id, ContainerType &collection, const utils::foreign_attributes &attr);
|
void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit relation_completer(const std::shared_ptr<schema_node> &node)
|
explicit relation_completer(const std::shared_ptr<schema_node> &node)
|
||||||
|
|
@ -123,6 +125,7 @@ private:
|
||||||
std::shared_ptr<schema_node> node_;
|
std::shared_ptr<schema_node> node_;
|
||||||
schema &schema_;
|
schema &schema_;
|
||||||
logger::logger log_;
|
logger::logger log_;
|
||||||
|
join_columns_collector join_columns_collector_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -140,9 +143,9 @@ public:
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
|
||||||
// if (has_node(name)) {
|
// 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 = acquire_node<Type>(name);
|
auto node = schema_node::make_node<Type>(*this, name);
|
||||||
relation_completer<Type>::complete(node);
|
relation_completer<Type>::complete(node);
|
||||||
if (auto result = attach_node(node, parent); !result) {
|
if (auto result = attach_node(node, parent); !result) {
|
||||||
return utils::failure(result.err());
|
return utils::failure(result.err());
|
||||||
|
|
@ -239,25 +242,11 @@ private:
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
node_ptr acquire_node(const std::string &name) {
|
|
||||||
if (const auto it = expected_node_map_.find(typeid(Type)); it != expected_node_map_.end()) {
|
|
||||||
const auto node = it->second;
|
|
||||||
expected_node_map_.erase(it);
|
|
||||||
node->update_name(name);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
return schema_node::make_node<Type>(*this, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[[nodiscard]] bool has_node(const std::string &name) const;
|
[[nodiscard]] bool has_node(const std::string &name) const;
|
||||||
[[nodiscard]] bool has_node(const std::type_index &index) const;
|
[[nodiscard]] bool has_node(const std::type_index &index) const;
|
||||||
[[nodiscard]] bool has_node(const std::type_index &index, const std::string &name) const;
|
[[nodiscard]] bool has_node(const std::type_index &index, const std::string &name) const;
|
||||||
|
|
||||||
static void push_back_child(const node_ptr &parent, const node_ptr &child);
|
static void insert_node(const node_ptr &parent, const node_ptr &child);
|
||||||
void remove_node(const node_ptr &node);
|
void remove_node(const node_ptr &node);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -269,7 +258,6 @@ private:
|
||||||
|
|
||||||
t_node_map nodes_by_name_;
|
t_node_map nodes_by_name_;
|
||||||
t_type_index_node_map nodes_by_type_;
|
t_type_index_node_map nodes_by_type_;
|
||||||
t_type_index_node_map expected_node_map_;
|
|
||||||
logger::logger log_;
|
logger::logger log_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -330,70 +318,130 @@ template<typename Type>
|
||||||
template<class CollectionType>
|
template<class CollectionType>
|
||||||
void relation_completer<Type>::on_has_many(const char *id, CollectionType &, const char *join_column,
|
void relation_completer<Type>::on_has_many(const char *id, CollectionType &, const char *join_column,
|
||||||
const utils::foreign_attributes &,
|
const utils::foreign_attributes &,
|
||||||
std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>
|
std::enable_if_t<!is_object_ptr<typename CollectionType::value_type>::value>* /*unused*/) {
|
||||||
* /*unused*/) {
|
|
||||||
using value_type = typename CollectionType::value_type;
|
using value_type = typename CollectionType::value_type;
|
||||||
|
using relation_value_type = many_to_relation<Type, value_type>;
|
||||||
|
|
||||||
const auto node = schema_node::make_relation_node<many_to_many_relation<value_type, Type> >(
|
const auto node = schema_node::make_relation_node<relation_value_type>(
|
||||||
schema_, id, [join_column] {
|
schema_, id, [join_column] {
|
||||||
return new many_to_many_relation<value_type, Type>(join_column, "value");
|
return std::make_unique<relation_value_type>(join_column, "value");
|
||||||
});
|
});
|
||||||
|
|
||||||
const auto result = schema_.attach<many_to_relation<Type, value_type> >(id);
|
const auto result = schema_.attach_node(node, "");
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// Todo: throw internal exception
|
// Todo: throw internal exception
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HAS_MANY, node_);
|
||||||
|
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BELONGS_TO, node);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
template<class CollectionType>
|
||||||
|
void relation_completer<Type>::on_has_many_to_many(const char *id,
|
||||||
|
CollectionType &/*collection*/,
|
||||||
|
const char *join_column,
|
||||||
|
const char *inverse_join_column,
|
||||||
|
const utils::foreign_attributes &/*attr*/) {
|
||||||
|
auto result = schema_.find_node(id);
|
||||||
|
if (result) {
|
||||||
|
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);
|
||||||
|
const auto it = foreign_node->info_->find_relation_endpoint(node_->type_index());
|
||||||
|
if (it == foreign_node->info().endpoint_end()) {
|
||||||
|
// Todo: Throw error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
link_relation_endpoints(local_endpoint, it->second);
|
||||||
|
} else {
|
||||||
|
using relation_value_type = many_to_many_relation<typename CollectionType::value_type::value_type, Type>;
|
||||||
|
auto creator = [join_column, inverse_join_column] {
|
||||||
|
return std::make_unique<relation_value_type>(join_column, inverse_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_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);
|
||||||
|
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);
|
||||||
|
result = schema_.attach_node(node, "");
|
||||||
|
if (!result) {
|
||||||
|
// Todo: throw internal error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
template<class CollectionType>
|
template<class CollectionType>
|
||||||
void relation_completer<Type>::on_has_many_to_many(const char *id, CollectionType &/*collection*/,
|
void relation_completer<Type>::on_has_many_to_many(const char *id,
|
||||||
const char *join_column,
|
CollectionType &/*collection*/,
|
||||||
const char *inverse_join_column,
|
const utils::foreign_attributes &/*attr*/) {
|
||||||
const utils::foreign_attributes &attr) {
|
|
||||||
auto result = schema_.find_node(id);
|
|
||||||
if (result) {
|
|
||||||
} else {
|
|
||||||
//
|
|
||||||
// using relation_type = many_to_many_relation<typename CollectionType::value_type, Type>;
|
|
||||||
// auto creator = [join_column, inverse_join_column] {
|
|
||||||
// return new many_to_many_relation<typename CollectionType::value_type, Type>(join_column, inverse_join_column);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// auto node = schema_node::make_relation_node<relation_type>(schema_, id);
|
|
||||||
|
|
||||||
// schema_.attach_node(node, typeid(relation_type));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
template<class ContainerType>
|
|
||||||
void relation_completer<Type>::on_has_many_to_many(const char *id, ContainerType &collection,
|
|
||||||
const utils::foreign_attributes &attr) {
|
|
||||||
auto result = schema_.find_node(id);
|
auto result = schema_.find_node(id);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// using relation_type = many_to_many_relation<typename ContainerType::value_type, Type>;
|
const auto join_columns = join_columns_collector_.collect<typename CollectionType::value_type::value_type>();
|
||||||
// auto creator = [attr] {
|
using relation_value_type = many_to_many_relation<typename CollectionType::value_type::value_type, Type>;
|
||||||
// return new relation_type(attr.join_column, attr.inverse_join_column);
|
auto creator = [&join_columns] {
|
||||||
// };
|
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);
|
||||||
|
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);
|
||||||
|
result = schema_.attach_node(node, "");
|
||||||
|
if (!result) {
|
||||||
|
// Todo: throw internal error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
const auto it = foreign_node->info_->find_relation_endpoint(node_->type_index());
|
||||||
|
if (it == foreign_node->info().endpoint_end()) {
|
||||||
|
// Todo: Throw internal error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
link_relation_endpoints(local_endpoint, it->second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
template<class ForeignPointerType>
|
template<class ForeignPointerType>
|
||||||
void relation_completer<Type>::on_has_one(const char *id, ForeignPointerType &/*obj*/,
|
void relation_completer<Type>::on_has_one(const char *id,
|
||||||
|
ForeignPointerType &/*obj*/,
|
||||||
const utils::foreign_attributes &/*attr*/) {
|
const utils::foreign_attributes &/*attr*/) {
|
||||||
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
|
auto ti = std::type_index(typeid(typename ForeignPointerType::value_type));
|
||||||
if (const auto result = schema_.find_node(ti); !result.is_ok() && schema_.expected_node_map_.count(ti) == 0) {
|
if (const auto result = schema_.find_node(ti); !result.is_ok()) {
|
||||||
schema_.expected_node_map_.insert({
|
// Node was not found
|
||||||
ti, schema_node::make_node<typename ForeignPointerType::value_type>(schema_, ti.name())
|
// 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 {
|
} else {
|
||||||
const auto &foreign_node = result.value();
|
const auto &foreign_node = result.value();
|
||||||
if (const auto rit = foreign_node->info().find_relation_endpoint(ti); rit != foreign_node->info().endpoint_end()) {
|
if (const auto it = foreign_node->info().find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
|
||||||
if (rit->second->is_has_many()) {
|
if (it->second->is_belongs_to()) {
|
||||||
} else if (rit->second->is_has_one()) {
|
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 {
|
} else {
|
||||||
|
// Todo: Throw internal error
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -401,7 +449,8 @@ void relation_completer<Type>::on_has_one(const char *id, ForeignPointerType &/*
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
template<class ForeignPointerType>
|
template<class ForeignPointerType>
|
||||||
void relation_completer<Type>::on_belongs_to(const char *id, ForeignPointerType & /*obj*/,
|
void relation_completer<Type>::on_belongs_to(const char *id,
|
||||||
|
ForeignPointerType & /*obj*/,
|
||||||
const utils::foreign_attributes & /*attr*/) {
|
const utils::foreign_attributes & /*attr*/) {
|
||||||
using value_type = typename ForeignPointerType::value_type;
|
using value_type = typename ForeignPointerType::value_type;
|
||||||
const auto ti = std::type_index(typeid(value_type));
|
const auto ti = std::type_index(typeid(value_type));
|
||||||
|
|
@ -416,7 +465,11 @@ void relation_completer<Type>::on_belongs_to(const char *id, ForeignPointerType
|
||||||
// Check foreign node and relation endpoint
|
// 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
|
// Found corresponding relation endpoint in the foreign node
|
||||||
if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
|
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>)) {
|
||||||
// Endpoint is a "many_to_many_relation". This means there
|
// Endpoint is a "many_to_many_relation". This means there
|
||||||
// is a "many_to_many_relation" node attached. Because of being a
|
// is a "many_to_many_relation" node attached. Because of being a
|
||||||
// "belongs_to"-relation the "many_to_many_relation" can be removed
|
// "belongs_to"-relation the "many_to_many_relation" can be removed
|
||||||
|
|
@ -429,10 +482,9 @@ void relation_completer<Type>::on_belongs_to(const char *id, ForeignPointerType
|
||||||
// check type
|
// check type
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Relation node was not found, create endpoint.
|
// Relation node was not found, create only endpoint.
|
||||||
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, node_);
|
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BELONGS_TO, node_);
|
||||||
node_->info_->register_relation_endpoint(ti, endpoint);
|
node_->info_->register_relation_endpoint(ti, endpoint);
|
||||||
link_relation_endpoints(endpoint, it->second);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include "matador/sql/connection.hpp"
|
#include "matador/sql/connection.hpp"
|
||||||
|
|
||||||
|
#include "matador/object/join_columns_collector.hpp"
|
||||||
#include "matador/object/schema.hpp"
|
#include "matador/object/schema.hpp"
|
||||||
|
|
||||||
#include "matador/utils/result.hpp"
|
#include "matador/utils/result.hpp"
|
||||||
|
|
@ -18,48 +19,6 @@
|
||||||
|
|
||||||
namespace matador::orm {
|
namespace matador::orm {
|
||||||
|
|
||||||
struct join_columns
|
|
||||||
{
|
|
||||||
std::string join_column;
|
|
||||||
std::string inverse_join_column;
|
|
||||||
};
|
|
||||||
|
|
||||||
class join_column_collector
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
template<class Type>
|
|
||||||
join_columns collect() {
|
|
||||||
join_columns_ = {};
|
|
||||||
Type obj;
|
|
||||||
|
|
||||||
access::process(*this, obj);
|
|
||||||
|
|
||||||
return join_columns_;
|
|
||||||
}
|
|
||||||
template < class V >
|
|
||||||
void on_primary_key(const char * /*id*/, V &, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr) {}
|
|
||||||
static void on_primary_key(const char * /*id*/, std::string &, size_t) {}
|
|
||||||
static void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
|
||||||
template<typename Type>
|
|
||||||
static void on_attribute(const char * /*id*/, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
|
||||||
template<class Pointer>
|
|
||||||
static void on_belongs_to(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
|
|
||||||
template<class Pointer>
|
|
||||||
static void on_has_one(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many(ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
|
|
||||||
join_columns_.join_column = join_column;
|
|
||||||
join_columns_.inverse_join_column = inverse_join_column;
|
|
||||||
}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many_to_many(const char * /*id*/, ContainerType &/*c*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
join_columns join_columns_;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct entity_query_data {
|
struct entity_query_data {
|
||||||
std::shared_ptr<sql::table> root_table;
|
std::shared_ptr<sql::table> root_table;
|
||||||
std::string pk_column_name{};
|
std::string pk_column_name{};
|
||||||
|
|
@ -255,7 +214,7 @@ public:
|
||||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto join_columns = join_column_collector_.collect<typename ContainerType::value_type::value_type>();
|
const auto join_columns = join_columns_collector_.collect<typename ContainerType::value_type::value_type>();
|
||||||
|
|
||||||
append_join(
|
append_join(
|
||||||
sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
|
sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
|
||||||
|
|
@ -288,7 +247,7 @@ private:
|
||||||
entity_query_data entity_query_data_;
|
entity_query_data entity_query_data_;
|
||||||
unsigned int column_index{0};
|
unsigned int column_index{0};
|
||||||
unsigned int table_index{0};
|
unsigned int table_index{0};
|
||||||
join_column_collector join_column_collector_;
|
object::join_columns_collector join_columns_collector_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
|
|
|
||||||
|
|
@ -70,10 +70,6 @@ utils::result<std::shared_ptr<attribute_definition>, utils::error> schema::refer
|
||||||
return utils::ok((*result)->info().reference_column());
|
return utils::ok((*result)->info().reference_column());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (const auto it = expected_node_map_.find(type_index); it != expected_node_map_.end()) {
|
|
||||||
return utils::ok(it->second->info().reference_column());
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils::failure(result.err());
|
return utils::failure(result.err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,7 +77,12 @@ void schema::dump(std::ostream &os) const {
|
||||||
for (const auto &node : *this) {
|
for (const auto &node : *this) {
|
||||||
os << "node [" << node.name() << "] (" << node.type_index().name() << ")\n";
|
os << "node [" << node.name() << "] (" << node.type_index().name() << ")\n";
|
||||||
for (auto it = node.info().endpoint_begin(); it != node.info().endpoint_end(); ++it) {
|
for (auto it = node.info().endpoint_begin(); it != node.info().endpoint_end(); ++it) {
|
||||||
os << " " << node.name() << "::" << it->second->field_name() << " (" << it->second->type_name() << ") <---> " << it->second->foreign_endpoint()->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
|
os << " " << /*node.name() << "::" <<*/ it->second->field_name() << " (" << it->second->type_name() << ")";
|
||||||
|
if (it->second->foreign_endpoint()) {
|
||||||
|
os << " <---> " << it->second->foreign_endpoint()->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
|
||||||
|
} else {
|
||||||
|
os << "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -104,7 +105,7 @@ utils::result<schema::node_ptr, utils::error> schema::attach_node(const std::sha
|
||||||
parent_node = *result;
|
parent_node = *result;
|
||||||
}
|
}
|
||||||
|
|
||||||
push_back_child(parent_node, node);
|
insert_node(parent_node, node);
|
||||||
|
|
||||||
// Todo: check return value
|
// Todo: check return value
|
||||||
nodes_by_name_.insert({node->name(), node})/*.first*/;
|
nodes_by_name_.insert({node->name(), node})/*.first*/;
|
||||||
|
|
@ -123,7 +124,7 @@ utils::result<schema::node_ptr, utils::error> schema::attach_node(const std::sha
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
push_back_child(*result, node);
|
insert_node(*result, node);
|
||||||
|
|
||||||
// Todo: check return value
|
// Todo: check return value
|
||||||
nodes_by_name_.insert({node->name(), node})/*.first*/;
|
nodes_by_name_.insert({node->name(), node})/*.first*/;
|
||||||
|
|
@ -150,7 +151,7 @@ utils::result<schema::node_ptr, utils::error> schema::find_node(const std::type_
|
||||||
return utils::ok(i->second);
|
return utils::ok(i->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
void schema::push_back_child(const node_ptr &parent, const node_ptr &child) {
|
void schema::insert_node(const node_ptr &parent, const node_ptr &child) {
|
||||||
child->parent_ = parent;
|
child->parent_ = parent;
|
||||||
child->previous_sibling_ = parent->last_child_->previous_sibling_;
|
child->previous_sibling_ = parent->last_child_->previous_sibling_;
|
||||||
child->next_sibling_ = parent->last_child_;
|
child->next_sibling_ = parent->last_child_;
|
||||||
|
|
|
||||||
|
|
@ -197,8 +197,8 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
|
||||||
connection db("noop://noop.db");
|
connection db("noop://noop.db");
|
||||||
schema scm("noop");
|
schema scm("noop");
|
||||||
auto result = scm.attach<recipe>("recipes")
|
auto result = scm.attach<recipe>("recipes")
|
||||||
.and_then( [&scm] { return scm.attach<ingredient>("ingredients"); } )
|
.and_then( [&scm] { return scm.attach<ingredient>("ingredients"); } );
|
||||||
.and_then( [&scm] { return scm.attach<recipe_ingredient>("recipe_ingredients"); } );
|
// .and_then( [&scm] { return scm.attach<recipe_ingredient>("recipe_ingredients"); } );
|
||||||
|
|
||||||
session_query_builder eqb(scm);
|
session_query_builder eqb(scm);
|
||||||
|
|
||||||
|
|
@ -242,8 +242,8 @@ TEST_CASE("Create sql query data for entity with eager many to many (inverse par
|
||||||
connection db("noop://noop.db");
|
connection db("noop://noop.db");
|
||||||
schema scm("noop");
|
schema scm("noop");
|
||||||
auto result = scm.attach<student>("students")
|
auto result = scm.attach<student>("students")
|
||||||
.and_then( [&scm] { return scm.attach<course>("courses"); } )
|
.and_then( [&scm] { return scm.attach<course>("courses"); } );
|
||||||
.and_then( [&scm] { return scm.attach<student_course>("student_courses"); } );
|
// .and_then( [&scm] { return scm.attach<student_course>("student_courses"); } );
|
||||||
|
|
||||||
session_query_builder eqb(scm);
|
session_query_builder eqb(scm);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue