collection_resolver progress

This commit is contained in:
Sascha Kühl 2026-01-30 15:53:31 +01:00
parent bcec740d60
commit 0d3ab09c14
7 changed files with 58 additions and 4 deletions

View File

@ -2,6 +2,7 @@
#define MATADOR_QUERY_CONTAINER_RESOLVER_HPP
#include "matador/object/collection_resolver.hpp"
#include "matador/object/object_resolver.hpp"
#include "matador/sql/internal/identifier_statement_binder.hpp"
#include "matador/sql/statement.hpp"
@ -25,6 +26,38 @@ protected:
std::type_index index{typeid(Type)};
};
struct value_to_identifier{
void operator()(const int8_t &x) { assign(x); }
void operator()(const int16_t &x) { assign(x); }
void operator()(const int32_t &x) { assign(x); }
void operator()(const int64_t &x) { assign(x); }
void operator()(const uint8_t &x) { assign(x); }
void operator()(const uint16_t &x) { assign(x); }
void operator()(const uint32_t &x) { assign(x); }
void operator()(const uint64_t &x) { assign(x); }
void operator()(const bool &x) {}
void operator()(const float &x) {}
void operator()(const double &x) { /* assign(x);*/ }
void operator()(const char *x) {}
void operator()(const std::string &x) { assign(x); }
void operator()(const utils::date_type_t &x) {}
void operator()(const utils::time_type_t &x) {}
void operator()(const utils::timestamp_type_t &x) {}
void operator()(const utils::blob_type_t &x) {}
template<typename Type>
void assign(const Type &x) { id_ = x; }
utils::identifier id_;
};
struct identifier_creator {
value_to_identifier visitor;
void on_attribute(const char*, const utils::value &val, const utils::field_attributes &/*attr*/) {
std::visit(visitor, val.raw_value());
}
};
template<typename Type>
std::vector<Type> query_collection_resolver<Type>::resolve(const utils::identifier &id) {
sql::identifier_statement_binder binder(stmt_);
@ -35,10 +68,16 @@ std::vector<Type> query_collection_resolver<Type>::resolve(const utils::identifi
return {};
}
std::vector<Type> out;
for (const auto &i: *result) {
for (auto &r : *result) {
// Todo: convert the first value of record into an utils::identifier
// then create a object_proxy<Type>(resolver, identifier)
// out.emplace_back(resolver, identifier);
if (r.size() != 1) {
continue;
}
identifier_creator creator;
r.at(0).process(creator);
const auto op = std::make_shared<object::object_proxy<typename Type::value_type>>(std::shared_ptr<object::object_resolver<typename Type::value_type>>{}, creator.visitor.id_);
out.emplace_back(op);
}
return out;
}

View File

@ -64,7 +64,6 @@ public:
friend std::ostream& operator<<(std::ostream &out, const field &col);
private:
template<class Operator>
void process(Operator &op) {
op.on_attribute(name_.c_str(), value_, { value_.size(), type_ } );

View File

@ -128,8 +128,8 @@ public:
template<class CollectionType>
void on_has_many(const char * /*id*/, CollectionType &cont, const char *join_column, const utils::foreign_attributes &attr, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
using value_type = typename CollectionType::value_type::value_type;
auto resolver = resolver_->collection_resolver<typename CollectionType::value_type>(result_type_, join_column);
auto object_resolver = resolver_->object_resolver<value_type>();
auto resolver = resolver_->collection_resolver<typename CollectionType::value_type>(result_type_, join_column);
std::vector<typename CollectionType::value_type> objects;
if (attr.fetch() == utils::fetch_type::Lazy) {

View File

@ -44,6 +44,8 @@ public:
[[nodiscard]] const field& at(const std::string &name) const;
[[nodiscard]] const field& at(size_t index) const;
[[nodiscard]] field& at(const std::string &name);
[[nodiscard]] field& at(size_t index);
template<class Type>
std::optional<Type> at(const std::string &name) const {
return at(name).as<Type>();

View File

@ -85,6 +85,8 @@ public:
[[nodiscard]] bool is_blob() const;
[[nodiscard]] bool is_null() const;
const database_type& raw_value() const;
private:
database_type value_;
size_t size_{};

View File

@ -103,4 +103,7 @@ bool value::is_blob() const {
bool value::is_null() const {
return type_ == basic_type::Null;
}
const database_type &value::raw_value() const {
return value_;
}
} // namespace matador::utils

View File

@ -59,6 +59,15 @@ const field &record::at(const size_t index) const
return fields_.at(index);
}
field &record::at(const std::string &name) {
auto &[fst, snd] = fields_by_name_.at(name);
return fst;
}
field &record::at(const size_t index) {
return fields_.at(index);
}
record::iterator record::find(const std::string &column_name)
{
auto it = fields_by_name_.find(column_name);