From 9ffcee13174ea66988ec6534dd6f67658b49c700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Mon, 17 Nov 2025 13:16:18 +0100 Subject: [PATCH] removed the value field from attribute_definition because it's a definition and should not hold a value. --- backends/postgres/src/postgres_connection.cpp | 4 +- .../matador/object/attribute_definition.hpp | 58 +++++-------------- include/matador/utils/value.hpp | 5 +- source/core/object/attribute_definition.cpp | 58 +++++++++---------- source/core/object/repository_node.cpp | 2 +- source/core/utils/value.cpp | 9 +-- source/orm/orm/session.cpp | 2 +- source/orm/query/query_compiler.cpp | 2 +- source/orm/sql/connection.cpp | 2 +- test/orm/sql/ColumnTest.cpp | 16 +---- 10 files changed, 53 insertions(+), 105 deletions(-) diff --git a/backends/postgres/src/postgres_connection.cpp b/backends/postgres/src/postgres_connection.cpp index 3d7847f..5cbdbc1 100644 --- a/backends/postgres/src/postgres_connection.cpp +++ b/backends/postgres/src/postgres_connection.cpp @@ -102,7 +102,7 @@ utils::result, utils::error> postgres_co const auto type = oid2type(PQftype(res, i)); // const char *col_name = PQfname(res, i); // const auto size = PQfmod(res, i); - prototype.at(i).type(type); + prototype.at(i).change_type(type); } return utils::ok(std::make_unique(std::make_unique(res), prototype)); @@ -110,7 +110,7 @@ utils::result, utils::error> postgres_co std::string postgres_connection::generate_statement_name(const sql::query_context &query) { std::stringstream name; - name << query.table.name << "_" << query.command_name; + name << query.table.name() << "_" << query.command_name; auto result = postgres_connection::statement_name_map_.find(name.str()); if (result == postgres_connection::statement_name_map_.end()) { diff --git a/include/matador/object/attribute_definition.hpp b/include/matador/object/attribute_definition.hpp index c32b31d..720c45e 100644 --- a/include/matador/object/attribute_definition.hpp +++ b/include/matador/object/attribute_definition.hpp @@ -27,14 +27,14 @@ public: attribute_definition() = default; template - explicit attribute_definition(std::string name, std::string table_name, const utils::field_attributes& attr) + 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 - explicit attribute_definition(std::string name, const utils::field_attributes& attr) + attribute_definition(std::string name, const utils::field_attributes& attr) : attribute_definition(std::move(name), utils::data_type_traits::type(attr.size()), attr) {} @@ -48,15 +48,15 @@ public: : 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, size_t index = 0); - attribute_definition(std::string name, std::string table_name, utils::basic_type type, const utils::field_attributes&, null_option null_opt, size_t index = 0); + 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); template attribute_definition(std::string name, const std::shared_ptr &ref_column, const utils::field_attributes& attr, null_option 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, size_t 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 null_opt); [[nodiscard]] const std::string& name() const; void name(const std::string& n); @@ -68,6 +68,11 @@ public: [[nodiscard]] utils::field_attributes& attributes(); [[nodiscard]] bool is_nullable() const; [[nodiscard]] utils::basic_type type() const; + void change_type(utils::basic_type type, const utils::field_attributes &attr = utils::null_attributes); + template < typename Type > + void change_type(const utils::field_attributes &attr = utils::null_attributes) { + type_ = utils::data_type_traits::type(attr.size()); + } [[nodiscard]] std::shared_ptr reference_column() const; [[nodiscard]] bool is_foreign_reference() const; @@ -81,49 +86,12 @@ public: [[nodiscard]] bool is_blob() const; [[nodiscard]] bool is_null() const; - void type(utils::basic_type type); - template< typename Type > [[nodiscard]] bool is_type_of() const { - return std::holds_alternative(value_); + return type() == utils::data_type_traits::type(attributes().size()); } - [[nodiscard]] std::string str() const; - - template - void set(const Type &value, const utils::field_attributes &attr = utils::null_attributes) - { - attributes_ = attr; - value_ = value; - } - - void set(const std::string &value, const utils::field_attributes &attr) - { - value_ = value; - attributes_ = attr; - } - - void set(const char *value, const utils::field_attributes &attr) - { - value_ = std::string(value); - attributes_ = attr; - } - - template - std::optional as() const - { - return value_.as(); - } - - friend std::ostream& operator<<(std::ostream &out, const attribute_definition &col); - private: - template - void process(Operator &op) - { - op.on_attribute(name_.c_str(), value_, attributes_); - } - using data_type_index = std::vector; private: @@ -134,7 +102,9 @@ private: int index_{-1}; utils::field_attributes attributes_; null_option null_option_{null_option::NOT_NULL}; - utils::value value_; + utils::basic_type type_{utils::basic_type::type_null}; + size_t size_{0}; + std::shared_ptr reference_column_; }; diff --git a/include/matador/utils/value.hpp b/include/matador/utils/value.hpp index dc0e185..1a62115 100644 --- a/include/matador/utils/value.hpp +++ b/include/matador/utils/value.hpp @@ -11,10 +11,7 @@ namespace matador::utils { namespace detail { template -size_t determine_size(const Type &/*val*/) -{ - return 0; -} +size_t determine_size(const Type &/*val*/) { return 0; } size_t determine_size(const std::string &val); size_t determine_size(const char *val); size_t determine_size(const blob &val); diff --git a/source/core/object/attribute_definition.cpp b/source/core/object/attribute_definition.cpp index cbd9c5b..25a4bd4 100644 --- a/source/core/object/attribute_definition.cpp +++ b/source/core/object/attribute_definition.cpp @@ -22,42 +22,44 @@ attribute_definition::attribute_definition(std::string name, , table_(std::move(table_name)) , index_(0) , attributes_(attr) -, value_(type, attr.size()) { +, type_(type){ } -attribute_definition::attribute_definition(std::string name, const utils::basic_type type, - const utils::field_attributes &attr, const null_option null_opt, - const size_t index) +attribute_definition::attribute_definition(std::string name, + const utils::basic_type type, + const utils::field_attributes &attr, + const null_option null_opt, + const int index) : name_(std::move(name)) , index_(index) , attributes_(attr) , null_option_(null_opt) -, value_(type, attr.size()) { +, 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 size_t index) + const int index) : name_(std::move(name)) , table_(std::move(table_name)) , index_(index) , attributes_(attr) , null_option_(null_opt) -, value_(type, attr.size()) { +, type_(type) { } attribute_definition::attribute_definition(std::string name, const utils::basic_type type, - const size_t index, + 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) -, value_(type, attr.size()) +, type_(type) , reference_column_(ref_column) { } @@ -98,7 +100,12 @@ bool attribute_definition::is_nullable() const { } utils::basic_type attribute_definition::type() const { - return value_.type(); + return type_; +} + +void attribute_definition::change_type(const utils::basic_type type, const utils::field_attributes& attr) { + attributes_ = attr; + type_ = type; } std::shared_ptr attribute_definition::reference_column() const { @@ -110,52 +117,39 @@ bool attribute_definition::is_foreign_reference() const { } bool attribute_definition::is_integer() const { - return value_.is_integer(); + return type_ >= utils::basic_type::type_int8 && type_ <= utils::basic_type::type_uint64; } bool attribute_definition::is_floating_point() const { - return value_.is_floating_point(); + return type_ == utils::basic_type::type_float || type_ == utils::basic_type::type_double; } bool attribute_definition::is_bool() const { - return value_.is_bool(); + return type_ == utils::basic_type::type_bool; } bool attribute_definition::is_string() const { - return value_.is_string(); + return type_ == utils::basic_type::type_text; } bool attribute_definition::is_varchar() const { - return value_.is_varchar(); + return type_ == utils::basic_type::type_varchar; } bool attribute_definition::is_date() const { - return value_.is_date(); + return type_ == utils::basic_type::type_date; } bool attribute_definition::is_time() const { - return value_.is_time(); + return type_ == utils::basic_type::type_time; } bool attribute_definition::is_blob() const { - return value_.is_blob(); + return type_ == utils::basic_type::type_blob; } bool attribute_definition::is_null() const { - return value_.is_null(); -} - -void attribute_definition::type(const utils::basic_type type) { - value_.type(type); -} - -std::string attribute_definition::str() const { - return value_.str(); -} - -std::ostream &operator<<(std::ostream &out, const attribute_definition &col) { - out << col.str(); - return out; + return type_ == utils::basic_type::type_null; } attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr, diff --git a/source/core/object/repository_node.cpp b/source/core/object/repository_node.cpp index 941d502..ee73e94 100644 --- a/source/core/object/repository_node.cpp +++ b/source/core/object/repository_node.cpp @@ -112,7 +112,7 @@ std::shared_ptr repository_node::determine_reference_colum tree.missing_references_.erase(it); ref_column->name(pk_info.pk_column_name); ref_column->table_name(name); - ref_column->type(pk_info.type); + ref_column->change_type(pk_info.type); ref_column->attributes() = utils::constraints::FOREIGN_KEY; if (name.empty()) { diff --git a/source/core/utils/value.cpp b/source/core/utils/value.cpp index 097ff09..c6f3f87 100644 --- a/source/core/utils/value.cpp +++ b/source/core/utils/value.cpp @@ -7,18 +7,15 @@ namespace detail { void initialize_by_basic_type(basic_type type, database_type &val); -size_t determine_size(const std::string &val) -{ +size_t determine_size(const std::string &val) { return val.size(); } -size_t determine_size(const char *val) -{ +size_t determine_size(const char *val) { return strlen(val); } -size_t determine_size(const blob &val) -{ +size_t determine_size(const blob &val) { return val.size(); } diff --git a/source/orm/orm/session.cpp b/source/orm/orm/session.cpp index f3ddc07..4c5d796 100644 --- a/source/orm/orm/session.cpp +++ b/source/orm/orm/session.cpp @@ -101,7 +101,7 @@ utils::result, utils::error> session::fetch_all(c // adjust columns from given query for (auto &col: q.prototype) { if (const auto rit = it->second.find(col.name()); rit != it->second.end()) { - const_cast(col).type(rit->type()); + const_cast(col).change_type(rit->type()); } } auto res = fetch(q); diff --git a/source/orm/query/query_compiler.cpp b/source/orm/query/query_compiler.cpp index 5f4dfdd..196dd3e 100644 --- a/source/orm/query/query_compiler.cpp +++ b/source/orm/query/query_compiler.cpp @@ -39,7 +39,7 @@ std::string handle_column(sql::query_context &ctx, const sql::dialect *d, const ctx.column_aliases.insert({column_table->has_alias() ? column_table->alias() : column_table->name() + "." + col.column_name(), col.alias()}); if (col.is_function()) { ctx.prototype.emplace_back(col.has_alias() ? col.alias() : col.column_name()); - ctx.prototype.back().type(utils::basic_type::type_int32); + ctx.prototype.back().change_type(utils::basic_type::type_int32); } else { ctx.prototype.emplace_back(col.column_name()); } diff --git a/source/orm/sql/connection.cpp b/source/orm/sql/connection.cpp index 92a5b93..b6a21dd 100644 --- a/source/orm/sql/connection.cpp +++ b/source/orm/sql/connection.cpp @@ -229,7 +229,7 @@ utils::result, utils::error> connection::perform [&col](const auto &value) { return value.name() == col.name(); } ); if (col.type() == utils::basic_type::type_null && rit != result->end()) { - const_cast(col).type(rit->type()); + const_cast(col).change_type(rit->type()); } } } diff --git a/test/orm/sql/ColumnTest.cpp b/test/orm/sql/ColumnTest.cpp index 35fd492..43db791 100644 --- a/test/orm/sql/ColumnTest.cpp +++ b/test/orm/sql/ColumnTest.cpp @@ -13,15 +13,11 @@ TEST_CASE("Test create empty column", "[column]") { REQUIRE(c.type() == basic_type::type_null); REQUIRE(!c.reference_column()); - c.set(std::string{"george"}, 255); + c.change_type(255); REQUIRE(c.type() == basic_type::type_varchar); - REQUIRE(c.as() == "george"); - c.set(7); + c.change_type(7); REQUIRE(c.type() == basic_type::type_int32); - REQUIRE(c.as() == "7"); - REQUIRE(c.as() == 7); - REQUIRE(c.str() == "7"); } TEST_CASE("Test copy and move column", "[column]") { @@ -30,17 +26,15 @@ TEST_CASE("Test copy and move column", "[column]") { basic_type::type_varchar, 2, std::make_shared("author", "books", basic_type::type_uint32, constraints::FOREIGN_KEY), - constraints::FOREIGN_KEY, + {255, constraints::FOREIGN_KEY}, null_option::NOT_NULL ); - c.set(std::string{"george"}, 255); REQUIRE(c.name() == "name"); REQUIRE(c.index() == 2); REQUIRE(c.reference_column()); REQUIRE(c.reference_column()->name() == "author"); REQUIRE(c.reference_column()->table_name() == "books"); REQUIRE(c.type() == basic_type::type_varchar); - REQUIRE(c.as() == "george"); REQUIRE(c.attributes().size() == 255); auto c2 = c; @@ -50,7 +44,6 @@ TEST_CASE("Test copy and move column", "[column]") { REQUIRE(c2.reference_column()->name() == "author"); REQUIRE(c2.reference_column()->table_name() == "books"); REQUIRE(c2.type() == basic_type::type_varchar); - REQUIRE(c2.as() == "george"); REQUIRE(c2.attributes().size() == 255); auto c3 = std::move(c2); @@ -60,13 +53,10 @@ TEST_CASE("Test copy and move column", "[column]") { REQUIRE(c3.reference_column()->name() == "author"); REQUIRE(c3.reference_column()->table_name() == "books"); REQUIRE(c3.type() == basic_type::type_varchar); - REQUIRE(c3.as() == "george"); REQUIRE(c3.attributes().size() == 255); REQUIRE(c2.name().empty()); REQUIRE(c2.index() == 2); REQUIRE(!c2.reference_column()); - REQUIRE(c2.type() == basic_type::type_null); - // REQUIRE(!c2.as().has_value()); REQUIRE(c2.attributes().size() == 255); } \ No newline at end of file