From e67c0f39a375d01742e2f64f38676efec6f9e3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Sun, 23 Nov 2025 15:58:14 +0100 Subject: [PATCH] preparations for object_generator (generating objects with attributes and constraints) --- demo/CMakeLists.txt | 9 +- demo/object.cpp | 233 ++++++++++++++++++ include/matador/object/attribute.hpp | 34 +-- .../matador/object/attribute_generator.hpp | 4 +- include/matador/object/constraint.hpp | 53 ++++ include/matador/object/object_generator.hpp | 67 +++++ include/matador/object/repository_node.hpp | 2 +- include/matador/sql/field.hpp | 6 +- include/matador/utils/constraints.hpp | 12 +- include/matador/utils/field_attributes.hpp | 2 +- source/core/CMakeLists.txt | 3 + source/core/object/attribute.cpp | 12 +- source/core/object/constraint.cpp | 48 ++++ source/core/object/repository_node.cpp | 4 +- source/core/utils/field_attributes.cpp | 2 +- source/orm/query/query_compiler.cpp | 6 +- source/orm/sql/field.cpp | 4 +- .../sql/internal/query_result_pk_resolver.cpp | 2 +- .../AttributeDefinitionGeneratorTest.cpp | 8 +- test/core/utils/FieldAttributeTest.cpp | 18 +- test/orm/query/QueryBuilderTest.cpp | 2 +- test/orm/sql/ColumnTest.cpp | 4 +- test/orm/sql/FieldTest.cpp | 2 +- 23 files changed, 468 insertions(+), 69 deletions(-) create mode 100644 demo/object.cpp create mode 100644 include/matador/object/constraint.hpp create mode 100644 include/matador/object/object_generator.hpp create mode 100644 source/core/object/constraint.cpp diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index d2439cf..6c940f9 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -1,4 +1,5 @@ -add_executable(demo main.cpp) +add_executable(demo main.cpp + object.cpp) target_link_libraries(demo PRIVATE matador-core matador-orm @@ -23,3 +24,9 @@ target_link_libraries(work PRIVATE ) add_dependencies(work matador-postgres) + +add_executable(object object.cpp) +target_link_libraries(object PRIVATE + matador-core + ${CMAKE_DL_LIBS} +) diff --git a/demo/object.cpp b/demo/object.cpp new file mode 100644 index 0000000..a855e7d --- /dev/null +++ b/demo/object.cpp @@ -0,0 +1,233 @@ +#include "matador/utils/basic_types.hpp" +#include "matador/utils/constraints.hpp" +#include "matador/utils/field_attributes.hpp" + +#include +#include + + +namespace object { + +namespace utils = matador::utils; + +class object; + +enum class null_option_type : uint8_t { + Nullable, + NotNull +}; + +class attribute { +public: + explicit attribute(std::string name); // NOLINT(*-explicit-constructor) + + attribute(const attribute&) = default; + attribute& operator=(const attribute&) = default; + attribute(attribute&&) noexcept = default; + attribute& operator=(attribute&&) noexcept = default; + + attribute() = default; + attribute(std::string name, + utils::basic_type type, + const utils::field_attributes& opts = utils::null_attributes, + null_option_type null_opt = null_option_type::NotNull) + : name_(std::move(name)), type_(type), options_(opts), null_option_type_(null_opt) {} + + [[nodiscard]] const std::string& name() const { return name_; } + void name(const std::string& n) { name_ = n; } + [[nodiscard]] const utils::field_attributes& options() const { return options_; } + [[nodiscard]] utils::field_attributes& options(); + + [[nodiscard]] bool is_nullable() const { return null_option_type_ == null_option_type::Nullable; } + [[nodiscard]] utils::basic_type type() const { return type_; } + + [[nodiscard]] bool is_integer() const { return type_ >= utils::basic_type::type_int8 && type_ <= utils::basic_type::type_uint64; } + [[nodiscard]] bool is_floating_point() const { return type_ == utils::basic_type::type_float || type_ == utils::basic_type::type_double; } + [[nodiscard]] bool is_bool() const { return type_ == utils::basic_type::type_bool; } + [[nodiscard]] bool is_string() const { return type_ == utils::basic_type::type_text; } + [[nodiscard]] bool is_varchar() const { return type_ == utils::basic_type::type_varchar; } + [[nodiscard]] bool is_date() const { return type_ == utils::basic_type::type_date; } + [[nodiscard]] bool is_time() const { return type_ == utils::basic_type::type_time; } + [[nodiscard]] bool is_blob() const { return type_ == utils::basic_type::type_blob; } + [[nodiscard]] bool is_null() const { return type_ == utils::basic_type::type_null; } + +private: + friend class object; + + std::string name_; + std::string alias_; + utils::basic_type type_{utils::basic_type::type_null}; + utils::field_attributes options_; + null_option_type null_option_type_{null_option_type::NotNull}; + + object* parent_{nullptr}; +}; + +class constraint { +public: + constraint() = default; + + explicit constraint(std::string name) : name_(std::move(name)) {} + [[nodiscard]] const std::string& name() const { return name_; } + +private: + friend class object; + + std::string name_; + std::string attribute_name_; + utils::constraints options_{utils::constraints::None}; + + object* parent_{nullptr}; + attribute* attribute_{nullptr}; +}; + +class object { +public: + explicit object(std::string name, std::string alias = ""); + + void add_attribute(attribute attr) { + auto &ref = attributes_.emplace_back(std::move(attr)); + ref.parent_ = this; + } + + static const attribute& create_attribute(std::string name, object& obj) { + attribute attr{std::move(name)}; + attr.parent_ = &obj; + return obj.attributes_.emplace_back(std::move(attr)); + } + + void add_constraint(constraint c) { + auto &ref = constraints_.emplace_back(std::move(c)); + ref.parent_ = this; + } + + [[nodiscard]] const std::string& name() const { return name_; } + [[nodiscard]] const std::string& alias() const { return alias_; } + + [[nodiscard]] bool has_attributes() const { return attributes_.empty(); } + [[nodiscard]] size_t attribute_count() const { return attributes_.size(); } + [[nodiscard]] const std::vector& attributes() const { return attributes_; } + + [[nodiscard]] bool has_constraints() const { return constraints_.empty(); } + [[nodiscard]] size_t constraint_count() const { return constraints_.size(); } + [[nodiscard]] const std::vector& constraints() const { return constraints_; } + +private: + std::string name_; + std::string alias_; + + std::vector attributes_; + std::vector constraints_; +}; + +template +class typed_object : public object { +public: + using object::object; + + Type as(std::string alias) { return Type{std::move(alias)}; } +}; + +struct column_builder { + explicit column_builder(std::string column_name) + : column_name( std::move(column_name) ) { + } + + column_builder& not_null() { + return *this; + } + + column_builder& primary_key() { + return *this; + } + + operator attribute() const { + return attribute{column_name}; + } + + std::string column_name; +}; + +column_builder column(std::string name) { + return column_builder(std::move(name)); +} + +namespace matador::sql { +struct constraint { + std::string name; +}; +} + +struct constraint_builder { + constraint_builder& constraint(std::string name) { + constraint_name = std::move(name); + return *this; + } + + constraint_builder& primary_key(std::string name) { + pk_column_name = std::move(name); + return *this; + } + constraint_builder& foreign_key(std::string name) { + fk_column_name = std::move(name); + return *this; + } + + constraint_builder& references(std::string table, std::string column) { + this->table_name = std::move(table); + this->column_name = std::move(column); + return *this; + } + + operator matador::sql::constraint() const { + return matador::sql::constraint{constraint_name}; + } + + std::string constraint_name; + std::string pk_column_name; + std::string fk_column_name; + std::string table_name; + std::string column_name; +}; + +constraint_builder constraint(std::string name) { + constraint_builder builder; + return builder.constraint(std::move(name)); +} + +template +std::string column_prefix(typed_object *tab) { + if (!tab || tab->empty()) { + return ""; + } + if (!tab->alias().empty()) { + return tab->alias(); + } + return tab->name(); +} + +struct table_builder { + explicit table_builder(std::string name) + : table_name( std::move(name) ) {} + + table_builder& as(std::string table_alias) { + this->alias = std::move(table_alias); + return *this; + } + + operator object() const { + return object{table_name, alias}; + } + std::string table_name; + std::string alias; +}; + +table_builder table(std::string name) { + return table_builder(std::move(name)); +} + +} + +int main() { + +} \ No newline at end of file diff --git a/include/matador/object/attribute.hpp b/include/matador/object/attribute.hpp index e9b7c8a..5f0bec4 100644 --- a/include/matador/object/attribute.hpp +++ b/include/matador/object/attribute.hpp @@ -18,6 +18,7 @@ struct attribute_options { null_option_type null_option{null_option_type::NOT_NULL}; int index{-1}; }; +class object; class attribute { public: @@ -85,7 +86,10 @@ public: } private: + friend class object; + std::string name_; + object *owner_{nullptr}; std::string table_name_; attribute_options options_; utils::basic_type type_{utils::basic_type::type_null}; @@ -94,22 +98,6 @@ private: }; -class object { -public: - using iterator = std::vector::iterator; - using const_iterator = std::vector::const_iterator; - -private: - std::vector columns_; - std::string name_; - std::string alias_; -}; - -class constraint { -public: -private: - std::shared_ptr column_; -}; /** * User defined literal to have a shortcut creating a column object * @param name Name of the column @@ -131,7 +119,7 @@ attribute make_column(const std::string &name, utils::field_attribu template < typename Type > attribute make_pk_column(const std::string &name, size_t size = 0) { - return make_column(name, { size, utils::constraints::PRIMARY_KEY }); + return make_column(name, { size, utils::constraints::PrimaryKey }); } template <> @@ -139,13 +127,13 @@ attribute make_pk_column(const std::string &name, size_t size); template < typename Type > attribute make_fk_column(const std::string &name, size_t size, const std::shared_ptr &ref_column) { - return {name, utils::data_type_traits::type(size), ref_column, { size, utils::constraints::FOREIGN_KEY }}; + return {name, utils::data_type_traits::type(size), ref_column, { size, utils::constraints::ForeignKey }}; } template < typename Type > [[maybe_unused]] attribute 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_type::NOT_NULL}; + return {name, utils::data_type_traits::type(0), 0, ref_column, { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL}; } template <> @@ -155,8 +143,8 @@ template < typename Type > [[maybe_unused]] attribute make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) { 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_type::NOT_NULL + std::make_shared(ref_column_name, ref_table_name, utils::data_type_traits::type(size), utils::constraints::ForeignKey), + { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL }; } @@ -164,8 +152,8 @@ template < typename Type > [[maybe_unused]] attribute make_fk_column(const std::string &name, const std::string &ref_table_name, const std::string &ref_column_name) { return { name, utils::data_type_traits::type(0), 0, - std::make_shared(ref_column_name, utils::data_type_traits::type(0), ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}), - { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL + std::make_shared(ref_column_name, utils::data_type_traits::type(0), ref_table_name, attribute_options{utils::constraints::ForeignKey}), + { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL }; } diff --git a/include/matador/object/attribute_generator.hpp b/include/matador/object/attribute_generator.hpp index a700dca..03843d6 100644 --- a/include/matador/object/attribute_generator.hpp +++ b/include/matador/object/attribute_generator.hpp @@ -26,7 +26,7 @@ public: template attribute generate(const char *id, Type &x, const std::shared_ptr &ref_column) { access::process(*this, x); - return attribute{id, type_, 0, ref_column, {utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL}; + return attribute{id, type_, 0, ref_column, {utils::constraints::ForeignKey }, null_option_type::NOT_NULL}; } template @@ -133,7 +133,7 @@ private: template void attribute_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) { - on_attribute(id, x, { attr.size(), utils::constraints::PRIMARY_KEY }); + on_attribute(id, x, { attr.size(), utils::constraints::PrimaryKey }); } template diff --git a/include/matador/object/constraint.hpp b/include/matador/object/constraint.hpp new file mode 100644 index 0000000..fae2a17 --- /dev/null +++ b/include/matador/object/constraint.hpp @@ -0,0 +1,53 @@ +#ifndef MATADOR_CONSTRAINT_HPP +#define MATADOR_CONSTRAINT_HPP + +#include "matador/utils/constraints.hpp" + +#include + +namespace matador::object { + +class attribute; +class constraint_builder; +class object; + +class constraint { +public: + constraint() = default; + explicit constraint(std::string name); + + [[nodiscard]] const std::string& name() const; + +private: + friend class constraint_builder; + friend class object; + + std::string name_; + attribute* attr_{nullptr}; + object *owner_{nullptr}; + utils::constraints options_{utils::constraints::None}; + std::string ref_table_name_{}; + std::string ref_column_name_{}; +}; + +class constraint_builder { +public: + constraint_builder& constraint(std::string name); + constraint_builder& primary_key(std::string name); + constraint_builder& foreign_key(std::string name); + constraint_builder& references(std::string table, std::string column); + + operator class constraint() const; + +private: + std::string constraint_name; + utils::constraints options_{utils::constraints::None}; + std::string column_name; + std::string ref_table_name; + std::string ref_column_name; +}; + +constraint_builder constraint(std::string name); + +} +#endif //MATADOR_CONSTRAINT_HPP \ No newline at end of file diff --git a/include/matador/object/object_generator.hpp b/include/matador/object/object_generator.hpp new file mode 100644 index 0000000..61e0cce --- /dev/null +++ b/include/matador/object/object_generator.hpp @@ -0,0 +1,67 @@ +#ifndef MATADOR_OBJECT_GENERATOR_HPP +#define MATADOR_OBJECT_GENERATOR_HPP + +#include "matador/object/attribute.hpp" +#include "matador/object/attribute_generator.hpp" +#include "matador/object/constraint.hpp" + +namespace matador::object { + +class object_generator; + +class object { +public: + explicit object(std::string name, std::string alias = ""); + + void add_attribute(attribute attr) { + auto &ref = attributes_.emplace_back(std::move(attr)); + ref.owner_ = this; + } + + static const attribute& create_attribute(std::string name, object& obj) { + attribute attr{std::move(name)}; + attr.owner_ = &obj; + return obj.attributes_.emplace_back(std::move(attr)); + } + + void add_constraint(class constraint c) { + auto &ref = constraints_.emplace_back(std::move(c)); + ref.owner_ = this; + } + + [[nodiscard]] const std::string& name() const { return name_; } + [[nodiscard]] const std::string& alias() const { return alias_; } + + [[nodiscard]] bool has_attributes() const { return attributes_.empty(); } + [[nodiscard]] size_t attribute_count() const { return attributes_.size(); } + [[nodiscard]] const std::vector& attributes() const { return attributes_; } + + [[nodiscard]] bool has_constraints() const { return constraints_.empty(); } + [[nodiscard]] size_t constraint_count() const { return constraints_.size(); } + [[nodiscard]] const std::vector& constraints() const { return constraints_; } + +private: + friend class object_generator; + + std::string name_; + std::string alias_; + + std::vector attributes_; + std::vector constraints_; +}; + +class object_generator { +public: + template + object generate(std::string name, std::string alias = "") { + object obj{std::move(name), std::move(alias)}; + attribute_generator::generate(*this); + Type obj2; + access::process(gen, obj2); + return obj; + } + +private: +}; +} +#endif //MATADOR_OBJECT_GENERATOR_HPP \ No newline at end of file diff --git a/include/matador/object/repository_node.hpp b/include/matador/object/repository_node.hpp index 9306e75..f10d6fe 100644 --- a/include/matador/object/repository_node.hpp +++ b/include/matador/object/repository_node.hpp @@ -48,7 +48,7 @@ public: auto info = std::make_unique>( result.value(), attribute_generator::generate(*obj, repo), - std::move(creator) + std::forward(creator) ); result.value()->info_ = std::move(info); diff --git a/include/matador/sql/field.hpp b/include/matador/sql/field.hpp index 82a4b3e..4bac0d9 100644 --- a/include/matador/sql/field.hpp +++ b/include/matador/sql/field.hpp @@ -17,12 +17,12 @@ class field { public: explicit field(std::string name); template - field(std::string name, Type value, const utils::constraints type = utils::constraints::NONE, 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, utils::constraints type = utils::constraints::NONE, 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; @@ -71,7 +71,7 @@ private: friend class record; std::string name_; - utils::constraints type_{utils::constraints::NONE}; + utils::constraints type_{utils::constraints::None}; int index_{-1}; utils::value value_; diff --git a/include/matador/utils/constraints.hpp b/include/matador/utils/constraints.hpp index aa7304d..e445be4 100644 --- a/include/matador/utils/constraints.hpp +++ b/include/matador/utils/constraints.hpp @@ -4,12 +4,12 @@ namespace matador::utils { enum class constraints : unsigned char { - NONE = 0, - INDEX = 1 << 0, - UNIQUE = 1 << 1, - PRIMARY_KEY = 1 << 2, - FOREIGN_KEY = 1 << 3, - DEFAULT = 1 << 4 + None = 0, + Index = 1 << 0, + Unique = 1 << 1, + PrimaryKey = 1 << 2, + ForeignKey = 1 << 3, + Default = 1 << 4 }; inline constraints operator|(constraints a, constraints b) { return static_cast(static_cast(a) | static_cast(b)); } diff --git a/include/matador/utils/field_attributes.hpp b/include/matador/utils/field_attributes.hpp index c6a92b1..6076c59 100644 --- a/include/matador/utils/field_attributes.hpp +++ b/include/matador/utils/field_attributes.hpp @@ -71,7 +71,7 @@ public: private: size_t size_ = 0; - constraints options_ = constraints::NONE; + constraints options_ = constraints::None; }; const field_attributes null_attributes {}; diff --git a/source/core/CMakeLists.txt b/source/core/CMakeLists.txt index b404006..c6fb4ff 100644 --- a/source/core/CMakeLists.txt +++ b/source/core/CMakeLists.txt @@ -16,10 +16,12 @@ add_library(matador-core STATIC ../../include/matador/object/attribute.hpp ../../include/matador/object/attribute_generator.hpp ../../include/matador/object/basic_object_info.hpp + ../../include/matador/object/constraint.hpp ../../include/matador/object/error_code.hpp ../../include/matador/object/foreign_node_completer.hpp ../../include/matador/object/internal/shadow_repository.hpp ../../include/matador/object/many_to_many_relation.hpp + ../../include/matador/object/object_generator.hpp ../../include/matador/object/object_info.hpp ../../include/matador/object/object_proxy.hpp ../../include/matador/object/object_ptr.hpp @@ -77,6 +79,7 @@ add_library(matador-core STATIC object/attribute.cpp object/attribute_generator.cpp object/basic_object_info.cpp + object/constraint.cpp object/error_code.cpp object/foreign_node_completer.cpp object/internal/shadow_repository.cpp diff --git a/source/core/object/attribute.cpp b/source/core/object/attribute.cpp index e629a40..4d5d9f6 100644 --- a/source/core/object/attribute.cpp +++ b/source/core/object/attribute.cpp @@ -147,14 +147,14 @@ attribute make_column(const std::string &name, utils::field_attribu template<> attribute make_pk_column(const std::string &name, size_t size) { - return make_column(name, {size, utils::constraints::PRIMARY_KEY}); + return make_column(name, {size, utils::constraints::PrimaryKey}); } template<> attribute 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_type::NOT_NULL + {size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL }; } @@ -162,17 +162,17 @@ template<> attribute 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, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}), - { 0, utils::constraints::FOREIGN_KEY }, null_option_type::NOT_NULL + std::make_shared(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey}), + { 0, utils::constraints::ForeignKey }, null_option_type::NOT_NULL }; } template<> attribute make_fk_column(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) { - const auto ref_column = std::make_shared(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}); + const auto ref_column = std::make_shared(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::ForeignKey}); return { name, utils::data_type_traits::type(size), 0, ref_column, - {size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL + {size, utils::constraints::ForeignKey}, null_option_type::NOT_NULL }; } } diff --git a/source/core/object/constraint.cpp b/source/core/object/constraint.cpp new file mode 100644 index 0000000..92917d3 --- /dev/null +++ b/source/core/object/constraint.cpp @@ -0,0 +1,48 @@ +#include "matador/object/constraint.hpp" + +namespace matador::object { +constraint::constraint(std::string name) +: name_(std::move(name)) {} + +const std::string & constraint::name() const { + return name_; +} + +constraint_builder & constraint_builder::constraint(std::string name) { + constraint_name = std::move(name); + return *this; +} + +constraint_builder & constraint_builder::primary_key(std::string name) { + column_name = std::move(name); + options_ |= utils::constraints::PrimaryKey; + return *this; +} + +constraint_builder & constraint_builder::foreign_key(std::string name) { + column_name = std::move(name); + options_ |= utils::constraints::ForeignKey; + return *this; +} + +constraint_builder & constraint_builder::references(std::string table, std::string column) { + ref_table_name = std::move(table); + ref_column_name = std::move(column); + + return *this; +} + +constraint_builder::operator class constraint() const { + class constraint c; + c.name_ = constraint_name; + c.options_ = options_; + c.ref_column_name_ = ref_column_name; + c.ref_table_name_ = ref_table_name; + return {}; +} + +constraint_builder constraint(std::string name) { + constraint_builder builder; + return builder.constraint(std::move(name)); +} +} diff --git a/source/core/object/repository_node.cpp b/source/core/object/repository_node.cpp index 0946cf0..2a1ef43 100644 --- a/source/core/object/repository_node.cpp +++ b/source/core/object/repository_node.cpp @@ -110,7 +110,7 @@ std::shared_ptr repository_node::determine_reference_column(const std 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, table_name, attribute_options{utils::constraints::FOREIGN_KEY}); + return std::make_shared(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::ForeignKey}); } auto ref_column = it->second; @@ -118,7 +118,7 @@ std::shared_ptr repository_node::determine_reference_column(const std ref_column->name(pk_info.pk_column_name); ref_column->table_name(table_name); ref_column->change_type(pk_info.type); - ref_column->attributes() = utils::constraints::FOREIGN_KEY; + ref_column->attributes() = utils::constraints::ForeignKey; if (table_name.empty()) { repo.missing_references_.insert({ti, ref_column}); diff --git a/source/core/utils/field_attributes.cpp b/source/core/utils/field_attributes.cpp index ac64775..5324c5e 100644 --- a/source/core/utils/field_attributes.cpp +++ b/source/core/utils/field_attributes.cpp @@ -16,7 +16,7 @@ field_attributes::field_attributes(const size_t size, const constraints options) field_attributes& field_attributes::operator=(const size_t size) { size_ = size; - options_ = constraints::NONE; + options_ = constraints::None; return *this; } field_attributes& field_attributes::operator=(constraints opt) { diff --git a/source/orm/query/query_compiler.cpp b/source/orm/query/query_compiler.cpp index 9d3d732..abc89c3 100644 --- a/source/orm/query/query_compiler.cpp +++ b/source/orm/query/query_compiler.cpp @@ -396,13 +396,13 @@ std::string build_create_column(const object::attribute &col, const sql::dialect if (!col.is_nullable()) { result.append(" NOT NULL"); } - if (is_constraint_set(col.attributes().options(), utils::constraints::UNIQUE)) { + if (is_constraint_set(col.attributes().options(), utils::constraints::Unique)) { result.append(" UNIQUE"); } - if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) { + if (is_constraint_set(col.attributes().options(), utils::constraints::PrimaryKey)) { context.primary_keys.emplace_back(col.name()); } - if (is_constraint_set(col.attributes().options(), utils::constraints::FOREIGN_KEY)) { + if (is_constraint_set(col.attributes().options(), utils::constraints::ForeignKey)) { context.foreign_contexts.push_back({col.name(), col.reference_column()}); } diff --git a/source/orm/sql/field.cpp b/source/orm/sql/field.cpp index 1568bd4..3020d0b 100644 --- a/source/orm/sql/field.cpp +++ b/source/orm/sql/field.cpp @@ -101,11 +101,11 @@ bool field::is_null() const } bool field::is_primary_key() const { - return type_ == utils::constraints::PRIMARY_KEY; + return type_ == utils::constraints::PrimaryKey; } bool field::is_foreign_key() const { - return type_ == utils::constraints::FOREIGN_KEY; + return type_ == utils::constraints::ForeignKey; } bool field::is_attribute() const { diff --git a/source/orm/sql/internal/query_result_pk_resolver.cpp b/source/orm/sql/internal/query_result_pk_resolver.cpp index 8e17f49..b3573b0 100644 --- a/source/orm/sql/internal/query_result_pk_resolver.cpp +++ b/source/orm/sql/internal/query_result_pk_resolver.cpp @@ -4,7 +4,7 @@ namespace matador::sql::internal { void query_result_pk_resolver::on_attribute(const char *id, const utils::value &x, const utils::field_attributes &attr) { - if (is_constraint_set(attr.options(), utils::constraints::PRIMARY_KEY)) { + if (is_constraint_set(attr.options(), utils::constraints::PrimaryKey)) { if (x.is_integer()) { uint64_t val; utils::data_type_traits::read_value(reader_, id, column_index_++, val); diff --git a/test/core/object/AttributeDefinitionGeneratorTest.cpp b/test/core/object/AttributeDefinitionGeneratorTest.cpp index b1043b5..1e10790 100644 --- a/test/core/object/AttributeDefinitionGeneratorTest.cpp +++ b/test/core/object/AttributeDefinitionGeneratorTest.cpp @@ -20,9 +20,9 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener auto columns = attribute_generator::generate(repo); const std::vector expected_columns = { - attribute{"product_name", basic_type::type_varchar, constraints::PRIMARY_KEY, null_option_type::NOT_NULL }, - attribute{"supplier_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option_type::NOT_NULL }, - attribute{"category_id", basic_type::type_uint32, constraints::FOREIGN_KEY, null_option_type::NOT_NULL }, + attribute{"product_name", basic_type::type_varchar, constraints::PrimaryKey, null_option_type::NOT_NULL }, + attribute{"supplier_id", basic_type::type_uint32, constraints::ForeignKey, null_option_type::NOT_NULL }, + attribute{"category_id", basic_type::type_uint32, constraints::ForeignKey, null_option_type::NOT_NULL }, attribute{"quantity_per_unit", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL }, attribute{"unit_price", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }, attribute{"units_in_stock", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL }, @@ -46,7 +46,7 @@ TEST_CASE("Generate columns from object with nullable columns", "[column generat auto columns = attribute_generator::generate(repo); const std::vector expected_columns = { - attribute{"id", basic_type::type_uint32, constraints::PRIMARY_KEY, null_option_type::NOT_NULL }, + attribute{"id", basic_type::type_uint32, constraints::PrimaryKey, null_option_type::NOT_NULL }, attribute{"name", basic_type::type_varchar, null_attributes, null_option_type::NOT_NULL }, attribute{"age", basic_type::type_uint32, null_attributes, null_option_type::NOT_NULL } }; diff --git a/test/core/utils/FieldAttributeTest.cpp b/test/core/utils/FieldAttributeTest.cpp index 10e7064..49c0f92 100644 --- a/test/core/utils/FieldAttributeTest.cpp +++ b/test/core/utils/FieldAttributeTest.cpp @@ -8,25 +8,25 @@ TEST_CASE("Test field attribute", "[field-attribute]") { field_attributes attr; REQUIRE(attr.size() == 0); - REQUIRE(attr.options() == constraints::NONE); + REQUIRE(attr.options() == constraints::None); attr = 255; REQUIRE(attr.size() == 255); - REQUIRE(attr.options() == constraints::NONE); + REQUIRE(attr.options() == constraints::None); - attr = constraints::INDEX; + attr = constraints::Index; REQUIRE(attr.size() == 0); - REQUIRE(attr.options() == constraints::INDEX); + REQUIRE(attr.options() == constraints::Index); - attr = { 255, constraints::DEFAULT }; + attr = { 255, constraints::Default }; REQUIRE(attr.size() == 255); - REQUIRE(attr.options() == constraints::DEFAULT); + REQUIRE(attr.options() == constraints::Default); field_attributes attr2{255}; REQUIRE(attr2.size() == 255); - REQUIRE(attr2.options() == constraints::NONE); + REQUIRE(attr2.options() == constraints::None); - field_attributes attr3{constraints::UNIQUE}; + field_attributes attr3{constraints::Unique}; REQUIRE(attr3.size() == 0); - REQUIRE(attr3.options() == constraints::UNIQUE); + REQUIRE(attr3.options() == constraints::Unique); } \ No newline at end of file diff --git a/test/orm/query/QueryBuilderTest.cpp b/test/orm/query/QueryBuilderTest.cpp index 26b81ef..b8b2b7e 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_type::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 7c45a72..ecd9457 100644 --- a/test/orm/sql/ColumnTest.cpp +++ b/test/orm/sql/ColumnTest.cpp @@ -25,8 +25,8 @@ TEST_CASE("Test copy and move column", "[column]") { "name", basic_type::type_varchar, 2, - std::make_shared("author", basic_type::type_uint32, "books", attribute_options{constraints::FOREIGN_KEY}), - {255, constraints::FOREIGN_KEY}, + std::make_shared("author", basic_type::type_uint32, "books", attribute_options{constraints::ForeignKey}), + {255, constraints::ForeignKey}, null_option_type::NOT_NULL ); REQUIRE(c.name() == "name"); diff --git a/test/orm/sql/FieldTest.cpp b/test/orm/sql/FieldTest.cpp index 582a91a..eb55b23 100644 --- a/test/orm/sql/FieldTest.cpp +++ b/test/orm/sql/FieldTest.cpp @@ -37,7 +37,7 @@ TEST_CASE("Test field", "[field]") { REQUIRE(bool_val.has_value()); REQUIRE(bool_val.value()); - f = sql::field("name", utils::blob{ 7,8,6,5,4,3 }, utils::constraints::NONE, 0, 1); + f = sql::field("name", utils::blob{ 7,8,6,5,4,3 }, utils::constraints::None, 0, 1); REQUIRE(f.index() == 1); REQUIRE(!f.is_null()); REQUIRE(!f.is_integer());