has many to many eager loading via sql join (progress)

This commit is contained in:
Sascha Kühl 2025-02-16 22:14:20 +01:00
parent 66d5bc6c86
commit 6cb19f6ec8
3 changed files with 37 additions and 30 deletions

View File

@ -97,8 +97,8 @@ public:
return utils::failure(query_build_error::UnknownType); return utils::failure(query_build_error::UnknownType);
} }
pk_ = pk; pk_ = pk;
table_info_stack_.push(info.value()); table_info_stack_.push({info.value(), std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index))});
entity_query_data_ = { std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index)) }; entity_query_data_ = { table_info_stack_.top().table };
processed_tables_.insert({info->get().name(), entity_query_data_.root_table}); processed_tables_.insert({info->get().name(), entity_query_data_.root_table});
try { try {
access::process(*this, info->get().prototype()); access::process(*this, info->get().prototype());
@ -118,8 +118,8 @@ public:
return utils::failure(query_build_error::UnknownType); return utils::failure(query_build_error::UnknownType);
} }
pk_ = nullptr; pk_ = nullptr;
table_info_stack_.push(info.value()); table_info_stack_.push({info.value(), std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index))});
entity_query_data_ = { std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index)) }; entity_query_data_ = { table_info_stack_.top().table };
processed_tables_.insert({info->get().name(), entity_query_data_.root_table}); processed_tables_.insert({info->get().name(), entity_query_data_.root_table});
try { try {
access::process(*this, info->get().prototype()); access::process(*this, info->get().prototype());
@ -142,7 +142,7 @@ public:
if (pk_.is_null()) { if (pk_.is_null()) {
entity_query_data_.pk_column_name = id; entity_query_data_.pk_column_name = id;
} else if (pk_.is_integer()) { } else if (pk_.is_integer()) {
const auto t = std::make_shared<sql::table>(table_info_stack_.top().get().name()); const auto t = std::make_shared<sql::table>(table_info_stack_.top().info.get().name());
auto v = *pk_.as<V>(); auto v = *pk_.as<V>();
auto c = sql::column{t, id, ""}; auto c = sql::column{t, id, ""};
auto co = std::make_unique<query::condition<sql::column, V>>(c, query::basic_condition::operand_type::EQUAL, v); auto co = std::make_unique<query::condition<sql::column, V>>(c, query::basic_condition::operand_type::EQUAL, v);
@ -180,16 +180,12 @@ public:
throw query_builder_exception{query_build_error::UnknownType}; throw query_builder_exception{query_build_error::UnknownType};
} }
const auto curr = processed_tables_.find(table_info_stack_.top().get().name());
if (curr == processed_tables_.end()) {
throw query_builder_exception{query_build_error::UnexpectedError};
};
auto next = processed_tables_.find(info->get().name()); auto next = processed_tables_.find(info->get().name());
if (next != processed_tables_.end()) { if (next != processed_tables_.end()) {
return; return;
} }
table_info_stack_.push(info.value()); table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
next = processed_tables_.insert({info->get().name(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))}).first; next = processed_tables_.insert({info->get().name(), table_info_stack_.top().table}).first;
typename ContainerType::value_type::value_type obj; typename ContainerType::value_type::value_type obj;
access::process(*this , obj); access::process(*this , obj);
table_info_stack_.pop(); table_info_stack_.pop();
@ -200,7 +196,7 @@ public:
} }
append_join( append_join(
sql::column{curr->second, table_info_stack_.top().get().definition().primary_key()->name()}, sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
sql::column{next->second, join_column} sql::column{next->second, join_column}
); );
} }
@ -216,7 +212,13 @@ public:
if (!info) { if (!info) {
throw query_builder_exception{query_build_error::UnknownType}; throw query_builder_exception{query_build_error::UnknownType};
} }
table_info_stack_.push(info.value());
auto next = processed_tables_.find(info->get().name());
if (next != processed_tables_.end()) {
return;
}
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
next = processed_tables_.insert({info->get().name(), table_info_stack_.top().table}).first;
typename ContainerType::value_type::value_type obj; typename ContainerType::value_type::value_type obj;
access::process(*this , obj); access::process(*this , obj);
table_info_stack_.pop(); table_info_stack_.pop();
@ -227,11 +229,11 @@ public:
} }
append_join( append_join(
sql::column{std::make_shared<sql::table>(table_info_stack_.top().get().name()), table_info_stack_.top().get().definition().primary_key()->name()}, sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
sql::column{std::make_shared<sql::table>(id), join_column} sql::column{std::make_shared<sql::table>(id), join_column}
); );
append_join( append_join(
sql::column{std::make_shared<sql::table>(id), inverse_join_column}, sql::column{next->second, inverse_join_column},
sql::column{std::make_shared<sql::table>(info->get().name()), pk->name()} sql::column{std::make_shared<sql::table>(info->get().name()), pk->name()}
); );
} }
@ -246,7 +248,12 @@ public:
if (!info) { if (!info) {
throw query_builder_exception{query_build_error::UnknownType}; throw query_builder_exception{query_build_error::UnknownType};
} }
table_info_stack_.push(info.value());
auto next = processed_tables_.find(info->get().name());
if (next != processed_tables_.end()) {
return;
}
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
typename ContainerType::value_type::value_type obj; typename ContainerType::value_type::value_type obj;
access::process(*this , obj); access::process(*this , obj);
table_info_stack_.pop(); table_info_stack_.pop();
@ -259,7 +266,7 @@ public:
const auto join_columns = join_column_collector_.collect<typename ContainerType::value_type::value_type>(); const auto join_columns = join_column_collector_.collect<typename ContainerType::value_type::value_type>();
append_join( append_join(
sql::column{std::make_shared<sql::table>(table_info_stack_.top().get().name()), table_info_stack_.top().get().definition().primary_key()->name()}, sql::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()},
sql::column{std::make_shared<sql::table>(id), join_columns.inverse_join_column} sql::column{std::make_shared<sql::table>(id), join_columns.inverse_join_column}
); );
append_join( append_join(
@ -278,7 +285,12 @@ private:
private: private:
utils::value pk_; utils::value pk_;
std::stack<std::reference_wrapper<const object::basic_object_info>> table_info_stack_; struct table_info {
std::reference_wrapper<const object::basic_object_info> info;
std::shared_ptr<sql::table> table;
};
std::stack<table_info> table_info_stack_;
std::unordered_map<std::string, std::shared_ptr<sql::table>> processed_tables_; std::unordered_map<std::string, std::shared_ptr<sql::table>> processed_tables_;
const object::schema &schema_; const object::schema &schema_;
entity_query_data entity_query_data_; entity_query_data entity_query_data_;
@ -288,24 +300,19 @@ private:
}; };
template<class Pointer> template<class Pointer>
void session_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) void session_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) {
{
if (attr.fetch() == utils::fetch_type::EAGER) { if (attr.fetch() == utils::fetch_type::EAGER) {
const auto info = schema_.info<typename Pointer::value_type>(); const auto info = schema_.info<typename Pointer::value_type>();
if (!info) { if (!info) {
throw query_builder_exception{query_build_error::UnknownType}; throw query_builder_exception{query_build_error::UnknownType};
} }
const auto curr = processed_tables_.find(table_info_stack_.top().get().name());
if (curr == processed_tables_.end()) {
throw query_builder_exception{query_build_error::UnexpectedError};
};
auto next = processed_tables_.find(info->get().name()); auto next = processed_tables_.find(info->get().name());
if (next != processed_tables_.end()) { if (next != processed_tables_.end()) {
return; return;
} }
next = processed_tables_.insert({info->get().name(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))}).first; table_info_stack_.push({info.value(), std::make_shared<sql::table>(info->get().name(), build_alias('t', ++table_index))});
table_info_stack_.push(info.value()); next = processed_tables_.insert({info->get().name(), table_info_stack_.top().table}).first;
typename Pointer::value_type obj; typename Pointer::value_type obj;
access::process(*this, obj); access::process(*this, obj);
table_info_stack_.pop(); table_info_stack_.pop();
@ -315,7 +322,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u
throw query_builder_exception{query_build_error::MissingPrimaryKey}; throw query_builder_exception{query_build_error::MissingPrimaryKey};
} }
append_join( append_join(
sql::column{curr->second, id}, sql::column{table_info_stack_.top().table, id},
sql::column{next->second, pk->name()} sql::column{next->second, pk->name()}
); );
} else { } else {

View File

@ -16,7 +16,7 @@ void session_query_builder::on_revision(const char *id, unsigned long long &/*re
} }
void session_query_builder::push(const std::string &column_name) { void session_query_builder::push(const std::string &column_name) {
const auto it = processed_tables_.find(table_info_stack_.top().get().name()); const auto it = processed_tables_.find(table_info_stack_.top().info.get().name());
if (it == processed_tables_.end()) { if (it == processed_tables_.end()) {
throw query_builder_exception{query_build_error::UnexpectedError}; throw query_builder_exception{query_build_error::UnexpectedError};
} }

View File

@ -215,8 +215,8 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
} }
std::vector<std::pair<std::string, std::string>> expected_join_data { std::vector<std::pair<std::string, std::string>> expected_join_data {
{ "recipe_ingredients", R"("ingredients"."id" = "recipe_ingredients"."ingredient_id")"}, { "recipe_ingredients", R"("t01"."id" = "recipe_ingredients"."ingredient_id")"},
{ "recipes", R"("recipe_ingredients"."recipe_id" = "recipes"."id")"} { "recipes", R"("t02"."recipe_id" = "recipes"."id")"}
}; };
query_context qc; query_context qc;