From 5d2d6d4a30b9b5d90fd67395a562d04fc16bc748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Tue, 30 Sep 2025 22:30:34 +0200 Subject: [PATCH] fixed some query record tests --- include/matador/sql/query_result.hpp | 4 ++- .../query/intermediates/fetchable_query.cpp | 35 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/matador/sql/query_result.hpp b/include/matador/sql/query_result.hpp index e2d2282..56fdc36 100644 --- a/include/matador/sql/query_result.hpp +++ b/include/matador/sql/query_result.hpp @@ -130,7 +130,9 @@ public: public: explicit query_result(std::unique_ptr &&impl) : impl_(std::move(impl)) - , creator_([this]{ return detail::create_prototype(impl_->prototype()); } ) {} + , creator_([this] { + return detail::create_prototype(impl_->prototype()); + }) {} query_result(std::unique_ptr &&impl, creator_func&& creator) : impl_(std::move(impl)) diff --git a/source/orm/query/intermediates/fetchable_query.cpp b/source/orm/query/intermediates/fetchable_query.cpp index 84fc4fe..452f0a9 100644 --- a/source/orm/query/intermediates/fetchable_query.cpp +++ b/source/orm/query/intermediates/fetchable_query.cpp @@ -1,16 +1,47 @@ #include "matador/query/intermediates/fetchable_query.hpp" #include "matador/sql/executor.hpp" +#include "matador/sql/field.hpp" #include "matador/sql/statement.hpp" namespace matador::query { +namespace detail { +sql::field_type determine_field_type(const utils::constraints c) { + if (is_constraint_set(c, utils::constraints::FOREIGN_KEY)) { + return sql::field_type::ForeignKey; + } + if (is_constraint_set(c, utils::constraints::PRIMARY_KEY)) { + return sql::field_type::PrimaryKey; + } + return sql::field_type::Attribute; +} + +sql::record *create_prototype(const std::vector &prototype) { + auto result = std::make_unique(); + for (const auto &col: prototype) { + result->append({ + col.name(), + col.type(), + determine_field_type(col.attributes().options()), + col.attributes().size(), + col.index() + }); + } + return result.release(); +} + +} utils::result, utils::error> fetchable_query::fetch_all(const sql::executor &exec) const { query_compiler compiler; context_->mode = query_mode::Direct; - return exec.fetch(compiler.compile(*context_, exec.dialect(), std::nullopt)) + const auto ctx = compiler.compile(*context_, exec.dialect(), std::nullopt); + return exec.fetch(ctx) .and_then([](auto &&res) { - return utils::ok(sql::query_result(std::forward(res))); + const auto prototype = res->prototype(); + return utils::ok(sql::query_result(std::forward(res), [prototype] { + return detail::create_prototype(prototype); + })); }); }