session and insert query builder progress

This commit is contained in:
sascha 2026-04-22 14:04:36 +02:00
parent 9b25f7f556
commit b6456356b4
5 changed files with 100 additions and 15 deletions

View File

@ -8,24 +8,39 @@
#include <memory>
namespace matador::object {
struct null_object_ptr_t {
constexpr null_object_ptr_t() = default;
};
inline constexpr null_object_ptr_t nullobj{};
template <typename Type>
class object_ptr {
public:
object_ptr()
: proxy_(std::make_shared<object_proxy<Type>>()) {}
object_ptr(null_object_ptr_t) {}
explicit object_ptr(std::shared_ptr<Type> obj)
: proxy_(std::make_shared<object_proxy<Type>>(obj)) {}
explicit object_ptr(std::shared_ptr<object_proxy<Type>> obj)
: proxy_(std::move(obj)) {}
object_ptr(const object_ptr &other) = default;
object_ptr(object_ptr &&other) noexcept = default;
object_ptr &operator=(const object_ptr &other) = default;
object_ptr &operator=(object_ptr &&other) = default;
object_ptr& operator=(const object_ptr &other) = default;
object_ptr& operator=(object_ptr &&other) = default;
object_ptr& operator=(null_object_ptr_t) {
proxy_.reset();
return *this;
}
bool operator==(const object_ptr &other) const {
return get() == other.get();
}
bool operator==(null_object_ptr_t) const {
return empty();
}
bool operator!=(const object_ptr &other) const { return !operator==(other); }
bool operator!=(null_object_ptr_t) const { return !empty(); }
using value_type = Type;

View File

@ -169,10 +169,6 @@ public:
continue;
}
if (obj.is_persistent()) {
continue;
}
// Ensure target exists as dependency (deps first)
if (obj.is_transient()) {
build_for(obj, relation_steps_);
@ -230,10 +226,6 @@ public:
continue;
}
if (obj.is_persistent()) {
continue;
}
// Ensure target exists as dependency (deps first)
if (obj.is_transient()) {
build_for(obj, relation_steps_);

View File

@ -48,6 +48,7 @@ private:
};
const primary_key_attribute DefaultPkAttributes {};
const primary_key_attribute Identity {generator_type::Identity};
const primary_key_attribute ManualVarChar63 {63};
const primary_key_attribute ManualVarChar127 {127};
const primary_key_attribute ManualVarChar255 {255};

View File

@ -11,6 +11,47 @@ using namespace matador;
using namespace matador::object;
using namespace matador::test;
namespace matador::test {
struct book_identity;
struct author_identity {
unsigned int id{};
std::string name;
collection<object_ptr<book_identity> > books;
author_identity() = default;
explicit author_identity(std::string name)
: name(std::move(name)) {}
template<typename Operator>
void process(Operator &op) {
namespace field = matador::access;
field::primary_key(op, "id", id, utils::Identity);
field::attribute(op, "first_name", name, VarChar63);
field::has_many(op, "books", books, "author_id", utils::CascadeAllFetchLazy);
}
};
struct book_identity {
unsigned int id{};
std::string title;
object_ptr<author_identity> book_author;
unsigned short published_in{};
book_identity() = default;
book_identity(std::string title, object_ptr<author_identity> 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::attribute(op, "title", title, VarChar511);
field::belongs_to(op, "author_id", book_author, utils::CascadeAllFetchEager);
field::attribute(op, "published_in", published_in);
}
};
}
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[session][insert][has_many]") {
const auto result = schema.attach<book>("books")
.and_then( [this] { return schema.attach<author>("authors"); } )
@ -22,11 +63,32 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many relation", "[
auto s_king = make_object<author>(1, "Steven", "King", "21.9.1947", 1956, false);
s_king->books.push_back(make_object<book>(2, "Carrie", object_ptr<author>{}, 1974));
s_king->books.push_back(make_object<book>(3, "The Shining", object_ptr<author>{}, 1977));
s_king->books.push_back(make_object<book>(4, "It", object_ptr<author>{}, 1986));
s_king->books.push_back(make_object<book>(5, "Misery", object_ptr<author>{}, 1987));
s_king->books.push_back(make_object<book>(6, "The Dark Tower: The Gunslinger", object_ptr<author>{}, 1982));
s_king->books.push_back(make_object<book>(2, "Carrie", nullobj, 1974));
s_king->books.push_back(make_object<book>(3, "The Shining", nullobj, 1977));
s_king->books.push_back(make_object<book>(4, "It", nullobj, 1986));
s_king->books.push_back(make_object<book>(5, "Misery", nullobj, 1987));
s_king->books.push_back(make_object<book>(6, "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 identity", "[session][insert][has_many][identity]") {
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());
orm::session ses({bus, connection::dns, 4}, schema);
schema.initialize(ses);
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));
auto res = ses.insert(s_king);
REQUIRE(res.is_ok());

View File

@ -5,10 +5,12 @@
#include "connection.hpp"
#include "models/recipe.hpp"
#include "models/model_metas.hpp"
using namespace matador;
using namespace matador::object;
using namespace matador::test;
using namespace matador::query::meta;
TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relation", "[session][insert][has_many_to_many]") {
auto result = schema.attach<recipe>("recipes")
@ -16,6 +18,7 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relat
.and_then([this] { return schema.create(db); } );
orm::session ses({bus, connection::dns, 4}, schema);
schema.initialize(ses);
std::vector ingredients {
make_object<ingredient>(1, "Apple"),
@ -37,4 +40,16 @@ TEST_CASE_METHOD(SessionFixture, "Test insert object with has many to many relat
auto res = ses.insert(r);
REQUIRE(res.is_ok());
}
auto recipe_result = ses.find<recipe>(1);
// auto recipe_result = ses.find<recipe>(RECIPE.id == 1);
REQUIRE(recipe_result.is_ok());
// auto r = *recipe_result->begin();
auto r = *recipe_result;
std::cout << r->name << " (ingredients: " << r->ingredients.size() << ")" << std::endl;
// REQUIRE(r->name != "");
for (const auto &ingr: r->ingredients) {
std::cout << " " << ingr->name << std::endl;
}
}