diff --git a/include/matador/object/object.hpp b/include/matador/object/object.hpp index a77a5b9..6d9918d 100644 --- a/include/matador/object/object.hpp +++ b/include/matador/object/object.hpp @@ -15,7 +15,7 @@ class repository; class object { public: - explicit object(std::string name, std::string alias = ""); + explicit object(std::string name); static const attribute& create_attribute(std::string name, const std::shared_ptr& obj); @@ -24,7 +24,6 @@ public: [[nodiscard]] bool has_primary_key() const; [[nodiscard]] const std::string& name() const; - [[nodiscard]] const std::string& alias() const; void update_name(const std::string& name); @@ -44,8 +43,6 @@ private: friend class object_generator; std::string name_; - std::string alias_; - attribute* pk_attribute_{nullptr}; utils::identifier pk_identifier_; std::list attributes_; diff --git a/include/matador/object/object_generator.hpp b/include/matador/object/object_generator.hpp index e9926d6..3bee984 100644 --- a/include/matador/object/object_generator.hpp +++ b/include/matador/object/object_generator.hpp @@ -54,13 +54,13 @@ private: public: template < class Type > - static std::shared_ptr generate(repository &repo, const std::string &name, const std::string &alias = "") { - return generate(std::make_unique(), repo, name, alias); + static std::shared_ptr generate(repository &repo, const std::string &name) { + return generate(std::make_unique(), repo, name); } template < class Type > - static std::shared_ptr generate(std::unique_ptr&& t, repository &repo, const std::string &name, const std::string &alias = "") { - auto obj = std::make_shared(name, alias); + static std::shared_ptr generate(std::unique_ptr&& t, repository &repo, const std::string &name) { + auto obj = acquire_object(repo, typeid(Type), name); object_generator gen(repo, obj); access::process(gen, *t); return obj; @@ -115,6 +115,7 @@ private: [[nodiscard]] std::shared_ptr fk_object(const std::type_index& ti) const; + static std::shared_ptr acquire_object(repository &repo, const std::type_index &ti, const std::string& name); private: repository &repo_; std::shared_ptr object_; diff --git a/include/matador/object/repository_node.hpp b/include/matador/object/repository_node.hpp index e6dd865..0638642 100644 --- a/include/matador/object/repository_node.hpp +++ b/include/matador/object/repository_node.hpp @@ -20,7 +20,8 @@ public: template < typename Type > static std::shared_ptr make_node(repository& repo, const std::string& name, creator_func creator = []{ return std::make_unique(); }) { - auto node = std::shared_ptr(new repository_node(repo, name, typeid(Type))); + const std::type_index ti(typeid(Type)); + auto node = std::shared_ptr(new repository_node(repo, name, ti)); auto obj = object_generator::generate(creator(), repo, name); auto info = std::make_unique>( diff --git a/include/matador/object/restriction.hpp b/include/matador/object/restriction.hpp index cd4d32e..e95be7a 100644 --- a/include/matador/object/restriction.hpp +++ b/include/matador/object/restriction.hpp @@ -20,7 +20,7 @@ public: restriction() = default; explicit restriction(std::string name); - [[nodiscard]] const std::string& name() const; + [[nodiscard]] std::string name() const; [[nodiscard]] const class attribute* attribute() const; [[nodiscard]] std::string column_name() const; [[nodiscard]] std::shared_ptr owner() const; @@ -32,6 +32,8 @@ public: friend std::ostream& operator<<(std::ostream& os, const restriction& c); + std::string prefix() const; + private: friend class constraint_builder; friend class constraints_generator; diff --git a/source/core/object/object.cpp b/source/core/object/object.cpp index 3be8977..fe4bc0d 100644 --- a/source/core/object/object.cpp +++ b/source/core/object/object.cpp @@ -1,9 +1,10 @@ #include "matador/object/object.hpp" namespace matador::object { -object::object( std::string name, std::string alias ) -: name_(std::move(name)) -, alias_(std::move(alias)) {} +object::object(std::string name) +: name_(std::move(name)) { + int i = 9; +} const attribute& object::create_attribute(std::string name, const std::shared_ptr& obj) { attribute attr{std::move(name)}; @@ -11,16 +12,6 @@ const attribute& object::create_attribute(std::string name, const std::shared_pt return obj->attributes_.emplace_back(std::move(attr)); } -// void object::add_attribute( attribute attr ) { -// auto &ref = attributes_.emplace_back(std::move(attr)); -// ref.owner_ = this; -// } -// -// void object::add_constraint( class constraint c ) { -// auto &ref = constraints_.emplace_back(std::move(c)); -// ref.owner_ = this; -// } - attribute* object::primary_key_attribute() const { return pk_attribute_; } @@ -37,19 +28,15 @@ const std::string& object::name() const { return name_; } -const std::string& object::alias() const { - return alias_; -} - void object::update_name(const std::string& name) { name_ = name; - for (auto& con : constraints_) { - if (con.is_primary_key_constraint()) { - con.name_ += name; - } else if (con.is_foreign_key_constraint()) { - con.name_ = "FK_" + name + "_" + con.column_name(); - } - } + // for (auto& con : constraints_) { + // if (con.is_primary_key_constraint()) { + // con.name_ += name; + // } else if (con.is_foreign_key_constraint()) { + // con.name_ = "FK_" + name + "_" + con.column_name(); + // } + // } } bool object::has_attributes() const { diff --git a/source/core/object/object_generator.cpp b/source/core/object/object_generator.cpp index 6c824b1..b2666d3 100644 --- a/source/core/object/object_generator.cpp +++ b/source/core/object/object_generator.cpp @@ -9,6 +9,17 @@ object_generator::object_generator(repository& repo, const std::shared_ptr object_generator::acquire_object(repository &repo, const std::type_index &ti, const std::string& name) { + if (repo.has_object_for_type(ti)) { + auto obj = repo.object_for_type(ti); + repo.remove_object_for_type(ti); + obj->update_name(name); + return obj; + } + + return repo.provide_object_in_advance(ti, std::make_shared(name)); +} + void object_generator::on_revision(const char* id, uint64_t& rev) { on_attribute(id, rev); } @@ -61,6 +72,9 @@ std::shared_ptr object_generator::fk_object(const std::type_index& return result->get().object(); } + if (repo_.has_object_for_type(ti)) { + return repo_.object_for_type(ti); + } return repo_.provide_object_in_advance(ti, std::make_shared("")); } } diff --git a/source/core/object/restriction.cpp b/source/core/object/restriction.cpp index 3be83d9..47cad89 100644 --- a/source/core/object/restriction.cpp +++ b/source/core/object/restriction.cpp @@ -7,8 +7,8 @@ namespace matador::object { restriction::restriction(std::string name) : name_(std::move(name)) {} -const std::string & restriction::name() const { - return name_; +std::string restriction::name() const { + return prefix() + name_; } const class attribute* restriction::attribute() const { @@ -55,10 +55,23 @@ const std::string& restriction::ref_column_name() const { } std::ostream & operator<<(std::ostream &os, const class restriction &c) { - os << "constraint " << c.name_ << " for column " << c.column_name(); + os << "constraint " << c.name() << " for column " << c.column_name(); return os; } +std::string restriction::prefix() const { + if (utils::is_constraint_set(options_, utils::constraints::PrimaryKey)) { + return "PK_"; + } + if (utils::is_constraint_set(options_, utils::constraints::ForeignKey)) { + return "FK_"; + } + if (utils::is_constraint_set(options_, utils::constraints::Unique)) { + return "UK_"; + } + return ""; +} + // constraint_builder & constraint_builder::constraint(std::string name) { // constraint_name = std::move(name); // return *this; diff --git a/test/core/object/ObjectTest.cpp b/test/core/object/ObjectTest.cpp index 652bd9f..d07c8e5 100644 --- a/test/core/object/ObjectTest.cpp +++ b/test/core/object/ObjectTest.cpp @@ -15,7 +15,6 @@ TEST_CASE("Generate object from type", "[object][generate]") { const auto obj = object::object_generator::generate(repo, "books"); REQUIRE(obj->name() == "books"); - REQUIRE(obj->alias().empty()); REQUIRE(obj->attributes().size() == 4); REQUIRE(obj->constraints().size() == 2); REQUIRE(obj->has_primary_key()); diff --git a/test/core/object/SchemaTest.cpp b/test/core/object/SchemaTest.cpp index c0cb54e..a0870a0 100644 --- a/test/core/object/SchemaTest.cpp +++ b/test/core/object/SchemaTest.cpp @@ -105,8 +105,8 @@ TEST_CASE("Test one to many", "[relation][one-to-many]") { REQUIRE(repo.contains("departments")); REQUIRE(repo.contains("employees")); - std::cout << repo.basic_info("departments")->get().object(); - std::cout << repo.basic_info("employees")->get().object(); + std::cout << *repo.basic_info("departments")->get().object(); + std::cout << *repo.basic_info("employees")->get().object(); } TEST_CASE("Test one to many reverse", "[relation][one-to-many][reverse]") { @@ -121,8 +121,8 @@ TEST_CASE("Test one to many reverse", "[relation][one-to-many][reverse]") { REQUIRE(repo.contains("departments")); REQUIRE(repo.contains("employees")); - std::cout << repo.basic_info("departments")->get().object(); - std::cout << repo.basic_info("employees")->get().object(); + std::cout << *repo.basic_info("departments")->get().object(); + std::cout << *repo.basic_info("employees")->get().object(); } TEST_CASE("Test many to many relation", "[relation][many-to-many]") { @@ -139,7 +139,7 @@ TEST_CASE("Test many to many relation", "[relation][many-to-many]") { REQUIRE(repo.contains("recipes")); REQUIRE(repo.contains("recipe_ingredients")); - std::cout << repo.basic_info("ingredients")->get().object(); - std::cout << repo.basic_info("recipes")->get().object(); - std::cout << repo.basic_info("recipe_ingredients")->get().object(); + std::cout << *repo.basic_info("ingredients")->get().object(); + std::cout << *repo.basic_info("recipes")->get().object(); + std::cout << *repo.basic_info("recipe_ingredients")->get().object(); }