From 3f3773dc71299ab03f848645cef2cabe02f3b335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Thu, 20 Nov 2025 15:35:40 +0100 Subject: [PATCH] fixed core- and orm-tests. postgres-tests needs some work --- backends/postgres/src/postgres_connection.cpp | 2 +- .../matador/object/attribute_definition.hpp | 67 +++++++++++-------- .../object/attribute_definition_generator.hpp | 5 +- include/matador/object/basic_object_info.hpp | 13 ++-- include/matador/object/object_info.hpp | 39 ++++++----- include/matador/object/repository_node.hpp | 15 +++-- include/matador/orm/session.hpp | 2 +- include/matador/orm/session_query_builder.hpp | 26 +++---- source/core/object/attribute_definition.cpp | 52 ++++++++------ source/core/object/basic_object_info.cpp | 36 +++++----- source/core/object/repository_node.cpp | 25 ++++--- source/orm/orm/schema.cpp | 2 +- source/orm/orm/session.cpp | 6 +- source/orm/query/query_compiler.cpp | 8 +-- source/orm/sql/dialect.cpp | 1 + test/backends/QueryStatementTests.cpp | 2 +- test/orm/sql/ColumnTest.cpp | 8 +-- 17 files changed, 170 insertions(+), 139 deletions(-) diff --git a/backends/postgres/src/postgres_connection.cpp b/backends/postgres/src/postgres_connection.cpp index 9294161..f970dd4 100644 --- a/backends/postgres/src/postgres_connection.cpp +++ b/backends/postgres/src/postgres_connection.cpp @@ -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.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 84dae91..de0c3d7 100644 --- a/include/matador/object/attribute_definition.hpp +++ b/include/matador/object/attribute_definition.hpp @@ -13,8 +13,6 @@ 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}; @@ -33,43 +31,53 @@ public: attribute_definition() = default; - template - attribute_definition(std::string name, const utils::field_attributes& attr) - : attribute_definition(std::move(name), utils::data_type_traits::type(attr.size()), attr) - {} + // template + // attribute_definition(std::string name, const utils::field_attributes& attr) + // : attribute_definition(std::move(name), utils::data_type_traits::type(attr.size()), attr) + // {} + // + // template + // 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) + // {} - template - 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) - {} + 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 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) - {} + // template + // 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_type null_opt); - 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_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_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 = {}); + attribute_definition(std::string name, + utils::basic_type type, + const std::shared_ptr &ref_column = {}); + attribute_definition(std::string name, + utils::basic_type type, + std::string table_name, + const attribute_options& options, + 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::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(); [[nodiscard]] bool is_nullable() const; [[nodiscard]] utils::basic_type type() const; + [[nodiscard]] const std::string& table_name() const; + void table_name(const std::string& name); 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) { @@ -97,7 +105,8 @@ private: friend class object_definition; std::string name_; - std::shared_ptr object_{}; + // std::shared_ptr object_{}; + std::string table_name_; attribute_options options_; utils::basic_type type_{utils::basic_type::type_null}; @@ -158,7 +167,7 @@ template < typename Type > [[maybe_unused]] attribute_definition 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, ref_table_name, utils::data_type_traits::type(0), utils::constraints::FOREIGN_KEY), + 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 }; } diff --git a/include/matador/object/attribute_definition_generator.hpp b/include/matador/object/attribute_definition_generator.hpp index 756f716..30b24d4 100644 --- a/include/matador/object/attribute_definition_generator.hpp +++ b/include/matador/object/attribute_definition_generator.hpp @@ -138,9 +138,8 @@ 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_type::NOT_NULL); +void attribute_definition_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr) { + columns_.emplace_back(id, utils::data_type_traits::type(attr.size()), attr, null_option_type::NOT_NULL); } template diff --git a/include/matador/object/basic_object_info.hpp b/include/matador/object/basic_object_info.hpp index 7d657e4..1060c8f 100644 --- a/include/matador/object/basic_object_info.hpp +++ b/include/matador/object/basic_object_info.hpp @@ -24,7 +24,7 @@ public: [[nodiscard]] std::type_index type_index() const; [[nodiscard]] std::string name() const; - [[nodiscard]] std::shared_ptr definition() const; + [[nodiscard]] const std::vector& attributes() const; [[nodiscard]] std::shared_ptr reference_column() const; [[nodiscard]] bool has_primary_key() const; @@ -49,15 +49,16 @@ public: [[nodiscard]] bool endpoints_empty() const; protected: - basic_object_info(std::shared_ptr node, utils::identifier &&pk, const std::shared_ptr &pk_column, const std::shared_ptr &definition); - basic_object_info(std::shared_ptr node, utils::identifier &&pk, const std::shared_ptr &pk_column); - basic_object_info(std::shared_ptr node, const std::shared_ptr &definition); + // basic_object_info(std::shared_ptr node, utils::identifier &&pk, const std::shared_ptr &pk_column, const std::shared_ptr &definition); + basic_object_info(std::shared_ptr node, const std::vector &attributes, utils::identifier &&pk, const std::shared_ptr &pk_as_fk_column); + basic_object_info(std::shared_ptr node, const std::vector &attributes); protected: std::shared_ptr node_; /**< prototype node of the represented object type */ - std::shared_ptr definition_; + std::vector attributes_; + // std::shared_ptr definition_; std::optional identifier_; - std::shared_ptr pk_column_; + std::shared_ptr pk_as_fk_column_; t_endpoint_map relation_endpoints_; }; diff --git a/include/matador/object/object_info.hpp b/include/matador/object/object_info.hpp index 13e3f7b..0993d13 100644 --- a/include/matador/object/object_info.hpp +++ b/include/matador/object/object_info.hpp @@ -2,7 +2,7 @@ #define OBJECT_INFO_HPP #include "matador/object/basic_object_info.hpp" -#include "matador/object/object_definition.hpp" +// #include "matador/object/object_definition.hpp" namespace matador::object { class repository_node; @@ -12,32 +12,37 @@ class object_info final : public basic_object_info { public: using create_func = std::function()>; + // object_info(const std::shared_ptr& node, + // const std::shared_ptr &ref_column) + // : basic_object_info(node, {}, ref_column, {}) + // , creator_([]{return std::make_unique(); }){ + // } + // object_info(const std::shared_ptr& node, + // utils::identifier &&pk, + // const std::shared_ptr &ref_column, + // object_definition &&definition) + // : basic_object_info(node, std::move(pk), ref_column, std::move(definition)) + // , creator_([]{return std::make_unique(); }){ + // } object_info(const std::shared_ptr& node, - const std::shared_ptr &ref_column) - : basic_object_info(node, {}, ref_column, {}) - , creator_([]{return std::make_unique(); }){ - } - object_info(const std::shared_ptr& node, + const std::vector &attributes, utils::identifier &&pk, const std::shared_ptr &ref_column, - object_definition &&definition) - : basic_object_info(node, std::move(pk), ref_column, std::move(definition)) - , creator_([]{return std::make_unique(); }){ - } - object_info(const std::shared_ptr& node, - utils::identifier &&pk, - const std::shared_ptr &ref_column, - object_definition &&definition, + // object_definition &&definition, create_func&& creator) - : basic_object_info(node, std::move(pk), ref_column, std::move(definition)) + : basic_object_info(node, attributes, std::move(pk), ref_column/*, std::move(definition)*/) , creator_(std::move(creator)){ } object_info(const std::shared_ptr& node, - object_definition &&definition, + const std::vector &attributes, + // object_definition &&definition, create_func&& creator) - : basic_object_info(node, std::move(definition)) + : basic_object_info(node, attributes) , creator_(std::move(creator)){ } + explicit object_info(const std::shared_ptr& node) + : basic_object_info(node, {}) { + } const Type &prototype() const { return prototype_; } std::unique_ptr create() const { return creator_(); } diff --git a/include/matador/object/repository_node.hpp b/include/matador/object/repository_node.hpp index 3bb0d2e..a6e5cb8 100644 --- a/include/matador/object/repository_node.hpp +++ b/include/matador/object/repository_node.hpp @@ -21,18 +21,19 @@ public: auto node = std::shared_ptr(new repository_node(repo, name, typeid(Type))); primary_key_resolver resolver; - auto obj = std::make_shared(name); + // auto obj = std::make_shared(name); auto pk_info = resolver.resolve(); 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)); - } + // for (auto&& attr : attributes) { + // obj->append(std::move(attr)); + // } auto info = std::make_unique>( node, + attributes, std::move(pk_info.pk), ref_column, - obj, + // obj, []{ return std::make_unique(); } ); node->info_ = std::move(info); @@ -50,7 +51,7 @@ public: auto obj = creator(); auto info = std::make_unique>( result.value(), - std::make_shared(attribute_definition_generator::generate(*obj, repo)), + attribute_definition_generator::generate(*obj, repo), std::move(creator) ); result.value()->info_ = std::move(info); @@ -94,7 +95,7 @@ private: 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::shared_ptr& obj, + const std::string& table_name, const primary_key_info& pk_info, repository& repo); diff --git a/include/matador/orm/session.hpp b/include/matador/orm/session.hpp index 28f8e35..6eceeef 100644 --- a/include/matador/orm/session.hpp +++ b/include/matador/orm/session.hpp @@ -151,7 +151,7 @@ private: const sql::dialect &dialect_; std::unique_ptr schema_; - mutable std::unordered_map prototypes_; + mutable std::unordered_map> prototypes_; }; template diff --git a/include/matador/orm/session_query_builder.hpp b/include/matador/orm/session_query_builder.hpp index 6e44243..c1b1258 100644 --- a/include/matador/orm/session_query_builder.hpp +++ b/include/matador/orm/session_query_builder.hpp @@ -129,13 +129,12 @@ public: access::process(*this , obj); table_info_stack_.pop(); - auto pk = info->get().definition().primary_key(); - if (!pk) { + if (!info->get().has_primary_key()) { throw query_builder_exception{query_build_error::MissingPrimaryKey}; } append_join( - query::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()}, + query::column{table_info_stack_.top().table, table_info_stack_.top().info.get().reference_column()->name()}, query::column{next->second, join_column} ); } @@ -166,18 +165,17 @@ public: access::process(*this , obj); table_info_stack_.pop(); - auto pk = info->get().definition().primary_key(); - if (!pk) { + if (!info->get().has_primary_key()) { throw query_builder_exception{query_build_error::MissingPrimaryKey}; } append_join( - query::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()}, + query::column{table_info_stack_.top().table, table_info_stack_.top().info.get().reference_column()->name()}, query::column{relation->second, join_column} ); append_join( query::column{relation->second, inverse_join_column}, - query::column{next->second, pk->name()} + query::column{next->second, info->get().reference_column()->name()} ); } @@ -206,20 +204,19 @@ public: access::process(*this , obj); table_info_stack_.pop(); - auto pk = info->get().definition().primary_key(); - if (!pk) { + if (!info->get().has_primary_key()) { throw query_builder_exception{query_build_error::MissingPrimaryKey}; } const auto join_columns = join_columns_collector_.collect(); append_join( - query::column{table_info_stack_.top().table, table_info_stack_.top().info.get().definition().primary_key()->name()}, + query::column{table_info_stack_.top().table, table_info_stack_.top().info.get().reference_column()->name()}, query::column{relation->second, join_columns.inverse_join_column} ); append_join( query::column{relation->second, join_columns.join_column}, - query::column{next->second, pk->name()} + query::column{next->second, info->get().reference_column()->name()} ); } @@ -253,8 +250,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u if (!info) { throw query_builder_exception{query_build_error::UnknownType}; } - auto pk = info->get().definition().primary_key(); - if (!pk) { + if (!info->get().has_primary_key()) { throw query_builder_exception{query_build_error::MissingPrimaryKey}; } @@ -273,7 +269,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u append_join( query::column{table_info_stack_.top().table, id}, - query::column{next->second, pk->name()} + query::column{next->second, info->get().reference_column()->name()} ); } else { push(id); @@ -282,7 +278,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u // create select query auto result = matador::query::query::select(generator::columns(schema_, generator::column_generator_options::ForceLazy)) .from(*foreign_table) - .where(column(foreign_table, pk->name(), "") == _) + .where(column(foreign_table, info->get().reference_column()->name(), "") == _) .prepare(executor_); if (!result) { throw query_builder_exception(query_build_error::QueryError, result.release_error()); diff --git a/source/core/object/attribute_definition.cpp b/source/core/object/attribute_definition.cpp index cae01a9..dec49ad 100644 --- a/source/core/object/attribute_definition.cpp +++ b/source/core/object/attribute_definition.cpp @@ -7,11 +7,11 @@ namespace matador::object { attribute_definition::attribute_definition(const char *name) -: attribute_definition(name, utils::basic_type::type_null, {utils::null_attributes}) { +: attribute_definition(name, utils::basic_type::type_null, "", {utils::null_attributes}) { } attribute_definition::attribute_definition(std::string name) -: attribute_definition(std::move(name), utils::basic_type::type_null, {utils::null_attributes}) { +: attribute_definition(std::move(name), utils::basic_type::type_null, "", {utils::null_attributes}) { } attribute_definition::attribute_definition(std::string name, @@ -19,7 +19,7 @@ attribute_definition::attribute_definition(std::string name, const utils::field_attributes &attr, const null_option_type null_opt, const int index) -: attribute_definition(std::move(name), type, {attr, null_opt, index}) { +: attribute_definition(std::move(name), type, "", {attr, null_opt, index}) { } attribute_definition::attribute_definition(std::string name, @@ -28,16 +28,20 @@ attribute_definition::attribute_definition(std::string name, const std::shared_ptr &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(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 ) ) -, object_( obj ) +attribute_definition::attribute_definition(std::string name, + const utils::basic_type type, + std::string table_name, + const attribute_options& options, + const std::shared_ptr& ref_column) +: name_(std::move(name)) +, table_name_(std::move(table_name)) , options_( options ) , type_( type ) , reference_column_( ref_column ) { @@ -52,17 +56,17 @@ void attribute_definition::name( const std::string& n ) { } std::string attribute_definition::full_name() const { - return object_ ? object_->name() + "." + name_ : name_; -} - -std::shared_ptr attribute_definition::object() const { - return object_; -} - -void attribute_definition::object(const std::shared_ptr& obj) { - object_ = obj; + return !table_name_.empty() ? table_name_ + "." + name_ : name_; } +// std::shared_ptr attribute_definition::object() const { +// return object_; +// } +// +// void attribute_definition::object(const std::shared_ptr& obj) { +// object_ = obj; +// } +// int attribute_definition::index() const { return options_.index; } @@ -83,9 +87,17 @@ utils::basic_type attribute_definition::type() const { return type_; } +const std::string& attribute_definition::table_name() const { + return table_name_; +} + +void attribute_definition::table_name( const std::string& name ) { + table_name_ = name; +} + void attribute_definition::change_type(const utils::basic_type type, const utils::field_attributes& attr) { - options_.attributes = attr; - type_ = type; + options_.attributes = attr; + type_ = type; } std::shared_ptr attribute_definition::reference_column() const { @@ -160,14 +172,14 @@ 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, std::make_shared(ref_table_name), utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY), + 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 }; } template<> attribute_definition 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, ref_table_name, utils::basic_type::type_varchar, 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::FOREIGN_KEY}); return { name, utils::data_type_traits::type(size), 0, ref_column, {size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL diff --git a/source/core/object/basic_object_info.cpp b/source/core/object/basic_object_info.cpp index 601914f..4534df0 100644 --- a/source/core/object/basic_object_info.cpp +++ b/source/core/object/basic_object_info.cpp @@ -5,28 +5,30 @@ #include namespace matador::object { +// basic_object_info::basic_object_info(std::shared_ptr node, +// utils::identifier &&pk, +// const std::shared_ptr &pk_column, +// const std::shared_ptr &definition) +// : node_(std::move(node)) +// , definition_(definition) +// , identifier_(std::move(pk)) +// , pk_column_(pk_column) { +// } + basic_object_info::basic_object_info(std::shared_ptr node, + const std::vector &attributes, utils::identifier &&pk, - const std::shared_ptr &pk_column, - const std::shared_ptr &definition) + const std::shared_ptr &pk_as_fk_column) : node_(std::move(node)) -, definition_(definition) +, attributes_(attributes) , identifier_(std::move(pk)) -, pk_column_(pk_column) { +, pk_as_fk_column_(pk_as_fk_column) { } basic_object_info::basic_object_info(std::shared_ptr node, - utils::identifier &&pk, - const std::shared_ptr &pk_column) + const std::vector &attributes) : node_(std::move(node)) -, identifier_(std::move(pk)) -, pk_column_(pk_column) { -} - -basic_object_info::basic_object_info(std::shared_ptr node, - const std::shared_ptr &definition) -: node_(std::move(node)) -, definition_(definition) {} +, attributes_(attributes) {} std::type_index basic_object_info::type_index() const { return node_->type_index(); @@ -36,12 +38,12 @@ std::string basic_object_info::name() const { return node_->name(); } -std::shared_ptr basic_object_info::definition() const { - return definition_; +const std::vector& basic_object_info::attributes() const { + return attributes_; } std::shared_ptr basic_object_info::reference_column() const { - return pk_column_; + return pk_as_fk_column_; } bool basic_object_info::has_primary_key() const { diff --git a/source/core/object/repository_node.cpp b/source/core/object/repository_node.cpp index 1bcef2f..684a44a 100644 --- a/source/core/object/repository_node.cpp +++ b/source/core/object/repository_node.cpp @@ -4,17 +4,17 @@ #include "matador/object/repository_node.hpp" namespace matador::object { -repository_node::repository_node(object::repository &repo) +repository_node::repository_node(repository &repo) : repo_(repo) , type_index_(typeid(detail::null_type)){ } -repository_node::repository_node(object::repository &repo, const std::type_index& ti) +repository_node::repository_node(repository &repo, const std::type_index& ti) : repo_(repo) , type_index_(ti) { } -repository_node::repository_node(object::repository &repo, std::string name, const std::type_index& ti) +repository_node::repository_node(repository &repo, std::string name, const std::type_index& ti) : repo_(repo) , type_index_(ti) , first_child_(std::shared_ptr(new repository_node(repo))) @@ -24,9 +24,9 @@ repository_node::repository_node(object::repository &repo, std::string name, con last_child_->previous_sibling_ = first_child_; } -std::shared_ptr repository_node::make_null_node(object::repository &repo) { +std::shared_ptr repository_node::make_null_node(repository &repo) { auto node = std::shared_ptr(new repository_node(repo)); - node->info_ = std::make_unique(node, std::shared_ptr{}); + node->info_ = std::make_unique(node/*, std::shared_ptr{}*/); return node; } @@ -45,8 +45,8 @@ const basic_object_info &repository_node::info() const { void repository_node::update_name(const std::string& name) { name_ = name; - if (info_->reference_column() && info_->reference_column()->object() != nullptr) { - info_->reference_column()->object()->name_ = name; + if (info_->reference_column()) { + info_->reference_column()->table_name(name); } } @@ -104,20 +104,23 @@ utils::result repository_node::make_and return repo.attach_node(node, ""); } -std::shared_ptr repository_node::determine_reference_column(const std::type_index& ti, const std::shared_ptr& obj, const primary_key_info& pk_info, repository& repo) { +std::shared_ptr repository_node::determine_reference_column(const std::type_index& ti, + const std::string& table_name, + 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}); + return std::make_shared(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::FOREIGN_KEY}); } auto ref_column = it->second; repo.missing_references_.erase(it); ref_column->name(pk_info.pk_column_name); - ref_column->object(obj); + ref_column->table_name(table_name); ref_column->change_type(pk_info.type); ref_column->attributes() = utils::constraints::FOREIGN_KEY; - if (obj->name().empty()) { + if (table_name.empty()) { repo.missing_references_.insert({ti, ref_column}); } diff --git a/source/orm/orm/schema.cpp b/source/orm/orm/schema.cpp index b100234..4a5a567 100644 --- a/source/orm/orm/schema.cpp +++ b/source/orm/orm/schema.cpp @@ -57,7 +57,7 @@ matador::utils::result matador::orm::schema::create // std::cout << result.sql << std::endl; for (const auto &node: repo_) { auto ctx = query::query::create() - .table(node->name(), node->info().definition()->columns()) + .table(node->name(), node->info().attributes()) .compile(*c); for ( const auto& [sql, command] : ctx.additional_commands ) { diff --git a/source/orm/orm/session.cpp b/source/orm/orm/session.cpp index d1d7fa2..9b614da 100644 --- a/source/orm/orm/session.cpp +++ b/source/orm/orm/session.cpp @@ -52,7 +52,7 @@ utils::result session::create_schema() const { auto c = cache_.pool().acquire(); for (const auto &node: *schema_) { auto ctx = query::query::create() - .table(node->name(), node->info().definition()->columns()) + .table(node->name(), node->info().attributes()) .compile(*c); for ( const auto& [sql, command] : ctx.additional_commands ) { @@ -100,7 +100,9 @@ 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()) { + if (const auto rit = std::find_if(it->second.begin(), it->second.end(), [&col](const auto &value) { + return value.name() == col.name(); + }); rit != it->second.end()) { const_cast(col).change_type(rit->type()); } } diff --git a/source/orm/query/query_compiler.cpp b/source/orm/query/query_compiler.cpp index c24edc3..e01f010 100644 --- a/source/orm/query/query_compiler.cpp +++ b/source/orm/query/query_compiler.cpp @@ -138,14 +138,14 @@ void query_compiler::visit(internal::query_select_part &part) } void query_compiler::visit(internal::query_from_part &part) { - query_.table_name = part.table().alias(); - query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name); + query_.table_name = part.table().name(); + query_.sql += " " + build_table_name(part.token(), *dialect_, part.table()); query_.table_aliases.insert({query_.table_name, part.table().alias()}); } void query_compiler::visit(internal::query_join_part &part) { - query_.sql += " " + query_compiler::build_table_name(part.token(), *dialect_, part.table()); + query_.sql += " " + build_table_name(part.token(), *dialect_, part.table()); } void query_compiler::visit(internal::query_on_part &part) { @@ -335,7 +335,7 @@ void query_compiler::visit(internal::query_create_table_part &part) fk_cmd += " CONSTRAINT FK_" + query_.table_name; fk_cmd += "_" + column; fk_cmd += " FOREIGN KEY (" + dialect_->prepare_identifier_string(column) + ")"; - fk_cmd += " REFERENCES " + reference_column->object()->name() + "(" + reference_column->name() + ")"; + fk_cmd += " REFERENCES " + reference_column->table_name() + "(" + reference_column->name() + ")"; query_.additional_commands.push_back({fk_cmd, sql::sql_command::SQL_ALTER_TABLE}); } diff --git a/source/orm/sql/dialect.cpp b/source/orm/sql/dialect.cpp index fa9eebf..65dc728 100644 --- a/source/orm/sql/dialect.cpp +++ b/source/orm/sql/dialect.cpp @@ -3,6 +3,7 @@ #include "matador/sql/interface/connection_impl.hpp" #include "matador/utils/string.hpp" +#include "matador/utils/value.hpp" namespace matador::sql { diff --git a/test/backends/QueryStatementTests.cpp b/test/backends/QueryStatementTests.cpp index ce2320f..66d5289 100644 --- a/test/backends/QueryStatementTests.cpp +++ b/test/backends/QueryStatementTests.cpp @@ -21,7 +21,7 @@ using namespace matador::test; TEST_CASE_METHOD(QueryFixture, "Test create statement", "[query][statement][create]") { REQUIRE(repo.attach("person")); auto stmt = query::create() - .table("person", repo) + .table("person"_tab, repo) .prepare(db); REQUIRE(stmt); diff --git a/test/orm/sql/ColumnTest.cpp b/test/orm/sql/ColumnTest.cpp index f9af913..a7cdbfb 100644 --- a/test/orm/sql/ColumnTest.cpp +++ b/test/orm/sql/ColumnTest.cpp @@ -26,7 +26,7 @@ TEST_CASE("Test copy and move column", "[column]") { "name", basic_type::type_varchar, 2, - std::make_shared("author", "books", basic_type::type_uint32, constraints::FOREIGN_KEY), + std::make_shared("author", basic_type::type_uint32, "books", attribute_options{constraints::FOREIGN_KEY}), {255, constraints::FOREIGN_KEY}, null_option_type::NOT_NULL ); @@ -34,7 +34,7 @@ TEST_CASE("Test copy and move column", "[column]") { REQUIRE(c.index() == 2); REQUIRE(c.reference_column()); REQUIRE(c.reference_column()->name() == "author"); - REQUIRE(c.reference_column()->object()->name() == "books"); + REQUIRE(c.reference_column()->table_name() == "books"); REQUIRE(c.type() == basic_type::type_varchar); REQUIRE(c.attributes().size() == 255); @@ -43,7 +43,7 @@ TEST_CASE("Test copy and move column", "[column]") { REQUIRE(c2.index() == 2); REQUIRE(c2.reference_column()); REQUIRE(c2.reference_column()->name() == "author"); - REQUIRE(c2.reference_column()->object()->name() == "books"); + REQUIRE(c2.reference_column()->table_name() == "books"); REQUIRE(c2.type() == basic_type::type_varchar); REQUIRE(c2.attributes().size() == 255); @@ -52,7 +52,7 @@ TEST_CASE("Test copy and move column", "[column]") { REQUIRE(c3.index() == 2); REQUIRE(c3.reference_column()); REQUIRE(c3.reference_column()->name() == "author"); - REQUIRE(c3.reference_column()->object()->name() == "books"); + REQUIRE(c3.reference_column()->table_name() == "books"); REQUIRE(c3.type() == basic_type::type_varchar); REQUIRE(c3.attributes().size() == 255);