has many generator progress

This commit is contained in:
2026-04-28 16:43:01 +02:00
parent 1a22c70320
commit 96c6c78cb8
7 changed files with 71 additions and 37 deletions
+43 -13
View File
@@ -15,11 +15,14 @@ using namespace matador::object;
using namespace matador::test;
namespace matador::test {
template<const utils::primary_key_attribute &PkAttribute>
struct book_identity;
template<const utils::primary_key_attribute &PkAttribute = utils::Identity>
struct author_identity {
unsigned int id{};
std::string name;
collection<object_ptr<book_identity> > books;
collection<object_ptr<book_identity<PkAttribute>>> books;
author_identity() = default;
explicit author_identity(std::string name)
@@ -28,31 +31,38 @@ struct author_identity {
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, utils::Identity);
field::primary_key(op, "id", id, PkAttribute);
field::attribute(op, "name", name, VarChar63);
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
}
};
template<const utils::primary_key_attribute &PkAttribute = utils::Identity>
struct book_identity {
unsigned int id{};
std::string title;
object_ptr<author_identity> book_author;
object_ptr<author_identity<PkAttribute>> book_author;
unsigned short published_in{};
book_identity() = default;
book_identity(std::string title, object_ptr<author_identity> author, const unsigned short published_in)
book_identity(std::string title, object_ptr<author_identity<PkAttribute>> author, const unsigned short published_in)
: title(std::move(title)), book_author(std::move(author)), published_in(published_in) {}
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, utils::Identity);
field::primary_key(op, "id", id, PkAttribute);
field::attribute(op, "title", title, VarChar511);
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
field::attribute(op, "published_in", published_in);
}
};
using author_sequence = author_identity<utils::Sequence>;
using author_table = author_identity<utils::Table>;
using book_sequence = book_identity<utils::Sequence>;
using book_table = book_identity<utils::Table>;
}
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[session][insert][has_many]") {
@@ -76,20 +86,40 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[
}
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with identity", "[session][insert][has_many][identity]") {
const auto result = schema.attach<book_identity>("books")
.and_then( [this] { return schema.attach<author_identity>("authors"); } )
const auto result = schema.attach<book_identity<>>("books")
.and_then( [this] { return schema.attach<author_identity<>>("authors"); } )
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_identity>("Steven King");
auto s_king = make_object<author_identity<>>("Steven King");
s_king->books.push_back(make_object<book_identity>("Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book_identity>("The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book_identity>("It", nullobj, 1986));
s_king->books.push_back(make_object<book_identity>("Misery", nullobj, 1987));
s_king->books.push_back(make_object<book_identity>("The Dark Tower: The Gunslinger", nullobj, 1982));
s_king->books.push_back(make_object<book_identity<>>("Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book_identity<>>("The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book_identity<>>("It", nullobj, 1986));
s_king->books.push_back(make_object<book_identity<>>("Misery", nullobj, 1987));
s_king->books.push_back(make_object<book_identity<>>("The Dark Tower: The Gunslinger", nullobj, 1982));
auto res = ses.insert(s_king);
REQUIRE(res.is_ok());
}
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation with sequence", "[session][insert][has_many][sequence]") {
const auto result = schema.attach<book_sequence>("books")
.and_then( [this] { return schema.attach<author_sequence>("authors"); } )
.and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok());
session ses({bus, connection::dns, 4}, schema);
auto s_king = make_object<author_sequence>("Steven King");
s_king->books.push_back(make_object<book_sequence>("Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book_sequence>("The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book_sequence>("It", nullobj, 1986));
s_king->books.push_back(make_object<book_sequence>("Misery", nullobj, 1987));
s_king->books.push_back(make_object<book_sequence>("The Dark Tower: The Gunslinger", nullobj, 1982));
auto res = ses.insert(s_king);
REQUIRE(res.is_ok());