Compare commits
4 Commits
5afb616205
...
758ea96ea4
| Author | SHA1 | Date |
|---|---|---|
|
|
758ea96ea4 | |
|
|
79c46bbd38 | |
|
|
571cfd52ae | |
|
|
f842efb611 |
|
|
@ -15,7 +15,6 @@ enum class null_option_type : uint8_t {
|
||||||
};
|
};
|
||||||
|
|
||||||
class object;
|
class object;
|
||||||
class attribute_generator;
|
|
||||||
|
|
||||||
class attribute {
|
class attribute {
|
||||||
public:
|
public:
|
||||||
|
|
@ -65,7 +64,6 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class object;
|
friend class object;
|
||||||
friend class attribute_generator;
|
|
||||||
friend class object_generator;
|
friend class object_generator;
|
||||||
|
|
||||||
std::string name_;
|
std::string name_;
|
||||||
|
|
|
||||||
|
|
@ -1,157 +0,0 @@
|
||||||
#ifndef QUERY_COLUMN_DEFINITION_GENERATOR_HPP
|
|
||||||
#define QUERY_COLUMN_DEFINITION_GENERATOR_HPP
|
|
||||||
|
|
||||||
#include "matador/object/attribute.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/access.hpp"
|
|
||||||
#include "matador/utils/data_type_traits.hpp"
|
|
||||||
#include "matador/utils/error.hpp"
|
|
||||||
#include "matador/utils/field_attributes.hpp"
|
|
||||||
#include "matador/utils/identifier.hpp"
|
|
||||||
#include "matador/utils/primary_key_attribute.hpp"
|
|
||||||
#include "matador/utils/result.hpp"
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <typeindex>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace matador::object {
|
|
||||||
|
|
||||||
class repository;
|
|
||||||
|
|
||||||
class fk_attribute_generator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
fk_attribute_generator() = default;
|
|
||||||
|
|
||||||
template<class Type>
|
|
||||||
attribute generate(const char *id, Type &x) {
|
|
||||||
access::process(*this, x);
|
|
||||||
return attribute{id, type_, {utils::constraints::ForeignKey }, null_option_type::NotNull};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ValueType>
|
|
||||||
void on_primary_key(const char *, ValueType &/*pk*/, const utils::primary_key_attribute& attr = utils::default_pk_attributes) {
|
|
||||||
type_ = utils::data_type_traits<ValueType>::type(attr.size());
|
|
||||||
}
|
|
||||||
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
|
|
||||||
template < class Type >
|
|
||||||
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
|
||||||
static void on_attribute(const char * /*id*/, char * /*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
|
||||||
template<class Pointer>
|
|
||||||
static void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class Pointer>
|
|
||||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
utils::basic_type type_{};
|
|
||||||
};
|
|
||||||
|
|
||||||
class attribute_generator final {
|
|
||||||
private:
|
|
||||||
attribute_generator(std::vector<attribute> &columns, const repository &repo, const std::shared_ptr<object>& obj);
|
|
||||||
|
|
||||||
public:
|
|
||||||
~attribute_generator() = default;
|
|
||||||
|
|
||||||
template < class Type >
|
|
||||||
static std::vector<attribute> generate(const repository &repo, const std::shared_ptr<object> &obj) {
|
|
||||||
Type t;
|
|
||||||
return generate(t, repo, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
template < class Type >
|
|
||||||
static std::vector<attribute> generate(const Type& t, const repository &repo, const std::shared_ptr<object> &obj) {
|
|
||||||
std::vector<attribute> columns;
|
|
||||||
attribute_generator gen(columns, repo, obj);
|
|
||||||
access::process(gen, t);
|
|
||||||
return columns;
|
|
||||||
}
|
|
||||||
|
|
||||||
template < class Type >
|
|
||||||
void on_primary_key(const char *, Type &x, const utils::primary_key_attribute& attr = utils::default_pk_attributes);
|
|
||||||
void on_revision(const char *id, uint64_t &rev);
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::null_attributes);
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
void on_attribute(const char *id, std::optional<Type> &x, const utils::field_attributes &attr = utils::null_attributes);
|
|
||||||
|
|
||||||
template<class Pointer>
|
|
||||||
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
|
|
||||||
on_foreign_key(id, x);
|
|
||||||
}
|
|
||||||
template<class Pointer>
|
|
||||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
|
|
||||||
template <class Pointer>
|
|
||||||
void on_foreign_key(const char *id, Pointer &x) {
|
|
||||||
attribute* ref_column;
|
|
||||||
std::type_index ti = typeid(typename Pointer::value_type);
|
|
||||||
if (auto result = determine_foreign_ref(std::type_index(ti))) {
|
|
||||||
ref_column = *result;
|
|
||||||
} else {
|
|
||||||
ref_column = nullptr;
|
|
||||||
insert_missing_reference_column(ti, ref_column);
|
|
||||||
}
|
|
||||||
if (x.empty()) {
|
|
||||||
typename Pointer::value_type temp_val;
|
|
||||||
columns_.push_back(fk_column_generator_.generate(id, temp_val));
|
|
||||||
} else {
|
|
||||||
columns_.push_back(fk_column_generator_.generate(id, *x));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
[[nodiscard]] utils::result<attribute*, utils::error> determine_foreign_ref(const std::type_index &ti) const;
|
|
||||||
void insert_missing_reference_column(const std::type_index &ti, attribute* ref_column) const;
|
|
||||||
|
|
||||||
template<typename ValueType>
|
|
||||||
attribute &emplace_attribute(const char *id, const utils::field_attributes& attr, null_option_type null_option) {
|
|
||||||
auto &ref = columns_.emplace_back(id, utils::data_type_traits<ValueType>::type(attr.size()), attr, null_option);
|
|
||||||
ref.owner_ = obj_;
|
|
||||||
return ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
size_t index_ = 0;
|
|
||||||
std::vector<attribute> &columns_;
|
|
||||||
const repository &repo_;
|
|
||||||
const std::shared_ptr<object> obj_;
|
|
||||||
|
|
||||||
fk_attribute_generator fk_column_generator_;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename ValueType>
|
|
||||||
void attribute_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
|
||||||
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NotNull);
|
|
||||||
prepare_primary_key(ref, utils::identifier(x));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
void attribute_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) {
|
|
||||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::NotNull);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
void attribute_generator::on_attribute(const char *id, std::optional<Type> & /*x*/, const utils::field_attributes &attr) {
|
|
||||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::Nullable);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
#endif //QUERY_COLUMN_DEFINITION_GENERATOR_HPP
|
|
||||||
|
|
@ -1,78 +0,0 @@
|
||||||
#ifndef MATADOR_CONSTRAINTS_GENERATOR_HPP
|
|
||||||
#define MATADOR_CONSTRAINTS_GENERATOR_HPP
|
|
||||||
|
|
||||||
#include "matador/object/restriction.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/access.hpp"
|
|
||||||
#include "matador/utils/field_attributes.hpp"
|
|
||||||
#include "matador/utils/primary_key_attribute.hpp"
|
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <typeindex>
|
|
||||||
|
|
||||||
namespace matador::object {
|
|
||||||
class object;
|
|
||||||
class basic_object_info;
|
|
||||||
class repository;
|
|
||||||
|
|
||||||
class constraints_generator final {
|
|
||||||
private:
|
|
||||||
constraints_generator(std::list<class restriction> &constraints, const repository &repo, const std::shared_ptr<object> &obj);
|
|
||||||
|
|
||||||
public:
|
|
||||||
constraints_generator() = delete;
|
|
||||||
|
|
||||||
template < typename Type >
|
|
||||||
static std::list<class restriction> generate(const repository &repo, const std::shared_ptr<object> &obj) {
|
|
||||||
Type t;
|
|
||||||
return generate(t, repo, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
template < typename Type >
|
|
||||||
static std::list<class restriction> generate(const Type& t, const repository &repo, const std::shared_ptr<object> &obj) {
|
|
||||||
std::list<class restriction> constraints;
|
|
||||||
constraints_generator gen(constraints, repo, obj);
|
|
||||||
access::process(gen, t);
|
|
||||||
return constraints;
|
|
||||||
}
|
|
||||||
|
|
||||||
template < class Type >
|
|
||||||
void on_primary_key(const char *id, Type &/*x*/, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
|
||||||
create_pk_constraint(id);
|
|
||||||
}
|
|
||||||
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
|
|
||||||
template<typename Type>
|
|
||||||
void on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr = utils::null_attributes) {
|
|
||||||
if (utils::is_constraint_set(attr.options() ,utils::constraints::Unique)) {
|
|
||||||
create_unique_constraint(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Pointer>
|
|
||||||
void on_belongs_to(const char *id, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {
|
|
||||||
create_fk_constraint( typeid(typename Pointer::value_type), id);
|
|
||||||
}
|
|
||||||
template<class Pointer>
|
|
||||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
template<class ContainerType>
|
|
||||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void create_pk_constraint(const std::string& name) const;
|
|
||||||
void create_fk_constraint(const std::type_index& ti, const std::string& name) const;
|
|
||||||
void create_unique_constraint(const std::string& name) const;
|
|
||||||
|
|
||||||
[[nodiscard]] std::list<attribute>::iterator find_attribute_by_name(const std::string &name) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::list<class restriction> &constraints_;
|
|
||||||
const repository &repo_;
|
|
||||||
std::shared_ptr<object> obj_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif //MATADOR_CONSTRAINTS_GENERATOR_HPP
|
|
||||||
|
|
@ -29,8 +29,7 @@ private:
|
||||||
public:
|
public:
|
||||||
template<class Type>
|
template<class Type>
|
||||||
static void complete(const std::shared_ptr<repository_node> &node) {
|
static void complete(const std::shared_ptr<repository_node> &node) {
|
||||||
internal::shadow_repository shadow(node->repo_);
|
foreign_node_completer<Type> completer(internal::shadow_repository(node->repo_));
|
||||||
foreign_node_completer<Type> completer(shadow);
|
|
||||||
|
|
||||||
completer.complete_node(node);
|
completer.complete_node(node);
|
||||||
}
|
}
|
||||||
|
|
@ -83,7 +82,7 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
void attach_node(const std::string &name = "") {
|
void attach_node(const std::string &name) {
|
||||||
if (repo_.contains(typeid(Type))) {
|
if (repo_.contains(typeid(Type))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -93,6 +92,18 @@ private:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
void attach_node() {
|
||||||
|
if (repo_.contains(typeid(Type))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto node = repository_node::make_node<Type>(repo_.repo(), "");
|
||||||
|
if (auto result = repo_.attach_node(node)) {
|
||||||
|
complete<Type>(result.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
void attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
|
void attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
|
||||||
using relation_value_type = many_to_many_relation<Type, NodeType>;
|
using relation_value_type = many_to_many_relation<Type, NodeType>;
|
||||||
|
|
@ -183,30 +194,31 @@ void foreign_node_completer<NodeType>::on_has_many(const char * /*id*/,
|
||||||
|
|
||||||
template<typename NodeType>
|
template<typename NodeType>
|
||||||
template<class CollectionType>
|
template<class CollectionType>
|
||||||
void foreign_node_completer<NodeType>::on_has_many_to_many(const char *id, CollectionType & /*collection*/,
|
void foreign_node_completer<NodeType>::on_has_many_to_many(const char * /*id*/,
|
||||||
const char *join_column,
|
CollectionType & /*collection*/,
|
||||||
const char *inverse_join_column,
|
const char * /*join_column*/,
|
||||||
|
const char * /*inverse_join_column*/,
|
||||||
const utils::foreign_attributes & /*attr*/) {
|
const utils::foreign_attributes & /*attr*/) {
|
||||||
if (!repo_.expecting_relation_node(id)) {
|
// if (!repo_.expecting_relation_node(id)) {
|
||||||
repo_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
|
// repo_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
|
||||||
} else {
|
// } else {
|
||||||
attach_relation_node<typename CollectionType::value_type::value_type>(id, join_column, inverse_join_column);
|
// attach_relation_node<typename CollectionType::value_type::value_type>(id, join_column, inverse_join_column);
|
||||||
repo_.remove_expected_relation_node(id);
|
// repo_.remove_expected_relation_node(id);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename NodeType>
|
template<typename NodeType>
|
||||||
template<class CollectionType>
|
template<class CollectionType>
|
||||||
void foreign_node_completer<NodeType>::on_has_many_to_many(const char *id,
|
void foreign_node_completer<NodeType>::on_has_many_to_many(const char * /*id*/,
|
||||||
CollectionType & /*collection*/,
|
CollectionType & /*collection*/,
|
||||||
const utils::foreign_attributes & /*attr*/) {
|
const utils::foreign_attributes & /*attr*/) {
|
||||||
if (!repo_.expecting_relation_node(id)) {
|
// if (!repo_.expecting_relation_node(id)) {
|
||||||
repo_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
|
// repo_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
|
||||||
} else {
|
// } else {
|
||||||
const auto join_columns = join_columns_collector_.collect<typename CollectionType::value_type::value_type>();
|
// const auto join_columns = join_columns_collector_.collect<typename CollectionType::value_type::value_type>();
|
||||||
attach_relation_node<typename CollectionType::value_type::value_type>(id, join_columns.join_column, join_columns.inverse_join_column);
|
// attach_relation_node<typename CollectionType::value_type::value_type>(id, join_columns.join_column, join_columns.inverse_join_column);
|
||||||
repo_.remove_expected_relation_node(id);
|
// repo_.remove_expected_relation_node(id);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename NodeType>
|
template<typename NodeType>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef SHADOW_SCHEMA_HPP
|
#ifndef SHADOW_SCHEMA_HPP
|
||||||
#define SHADOW_SCHEMA_HPP
|
#define SHADOW_SCHEMA_HPP
|
||||||
|
|
||||||
|
#include "matador/object/basic_object_info.hpp"
|
||||||
|
|
||||||
#include "matador/utils/result.hpp"
|
#include "matador/utils/result.hpp"
|
||||||
#include "matador/utils/error.hpp"
|
#include "matador/utils/error.hpp"
|
||||||
|
|
||||||
|
|
@ -10,6 +12,7 @@
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
class repository;
|
class repository;
|
||||||
class repository_node;
|
class repository_node;
|
||||||
|
class object;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace matador::object::internal {
|
namespace matador::object::internal {
|
||||||
|
|
@ -22,6 +25,7 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] repository& repo() const;
|
[[nodiscard]] repository& repo() const;
|
||||||
[[nodiscard]] bool contains(const std::type_index& ti) const;
|
[[nodiscard]] bool contains(const std::type_index& ti) const;
|
||||||
|
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::type_index& ti) const;
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const std::shared_ptr<repository_node> &node) const;
|
[[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const std::shared_ptr<repository_node> &node) const;
|
||||||
|
|
@ -31,6 +35,11 @@ public:
|
||||||
void expect_relation_node(const std::string &name, const std::type_index &ti) const;
|
void expect_relation_node(const std::string &name, const std::type_index &ti) const;
|
||||||
void remove_expected_relation_node(const std::string &name) const;
|
void remove_expected_relation_node(const std::string &name) const;
|
||||||
|
|
||||||
|
[[nodiscard]] std::shared_ptr<object> provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj) const;
|
||||||
|
[[nodiscard]] bool has_object_for_type(const std::type_index &ti) const;
|
||||||
|
[[nodiscard]] std::shared_ptr<object> object_for_type(const std::type_index &ti) const;
|
||||||
|
void remove_object_for_type(const std::type_index &ti) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
repository& repo_;
|
repository& repo_;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ public:
|
||||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||||
[[nodiscard]] bool has_primary_key() const;
|
[[nodiscard]] bool has_primary_key() const;
|
||||||
|
|
||||||
|
[[nodiscard]] bool empty() const;
|
||||||
[[nodiscard]] const std::string& name() const;
|
[[nodiscard]] const std::string& name() const;
|
||||||
|
|
||||||
void update_name(const std::string& name);
|
void update_name(const std::string& name);
|
||||||
|
|
@ -33,12 +34,11 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] bool has_constraints() const;
|
[[nodiscard]] bool has_constraints() const;
|
||||||
[[nodiscard]] size_t constraint_count() const;
|
[[nodiscard]] size_t constraint_count() const;
|
||||||
[[nodiscard]] const std::list<class restriction>& constraints() const;
|
[[nodiscard]] const std::list<restriction>& constraints() const;
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const object& obj);
|
friend std::ostream& operator<<(std::ostream& os, const object& obj);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class constraints_generator;
|
|
||||||
friend class attribute_generator;
|
friend class attribute_generator;
|
||||||
friend class object_generator;
|
friend class object_generator;
|
||||||
|
|
||||||
|
|
@ -46,7 +46,7 @@ private:
|
||||||
attribute* pk_attribute_{nullptr};
|
attribute* pk_attribute_{nullptr};
|
||||||
utils::identifier pk_identifier_;
|
utils::identifier pk_identifier_;
|
||||||
std::list<attribute> attributes_;
|
std::list<attribute> attributes_;
|
||||||
std::list<class restriction> constraints_;
|
std::list<restriction> constraints_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif //MATADOR_OBJECT_HPP
|
#endif //MATADOR_OBJECT_HPP
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef MATADOR_OBJECT_GENERATOR_HPP
|
#ifndef MATADOR_OBJECT_GENERATOR_HPP
|
||||||
#define MATADOR_OBJECT_GENERATOR_HPP
|
#define MATADOR_OBJECT_GENERATOR_HPP
|
||||||
|
|
||||||
|
#include "matador/object/internal/shadow_repository.hpp"
|
||||||
#include "matador/object/object.hpp"
|
#include "matador/object/object.hpp"
|
||||||
|
|
||||||
#include "matador/utils/access.hpp"
|
#include "matador/utils/access.hpp"
|
||||||
|
|
@ -60,7 +61,16 @@ public:
|
||||||
|
|
||||||
template < class Type >
|
template < class Type >
|
||||||
static std::shared_ptr<object> generate(std::unique_ptr<Type>&& t, repository &repo, const std::string &name) {
|
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);
|
const internal::shadow_repository shadow_repo(repo);
|
||||||
|
const std::type_index ti(typeid(Type));
|
||||||
|
if (shadow_repo.has_object_for_type(ti)) {
|
||||||
|
auto obj = shadow_repo.object_for_type(ti);
|
||||||
|
shadow_repo.remove_object_for_type(ti);
|
||||||
|
obj->update_name(name);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto obj = shadow_repo.provide_object_in_advance(ti, std::make_shared<object>(name));
|
||||||
object_generator gen(repo, obj);
|
object_generator gen(repo, obj);
|
||||||
access::process(gen, *t);
|
access::process(gen, *t);
|
||||||
return obj;
|
return obj;
|
||||||
|
|
@ -79,7 +89,7 @@ public:
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
|
void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) {
|
||||||
on_foreign_key(id, x);
|
on_foreign_key(id, x);
|
||||||
create_fk_constraint( typeid(typename Pointer::value_type), id);
|
create_fk_constraint<typename Pointer::value_type>(id);
|
||||||
}
|
}
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
|
@ -106,14 +116,16 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void create_pk_constraint(const std::string& name) const;
|
void create_pk_constraint(const std::string& name) const;
|
||||||
void create_fk_constraint(const std::type_index& ti, const std::string& name) const;
|
template<typename Type>
|
||||||
|
void create_fk_constraint(const std::string& name) const;
|
||||||
void create_unique_constraint(const std::string& name) const;
|
void create_unique_constraint(const std::string& name) const;
|
||||||
|
|
||||||
[[nodiscard]] std::list<attribute>::iterator find_attribute_by_name(const std::string &name) const;
|
[[nodiscard]] std::list<attribute>::iterator find_attribute_by_name(const std::string &name) const;
|
||||||
|
|
||||||
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
|
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
|
||||||
|
|
||||||
[[nodiscard]] std::shared_ptr<class object> fk_object(const std::type_index& ti) const;
|
template<typename Type>
|
||||||
|
[[nodiscard]] std::shared_ptr<object> fk_object() const;
|
||||||
|
|
||||||
static std::shared_ptr<object> acquire_object(repository &repo, const std::type_index &ti, const std::string& name);
|
static std::shared_ptr<object> acquire_object(repository &repo, const std::type_index &ti, const std::string& name);
|
||||||
private:
|
private:
|
||||||
|
|
@ -138,5 +150,37 @@ void object_generator::on_attribute(const char *id, std::optional<Type> & /*x*/,
|
||||||
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::Nullable);
|
std::ignore = emplace_attribute<Type>(id, attr, null_option_type::Nullable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
void object_generator::create_fk_constraint(const std::string& name) const {
|
||||||
|
const auto pk_attr = find_attribute_by_name(name);
|
||||||
|
if (pk_attr == std::end(object_->attributes_)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const auto obj = fk_object<Type>();
|
||||||
|
restriction pk_constraint(*pk_attr);
|
||||||
|
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
||||||
|
pk_constraint.owner_ = object_;
|
||||||
|
pk_constraint.reference_ = obj;
|
||||||
|
object_->constraints_.emplace_back(std::move(pk_constraint));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
std::shared_ptr<object> object_generator::fk_object() const {
|
||||||
|
const auto ti = std::type_index(typeid(Type));
|
||||||
|
const internal::shadow_repository shadow_repo(repo_);
|
||||||
|
if (const auto result = shadow_repo.basic_info(ti)) {
|
||||||
|
return result->get().object();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shadow_repo.has_object_for_type(ti)) {
|
||||||
|
return shadow_repo.object_for_type(ti);
|
||||||
|
}
|
||||||
|
const auto obj = shadow_repo.provide_object_in_advance(ti, std::make_shared<object>(""));
|
||||||
|
object_generator gen(repo_, obj);
|
||||||
|
Type t;
|
||||||
|
access::process(gen, t);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //MATADOR_OBJECT_GENERATOR_HPP
|
#endif //MATADOR_OBJECT_GENERATOR_HPP
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define RELATION_COMPLETER_HPP
|
#define RELATION_COMPLETER_HPP
|
||||||
|
|
||||||
#include "matador/object/internal/shadow_repository.hpp"
|
#include "matador/object/internal/shadow_repository.hpp"
|
||||||
|
#include "matador/object/foreign_node_completer.hpp"
|
||||||
#include "matador/object/many_to_many_relation.hpp"
|
#include "matador/object/many_to_many_relation.hpp"
|
||||||
#include "matador/object/join_columns_collector.hpp"
|
#include "matador/object/join_columns_collector.hpp"
|
||||||
#include "matador/object/object_ptr.hpp"
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
@ -143,6 +144,9 @@ private:
|
||||||
nodes_.pop();
|
nodes_.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename NodeType>
|
||||||
|
void attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column);
|
||||||
|
|
||||||
static void register_relation_endpoints(const endpoint_ptr &endpoint,
|
static void register_relation_endpoints(const endpoint_ptr &endpoint,
|
||||||
const endpoint_ptr &other_endpoint);
|
const endpoint_ptr &other_endpoint);
|
||||||
static void link_relation_endpoints(const endpoint_ptr &endpoint,
|
static void link_relation_endpoints(const endpoint_ptr &endpoint,
|
||||||
|
|
@ -244,47 +248,12 @@ void relation_completer<Type>::on_has_many_to_many(const char *id,
|
||||||
const char *join_column,
|
const char *join_column,
|
||||||
const char *inverse_join_column,
|
const char *inverse_join_column,
|
||||||
const utils::foreign_attributes &/*attr*/) {
|
const utils::foreign_attributes &/*attr*/) {
|
||||||
using relation_value_type = many_to_many_relation<typename CollectionType::value_type::value_type, Type>;
|
if (!schema_.expecting_relation_node(id)) {
|
||||||
using value_type = typename CollectionType::value_type::value_type;
|
schema_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
|
||||||
|
} else {
|
||||||
// Check if the object_ptr type is already inserted in the schema (by id)
|
attach_relation_node<typename CollectionType::value_type::value_type>(id, join_column, inverse_join_column);
|
||||||
auto result = schema_.find_node(typeid(value_type));
|
schema_.remove_expected_relation_node(id);
|
||||||
if (!result) {
|
|
||||||
// Todo: throw internal error or attach node
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto foreign_node = result.value();
|
|
||||||
result = schema_.find_node(id);
|
|
||||||
if (result) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Relation not found.
|
|
||||||
auto creator = [join_column, inverse_join_column] {
|
|
||||||
return std::make_unique<relation_value_type>(join_column, inverse_join_column);
|
|
||||||
};
|
|
||||||
|
|
||||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, std::move(creator));
|
|
||||||
result = schema_.attach_node(node);
|
|
||||||
if (!result) {
|
|
||||||
// Todo: throw internal error
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
|
|
||||||
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
|
|
||||||
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BelongsTo, foreign_node);
|
|
||||||
const auto foreign_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
|
|
||||||
// register relation endpoint in local node
|
|
||||||
nodes_.top()->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), local_endpoint);
|
|
||||||
// register relation endpoint in foreign node
|
|
||||||
foreign_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
|
||||||
// register endpoints in relation node
|
|
||||||
node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
|
|
||||||
node->info_->register_relation_endpoint(typeid(typename CollectionType::value_type::value_type), inverse_join_endpoint);
|
|
||||||
// link endpoints
|
|
||||||
link_relation_endpoints(local_endpoint, join_endpoint);
|
|
||||||
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
|
|
@ -372,6 +341,54 @@ void relation_completer<Type>::on_belongs_to(const char *id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
template<typename NodeType>
|
||||||
|
void relation_completer<Type>::attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
|
||||||
|
using relation_value_type = many_to_many_relation<NodeType, Type>;
|
||||||
|
using value_type = NodeType;
|
||||||
|
|
||||||
|
// Check if the object_ptr type is already inserted in the schema (by id)
|
||||||
|
auto result = schema_.find_node(typeid(value_type));
|
||||||
|
if (!result) {
|
||||||
|
// Todo: throw internal error or attach node
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto foreign_node = result.value();
|
||||||
|
result = schema_.find_node(name);
|
||||||
|
if (result) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Relation does not exist. Create it.
|
||||||
|
auto creator = [join_column, inverse_join_column] {
|
||||||
|
return std::make_unique<relation_value_type>(join_column, inverse_join_column);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), name, std::move(creator));
|
||||||
|
result = schema_.attach_node(node);
|
||||||
|
if (!result) {
|
||||||
|
// Todo: throw internal error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreign_node_completer<Type>::template complete<relation_value_type>(result.value());
|
||||||
|
|
||||||
|
const auto local_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
|
||||||
|
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
|
||||||
|
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BelongsTo, foreign_node);
|
||||||
|
const auto foreign_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
|
||||||
|
// register relation endpoint in local node
|
||||||
|
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||||
|
// register relation endpoint in foreign node
|
||||||
|
foreign_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
||||||
|
// register endpoints in relation node
|
||||||
|
node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
|
||||||
|
node->info_->register_relation_endpoint(typeid(value_type), inverse_join_endpoint);
|
||||||
|
// link endpoints
|
||||||
|
link_relation_endpoints(local_endpoint, join_endpoint);
|
||||||
|
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
void relation_completer<Type>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
||||||
const endpoint_ptr &other_endpoint) {
|
const endpoint_ptr &other_endpoint) {
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ public:
|
||||||
return utils::failure(result.err());
|
return utils::failure(result.err());
|
||||||
}
|
}
|
||||||
foreign_node_completer<Type>::template complete<Type>(node);
|
foreign_node_completer<Type>::template complete<Type>(node);
|
||||||
// relation_completer<Type>::complete(node);
|
relation_completer<Type>::complete(node);
|
||||||
} else if (!has_node(name)) {
|
} else if (!has_node(name)) {
|
||||||
it->second->update_name(name);
|
it->second->update_name(name);
|
||||||
nodes_by_name_[name] = it->second;
|
nodes_by_name_[name] = it->second;
|
||||||
|
|
@ -189,7 +189,6 @@ private:
|
||||||
private:
|
private:
|
||||||
friend class internal::shadow_repository;
|
friend class internal::shadow_repository;
|
||||||
friend class repository_node;
|
friend class repository_node;
|
||||||
friend class attribute_generator;
|
|
||||||
template < typename NodeType >
|
template < typename NodeType >
|
||||||
friend class foreign_node_completer;
|
friend class foreign_node_completer;
|
||||||
friend class object_generator;
|
friend class object_generator;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
#ifndef SCHEMA_NODE_HPP
|
#ifndef SCHEMA_NODE_HPP
|
||||||
#define SCHEMA_NODE_HPP
|
#define SCHEMA_NODE_HPP
|
||||||
|
|
||||||
#include "matador/object/attribute_generator.hpp"
|
|
||||||
#include "matador/object/object_generator.hpp"
|
#include "matador/object/object_generator.hpp"
|
||||||
#include "matador/object/object_info.hpp"
|
#include "matador/object/object_info.hpp"
|
||||||
|
|
||||||
|
#include "matador/utils/error.hpp"
|
||||||
|
#include "matador/utils/result.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,9 @@ class object;
|
||||||
|
|
||||||
class restriction {
|
class restriction {
|
||||||
public:
|
public:
|
||||||
restriction() = default;
|
explicit restriction(const class attribute& attr);
|
||||||
explicit restriction(std::string name);
|
|
||||||
|
|
||||||
[[nodiscard]] std::string name() const;
|
[[nodiscard]] const class attribute& attribute() const;
|
||||||
[[nodiscard]] const class attribute* attribute() const;
|
|
||||||
[[nodiscard]] std::string column_name() const;
|
[[nodiscard]] std::string column_name() const;
|
||||||
[[nodiscard]] std::shared_ptr<object> owner() const;
|
[[nodiscard]] std::shared_ptr<object> owner() const;
|
||||||
[[nodiscard]] bool is_primary_key_constraint() const;
|
[[nodiscard]] bool is_primary_key_constraint() const;
|
||||||
|
|
@ -32,16 +30,14 @@ public:
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const restriction& c);
|
friend std::ostream& operator<<(std::ostream& os, const restriction& c);
|
||||||
|
|
||||||
std::string prefix() const;
|
std::string type_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class constraint_builder;
|
friend class constraint_builder;
|
||||||
friend class constraints_generator;
|
|
||||||
friend class object_generator;
|
friend class object_generator;
|
||||||
friend class object;
|
friend class object;
|
||||||
|
|
||||||
std::string name_;
|
const class attribute& attr_;
|
||||||
std::variant<class attribute*, std::string> attr_;
|
|
||||||
std::shared_ptr<object> owner_;
|
std::shared_ptr<object> owner_;
|
||||||
std::shared_ptr<object> reference_;
|
std::shared_ptr<object> reference_;
|
||||||
utils::constraints options_{utils::constraints::None};
|
utils::constraints options_{utils::constraints::None};
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,7 @@ add_library(matador-core STATIC
|
||||||
../../include/matador/net/select_fd_sets.hpp
|
../../include/matador/net/select_fd_sets.hpp
|
||||||
../../include/matador/net/socket_interrupter.hpp
|
../../include/matador/net/socket_interrupter.hpp
|
||||||
../../include/matador/object/attribute.hpp
|
../../include/matador/object/attribute.hpp
|
||||||
../../include/matador/object/attribute_generator.hpp
|
|
||||||
../../include/matador/object/basic_object_info.hpp
|
../../include/matador/object/basic_object_info.hpp
|
||||||
../../include/matador/object/constraints_generator.hpp
|
|
||||||
../../include/matador/object/error_code.hpp
|
../../include/matador/object/error_code.hpp
|
||||||
../../include/matador/object/foreign_node_completer.hpp
|
../../include/matador/object/foreign_node_completer.hpp
|
||||||
../../include/matador/object/internal/shadow_repository.hpp
|
../../include/matador/object/internal/shadow_repository.hpp
|
||||||
|
|
@ -79,9 +77,7 @@ add_library(matador-core STATIC
|
||||||
logger/logger.cpp
|
logger/logger.cpp
|
||||||
logger/rotating_file_sink.cpp
|
logger/rotating_file_sink.cpp
|
||||||
object/attribute.cpp
|
object/attribute.cpp
|
||||||
object/attribute_generator.cpp
|
|
||||||
object/basic_object_info.cpp
|
object/basic_object_info.cpp
|
||||||
object/constraints_generator.cpp
|
|
||||||
object/error_code.cpp
|
object/error_code.cpp
|
||||||
object/foreign_node_completer.cpp
|
object/foreign_node_completer.cpp
|
||||||
object/internal/shadow_repository.cpp
|
object/internal/shadow_repository.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
#include "matador/object/attribute_generator.hpp"
|
|
||||||
#include "matador/object/repository.hpp"
|
|
||||||
#include "matador/object/object.hpp"
|
|
||||||
|
|
||||||
namespace matador::object {
|
|
||||||
|
|
||||||
attribute_generator::attribute_generator(std::vector<attribute> &columns, const repository &repo, const std::shared_ptr<object> &obj)
|
|
||||||
: columns_(columns)
|
|
||||||
, repo_(repo)
|
|
||||||
, obj_(obj)
|
|
||||||
{}
|
|
||||||
|
|
||||||
void attribute_generator::on_revision(const char *id, uint64_t &rev) {
|
|
||||||
on_attribute(id, rev);
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<attribute*, utils::error> attribute_generator::determine_foreign_ref(const std::type_index &ti) const {
|
|
||||||
return repo_.primary_key_attribute(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
void attribute_generator::insert_missing_reference_column(const std::type_index& ti, attribute* ref_column) const {
|
|
||||||
const_cast<repository&>(repo_).missing_references_.insert({ti, ref_column});
|
|
||||||
}
|
|
||||||
|
|
||||||
void attribute_generator::prepare_primary_key(attribute& ref, utils::identifier &&pk) const {
|
|
||||||
obj_->pk_attribute_ = &ref;
|
|
||||||
obj_->pk_identifier_ = std::move(pk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
#include "matador/object/constraints_generator.hpp"
|
|
||||||
#include "matador/object/object.hpp"
|
|
||||||
#include "matador/object/repository.hpp"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
namespace matador::object {
|
|
||||||
constraints_generator::constraints_generator(std::list<class restriction> &constraints, const repository& repo, const std::shared_ptr<object> &obj)
|
|
||||||
: constraints_(constraints)
|
|
||||||
, repo_(repo)
|
|
||||||
, obj_(obj) {}
|
|
||||||
|
|
||||||
void constraints_generator::create_pk_constraint(const std::string& name) const {
|
|
||||||
restriction pk_constraint("PK_" + obj_->name());
|
|
||||||
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
|
||||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_->attributes_)) {
|
|
||||||
pk_constraint.attr_ = &*pk_attr;
|
|
||||||
}
|
|
||||||
pk_constraint.owner_ = obj_;
|
|
||||||
constraints_.emplace_back(std::move(pk_constraint));
|
|
||||||
}
|
|
||||||
|
|
||||||
void constraints_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const {
|
|
||||||
const auto result = repo_.basic_info(ti);
|
|
||||||
if (!result) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
restriction pk_constraint("FK_" + obj_->name() + "_" + name);
|
|
||||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
|
||||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_->attributes_)) {
|
|
||||||
pk_constraint.attr_ = &*pk_attr;
|
|
||||||
}
|
|
||||||
pk_constraint.owner_ = obj_;
|
|
||||||
constraints_.emplace_back(std::move(pk_constraint));
|
|
||||||
}
|
|
||||||
|
|
||||||
void constraints_generator::create_unique_constraint(const std::string& name) const {
|
|
||||||
class restriction pk_constraint("UK_" + obj_->name() + "_" + name);
|
|
||||||
pk_constraint.options_ |= utils::constraints::Unique;
|
|
||||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_->attributes_)) {
|
|
||||||
pk_constraint.attr_ = &*pk_attr;
|
|
||||||
}
|
|
||||||
pk_constraint.owner_ = obj_;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::list<attribute>::iterator constraints_generator::find_attribute_by_name(const std::string& name) const {
|
|
||||||
return std::find_if(std::begin(obj_->attributes_), std::end(obj_->attributes_), [&name](const attribute& elem) {
|
|
||||||
return elem.name() == name;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -15,6 +15,10 @@ bool shadow_repository::contains( const std::type_index& ti ) const {
|
||||||
return repo_.contains(ti);
|
return repo_.contains(ti);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils::result<basic_object_info_ref, utils::error> shadow_repository::basic_info(const std::type_index& ti) const {
|
||||||
|
return repo_.basic_info(ti);
|
||||||
|
}
|
||||||
|
|
||||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::type_index& type_index ) const {
|
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::type_index& type_index ) const {
|
||||||
return repo_.find_node(type_index);
|
return repo_.find_node(type_index);
|
||||||
}
|
}
|
||||||
|
|
@ -42,4 +46,20 @@ void shadow_repository::expect_relation_node(const std::string &name, const std:
|
||||||
void shadow_repository::remove_expected_relation_node(const std::string &name) const {
|
void shadow_repository::remove_expected_relation_node(const std::string &name) const {
|
||||||
repo_.remove_expected_relation_node(name);
|
repo_.remove_expected_relation_node(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<object> shadow_repository::provide_object_in_advance(const std::type_index& ti, const std::shared_ptr<object>& obj) const {
|
||||||
|
return repo_.provide_object_in_advance(ti, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool shadow_repository::has_object_for_type(const std::type_index& ti) const {
|
||||||
|
return repo_.has_object_for_type(ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<object> shadow_repository::object_for_type(const std::type_index& ti) const {
|
||||||
|
return repo_.object_for_type(ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
void shadow_repository::remove_object_for_type(const std::type_index& ti) const {
|
||||||
|
repo_.remove_object_for_type(ti);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
object::object(std::string name)
|
object::object(std::string name)
|
||||||
: name_(std::move(name)) {
|
: name_(std::move(name)) {}
|
||||||
int i = 9;
|
|
||||||
}
|
|
||||||
|
|
||||||
const attribute& object::create_attribute(std::string name, const std::shared_ptr<object>& obj) {
|
const attribute& object::create_attribute(std::string name, const std::shared_ptr<object>& obj) {
|
||||||
attribute attr{std::move(name)};
|
attribute attr{std::move(name)};
|
||||||
|
|
@ -30,13 +28,6 @@ const std::string& object::name() const {
|
||||||
|
|
||||||
void object::update_name(const std::string& name) {
|
void object::update_name(const std::string& name) {
|
||||||
name_ = 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();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool object::has_attributes() const {
|
bool object::has_attributes() const {
|
||||||
|
|
@ -59,7 +50,7 @@ size_t object::constraint_count() const {
|
||||||
return constraints_.size();
|
return constraints_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::list<class restriction>& object::constraints() const {
|
const std::list<restriction>& object::constraints() const {
|
||||||
return constraints_;
|
return constraints_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,33 +25,23 @@ void object_generator::on_revision(const char* id, uint64_t& rev) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void object_generator::create_pk_constraint(const std::string& name) const {
|
void object_generator::create_pk_constraint(const std::string& name) const {
|
||||||
restriction pk_constraint("PK_" + object_->name());
|
const auto pk_attr = find_attribute_by_name(name);
|
||||||
|
if (pk_attr == std::end(object_->attributes_)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
restriction pk_constraint(*pk_attr);
|
||||||
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
||||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
|
||||||
pk_constraint.attr_ = &*pk_attr;
|
|
||||||
}
|
|
||||||
pk_constraint.owner_ = object_;
|
pk_constraint.owner_ = object_;
|
||||||
object_->constraints_.emplace_back(std::move(pk_constraint));
|
object_->constraints_.emplace_back(std::move(pk_constraint));
|
||||||
}
|
}
|
||||||
|
|
||||||
void object_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const {
|
|
||||||
const auto obj = fk_object(ti);
|
|
||||||
restriction pk_constraint("FK_" + object_->name() + "_" + name);
|
|
||||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
|
||||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
|
||||||
pk_constraint.attr_ = &*pk_attr;
|
|
||||||
}
|
|
||||||
pk_constraint.owner_ = object_;
|
|
||||||
pk_constraint.reference_ = obj;
|
|
||||||
object_->constraints_.emplace_back(std::move(pk_constraint));
|
|
||||||
}
|
|
||||||
|
|
||||||
void object_generator::create_unique_constraint(const std::string& name) const {
|
void object_generator::create_unique_constraint(const std::string& name) const {
|
||||||
restriction pk_constraint("UK_" + object_->name() + "_" + name);
|
const auto pk_attr = find_attribute_by_name(name);
|
||||||
pk_constraint.options_ |= utils::constraints::Unique;
|
if (pk_attr == std::end(object_->attributes_)) {
|
||||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
return;
|
||||||
pk_constraint.attr_ = &*pk_attr;
|
|
||||||
}
|
}
|
||||||
|
restriction pk_constraint(*pk_attr);
|
||||||
|
pk_constraint.options_ |= utils::constraints::Unique;
|
||||||
pk_constraint.owner_ = object_;
|
pk_constraint.owner_ = object_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,16 +55,4 @@ void object_generator::prepare_primary_key(attribute& ref, utils::identifier &&p
|
||||||
object_->pk_attribute_ = &ref;
|
object_->pk_attribute_ = &ref;
|
||||||
object_->pk_identifier_ = std::move(pk);
|
object_->pk_identifier_ = std::move(pk);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<class object> object_generator::fk_object(const std::type_index& ti) const {
|
|
||||||
const auto result = repo_.basic_info(ti);
|
|
||||||
if (result) {
|
|
||||||
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>(""));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,32 +4,16 @@
|
||||||
#include "matador/object/object.hpp"
|
#include "matador/object/object.hpp"
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
restriction::restriction(std::string name)
|
restriction::restriction(const class attribute& attr)
|
||||||
: name_(std::move(name)) {}
|
: attr_(attr) {}
|
||||||
|
|
||||||
std::string restriction::name() const {
|
const class attribute& restriction::attribute() const {
|
||||||
return prefix() + name_;
|
return attr_;
|
||||||
}
|
|
||||||
|
|
||||||
const class attribute* restriction::attribute() const {
|
|
||||||
if (std::holds_alternative<class attribute*>(attr_)) {
|
|
||||||
return std::get<class attribute*>(attr_);
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string restriction::column_name() const {
|
std::string restriction::column_name() const {
|
||||||
if (std::holds_alternative<class attribute*>(attr_)) {
|
return attr_.name();
|
||||||
return std::get<class attribute*>(attr_)->name();
|
|
||||||
}
|
}
|
||||||
if (std::holds_alternative<std::string>(attr_)) {
|
|
||||||
return std::get<std::string>(attr_);
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<object> restriction::owner() const {
|
std::shared_ptr<object> restriction::owner() const {
|
||||||
return owner_;
|
return owner_;
|
||||||
}
|
}
|
||||||
|
|
@ -55,21 +39,24 @@ const std::string& restriction::ref_column_name() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
||||||
os << "constraint " << c.name() << " for column " << c.column_name();
|
os << "constraint " << c.type_string() << " for column " << c.column_name();
|
||||||
|
if (c.is_foreign_key_constraint()) {
|
||||||
|
os << " references " << c.ref_table_name() << "." << c.ref_column_name();
|
||||||
|
}
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string restriction::prefix() const {
|
std::string restriction::type_string() const {
|
||||||
if (utils::is_constraint_set(options_, utils::constraints::PrimaryKey)) {
|
if (utils::is_constraint_set(options_, utils::constraints::PrimaryKey)) {
|
||||||
return "PK_";
|
return "PrimaryKey";
|
||||||
}
|
}
|
||||||
if (utils::is_constraint_set(options_, utils::constraints::ForeignKey)) {
|
if (utils::is_constraint_set(options_, utils::constraints::ForeignKey)) {
|
||||||
return "FK_";
|
return "ForeignKey";
|
||||||
}
|
}
|
||||||
if (utils::is_constraint_set(options_, utils::constraints::Unique)) {
|
if (utils::is_constraint_set(options_, utils::constraints::Unique)) {
|
||||||
return "UK_";
|
return "Unique";
|
||||||
}
|
}
|
||||||
return "";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
// constraint_builder & constraint_builder::constraint(std::string name) {
|
// constraint_builder & constraint_builder::constraint(std::string name) {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
|
||||||
add_executable(CoreTests
|
add_executable(CoreTests
|
||||||
../backends/SchemaFixture.hpp
|
../backends/SchemaFixture.hpp
|
||||||
logger/LoggerTest.cpp
|
logger/LoggerTest.cpp
|
||||||
object/AttributeGeneratorTest.cpp
|
|
||||||
object/ObjectTest.cpp
|
object/ObjectTest.cpp
|
||||||
object/PrimaryKeyResolverTest.cpp
|
object/PrimaryKeyResolverTest.cpp
|
||||||
object/SchemaTest.cpp
|
object/SchemaTest.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,64 +0,0 @@
|
||||||
#include <catch2/catch_test_macros.hpp>
|
|
||||||
|
|
||||||
#include "matador/object/attribute_generator.hpp"
|
|
||||||
#include "matador/object/object.hpp"
|
|
||||||
#include "matador/object/repository.hpp"
|
|
||||||
|
|
||||||
#include "../test/models/product.hpp"
|
|
||||||
#include "../test/models/optional.hpp"
|
|
||||||
|
|
||||||
using namespace matador::object;
|
|
||||||
using namespace matador::utils;
|
|
||||||
|
|
||||||
TEST_CASE("Generate column definitions from object", "[column][definition][generator]") {
|
|
||||||
repository repo("main");
|
|
||||||
|
|
||||||
auto result = repo.attach<matador::test::product>("products")
|
|
||||||
.and_then([&repo] { return repo.attach<matador::test::supplier>("supplier"); })
|
|
||||||
.and_then([&repo] { return repo.attach<matador::test::category>("categories"); });
|
|
||||||
REQUIRE(result);
|
|
||||||
|
|
||||||
auto obj = std::make_shared<object>("products");
|
|
||||||
auto columns = attribute_generator::generate<matador::test::product>(repo, obj);
|
|
||||||
|
|
||||||
const std::vector expected_columns = {
|
|
||||||
attribute{"product_name", basic_type::Varchar, constraints::PrimaryKey, null_option_type::NotNull },
|
|
||||||
attribute{"supplier_id", basic_type::UInt32, constraints::ForeignKey, null_option_type::NotNull },
|
|
||||||
attribute{"category_id", basic_type::UInt32, constraints::ForeignKey, null_option_type::NotNull },
|
|
||||||
attribute{"quantity_per_unit", basic_type::Varchar, null_attributes, null_option_type::NotNull },
|
|
||||||
attribute{"unit_price", basic_type::UInt32, null_attributes, null_option_type::NotNull },
|
|
||||||
attribute{"units_in_stock", basic_type::UInt32, null_attributes, null_option_type::NotNull },
|
|
||||||
attribute{"units_in_order", basic_type::UInt32, null_attributes, null_option_type::NotNull },
|
|
||||||
attribute{"reorder_level", basic_type::UInt32, null_attributes, null_option_type::NotNull },
|
|
||||||
attribute{"discontinued", basic_type::Boolean, null_attributes, null_option_type::NotNull }
|
|
||||||
};
|
|
||||||
REQUIRE(!columns.empty());
|
|
||||||
REQUIRE(columns.size() == expected_columns.size());
|
|
||||||
|
|
||||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
|
||||||
REQUIRE(expected_columns[i].name() == columns[i].name());
|
|
||||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
|
||||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
|
||||||
repository repo("main");
|
|
||||||
|
|
||||||
auto obj = std::make_shared<object>("optionals");
|
|
||||||
auto columns = attribute_generator::generate<matador::test::optional>(repo, obj);
|
|
||||||
|
|
||||||
const std::vector expected_columns = {
|
|
||||||
attribute{"id", basic_type::UInt32, constraints::PrimaryKey, null_option_type::NotNull },
|
|
||||||
attribute{"name", basic_type::Varchar, null_attributes, null_option_type::NotNull },
|
|
||||||
attribute{"age", basic_type::UInt32, null_attributes, null_option_type::NotNull }
|
|
||||||
};
|
|
||||||
REQUIRE(!columns.empty());
|
|
||||||
REQUIRE(columns.size() == expected_columns.size());
|
|
||||||
|
|
||||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
|
||||||
REQUIRE(expected_columns[i].name() == columns[i].name());
|
|
||||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
|
||||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue