object generation progress
This commit is contained in:
parent
9b7b74524f
commit
5afb616205
|
|
@ -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<object>& 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<attribute> attributes_;
|
||||
|
|
|
|||
|
|
@ -54,13 +54,13 @@ private:
|
|||
|
||||
public:
|
||||
template < class Type >
|
||||
static std::shared_ptr<object> generate(repository &repo, const std::string &name, const std::string &alias = "") {
|
||||
return generate(std::make_unique<Type>(), repo, name, alias);
|
||||
static std::shared_ptr<object> generate(repository &repo, const std::string &name) {
|
||||
return generate(std::make_unique<Type>(), repo, name);
|
||||
}
|
||||
|
||||
template < class Type >
|
||||
static std::shared_ptr<object> generate(std::unique_ptr<Type>&& t, repository &repo, const std::string &name, const std::string &alias = "") {
|
||||
auto obj = std::make_shared<object>(name, alias);
|
||||
static std::shared_ptr<object> generate(std::unique_ptr<Type>&& 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<class object> fk_object(const std::type_index& ti) const;
|
||||
|
||||
static std::shared_ptr<object> acquire_object(repository &repo, const std::type_index &ti, const std::string& name);
|
||||
private:
|
||||
repository &repo_;
|
||||
std::shared_ptr<object> object_;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@ public:
|
|||
|
||||
template < typename Type >
|
||||
static std::shared_ptr<repository_node> make_node(repository& repo, const std::string& name, creator_func<Type> creator = []{ return std::make_unique<Type>(); }) {
|
||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, typeid(Type)));
|
||||
const std::type_index ti(typeid(Type));
|
||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, ti));
|
||||
|
||||
auto obj = object_generator::generate<Type>(creator(), repo, name);
|
||||
auto info = std::make_unique<object_info<Type>>(
|
||||
|
|
|
|||
|
|
@ -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<object> 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;
|
||||
|
|
|
|||
|
|
@ -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<object>& 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 {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,17 @@ object_generator::object_generator(repository& repo, const std::shared_ptr<objec
|
|||
: repo_(repo)
|
||||
, object_(object) {}
|
||||
|
||||
std::shared_ptr<object> 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<object>(name));
|
||||
}
|
||||
|
||||
void object_generator::on_revision(const char* id, uint64_t& rev) {
|
||||
on_attribute(id, rev);
|
||||
}
|
||||
|
|
@ -61,6 +72,9 @@ std::shared_ptr<class object> 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<object>(""));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ TEST_CASE("Generate object from type", "[object][generate]") {
|
|||
|
||||
const auto obj = object::object_generator::generate<test::book>(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());
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue