From 510965988284f7f732c9fc52c0dbdb27032e589b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Mon, 17 Feb 2025 16:07:41 +0100 Subject: [PATCH] session progress --- include/matador/orm/session_query_builder.hpp | 8 +++----- .../matador/sql/internal/query_result_impl.hpp | 17 +++++++++++------ test/backends/SessionFixture.cpp | 4 +++- test/models/department.hpp | 2 ++ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/include/matador/orm/session_query_builder.hpp b/include/matador/orm/session_query_builder.hpp index bb9070b..ef3db8e 100644 --- a/include/matador/orm/session_query_builder.hpp +++ b/include/matador/orm/session_query_builder.hpp @@ -142,10 +142,9 @@ public: if (pk_.is_null()) { entity_query_data_.pk_column_name = id; } else if (pk_.is_integer()) { - const auto t = std::make_shared(table_info_stack_.top().info.get().name()); - auto v = *pk_.as(); - auto c = sql::column{t, id, ""}; - auto co = std::make_unique>(c, query::basic_condition::operand_type::EQUAL, v); + auto v = *pk_.as(); + auto c = sql::column{table_info_stack_.top().table, id, ""}; + auto co = std::make_unique>(c, query::basic_condition::operand_type::EQUAL, v); entity_query_data_.where_clause = std::move(co); entity_query_data_.pk_column_name = id; } @@ -281,7 +280,6 @@ public: append_join( sql::column{relation->second, join_columns.join_column}, sql::column{next->second, pk->name()} - // sql::column{std::make_shared(info->get().name()), pk->name()} ); } diff --git a/include/matador/sql/internal/query_result_impl.hpp b/include/matador/sql/internal/query_result_impl.hpp index 7be650e..a3ea4c4 100644 --- a/include/matador/sql/internal/query_result_impl.hpp +++ b/include/matador/sql/internal/query_result_impl.hpp @@ -12,6 +12,8 @@ #include #include +#include +#include namespace matador::utils { class value; @@ -82,14 +84,14 @@ public: void on_attribute(const char *id, utils::value &val, const utils::field_attributes &attr = utils::null_attributes); template < class Pointer > - void on_belongs_to(const char * /*id*/, Pointer &x, const utils::foreign_attributes &attr) - { + void on_belongs_to(const char * /*id*/, Pointer &x, const utils::foreign_attributes &attr) { if (x.empty()) { x = new typename Pointer::value_type; } if (attr.fetch() == utils::fetch_type::LAZY) { pk_reader_.read(*x, column_index_++); - } else { + } else if (const auto ti = std::type_index(typeid(*x)); processed_types_.count(ti) == 0) { + processed_types_.insert(ti); access::process(*this, *x); } } @@ -101,8 +103,8 @@ public: } if (attr.fetch() == utils::fetch_type::LAZY) { pk_reader_.read(*x, column_index_++); - } else { - access::process(*this, *x); + } else if (const auto ti = std::type_index(typeid(*x)); processed_types_.count(ti) == 0) { + processed_types_.insert(ti); } } @@ -114,7 +116,8 @@ public: void on_has_many(const char * /*id*/, ContainerType &cont, const char * /*join_column*/, const utils::foreign_attributes &attr) { if ( attr.fetch() == utils::fetch_type::LAZY ) { // pk_reader_.read(*id, column_index_++); - } else { + } else if (const auto ti = std::type_index(typeid(typename ContainerType::value_type::value_type)); processed_types_.count(ti) == 0) { + processed_types_.insert(ti); auto obj = std::make_unique(); // typename ContainerType::value_type x(new typename ContainerType::value_type::value_type); access::process(*this, *obj); @@ -145,6 +148,7 @@ public: if (!*fetched) { return false; } + processed_types_.insert(typeid(Type)); access::process(*this, obj); return true; } @@ -156,6 +160,7 @@ protected: std::vector prototype_; std::unique_ptr reader_; detail::pk_reader pk_reader_; + std::unordered_set processed_types_; }; namespace detail { diff --git a/test/backends/SessionFixture.cpp b/test/backends/SessionFixture.cpp index 95e4b39..2fe89a6 100644 --- a/test/backends/SessionFixture.cpp +++ b/test/backends/SessionFixture.cpp @@ -1,5 +1,7 @@ #include "SessionFixture.hpp" +#include "catch2/catch_test_macros.hpp" + namespace matador::test { SessionFixture::SessionFixture() @@ -14,7 +16,7 @@ SessionFixture::~SessionFixture() { void SessionFixture::drop_table_if_exists(const std::string &table_name) const { if (ses.table_exists(table_name)) { - ses.drop_table(table_name); + REQUIRE(ses.drop_table(table_name)); } } diff --git a/test/models/department.hpp b/test/models/department.hpp index 7c988c5..46190e4 100644 --- a/test/models/department.hpp +++ b/test/models/department.hpp @@ -14,6 +14,7 @@ struct department { unsigned int id{}; std::string name; std::vector> employees; + object::object_ptr manager; template void process(Operator &op) { @@ -21,6 +22,7 @@ struct department { field::primary_key(op, "id", id); field::attribute(op, "name", name, 63); field::has_many(op, "employees", employees, "dep_id", utils::fetch_type::EAGER); + field::belongs_to(op, "manager_id", manager, utils::fetch_type::EAGER); } };