Compare commits

..

2 Commits

Author SHA1 Message Date
Sascha Kühl a2c5bca709 has many to many relation inserts 2026-02-19 22:32:15 +01:00
Sascha Kühl cfd2a9c423 some test adjustments 2026-02-19 22:31:51 +01:00
3 changed files with 123 additions and 23 deletions

View File

@ -59,6 +59,28 @@ struct pk_unset_checker {
static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {} static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {}
}; };
struct pk_value_extractor {
std::uint64_t value{0};
template<class V>
void on_primary_key(const char * /*id*/, V &pk, const utils::primary_key_attribute & = utils::default_pk_attributes) {
value = static_cast<std::uint64_t>(pk);
}
static void on_revision(const char * /*id*/, uint64_t & /*rev*/) {}
template<typename T>
static void on_attribute(const char * /*id*/, T &, const utils::field_attributes & = utils::null_attributes) {}
template<class P>
static void on_belongs_to(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
template<class P>
static void on_has_one(const char * /*id*/, P &, const utils::foreign_attributes & ) {}
template<class C>
static void on_has_many(const char * /*id*/, C &, const char * /*join_column*/, const utils::foreign_attributes & ) {}
template<class C>
static void on_has_many_to_many(const char * /*id*/, C &, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes & ) {}
template<class C>
static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {}
};
class insert_query_builder { class insert_query_builder {
public: public:
using step_query_t = std::variant<executable_query, fetchable_query>; using step_query_t = std::variant<executable_query, fetchable_query>;
@ -91,6 +113,12 @@ public:
build_for(ptr); build_for(ptr);
// relation inserts must run after all entity inserts were collected
for (auto &s : relation_steps_) {
steps_.push_back(std::move(s));
}
relation_steps_.clear();
return utils::ok(steps_); return utils::ok(steps_);
} }
@ -110,10 +138,53 @@ public:
void on_has_one(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) { void on_has_one(const char * /*id*/, Pointer &obj, const utils::foreign_attributes &attr) {
on_foreign_object(obj, attr); on_foreign_object(obj, attr);
} }
template<class C> template<class Collection>
static void on_has_many(const char * /*id*/, C &, const char * /*join_column*/, const utils::foreign_attributes & ) {} static void on_has_many(const char * /*id*/, Collection &con, const char * /*join_column*/, const utils::foreign_attributes & ) {}
template<class C> template<class Collection>
static void on_has_many_to_many(const char * /*id*/, C &, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes & ) {} void on_has_many_to_many(const char *id, Collection &con, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes & ) {
if (id == nullptr || join_column == nullptr || inverse_join_column == nullptr) {
return;
}
if (current_entity_pk_ == 0ULL) {
// Without a local PK we cannot create relation rows
// (PK may be assigned later by Identity; that case needs a different mechanism than requested here)
return;
}
const auto rel_it = schema_.find(std::string{id});
if (rel_it == schema_.end()) {
throw query_builder_exception(query_build_error::UnknownType);
}
const table &rel_table = rel_it->second.table();
for (auto &obj : con) {
if (!obj) {
continue;
}
// Ensure target exists as dependency (deps first)
if (!obj.is_persistent()) {
using dep_t = std::remove_reference_t<decltype(*obj)>;
build_for<dep_t>(obj);
}
// Extract FK value from the foreign object
pk_value_extractor fk{};
access::process(fk, *obj);
if (fk.value == 0ULL) {
continue;
}
// Build INSERT into relation table with the 2 FK columns
insert_step rel_step{};
rel_step.query = executable_query{
query::query::insert()
.into(rel_table, {table_column{&rel_table, join_column}, table_column{&rel_table, inverse_join_column}})
.values({utils::database_type{current_entity_pk_}, utils::database_type{fk.value}})
};
relation_steps_.push_back(std::move(rel_step));
}
}
template<class C> template<class C>
static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {} static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {}
@ -204,7 +275,10 @@ private:
const basic_schema &schema_; const basic_schema &schema_;
std::vector<insert_step> steps_; std::vector<insert_step> steps_;
std::vector<insert_step> relation_steps_;
std::unordered_set<std::pair<std::type_index, const void *>, visit_key_hash> visited_; std::unordered_set<std::pair<std::type_index, const void *>, visit_key_hash> visited_;
std::uint64_t current_entity_pk_{0};
}; };
} }
#endif //MATADOR_INSERT_QUERY_BUILDER_HPP #endif //MATADOR_INSERT_QUERY_BUILDER_HPP

View File

@ -482,10 +482,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
select_result = query::select({r.id, r.name, ri.ingredient_id, i.name}) select_result = query::select({r.id, r.name, ri.ingredient_id, i.name})
.from(r) .from(r)
.join_left(ri) .join_left(ri).on(r.id == ri.recipe_id)
.on(r.id == ri.recipe_id) .join_left(i).on(ri.ingredient_id == i.id)
.join_left(i)
.on(ri.ingredient_id == i.id)
.fetch_all(db); .fetch_all(db);
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());

View File

@ -17,9 +17,7 @@ using namespace matador::test;
TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") { TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
const auto result = schema.attach<airplane>("airplanes") const auto result = schema.attach<airplane>("airplanes")
.and_then([this] { .and_then([this] { return schema.create(db); } );
return schema.create(db);
} );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
auto plane = ses.insert(make_object<airplane>(1, "Boeing", "A380")); auto plane = ses.insert(make_object<airplane>(1, "Boeing", "A380"));
@ -35,9 +33,7 @@ TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") { TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
const auto res = schema.attach<airplane>("airplanes") const auto res = schema.attach<airplane>("airplanes")
.and_then([this] { .and_then([this] { return schema.create(db); } );
return schema.create(db);
} );
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
auto result = ses.insert(make_object<airplane>(1, "Boeing", "A380")); auto result = ses.insert(make_object<airplane>(1, "Boeing", "A380"));
@ -131,9 +127,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") { TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][find]") {
const auto result = schema.attach<airplane>("airplanes") const auto result = schema.attach<airplane>("airplanes")
.and_then([this] { .and_then([this] { return schema.create(db); } );
return schema.create(db);
} );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
std::vector planes { std::vector planes {
@ -187,7 +181,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
auto all_authors = find_result.release(); auto all_authors = find_result.release();
std::vector<object_ptr<author>> author_repo; std::vector<object_ptr<author>> author_repo;
for (auto it = all_authors.begin(); it != all_authors.end(); ++it) { for (auto it = all_authors.begin(); it != all_authors.end(); ++it) {
REQUIRE(it->books.size() == 0); REQUIRE(it->books.empty());
author_repo.emplace_back(it.optr()); author_repo.emplace_back(it.optr());
} }
REQUIRE(author_repo.size() == 2); REQUIRE(author_repo.size() == 2);
@ -240,7 +234,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
auto all_departments = find_result.release(); auto all_departments = find_result.release();
std::vector<object_ptr<department>> departments_repo; std::vector<object_ptr<department>> departments_repo;
for (auto it = all_departments.begin(); it != all_departments.end(); ++it) { for (auto it = all_departments.begin(); it != all_departments.end(); ++it) {
std::cout << "department: " << it->name << " (employees: " << it->employees.size() << ")\n"; REQUIRE(it->employees.empty());
departments_repo.emplace_back(it.optr()); departments_repo.emplace_back(it.optr());
} }
REQUIRE(departments_repo.size() == 2); REQUIRE(departments_repo.size() == 2);
@ -268,10 +262,10 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
all_departments = find_result.release(); all_departments = find_result.release();
for (auto it = all_departments.begin(); it != all_departments.end(); ++it) { for (auto it = all_departments.begin(); it != all_departments.end(); ++it) {
std::cout << "department: " << it->name << " (id: " << it->id << ", employees: " << it->employees.size() << ")\n"; REQUIRE(it->employees.size() == 5);
for (const auto& emp : it->employees) { // for (const auto& emp : it->employees) {
std::cout << "\temployee: " << emp->first_name << " " << emp->last_name << "( " << emp->id << ")\n"; // REQUIRE(emp->dep->id == it->id);
} // }
} }
} }
@ -303,6 +297,40 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-m
make_object<recipe>(3, "Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]}) make_object<recipe>(3, "Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]})
}; };
for (auto &r: recipes) {
auto res = ses.insert(r);
REQUIRE(res.is_ok());
}
}
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-many lazy relation", "[session][find][many-to-many][lazy]") {
auto result = schema.attach<recipe>("recipes")
.and_then( [this] { return schema.attach<ingredient>("ingredients"); } )
.and_then([this] {
return schema.create(db);
} );
std::vector ingredients {
make_object<ingredient>(1, "Apple"),
make_object<ingredient>(2, "Strawberry"),
make_object<ingredient>(3, "Pineapple"),
make_object<ingredient>(4, "Sugar"),
make_object<ingredient>(5, "Flour"),
make_object<ingredient>(6, "Butter"),
make_object<ingredient>(7, "Beans")
};
for (auto &i: ingredients) {
auto res = ses.insert(i);
REQUIRE(res.is_ok());
}
std::vector recipes {
make_object<recipe>(1, "Apple Pie", std::vector{ingredients[0], ingredients[3], ingredients[4]}),
make_object<recipe>(2, "Strawberry Cake", std::vector{ingredients[5], ingredients[6]}),
make_object<recipe>(3, "Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]})
};
for (auto &r: recipes) { for (auto &r: recipes) {
auto res = ses.insert(r); auto res = ses.insert(r);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());