diff --git a/include/matador/object/object_proxy.hpp b/include/matador/object/object_proxy.hpp index 81f4788..2362b7c 100644 --- a/include/matador/object/object_proxy.hpp +++ b/include/matador/object/object_proxy.hpp @@ -1,6 +1,8 @@ #ifndef OBJECT_PROXY_HPP #define OBJECT_PROXY_HPP +#include "matador/object/primary_key_resolver.hpp" + #include namespace matador::object { @@ -24,12 +26,12 @@ public: }; template < typename Type > -class simple_object_resolver final : public object_resolver { +class static_object_resolver final : public object_resolver { public: - simple_object_resolver() = default; - explicit simple_object_resolver(Type* obj) + static_object_resolver() = default; + explicit static_object_resolver(Type* obj) : obj_(obj) {} - explicit simple_object_resolver(std::unique_ptr obj) + explicit static_object_resolver(std::unique_ptr obj) : obj_(std::move(obj)) {} Type* resolve(const object_proxy &/*proxy*/) const override { @@ -44,40 +46,48 @@ private: template class object_proxy final : public basic_object_proxy { public: - object_proxy() = default; - explicit object_proxy(Type* obj) - : resolver_(std::make_unique>(obj)) {} - explicit object_proxy(std::unique_ptr obj) - : resolver_(std::move(obj)) {} + object_proxy() = default; + explicit object_proxy(Type* obj) + : resolver_(std::make_unique>(obj)) + , pk_(primary_key_resolver::resolve_object(*obj).pk) {} - [[nodiscard]] void *get() const override { return static_cast(pointer()); } + [[nodiscard]] void *get() const override { return static_cast(pointer()); } - Type* operator->() const { return pointer(); } - Type& operator*() { return *pointer(); } - const Type& operator*() const { return *pointer(); } + Type* operator->() const { return pointer(); } + Type& operator*() { return *pointer(); } + const Type& operator*() const { return *pointer(); } - Type* pointer() const { return resolve(); } + Type* pointer() const { return resolve(); } - Type& ref() { return *pointer(); } - const Type& ref() const { return *pointer(); } + Type& ref() { return *pointer(); } + const Type& ref() const { return *pointer(); } - void reset(Type* obj) { - resolver_ = std::make_unique>(obj); - } - void reset(std::unique_ptr obj) { - resolver_ = std::make_unique>(std::move(obj)); - } - void reset(std::unique_ptr> &&resolver) { - resolver_ = std::move(resolver); - } + void reset(Type* obj) { + pk_ = primary_key_resolver::resolve_object(*obj).pk; + resolver_ = std::make_unique>(obj); + } + void reset(std::unique_ptr obj) { + pk_ = primary_key_resolver::resolve_object(*obj).pk; + resolver_ = std::make_unique>(std::move(obj)); + } + void reset(std::unique_ptr> &&resolver) { + resolver_ = std::move(resolver); + } + + [[nodiscard]] bool empty() const { return resolver_ == nullptr; } + [[nodiscard]] bool valid() const { return !empty(); } + [[nodiscard]] bool has_primary_key() const { return !pk_.is_null(); } + [[nodiscard]] const utils::identifier& primary_key() const { return pk_; } + void primary_key(const utils::identifier &pk) { pk_ = pk; } private: - Type* resolve() const { - return resolver_->resolve(*this); - } + Type* resolve() const { + return resolver_->resolve(*this); + } private: - std::unique_ptr> resolver_{std::make_unique>()}; + std::unique_ptr> resolver_{std::make_unique>()}; + utils::identifier pk_{}; }; } diff --git a/include/matador/object/object_ptr.hpp b/include/matador/object/object_ptr.hpp index db715d3..c32f57c 100644 --- a/include/matador/object/object_ptr.hpp +++ b/include/matador/object/object_ptr.hpp @@ -4,7 +4,6 @@ #include "matador/utils/identifier.hpp" #include "matador/object/object_proxy.hpp" -#include "matador/object/primary_key_resolver.hpp" #include @@ -15,8 +14,7 @@ public: object_ptr() : ptr_(std::make_shared>()) {} explicit object_ptr(Type *obj) - : ptr_(std::make_shared>(obj)) - , pk_(primary_key_resolver::resolve_object(*obj).pk) {} + : ptr_(std::make_shared>(obj)) {} object_ptr(const object_ptr &other) : ptr_(other.ptr_) {} object_ptr(object_ptr &&other) noexcept : ptr_(std::move(other.ptr_)) {} object_ptr &operator=(const object_ptr &other) = default; @@ -30,7 +28,6 @@ public: [[nodiscard]] bool empty() const { return ptr_->pointer() == nullptr; } void reset(Type *obj) { ptr_->reset(obj); - pk_ = primary_key_resolver::resolve_object(*obj).pk; } Type* get() const { return static_cast(ptr_->pointer()); } @@ -38,11 +35,11 @@ public: operator bool() { return valid(); } bool valid() { return ptr_ != nullptr; } - [[nodiscard]] const utils::identifier& primary_key() const { return pk_; } - void primary_key(const utils::identifier &pk) { pk_ = pk; } + [[nodiscard]] bool has_primary_key() const { return ptr_->has_primary_key(); } + [[nodiscard]] const utils::identifier& primary_key() const { return ptr_->primary_key(); } + void primary_key(const utils::identifier &pk) { ptr_->primary_key(pk); } private: std::shared_ptr> ptr_{}; - utils::identifier pk_; }; template diff --git a/include/matador/object/primary_key_resolver.hpp b/include/matador/object/primary_key_resolver.hpp index 54a69d9..4f1f75a 100644 --- a/include/matador/object/primary_key_resolver.hpp +++ b/include/matador/object/primary_key_resolver.hpp @@ -3,13 +3,16 @@ #include "matador/utils/access.hpp" #include "matador/utils/field_attributes.hpp" -#include "matador/utils/foreign_attributes.hpp" #include "matador/utils/identifier.hpp" #include "matador/utils/primary_key_attribute.hpp" #include #include +namespace matador::utils { +class foreign_attributes; +} + namespace matador::object { struct primary_key_info { diff --git a/include/matador/orm/session.hpp b/include/matador/orm/session.hpp index 75affa5..03e376b 100644 --- a/include/matador/orm/session.hpp +++ b/include/matador/orm/session.hpp @@ -29,6 +29,14 @@ struct session_context { size_t cache_size{500}; }; +template +class lazy_object_resolver final : public object::object_resolver { +public: + Type* resolve(const object::object_proxy& proxy) const override { + return proxy.resolve(); + } + +}; class session final : public sql::executor { public: explicit session(session_context &&ctx); diff --git a/source/core/utils/identifier.cpp b/source/core/utils/identifier.cpp index 21e72ec..a0c6d8c 100644 --- a/source/core/utils/identifier.cpp +++ b/source/core/utils/identifier.cpp @@ -62,10 +62,10 @@ size_t identifier::null_pk::hash() const } identifier::identifier() - : id_(std::make_shared()) {} +: id_(std::make_shared()) {} identifier::identifier(const identifier &x) - : id_(x.id_->copy()) {} +: id_(x.id_->copy()) {} identifier &identifier::operator=(const identifier &x) { if (this == &x) {