fixed compilation

This commit is contained in:
Sascha Kühl 2025-12-16 16:21:09 +01:00
parent 4a19322ef8
commit 9eec5b64fb
10 changed files with 98 additions and 88 deletions

View File

@ -281,7 +281,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u
// create select query // create select query
auto result = matador::query::query::select(generator::columns<typename Pointer::value_type>(schema_, generator::column_generator_options::ForceLazy)) auto result = matador::query::query::select(generator::columns<typename Pointer::value_type>(schema_, generator::column_generator_options::ForceLazy))
.from(*foreign_table) .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_); .prepare(executor_);
if (!result) { if (!result) {
throw query_builder_exception(query_build_error::QueryError, result.release_error()); throw query_builder_exception(query_build_error::QueryError, result.release_error());

View File

@ -41,6 +41,9 @@ public:
// ReSharper disable once CppNonExplicitConversionOperator // ReSharper disable once CppNonExplicitConversionOperator
operator const std::string&() const; // NOLINT(*-explicit-constructor) operator const std::string&() const; // NOLINT(*-explicit-constructor)
private:
static query::table* default_table();
private: private:
const query::table* table_{nullptr}; const query::table* table_{nullptr};
std::string name_; std::string name_;

View File

@ -10,7 +10,7 @@
namespace matador::query { namespace matador::query {
struct join_data { struct join_data {
table* join_table{nullptr}; const table* join_table{nullptr};
std::unique_ptr<abstract_criteria> condition; std::unique_ptr<abstract_criteria> condition;
}; };

View File

@ -227,7 +227,7 @@ query::fetchable_query session::build_select_query(entity_query_data &&data) {
.from(*data.root_table) .from(*data.root_table)
.join_left(data.joins) .join_left(data.joins)
.where(std::move(data.where_clause)) .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(); .asc();
} }
} }

View File

@ -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) { void session_query_builder::append_join(const query::column &left, const query::column &right) {
using namespace matador::query; using namespace matador::query;
entity_query_data_.joins.push_back({ entity_query_data_.joins.push_back({
{right.table()}, right.table(),
std::make_unique<binary_column_criteria>(left, binary_operator::EQUALS, right) std::make_unique<binary_column_criteria>(left, binary_operator::EQUALS, right)
}); });
} }

View File

@ -26,11 +26,13 @@ column::column(const char *name, const std::string& as)
{} {}
column::column(std::string name, 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)) {} , alias_(std::move(as)) {}
column::column(const sql::sql_function_t func, std::string name) column::column(const sql::sql_function_t func, std::string name)
: name_(std::move(name)) : table_(default_table())
, name_(std::move(name))
, function_(func) {} , function_(func) {}
column::column(const class table* tab, std::string name, std::string as) column::column(const class table* tab, std::string name, std::string as)
@ -95,4 +97,9 @@ column::operator const std::string&() const {
return name_; return name_;
} }
} query::table* column::default_table() {
static query::table default_table;
return &default_table;
}
}

View File

@ -1,7 +1,7 @@
#include "matador/query/generator.hpp" #include "matador/query/generator.hpp"
namespace matador::query::generator { 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) { : options_(options) {
table_stack_.push(table_name.empty() ? std::make_shared<table>() : std::make_shared<table>(table_name)); table_stack_.push(table_name.empty() ? std::make_shared<table>() : std::make_shared<table>(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<table>() : std::make_shared<table>(table_name)); table_stack_.push(table_name.empty() ? std::make_shared<table>() : std::make_shared<table>(table_name));
} }
void column_generator::on_revision( const char* id, uint64_t& ) { void column_generator::on_revision(const char* id, uint64_t&) {
push(id); 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)) { if (is_column_generator_option_set(options_, column_generator_options::GenerateAlias)) {
char str[4]; char str[4];
snprintf(str, 4, "c%02d", ++column_index); 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 { } else {
result_.emplace_back(table_stack_.top(), column_name); result_.emplace_back(table_stack_.top().get(), column_name);
} }
} }

View File

@ -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) { 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) { std::string query_compiler::build_constraint_name(const class constraint &c) {

View File

@ -46,14 +46,14 @@ TEST_CASE("Create sql query data for entity with eager has one", "[query][entity
REQUIRE(data.is_ok()); REQUIRE(data.is_ok());
REQUIRE(data->root_table->name() == "flights"); REQUIRE(data->root_table->name() == "flights");
REQUIRE(data->joins.size() == 1); REQUIRE(data->joins.size() == 1);
const auto flights = std::make_shared<table>("flights"); const auto flights = table("flights");
const auto airplanes = std::make_shared<table>("airplanes"); const auto airplanes = table("airplanes");
const std::vector<column> expected_columns { const std::vector<column> expected_columns {
{ flights, "id", "c01" }, { &flights, "id", "c01" },
{ airplanes, "id", "c02" }, { &airplanes, "id", "c02" },
{ airplanes, "brand", "c03" }, { &airplanes, "brand", "c03" },
{ airplanes, "model", "c04" }, { &airplanes, "model", "c04" },
{ flights, "pilot_name", "c05" }, { &flights, "pilot_name", "c05" },
}; };
REQUIRE(data->columns.size() == expected_columns.size()); REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) { 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.is_ok());
REQUIRE(data->root_table->name() == "books"); REQUIRE(data->root_table->name() == "books");
REQUIRE(data->joins.size() == 1); REQUIRE(data->joins.size() == 1);
const auto books = std::make_shared<table>("books"); const auto books = table("books");
const auto authors = std::make_shared<table>("authors"); const auto authors = table("authors");
const std::vector<column> expected_columns { const std::vector<column> expected_columns {
{ books, "id", "c01" }, { &books, "id", "c01" },
{ books, "title", "c02" }, { &books, "title", "c02" },
{ authors, "id", "c03" }, { &authors, "id", "c03" },
{ authors, "first_name", "c04" }, { &authors, "first_name", "c04" },
{ authors, "last_name", "c05" }, { &authors, "last_name", "c05" },
{ authors, "date_of_birth", "c06" }, { &authors, "date_of_birth", "c06" },
{ authors, "year_of_birth", "c07" }, { &authors, "year_of_birth", "c07" },
{ authors, "distinguished", "c08" }, { &authors, "distinguished", "c08" },
{ books, "published_in", "c09" } { &books, "published_in", "c09" }
}; };
REQUIRE(data->columns.size() == expected_columns.size()); REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) { 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.is_ok());
REQUIRE(data->root_table->name() == "orders"); REQUIRE(data->root_table->name() == "orders");
REQUIRE(data->joins.size() == 1); REQUIRE(data->joins.size() == 1);
const auto orders = std::make_shared<table>("orders"); const auto orders = table("orders");
const auto order_details = std::make_shared<table>("order_details"); const auto order_details = table("order_details");
const std::vector<column> expected_columns = { const std::vector<column> expected_columns = {
{ orders, "order_id", "c01" }, { &orders, "order_id", "c01" },
{ orders, "order_date", "c02" }, { &orders, "order_date", "c02" },
{ orders, "required_date", "c03" }, { &orders, "required_date", "c03" },
{ orders, "shipped_date", "c04" }, { &orders, "shipped_date", "c04" },
{ orders, "ship_via", "c05" }, { &orders, "ship_via", "c05" },
{ orders, "freight", "c06" }, { &orders, "freight", "c06" },
{ orders, "ship_name", "c07" }, { &orders, "ship_name", "c07" },
{ orders, "ship_address", "c08" }, { &orders, "ship_address", "c08" },
{ orders, "ship_city", "c09" }, { &orders, "ship_city", "c09" },
{ orders, "ship_region", "c10" }, { &orders, "ship_region", "c10" },
{ orders, "ship_postal_code", "c11" }, { &orders, "ship_postal_code", "c11" },
{ orders, "ship_country", "c12" }, { &orders, "ship_country", "c12" },
{ order_details, "order_details_id", "c13" }, { &order_details, "order_details_id", "c13" },
{ order_details, "order_id", "c14" }, { &order_details, "order_id", "c14" },
{ order_details, "product_id", "c15" } { &order_details, "product_id", "c15" }
}; };
REQUIRE(data->columns.size() == expected_columns.size()); REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) { 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.is_ok());
REQUIRE(data->root_table->name() == "ingredients"); REQUIRE(data->root_table->name() == "ingredients");
REQUIRE(data->joins.size() == 2); REQUIRE(data->joins.size() == 2);
const auto ingredients = std::make_shared<table>("ingredients"); const auto ingredients = table("ingredients");
const auto recipes = std::make_shared<table>("recipes"); const auto recipes = table("recipes");
const std::vector<column> expected_columns { const std::vector<column> expected_columns {
{ ingredients, "id", "c01" }, { &ingredients, "id", "c01" },
{ ingredients, "name", "c02" }, { &ingredients, "name", "c02" },
{ recipes, "id", "c03" }, { &recipes, "id", "c03" },
{ recipes, "name", "c04" } { &recipes, "name", "c04" }
}; };
REQUIRE(data->columns.size() == expected_columns.size()); REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) { 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.is_ok());
REQUIRE(data->root_table->name() == "courses"); REQUIRE(data->root_table->name() == "courses");
REQUIRE(data->joins.size() == 2); REQUIRE(data->joins.size() == 2);
const auto courses = std::make_shared<table>("courses"); const auto courses = table("courses");
const auto students = std::make_shared<table>("students"); const auto students = table("students");
const std::vector<column> expected_columns { const std::vector<column> expected_columns {
{ courses, "id", "c01" }, { &courses, "id", "c01" },
{ courses, "title", "c02" }, { &courses, "title", "c02" },
{ students, "id", "c03" }, { &students, "id", "c03" },
{ students, "name", "c04" } { &students, "name", "c04" }
}; };
REQUIRE(data->columns.size() == expected_columns.size()); REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) { 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) .from(*data->root_table)
.join_left(data->joins) .join_left(data->joins)
.where(std::move(data->where_clause)) .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() .asc()
.str(db); .str(db);

View File

@ -52,24 +52,24 @@ TEST_CASE("Generate columns for object with has many relation", "[column][genera
auto columns = generator::columns<order>(s, generator::column_generator_options::GenerateAlias); auto columns = generator::columns<order>(s, generator::column_generator_options::GenerateAlias);
const auto order_table = std::make_shared<table>("order"); const auto order_table = table("order");
const auto order_details_table = std::make_shared<table>("order_details"); const auto order_details_table = table("order_details");
const std::vector<column> expected_columns = { const std::vector<column> expected_columns = {
{ order_table, "order_id", "c01" }, { &order_table, "order_id", "c01" },
{ order_table, "order_date", "c02" }, { &order_table, "order_date", "c02" },
{ order_table, "required_date", "c03" }, { &order_table, "required_date", "c03" },
{ order_table, "shipped_date", "c04" }, { &order_table, "shipped_date", "c04" },
{ order_table, "ship_via", "c05" }, { &order_table, "ship_via", "c05" },
{ order_table, "freight", "c06" }, { &order_table, "freight", "c06" },
{ order_table, "ship_name", "c07" }, { &order_table, "ship_name", "c07" },
{ order_table, "ship_address", "c08" }, { &order_table, "ship_address", "c08" },
{ order_table, "ship_city", "c09" }, { &order_table, "ship_city", "c09" },
{ order_table, "ship_region", "c10" }, { &order_table, "ship_region", "c10" },
{ order_table, "ship_postal_code", "c11" }, { &order_table, "ship_postal_code", "c11" },
{ order_table, "ship_country", "c12" }, { &order_table, "ship_country", "c12" },
{ order_details_table, "order_details_id", "c13" }, { &order_details_table, "order_details_id", "c13" },
{ order_details_table, "order_id", "c14" }, { &order_details_table, "order_id", "c14" },
{ order_details_table, "product_id", "c15" } { &order_details_table, "product_id", "c15" }
}; };
REQUIRE(!columns.empty()); REQUIRE(!columns.empty());
REQUIRE(columns.size() == expected_columns.size()); 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<author>("authors"); } ); .and_then( [&s] { return s.attach<author>("authors"); } );
REQUIRE(result); REQUIRE(result);
const auto books_table = std::make_shared<table>("books"); const auto books_table = table("books");
const auto authors_table = std::make_shared<table>("authors"); const auto authors_table = table("authors");
const std::vector<column> expected_columns { const std::vector<column> expected_columns {
{ books_table, "id", "c01" }, { &books_table, "id", "c01" },
{ books_table, "title", "c02" }, { &books_table, "title", "c02" },
{ authors_table, "id", "c03" }, { &authors_table, "id", "c03" },
{ authors_table, "first_name", "c04" }, { &authors_table, "first_name", "c04" },
{ authors_table, "last_name", "c05" }, { &authors_table, "last_name", "c05" },
{ authors_table, "date_of_birth", "c06" }, { &authors_table, "date_of_birth", "c06" },
{ authors_table, "year_of_birth", "c07" }, { &authors_table, "year_of_birth", "c07" },
{ authors_table, "distinguished", "c08" }, { &authors_table, "distinguished", "c08" },
{ books_table, "published_in", "c09" } { &books_table, "published_in", "c09" }
}; };
auto columns = generator::columns<book>(s, generator::column_generator_options::GenerateAlias); auto columns = generator::columns<book>(s, generator::column_generator_options::GenerateAlias);