From 9eec5b64fb1dd6a7f9791c5065c3353dab6c4788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Tue, 16 Dec 2025 16:21:09 +0100 Subject: [PATCH] fixed compilation --- include/matador/orm/session_query_builder.hpp | 2 +- include/matador/query/column.hpp | 3 + include/matador/query/join_data.hpp | 2 +- source/orm/orm/session.cpp | 2 +- source/orm/orm/session_query_builder.cpp | 2 +- source/orm/query/column.cpp | 13 ++- source/orm/query/generator.cpp | 8 +- source/orm/query/query_compiler.cpp | 2 +- test/orm/orm/SessionQueryBuilderTest.cpp | 96 +++++++++---------- test/orm/sql/ColumnGeneratorTest.cpp | 56 +++++------ 10 files changed, 98 insertions(+), 88 deletions(-) diff --git a/include/matador/orm/session_query_builder.hpp b/include/matador/orm/session_query_builder.hpp index f0ae1cf..184ed1d 100644 --- a/include/matador/orm/session_query_builder.hpp +++ b/include/matador/orm/session_query_builder.hpp @@ -281,7 +281,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, info->get().primary_key_attribute()->name(), "") == _) + .where(column(foreign_table.get(), info->get().primary_key_attribute()->name(), "") == _) .prepare(executor_); if (!result) { throw query_builder_exception(query_build_error::QueryError, result.release_error()); diff --git a/include/matador/query/column.hpp b/include/matador/query/column.hpp index e8ebb36..2408cd5 100644 --- a/include/matador/query/column.hpp +++ b/include/matador/query/column.hpp @@ -41,6 +41,9 @@ public: // ReSharper disable once CppNonExplicitConversionOperator operator const std::string&() const; // NOLINT(*-explicit-constructor) +private: + static query::table* default_table(); + private: const query::table* table_{nullptr}; std::string name_; diff --git a/include/matador/query/join_data.hpp b/include/matador/query/join_data.hpp index 9c51344..4ae918e 100644 --- a/include/matador/query/join_data.hpp +++ b/include/matador/query/join_data.hpp @@ -10,7 +10,7 @@ namespace matador::query { struct join_data { - table* join_table{nullptr}; + const table* join_table{nullptr}; std::unique_ptr condition; }; diff --git a/source/orm/orm/session.cpp b/source/orm/orm/session.cpp index 3ebbcfe..b85bdd7 100644 --- a/source/orm/orm/session.cpp +++ b/source/orm/orm/session.cpp @@ -227,7 +227,7 @@ query::fetchable_query session::build_select_query(entity_query_data &&data) { .from(*data.root_table) .join_left(data.joins) .where(std::move(data.where_clause)) - .order_by({data.root_table, data.pk_column_name}) + .order_by({data.root_table.get(), data.pk_column_name}) .asc(); } } diff --git a/source/orm/orm/session_query_builder.cpp b/source/orm/orm/session_query_builder.cpp index 6096e79..90b52db 100644 --- a/source/orm/orm/session_query_builder.cpp +++ b/source/orm/orm/session_query_builder.cpp @@ -73,7 +73,7 @@ std::string session_query_builder::build_alias(const char prefix, const unsigned void session_query_builder::append_join(const query::column &left, const query::column &right) { using namespace matador::query; entity_query_data_.joins.push_back({ - {right.table()}, + right.table(), std::make_unique(left, binary_operator::EQUALS, right) }); } diff --git a/source/orm/query/column.cpp b/source/orm/query/column.cpp index c1c11f3..676ed19 100644 --- a/source/orm/query/column.cpp +++ b/source/orm/query/column.cpp @@ -26,11 +26,13 @@ column::column(const char *name, const std::string& as) {} column::column(std::string name, std::string as) -: name_(std::move(name)) +: table_(default_table()) +, name_(std::move(name)) , alias_(std::move(as)) {} column::column(const sql::sql_function_t func, std::string name) -: name_(std::move(name)) +: table_(default_table()) +, name_(std::move(name)) , function_(func) {} column::column(const class table* tab, std::string name, std::string as) @@ -95,4 +97,9 @@ column::operator const std::string&() const { return name_; } -} \ No newline at end of file +query::table* column::default_table() { + static query::table default_table; + + return &default_table; +} +} diff --git a/source/orm/query/generator.cpp b/source/orm/query/generator.cpp index 949d174..58b276d 100644 --- a/source/orm/query/generator.cpp +++ b/source/orm/query/generator.cpp @@ -1,7 +1,7 @@ #include "matador/query/generator.hpp" namespace matador::query::generator { -column_generator::column_generator( const std::string& table_name, const column_generator_options options) +column_generator::column_generator(const std::string& table_name, const column_generator_options options) : options_(options) { table_stack_.push(table_name.empty() ? std::make_shared() : std::make_shared
(table_name)); } @@ -12,7 +12,7 @@ column_generator::column_generator(const object::repository& repo, const std::st table_stack_.push(table_name.empty() ? std::make_shared
() : std::make_shared
(table_name)); } -void column_generator::on_revision( const char* id, uint64_t& ) { +void column_generator::on_revision(const char* id, uint64_t&) { push(id); } @@ -20,9 +20,9 @@ void column_generator::push( const std::string& column_name ) { if (is_column_generator_option_set(options_, column_generator_options::GenerateAlias)) { char str[4]; snprintf(str, 4, "c%02d", ++column_index); - result_.emplace_back(table_stack_.top(), column_name, str); + result_.emplace_back(table_stack_.top().get(), column_name, str); } else { - result_.emplace_back(table_stack_.top(), column_name); + result_.emplace_back(table_stack_.top().get(), column_name); } } diff --git a/source/orm/query/query_compiler.cpp b/source/orm/query/query_compiler.cpp index 1b14fc8..28b2783 100644 --- a/source/orm/query/query_compiler.cpp +++ b/source/orm/query/query_compiler.cpp @@ -449,7 +449,7 @@ std::string query_compiler::build_add_constraint_string(const class constraint & } std::string query_compiler::build_drop_constraint_string(const class constraint &c) { - + return dialect_->drop_constraint() + " " + build_constraint_name(c); } std::string query_compiler::build_constraint_name(const class constraint &c) { diff --git a/test/orm/orm/SessionQueryBuilderTest.cpp b/test/orm/orm/SessionQueryBuilderTest.cpp index 44bcae1..4be12e8 100644 --- a/test/orm/orm/SessionQueryBuilderTest.cpp +++ b/test/orm/orm/SessionQueryBuilderTest.cpp @@ -46,14 +46,14 @@ TEST_CASE("Create sql query data for entity with eager has one", "[query][entity REQUIRE(data.is_ok()); REQUIRE(data->root_table->name() == "flights"); REQUIRE(data->joins.size() == 1); - const auto flights = std::make_shared
("flights"); - const auto airplanes = std::make_shared
("airplanes"); + const auto flights = table("flights"); + const auto airplanes = table("airplanes"); const std::vector expected_columns { - { flights, "id", "c01" }, - { airplanes, "id", "c02" }, - { airplanes, "brand", "c03" }, - { airplanes, "model", "c04" }, - { flights, "pilot_name", "c05" }, + { &flights, "id", "c01" }, + { &airplanes, "id", "c02" }, + { &airplanes, "brand", "c03" }, + { &airplanes, "model", "c04" }, + { &flights, "pilot_name", "c05" }, }; REQUIRE(data->columns.size() == expected_columns.size()); for (size_t i = 0; i != expected_columns.size(); ++i) { @@ -94,18 +94,18 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent REQUIRE(data.is_ok()); REQUIRE(data->root_table->name() == "books"); REQUIRE(data->joins.size() == 1); - const auto books = std::make_shared
("books"); - const auto authors = std::make_shared
("authors"); + const auto books = table("books"); + const auto authors = table("authors"); const std::vector expected_columns { - { books, "id", "c01" }, - { books, "title", "c02" }, - { authors, "id", "c03" }, - { authors, "first_name", "c04" }, - { authors, "last_name", "c05" }, - { authors, "date_of_birth", "c06" }, - { authors, "year_of_birth", "c07" }, - { authors, "distinguished", "c08" }, - { books, "published_in", "c09" } + { &books, "id", "c01" }, + { &books, "title", "c02" }, + { &authors, "id", "c03" }, + { &authors, "first_name", "c04" }, + { &authors, "last_name", "c05" }, + { &authors, "date_of_birth", "c06" }, + { &authors, "year_of_birth", "c07" }, + { &authors, "distinguished", "c08" }, + { &books, "published_in", "c09" } }; REQUIRE(data->columns.size() == expected_columns.size()); for (size_t i = 0; i != expected_columns.size(); ++i) { @@ -160,24 +160,24 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q REQUIRE(data.is_ok()); REQUIRE(data->root_table->name() == "orders"); REQUIRE(data->joins.size() == 1); - const auto orders = std::make_shared
("orders"); - const auto order_details = std::make_shared
("order_details"); + const auto orders = table("orders"); + const auto order_details = table("order_details"); const std::vector expected_columns = { - { orders, "order_id", "c01" }, - { orders, "order_date", "c02" }, - { orders, "required_date", "c03" }, - { orders, "shipped_date", "c04" }, - { orders, "ship_via", "c05" }, - { orders, "freight", "c06" }, - { orders, "ship_name", "c07" }, - { orders, "ship_address", "c08" }, - { orders, "ship_city", "c09" }, - { orders, "ship_region", "c10" }, - { orders, "ship_postal_code", "c11" }, - { orders, "ship_country", "c12" }, - { order_details, "order_details_id", "c13" }, - { order_details, "order_id", "c14" }, - { order_details, "product_id", "c15" } + { &orders, "order_id", "c01" }, + { &orders, "order_date", "c02" }, + { &orders, "required_date", "c03" }, + { &orders, "shipped_date", "c04" }, + { &orders, "ship_via", "c05" }, + { &orders, "freight", "c06" }, + { &orders, "ship_name", "c07" }, + { &orders, "ship_address", "c08" }, + { &orders, "ship_city", "c09" }, + { &orders, "ship_region", "c10" }, + { &orders, "ship_postal_code", "c11" }, + { &orders, "ship_country", "c12" }, + { &order_details, "order_details_id", "c13" }, + { &order_details, "order_id", "c14" }, + { &order_details, "product_id", "c15" } }; REQUIRE(data->columns.size() == expected_columns.size()); for (size_t i = 0; i != expected_columns.size(); ++i) { @@ -218,13 +218,13 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e REQUIRE(data.is_ok()); REQUIRE(data->root_table->name() == "ingredients"); REQUIRE(data->joins.size() == 2); - const auto ingredients = std::make_shared
("ingredients"); - const auto recipes = std::make_shared
("recipes"); + const auto ingredients = table("ingredients"); + const auto recipes = table("recipes"); const std::vector expected_columns { - { ingredients, "id", "c01" }, - { ingredients, "name", "c02" }, - { recipes, "id", "c03" }, - { recipes, "name", "c04" } + { &ingredients, "id", "c01" }, + { &ingredients, "name", "c02" }, + { &recipes, "id", "c03" }, + { &recipes, "name", "c04" } }; REQUIRE(data->columns.size() == expected_columns.size()); for (size_t i = 0; i != expected_columns.size(); ++i) { @@ -266,13 +266,13 @@ TEST_CASE("Create sql query data for entity with eager many to many (inverse par REQUIRE(data.is_ok()); REQUIRE(data->root_table->name() == "courses"); REQUIRE(data->joins.size() == 2); - const auto courses = std::make_shared
("courses"); - const auto students = std::make_shared
("students"); + const auto courses = table("courses"); + const auto students = table("students"); const std::vector expected_columns { - { courses, "id", "c01" }, - { courses, "title", "c02" }, - { students, "id", "c03" }, - { students, "name", "c04" } + { &courses, "id", "c01" }, + { &courses, "title", "c02" }, + { &students, "id", "c03" }, + { &students, "name", "c04" } }; REQUIRE(data->columns.size() == expected_columns.size()); for (size_t i = 0; i != expected_columns.size(); ++i) { @@ -315,7 +315,7 @@ TEST_CASE("Test eager relationship", "[session][eager]") { .from(*data->root_table) .join_left(data->joins) .where(std::move(data->where_clause)) - .order_by(column{data->root_table, data->pk_column_name}) + .order_by(column{data->root_table.get(), data->pk_column_name}) .asc() .str(db); diff --git a/test/orm/sql/ColumnGeneratorTest.cpp b/test/orm/sql/ColumnGeneratorTest.cpp index 02ac990..5c0cd99 100644 --- a/test/orm/sql/ColumnGeneratorTest.cpp +++ b/test/orm/sql/ColumnGeneratorTest.cpp @@ -52,24 +52,24 @@ TEST_CASE("Generate columns for object with has many relation", "[column][genera auto columns = generator::columns(s, generator::column_generator_options::GenerateAlias); - const auto order_table = std::make_shared
("order"); - const auto order_details_table = std::make_shared
("order_details"); + const auto order_table = table("order"); + const auto order_details_table = table("order_details"); const std::vector expected_columns = { - { order_table, "order_id", "c01" }, - { order_table, "order_date", "c02" }, - { order_table, "required_date", "c03" }, - { order_table, "shipped_date", "c04" }, - { order_table, "ship_via", "c05" }, - { order_table, "freight", "c06" }, - { order_table, "ship_name", "c07" }, - { order_table, "ship_address", "c08" }, - { order_table, "ship_city", "c09" }, - { order_table, "ship_region", "c10" }, - { order_table, "ship_postal_code", "c11" }, - { order_table, "ship_country", "c12" }, - { order_details_table, "order_details_id", "c13" }, - { order_details_table, "order_id", "c14" }, - { order_details_table, "product_id", "c15" } + { &order_table, "order_id", "c01" }, + { &order_table, "order_date", "c02" }, + { &order_table, "required_date", "c03" }, + { &order_table, "shipped_date", "c04" }, + { &order_table, "ship_via", "c05" }, + { &order_table, "freight", "c06" }, + { &order_table, "ship_name", "c07" }, + { &order_table, "ship_address", "c08" }, + { &order_table, "ship_city", "c09" }, + { &order_table, "ship_region", "c10" }, + { &order_table, "ship_postal_code", "c11" }, + { &order_table, "ship_country", "c12" }, + { &order_details_table, "order_details_id", "c13" }, + { &order_details_table, "order_id", "c14" }, + { &order_details_table, "product_id", "c15" } }; REQUIRE(!columns.empty()); REQUIRE(columns.size() == expected_columns.size()); @@ -87,18 +87,18 @@ TEST_CASE("Generate columns for object with eager foreign key relation", "[colum .and_then( [&s] { return s.attach("authors"); } ); REQUIRE(result); - const auto books_table = std::make_shared
("books"); - const auto authors_table = std::make_shared
("authors"); + const auto books_table = table("books"); + const auto authors_table = table("authors"); const std::vector expected_columns { - { books_table, "id", "c01" }, - { books_table, "title", "c02" }, - { authors_table, "id", "c03" }, - { authors_table, "first_name", "c04" }, - { authors_table, "last_name", "c05" }, - { authors_table, "date_of_birth", "c06" }, - { authors_table, "year_of_birth", "c07" }, - { authors_table, "distinguished", "c08" }, - { books_table, "published_in", "c09" } + { &books_table, "id", "c01" }, + { &books_table, "title", "c02" }, + { &authors_table, "id", "c03" }, + { &authors_table, "first_name", "c04" }, + { &authors_table, "last_name", "c05" }, + { &authors_table, "date_of_birth", "c06" }, + { &authors_table, "year_of_birth", "c07" }, + { &authors_table, "distinguished", "c08" }, + { &books_table, "published_in", "c09" } }; auto columns = generator::columns(s, generator::column_generator_options::GenerateAlias);