diff --git a/backends/postgres/src/postgres_connection.cpp b/backends/postgres/src/postgres_connection.cpp index 5cbdbc1..bac6c75 100644 --- a/backends/postgres/src/postgres_connection.cpp +++ b/backends/postgres/src/postgres_connection.cpp @@ -240,9 +240,9 @@ utils::result, utils::error> postgres_ // Todo: extract size auto type = (string2type(reader.column(2))); end = nullptr; - object::null_option null_opt{object::null_option::NULLABLE}; + object::null_option_type null_opt{object::null_option_type::NULLABLE}; if (strtoul(reader.column(4), &end, 10) == 0) { - null_opt = object::null_option::NOT_NULL; + null_opt = object::null_option_type::NOT_NULL; } // f.default_value(res->column(4)); prototype.emplace_back(name, type, utils::null_attributes, null_opt, index); diff --git a/include/matador/object/attribute_definition.hpp b/include/matador/object/attribute_definition.hpp index 720c45e..992f885 100644 --- a/include/matador/object/attribute_definition.hpp +++ b/include/matador/object/attribute_definition.hpp @@ -11,10 +11,18 @@ namespace matador::object { -enum class null_option : uint8_t { +enum class null_option_type : uint8_t { NULLABLE, NOT_NULL }; +class object_definition; + +struct attribute_options { + utils::field_attributes attributes; + null_option_type null_option{null_option_type::NOT_NULL}; + int index{-1}; +}; + class attribute_definition { public: explicit attribute_definition(const char *name); // NOLINT(*-explicit-constructor) @@ -26,12 +34,6 @@ public: attribute_definition& operator=(attribute_definition&&) noexcept = default; attribute_definition() = default; - template - attribute_definition(std::string name, std::string table_name, const utils::field_attributes& attr) - : attribute_definition(std::move(name), std::move(table_name), utils::data_type_traits::type(attr.size()), attr) - {} - - attribute_definition(std::string name, std::string table_name, utils::basic_type type, const utils::field_attributes& attr); template attribute_definition(std::string name, const utils::field_attributes& attr) @@ -39,30 +41,32 @@ public: {} template - attribute_definition(std::string name, const Type &, const utils::field_attributes& attr, null_option null_opt) + attribute_definition(std::string name, const Type &, const utils::field_attributes& attr, null_option_type null_opt) : attribute_definition(std::move(name), utils::data_type_traits::type(attr.size()), attr, null_opt) {} template - attribute_definition(std::string name, const char (&)[SIZE], const utils::field_attributes& attr, const null_option null_opt) + attribute_definition(std::string name, const char (&)[SIZE], const utils::field_attributes& attr, const null_option_type null_opt) : attribute_definition(std::move(name), utils::data_type_traits::type(attr.size()), attr, null_opt) {} - attribute_definition(std::string name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, int index = 0); - attribute_definition(std::string name, std::string table_name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, int index = 0); + attribute_definition(std::string name, utils::basic_type type, const utils::field_attributes&, null_option_type null_opt, int index = 0); template - attribute_definition(std::string name, const std::shared_ptr &ref_column, const utils::field_attributes& attr, null_option null_opt) + attribute_definition(std::string name, const std::shared_ptr &ref_column, const utils::field_attributes& attr, null_option_type null_opt) : attribute_definition(std::move(name), utils::data_type_traits::type(attr.size()), ref_column, attr, null_opt) {} - attribute_definition(std::string name, utils::basic_type type, int index, const std::shared_ptr &ref_column, const utils::field_attributes& attr, null_option null_opt); + attribute_definition(std::string name, utils::basic_type type, int index, const std::shared_ptr &ref_column, const utils::field_attributes& attr, null_option_type null_opt); + + attribute_definition(std::string name, utils::basic_type type, const std::shared_ptr &ref_column = {}); + attribute_definition(std::string name, utils::basic_type type, const attribute_options& options, const std::shared_ptr& obj = {}, const std::shared_ptr &ref_column = {}); [[nodiscard]] const std::string& name() const; void name(const std::string& n); [[nodiscard]] std::string full_name() const; - [[nodiscard]] std::string table_name() const; - void table_name(const std::string& tn); + [[nodiscard]] std::shared_ptr object() const; + void object(const std::shared_ptr &obj); [[nodiscard]] int index() const; [[nodiscard]] const utils::field_attributes& attributes() const; [[nodiscard]] utils::field_attributes& attributes(); @@ -95,15 +99,14 @@ private: using data_type_index = std::vector; private: + friend class object_definition; + static const data_type_index data_type_index_; std::string name_; - std::string table_; - int index_{-1}; - utils::field_attributes attributes_; - null_option null_option_{null_option::NOT_NULL}; + std::shared_ptr object_{}; + attribute_options options_; utils::basic_type type_{utils::basic_type::type_null}; - size_t size_{0}; std::shared_ptr reference_column_; }; @@ -116,15 +119,15 @@ private: * @param null_opt * @return A column object with a given name */ -attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option null_opt = null_option::NOT_NULL); +attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL); template < typename Type > -attribute_definition make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option null_opt = null_option::NOT_NULL) +attribute_definition make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes, null_option_type null_opt = null_option_type::NOT_NULL) { return make_column(name, utils::data_type_traits::type(0), attr, null_opt); } template <> -attribute_definition make_column(const std::string &name, utils::field_attributes attr, null_option null_opt); +attribute_definition make_column(const std::string &name, utils::field_attributes attr, null_option_type null_opt); template < typename Type > attribute_definition make_pk_column(const std::string &name, size_t size = 0) @@ -143,7 +146,7 @@ attribute_definition make_fk_column(const std::string &name, size_t size, const template < typename Type > [[maybe_unused]] attribute_definition make_fk_column(const std::string &name, const std::shared_ptr &ref_column) { - return {name, utils::data_type_traits::type(0), 0, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL}; + return {name, utils::data_type_traits::type(0), 0, ref_column, { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL}; } template <> @@ -154,7 +157,7 @@ template < typename Type > return { name, utils::data_type_traits::type(size), 0, std::make_shared(ref_column_name, ref_table_name, utils::data_type_traits::type(size), utils::constraints::FOREIGN_KEY), - { 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL + { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL }; } @@ -163,7 +166,7 @@ template < typename Type > return { name, utils::data_type_traits::type(0), 0, std::make_shared(ref_column_name, ref_table_name, utils::data_type_traits::type(0), utils::constraints::FOREIGN_KEY), - { 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL + { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL }; } diff --git a/include/matador/object/attribute_definition_generator.hpp b/include/matador/object/attribute_definition_generator.hpp index 9851679..e563a57 100644 --- a/include/matador/object/attribute_definition_generator.hpp +++ b/include/matador/object/attribute_definition_generator.hpp @@ -26,7 +26,7 @@ public: template attribute_definition generate(const char *id, Type &x, const std::shared_ptr &ref_column) { access::process(*this, x); - return attribute_definition{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL}; + return attribute_definition{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL}; } template @@ -122,7 +122,7 @@ public: private: [[nodiscard]] utils::result, utils::error> determine_foreign_ref(const std::type_index &ti) const; - void insert_missing_reference_column(const std::type_index &ti, std::shared_ptr ref_column) const; + void insert_missing_reference_column(const std::type_index &ti, const std::shared_ptr& ref_column) const; private: size_t index_ = 0; @@ -140,13 +140,13 @@ void attribute_definition_generator::on_primary_key(const char *id, ValueType &x template void attribute_definition_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr) { - columns_.emplace_back(id, x, attr, null_option::NOT_NULL); + columns_.emplace_back(id, x, attr, null_option_type::NOT_NULL); } template void attribute_definition_generator::on_attribute(const char *id, std::optional & /*x*/, const utils::field_attributes &attr) { - columns_.emplace_back(id, utils::data_type_traits::type(attr.size()), attr, null_option::NULLABLE); + columns_.emplace_back(id, utils::data_type_traits::type(attr.size()), attr, null_option_type::NULLABLE); } } diff --git a/include/matador/object/object_definition.hpp b/include/matador/object/object_definition.hpp index 564dfdf..2cfc83d 100644 --- a/include/matador/object/object_definition.hpp +++ b/include/matador/object/object_definition.hpp @@ -18,14 +18,17 @@ public: using const_iterator = column_by_index::const_iterator; object_definition() = default; - object_definition(std::initializer_list columns); - explicit object_definition(const std::vector &columns); + explicit object_definition(std::string name); + object_definition(std::string name, std::initializer_list columns); + explicit object_definition(std::string name, const std::vector &columns); object_definition(const object_definition &x); object_definition& operator=(const object_definition &x); object_definition(object_definition&&) noexcept = default; object_definition& operator=(object_definition&&) noexcept = default; ~object_definition() = default; + [[nodiscard]] const std::string& name() const; + [[nodiscard]] bool has_primary_key() const; [[nodiscard]] std::optional primary_key() const; @@ -33,7 +36,7 @@ public: void append(const std::string &name, long size = -1) { append(make_column(name, size)); } - void append(attribute_definition col); + void append(attribute_definition&& col); [[nodiscard]] const std::vector& columns() const; @@ -60,6 +63,9 @@ private: void add_to_map(attribute_definition &col, size_t index); private: + friend class repository_node; + + std::string name_; column_by_index columns_; column_by_name_map columns_by_name_; diff --git a/include/matador/object/repository_node.hpp b/include/matador/object/repository_node.hpp index 9d2e5b1..fe0a48e 100644 --- a/include/matador/object/repository_node.hpp +++ b/include/matador/object/repository_node.hpp @@ -17,17 +17,22 @@ public: using node_ptr = std::shared_ptr; template < typename Type > - static std::shared_ptr make_node(object::repository& tree, const std::string& name) { - auto node = std::shared_ptr(new repository_node(tree, name, typeid(Type))); + static std::shared_ptr make_node(repository& repo, const std::string& name) { + auto node = std::shared_ptr(new repository_node(repo, name, typeid(Type))); primary_key_resolver resolver; + object_definition obj{name, {}}; auto pk_info = resolver.resolve(); - auto ref_column = determine_reference_column(typeid(Type), name, pk_info, tree); + auto ref_column = determine_reference_column(typeid(Type), name, pk_info, repo); + const auto attributes = attribute_definition_generator::generate(repo); + for (auto&& attr : attributes) { + obj.append(std::move(attr)); + } auto info = std::make_unique>( node, std::move(pk_info.pk), ref_column, - object_definition{attribute_definition_generator::generate(tree)}, + std::move(obj), []{ return std::make_unique(); } ); node->info_ = std::move(info); @@ -87,11 +92,11 @@ private: void unlink(); - static utils::result make_and_attach_node(object::repository& tree, const std::string& name, const std::type_index& ti); + static utils::result make_and_attach_node(repository& repo, const std::string& name, const std::type_index& ti); static std::shared_ptr determine_reference_column(const std::type_index& ti, - const std::string& name, - const primary_key_info& pk_info, - object::repository& tree); + object_definition& obj, + const primary_key_info& pk_info, + repository& repo); private: friend class repository; @@ -100,7 +105,7 @@ private: friend class foreign_node_completer; friend class const_repository_node_iterator; - object::repository &repo_; + repository &repo_; std::type_index type_index_; std::unique_ptr info_; diff --git a/include/matador/sql/field.hpp b/include/matador/sql/field.hpp index 0a7c82f..82a4b3e 100644 --- a/include/matador/sql/field.hpp +++ b/include/matador/sql/field.hpp @@ -3,22 +3,13 @@ #include "matador/utils/value.hpp" -#include "matador/utils/basic_types.hpp" +#include "matador/utils/constraints.hpp" #include #include -namespace matador::utils { -enum class constraints : unsigned char; -} namespace matador::sql { -enum struct field_type { - Attribute, - PrimaryKey, - ForeignKey -}; - /** * */ @@ -26,12 +17,12 @@ class field { public: explicit field(std::string name); template - field(std::string name, Type value, const field_type type = field_type::Attribute, const size_t size = 0, const int index = -1) + field(std::string name, Type value, const utils::constraints type = utils::constraints::NONE, const size_t size = 0, const int index = -1) : name_(std::move(name)) , type_(type) , index_(index) , value_(value, size) {} - field(std::string name, utils::basic_type dt, field_type type = field_type::Attribute, size_t size = 0, int index = -1); + field(std::string name, utils::basic_type dt, utils::constraints type = utils::constraints::NONE, size_t size = 0, int index = -1); field(const field &x) = default; field& operator=(const field &x) = default; field(field &&x) noexcept; @@ -45,7 +36,7 @@ public: } [[nodiscard]] const std::string& name() const; - [[nodiscard]] field_type type() const; + [[nodiscard]] utils::constraints type() const; [[nodiscard]] size_t size() const; [[nodiscard]] int index() const; @@ -71,18 +62,16 @@ public: friend std::ostream& operator<<(std::ostream &out, const field &col); private: - static utils::constraints determine_constraint(field_type type); - template void process(Operator &op) { - op.on_attribute(name_.c_str(), value_, { value_.size(), determine_constraint(type_) } ); + op.on_attribute(name_.c_str(), value_, { value_.size(), type_ } ); } private: friend class record; std::string name_; - field_type type_{field_type::Attribute}; + utils::constraints type_{utils::constraints::NONE}; int index_{-1}; utils::value value_; diff --git a/source/core/object/attribute_definition.cpp b/source/core/object/attribute_definition.cpp index 25a4bd4..e2ac16b 100644 --- a/source/core/object/attribute_definition.cpp +++ b/source/core/object/attribute_definition.cpp @@ -1,66 +1,45 @@ #include "matador/object/attribute_definition.hpp" +#include "matador/object/object_definition.hpp" + #include #include namespace matador::object { attribute_definition::attribute_definition(const char *name) -: name_(name) -, attributes_(utils::null_attributes) { +: attribute_definition(name, utils::basic_type::type_null, {utils::null_attributes}) { } attribute_definition::attribute_definition(std::string name) -: name_(std::move(name)) -, attributes_(utils::null_attributes) { -} - -attribute_definition::attribute_definition(std::string name, - std::string table_name, - const utils::basic_type type, - const utils::field_attributes& attr) -: name_(std::move(name)) -, table_(std::move(table_name)) -, index_(0) -, attributes_(attr) -, type_(type){ +: attribute_definition(std::move(name), utils::basic_type::type_null, {utils::null_attributes}) { } attribute_definition::attribute_definition(std::string name, const utils::basic_type type, const utils::field_attributes &attr, - const null_option null_opt, + const null_option_type null_opt, const int index) -: name_(std::move(name)) -, index_(index) -, attributes_(attr) -, null_option_(null_opt) -, type_(type) { -} - -attribute_definition::attribute_definition(std::string name, - std::string table_name, - const utils::basic_type type, - const utils::field_attributes &attr, const null_option null_opt, - const int index) -: name_(std::move(name)) -, table_(std::move(table_name)) -, index_(index) -, attributes_(attr) -, null_option_(null_opt) -, type_(type) { +: attribute_definition(std::move(name), type, {attr, null_opt, index}) { } attribute_definition::attribute_definition(std::string name, const utils::basic_type type, const int index, const std::shared_ptr &ref_column, - const utils::field_attributes &attr, const null_option null_opt) -: name_(std::move(name)) -, index_(index) -, attributes_(attr) -, null_option_(null_opt) -, type_(type) -, reference_column_(ref_column) { + const utils::field_attributes &attr, + const null_option_type null_opt) +: attribute_definition(std::move(name), type, {attr, null_opt, index}, {}, ref_column) { +} + +attribute_definition::attribute_definition( std::string name, const utils::basic_type type, const std::shared_ptr& ref_column ) +: attribute_definition(std::move(name), type, {}, {}, ref_column) { +} + +attribute_definition::attribute_definition( std::string name, const utils::basic_type type, const attribute_options& options, const std::shared_ptr& obj, const std::shared_ptr& ref_column ) +: name_( std::move( name ) ) +, options_( options ) +, type_( type ) +, reference_column_( ref_column ) { } const std::string &attribute_definition::name() const { @@ -72,31 +51,31 @@ void attribute_definition::name( const std::string& n ) { } std::string attribute_definition::full_name() const { - return table_ + "." + name_; + return object_ ? object_->name() + "." + name_ : name_; } -std::string attribute_definition::table_name() const { - return table_; +std::shared_ptr attribute_definition::object() const { + return object_; } -void attribute_definition::table_name( const std::string& tn ) { - table_= tn; +void attribute_definition::object(const std::shared_ptr& obj) { + object_ = obj; } int attribute_definition::index() const { - return index_; + return options_.index; } const utils::field_attributes &attribute_definition::attributes() const { - return attributes_; + return options_.attributes; } utils::field_attributes& attribute_definition::attributes() { - return attributes_; + return options_.attributes; } bool attribute_definition::is_nullable() const { - return null_option_ == null_option::NULLABLE; + return options_.null_option == null_option_type::NULLABLE; } utils::basic_type attribute_definition::type() const { @@ -104,7 +83,7 @@ utils::basic_type attribute_definition::type() const { } void attribute_definition::change_type(const utils::basic_type type, const utils::field_attributes& attr) { - attributes_ = attr; + options_.attributes = attr; type_ = type; } @@ -153,13 +132,13 @@ bool attribute_definition::is_null() const { } attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr, - null_option null_opt) { + null_option_type null_opt) { return {name, type, attr, null_opt}; } template<> attribute_definition make_column(const std::string &name, utils::field_attributes attr, - null_option null_opt) { + null_option_type null_opt) { return make_column(name, utils::data_type_traits::type(attr.size()), attr, null_opt); } @@ -172,7 +151,7 @@ template<> attribute_definition make_fk_column(const std::string &name, size_t size, const std::shared_ptr &ref_column) { return { name, utils::data_type_traits::type(size), 0, ref_column, - {size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL + {size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL }; } @@ -180,8 +159,8 @@ template<> attribute_definition make_fk_column( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) { return { name, utils::basic_type::type_varchar, 0, - std::make_shared(ref_column_name, ref_table_name, utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY), - { 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL + std::make_shared(ref_column_name, std::make_shared(ref_table_name), utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY), + { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL }; } @@ -190,7 +169,7 @@ attribute_definition make_fk_column(const std::string &name, size_t const auto ref_column = std::make_shared(ref_column_name, ref_table_name, utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY); return { name, utils::data_type_traits::type(size), 0, ref_column, - {size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL + {size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL }; } } diff --git a/source/core/object/attribute_definition_generator.cpp b/source/core/object/attribute_definition_generator.cpp index a0f74e6..c6782e3 100644 --- a/source/core/object/attribute_definition_generator.cpp +++ b/source/core/object/attribute_definition_generator.cpp @@ -3,13 +3,12 @@ namespace matador::object { -attribute_definition_generator::attribute_definition_generator(std::vector &columns, const repository &repo) +attribute_definition_generator::attribute_definition_generator(std::vector &columns, const repository &repo) : columns_(columns) , repo_(repo) {} -void attribute_definition_generator::on_revision(const char *id, uint64_t &rev) -{ +void attribute_definition_generator::on_revision(const char *id, uint64_t &rev) { on_attribute(id, rev); } @@ -17,7 +16,7 @@ utils::result, utils::error> attribute_def return repo_.reference_column(ti); } -void attribute_definition_generator::insert_missing_reference_column(const std::type_index& ti, std::shared_ptr ref_column) const { +void attribute_definition_generator::insert_missing_reference_column(const std::type_index& ti, const std::shared_ptr& ref_column) const { const_cast(repo_).missing_references_.insert({ti, ref_column}); } diff --git a/source/core/object/object_definition.cpp b/source/core/object/object_definition.cpp index 306f2d7..5a560d5 100644 --- a/source/core/object/object_definition.cpp +++ b/source/core/object/object_definition.cpp @@ -1,18 +1,24 @@ #include "matador/object/object_definition.hpp" namespace matador::object { -object_definition::object_definition(const std::initializer_list columns) -: columns_(columns) { +object_definition::object_definition(std::string name) +: name_(std::move(name)) {} + +object_definition::object_definition(std::string name, const std::initializer_list columns) +: name_(std::move(name)) +, columns_(columns) { init(); } -object_definition::object_definition(const std::vector &columns) -: columns_(columns) { +object_definition::object_definition(std::string name, const std::vector &columns) +: name_(std::move(name)) +, columns_(columns) { init(); } object_definition::object_definition(const object_definition &x) -: columns_(x.columns_) +: name_(x.name_) +, columns_(x.columns_) , pk_index_(x.pk_index_) { for (auto& col : columns_) { add_to_map(col, col.index()); @@ -25,6 +31,7 @@ object_definition &object_definition::operator=(const object_definition &x) return *this; } + name_ = x.name_; columns_ = x.columns_; columns_by_name_.clear(); pk_index_ = x.pk_index_; @@ -34,8 +41,11 @@ object_definition &object_definition::operator=(const object_definition &x) return *this; } -bool object_definition::has_primary_key() const -{ +const std::string& object_definition::name() const { + return name_; +} + +bool object_definition::has_primary_key() const { return pk_index_ > -1; } @@ -48,14 +58,12 @@ std::optional object_definition::primary_key() const return columns_[pk_index_]; } -void object_definition::append(attribute_definition col) -{ +void object_definition::append(attribute_definition&& col) { auto &ref = columns_.emplace_back(std::move(col)); add_to_map(ref, columns_.size()-1); } -const std::vector &object_definition::columns() const -{ +const std::vector &object_definition::columns() const { return columns_; } @@ -71,12 +79,12 @@ const attribute_definition &object_definition::at( const size_t index) const object_definition::iterator object_definition::find(const std::string &column_name) { - auto it = columns_by_name_.find(column_name); + const auto it = columns_by_name_.find(column_name); return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end(); } object_definition::const_iterator object_definition::find(const std::string &column_name) const { - auto it = columns_by_name_.find(column_name); + const auto it = columns_by_name_.find(column_name); return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end(); } diff --git a/source/core/object/repository_node.cpp b/source/core/object/repository_node.cpp index ee73e94..7d1ad09 100644 --- a/source/core/object/repository_node.cpp +++ b/source/core/object/repository_node.cpp @@ -45,10 +45,12 @@ const basic_object_info &repository_node::info() const { void repository_node::update_name(const std::string& name) { name_ = name; - info_->reference_column()->table_name(name); + if (info_->reference_column() && info_->reference_column()->object() != nullptr) { + info_->reference_column()->object()->name_ = name; + } } -const object::repository& repository_node::schema() const { +const repository& repository_node::schema() const { return repo_; } @@ -96,27 +98,27 @@ void repository_node::unlink() { previous_sibling_.reset(); } -utils::result repository_node::make_and_attach_node(object::repository& tree, const std::string& name, const std::type_index& ti) { - const auto node = std::shared_ptr(new repository_node(tree, name, ti)); +utils::result repository_node::make_and_attach_node(repository& repo, const std::string& name, const std::type_index& ti) { + const auto node = std::shared_ptr(new repository_node(repo, name, ti)); - return tree.attach_node(node, ""); + return repo.attach_node(node, ""); } -std::shared_ptr repository_node::determine_reference_column(const std::type_index& ti, const std::string& name, const primary_key_info& pk_info, object::repository& tree) { - const auto it = tree.missing_references_.find(ti); - if (it == tree.missing_references_.end()) { - return std::make_shared(pk_info.pk_column_name, name, pk_info.type, utils::constraints::FOREIGN_KEY); +std::shared_ptr repository_node::determine_reference_column(const std::type_index& ti, object_definition& obj, const primary_key_info& pk_info, repository& repo) { + const auto it = repo.missing_references_.find(ti); + if (it == repo.missing_references_.end()) { + return std::make_shared(pk_info.pk_column_name, pk_info.type, attribute_options{utils::constraints::FOREIGN_KEY}); } auto ref_column = it->second; - tree.missing_references_.erase(it); + repo.missing_references_.erase(it); ref_column->name(pk_info.pk_column_name); - ref_column->table_name(name); + ref_column->object(obj); ref_column->change_type(pk_info.type); ref_column->attributes() = utils::constraints::FOREIGN_KEY; - if (name.empty()) { - tree.missing_references_.insert({ti, ref_column}); + if (obj.name().empty()) { + repo.missing_references_.insert({ti, ref_column}); } return ref_column; diff --git a/source/orm/query/intermediates/fetchable_query.cpp b/source/orm/query/intermediates/fetchable_query.cpp index 90b2c6e..39b0294 100644 --- a/source/orm/query/intermediates/fetchable_query.cpp +++ b/source/orm/query/intermediates/fetchable_query.cpp @@ -7,23 +7,13 @@ namespace matador::query { namespace detail { -sql::field_type determine_field_type(const utils::constraints c) { - if (is_constraint_set(c, utils::constraints::FOREIGN_KEY)) { - return sql::field_type::ForeignKey; - } - if (is_constraint_set(c, utils::constraints::PRIMARY_KEY)) { - return sql::field_type::PrimaryKey; - } - return sql::field_type::Attribute; -} - sql::record *create_prototype(const std::vector &prototype) { auto result = std::make_unique(); for (const auto &col: prototype) { result->append({ col.name(), col.type(), - determine_field_type(col.attributes().options()), + col.attributes().options(), col.attributes().size(), col.index() }); diff --git a/source/orm/sql/field.cpp b/source/orm/sql/field.cpp index 59ee145..1568bd4 100644 --- a/source/orm/sql/field.cpp +++ b/source/orm/sql/field.cpp @@ -1,7 +1,5 @@ #include "matador/sql/field.hpp" -#include "matador/utils/constraints.hpp" - #include namespace matador::sql { @@ -11,7 +9,7 @@ field::field(std::string name) , value_(nullptr) {} -field::field(std::string name, const utils::basic_type dt, const field_type type, const size_t size, const int index) +field::field(std::string name, const utils::basic_type dt, const utils::constraints type, const size_t size, const int index) : name_(std::move(name)) , type_(type) , index_(index) @@ -42,7 +40,7 @@ const std::string &field::name() const return name_; } -field_type field::type() const { +utils::constraints field::type() const { return type_; } @@ -62,19 +60,6 @@ std::ostream &operator<<(std::ostream &out, const field &col) return out; } -utils::constraints field::determine_constraint(const field_type type) { - switch (type) { - case field_type::PrimaryKey: - return utils::constraints::PRIMARY_KEY; - case field_type::ForeignKey: - return utils::constraints::FOREIGN_KEY; - case field_type::Attribute: - default: - return utils::constraints::NONE; - } - -} - std::string field::str() const { return as().value_or(""); @@ -116,15 +101,15 @@ bool field::is_null() const } bool field::is_primary_key() const { - return type_ == field_type::PrimaryKey; + return type_ == utils::constraints::PRIMARY_KEY; } bool field::is_foreign_key() const { - return type_ == field_type::ForeignKey; + return type_ == utils::constraints::FOREIGN_KEY; } bool field::is_attribute() const { - return type_ == field_type::Attribute; + return !is_primary_key() && !is_foreign_key(); } diff --git a/source/orm/sql/query_result.cpp b/source/orm/sql/query_result.cpp index 24624a8..d3cdabb 100644 --- a/source/orm/sql/query_result.cpp +++ b/source/orm/sql/query_result.cpp @@ -5,16 +5,6 @@ namespace matador::sql::detail { -field_type determine_field_type(const utils::constraints c) { - if (is_constraint_set(c, utils::constraints::FOREIGN_KEY)) { - return field_type::ForeignKey; - } - if (is_constraint_set(c, utils::constraints::PRIMARY_KEY)) { - return field_type::PrimaryKey; - } - return field_type::Attribute; -} - template<> record *create_prototype(const std::vector &prototype) { auto result = std::make_unique(); @@ -22,7 +12,7 @@ record *create_prototype(const std::vector result->append({ col.name(), col.type(), - determine_field_type(col.attributes().options()), + col.attributes().options(), col.attributes().size(), col.index() }); diff --git a/test/backends/SchemaTest.cpp b/test/backends/SchemaTest.cpp index 369ffed..5f2deae 100644 --- a/test/backends/SchemaTest.cpp +++ b/test/backends/SchemaTest.cpp @@ -14,7 +14,7 @@ using namespace matador::test; TEST_CASE_METHOD(SchemaFixture, "Test schema one-two-many", "[schema]") { using namespace matador::test; - orm::schema repo(pool, "NoopSchema"); + orm::schema repo(pool/*, "NoopSchema"*/); auto result = repo.attach("departments") .and_then( [&repo] { return repo.attach("employees"); } ); diff --git a/test/core/object/AttributeDefinitionGeneratorTest.cpp b/test/core/object/AttributeDefinitionGeneratorTest.cpp index a8a9249..bfbd3f2 100644 --- a/test/core/object/AttributeDefinitionGeneratorTest.cpp +++ b/test/core/object/AttributeDefinitionGeneratorTest.cpp @@ -20,15 +20,15 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener auto columns = attribute_definition_generator::generate(repo); const std::vector expected_columns = { - attribute_definition{"product_name", basic_type::type_varchar, constraints::PRIMARY_KEY, null_option::NOT_NULL }, - attribute_definition{"supplier_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option::NOT_NULL }, - attribute_definition{"category_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option::NOT_NULL }, - attribute_definition{"quantity_per_unit", basic_type::type_varchar, null_attributes, null_option::NOT_NULL }, - attribute_definition{"unit_price", basic_type::type_uint32, null_attributes, null_option::NOT_NULL }, - attribute_definition{"units_in_stock", basic_type::type_uint32, null_attributes, null_option::NOT_NULL }, - attribute_definition{"units_in_order", basic_type::type_uint32, null_attributes, null_option::NOT_NULL }, - attribute_definition{"reorder_level", basic_type::type_uint32, null_attributes, null_option::NOT_NULL }, - attribute_definition{"discontinued", basic_type::type_bool, null_attributes, null_option::NOT_NULL } + attribute_definition{"product_name", basic_type::type_varchar, constraints::PRIMARY_KEY, null_option_type::NOT_NULL }, + attribute_definition{"supplier_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option_type::NOT_NULL }, + attribute_definition{"category_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option_type::NOT_NULL }, + attribute_definition{"quantity_per_unit", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL }, + attribute_definition{"unit_price", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }, + attribute_definition{"units_in_stock", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }, + attribute_definition{"units_in_order", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }, + attribute_definition{"reorder_level", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }, + attribute_definition{"discontinued", basic_type::type_bool, null_attributes, null_option_type::NOT_NULL } }; REQUIRE(!columns.empty()); REQUIRE(columns.size() == expected_columns.size()); @@ -46,9 +46,9 @@ TEST_CASE("Generate columns from object with nullable columns", "[column generat auto columns = attribute_definition_generator::generate(repo); const std::vector expected_columns = { - attribute_definition{"id", basic_type::type_uint32, constraints::PRIMARY_KEY, null_option::NOT_NULL }, - attribute_definition{"name", basic_type::type_varchar, null_attributes, null_option::NOT_NULL }, - attribute_definition{"age", basic_type::type_uint32, null_attributes, null_option::NOT_NULL } + attribute_definition{"id", basic_type::type_uint32, constraints::PRIMARY_KEY, null_option_type::NOT_NULL }, + attribute_definition{"name", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL }, + attribute_definition{"age", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL } }; REQUIRE(!columns.empty()); REQUIRE(columns.size() == expected_columns.size()); diff --git a/test/orm/query/QueryBuilderTest.cpp b/test/orm/query/QueryBuilderTest.cpp index 0ab44f0..d9539d9 100644 --- a/test/orm/query/QueryBuilderTest.cpp +++ b/test/orm/query/QueryBuilderTest.cpp @@ -48,7 +48,7 @@ TEST_CASE_METHOD(QueryFixture, "Test create table sql statement string", "[query auto ctx = query::create() .table("person", { make_pk_column("id"), - make_column("name", {255, constraints::UNIQUE}, null_option::NOT_NULL), + make_column("name", {255, constraints::UNIQUE}, null_option_type::NOT_NULL), make_column("age"), make_fk_column("address", "address", "id") }).compile(*db); diff --git a/test/orm/sql/ColumnTest.cpp b/test/orm/sql/ColumnTest.cpp index 43db791..a308246 100644 --- a/test/orm/sql/ColumnTest.cpp +++ b/test/orm/sql/ColumnTest.cpp @@ -27,7 +27,7 @@ TEST_CASE("Test copy and move column", "[column]") { 2, std::make_shared("author", "books", basic_type::type_uint32, constraints::FOREIGN_KEY), {255, constraints::FOREIGN_KEY}, - null_option::NOT_NULL + null_option_type::NOT_NULL ); REQUIRE(c.name() == "name"); REQUIRE(c.index() == 2);