has many fetch with join

This commit is contained in:
Sascha Kühl
2025-02-19 14:51:42 +01:00
parent 784f6e768d
commit 8ab8274d93
19 changed files with 264 additions and 52 deletions
+1 -1
View File
@@ -163,7 +163,7 @@ attribute_definition make_column<std::string>(const std::string &name, utils::fi
template<>
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size) {
return make_column<std::string>(name, {size, utils::constraints::FOREIGN_KEY});
return make_column<std::string>(name, {size, utils::constraints::PRIMARY_KEY});
}
template<>
+2
View File
@@ -61,6 +61,7 @@ add_library(matador-orm STATIC
../../include/matador/sql/interface/statement_impl.hpp
../../include/matador/sql/internal/object_result_binder.hpp
../../include/matador/sql/internal/query_result_impl.hpp
../../include/matador/sql/internal/query_result_pk_resolver.hpp
../../include/matador/sql/query_context.hpp
../../include/matador/sql/query_macro.hpp
../../include/matador/sql/query_result.hpp
@@ -121,6 +122,7 @@ add_library(matador-orm STATIC
sql/interface/query_result_reader.cpp
sql/interface/statement_impl.cpp
sql/internal/object_result_binder.cpp
sql/internal/query_result_pk_resolver.cpp
sql/object_parameter_binder.cpp
sql/query_result.cpp
sql/record.cpp
+43 -10
View File
@@ -1,5 +1,7 @@
#include "matador/sql/field.hpp"
#include "matador/utils/constraints.hpp"
#include <ostream>
namespace matador::sql {
@@ -9,23 +11,24 @@ field::field(std::string name)
, value_(nullptr)
{}
field::field(std::string name, const utils::basic_type dt, const size_t size, const int index)
: name_(std::move(name))
, index_(index)
, value_(dt, size) {}
field::field(std::string name, const utils::basic_type dt, const field_type type, const size_t size, const int index)
: name_(std::move(name))
, type_(type)
, index_(index)
, value_(dt, size) {}
field::field(field &&x) noexcept
: name_(std::move(x.name_))
, index_(x.index_)
, value_(std::move(x.value_))
{
: name_(std::move(x.name_))
, type_(x.type_)
, index_(x.index_)
, value_(std::move(x.value_)) {
x.value_ = nullptr;
x.index_ = -1;
}
field &field::operator=(field &&x) noexcept
{
field &field::operator=(field &&x) noexcept {
name_ = std::move(x.name_);
type_ = x.type_;
index_ = x.index_;
value_ = std::move(x.value_);
x.index_ = -1;
@@ -39,6 +42,10 @@ const std::string &field::name() const
return name_;
}
field_type field::type() const {
return type_;
}
size_t field::size() const
{
return value_.size();
@@ -55,6 +62,19 @@ std::ostream &operator<<(std::ostream &out, const field &col)
return out;
}
utils::constraints field::determine_constraint(const field_type type) {
switch (type) {
case field_type::PrimaryKey:
return utils::constraints::PRIMARY_KEY;
case field_type::ForeignKey:
return utils::constraints::FOREIGN_KEY;
case field_type::Attribute:
default:
return utils::constraints::NONE;
}
}
std::string field::str() const
{
return as<std::string>().value_or("");
@@ -95,4 +115,17 @@ bool field::is_null() const
return value_.is_null();
}
bool field::is_primary_key() const {
return type_ == field_type::PrimaryKey;
}
bool field::is_foreign_key() const {
return type_ == field_type::ForeignKey;
}
bool field::is_attribute() const {
return type_ == field_type::Attribute;
}
}
@@ -0,0 +1,30 @@
#include "matador/sql/internal/query_result_pk_resolver.hpp"
#include "matador/utils/value.hpp"
namespace matador::sql::internal {
void query_result_pk_resolver::on_primary_key(const char* id, std::string& /*value*/, const size_t size) {
if (!type_stack_.empty()) {
return;
}
std::string value;
utils::data_type_traits<std::string>::read_value(reader_, id, column_index_++, value, size);
pk_ = value;
}
void query_result_pk_resolver::on_attribute(const char *id, const utils::value &x, const utils::field_attributes &attr) {
if (is_constraint_set(attr.options(), utils::constraints::PRIMARY_KEY)) {
if (x.is_integer()) {
uint64_t val;
utils::data_type_traits<uint64_t>::read_value(reader_, id, column_index_++, val);
pk_ = val;
} else if (x.is_varchar()) {
std::string value;
utils::data_type_traits<std::string>::read_value(reader_, id, column_index_++, value, attr.size());
pk_ = value;
}
}
++column_index_;
}
}
+18 -3
View File
@@ -5,12 +5,27 @@
namespace matador::sql::detail {
field_type determine_field_type(const utils::constraints c) {
if (is_constraint_set(c, utils::constraints::FOREIGN_KEY)) {
return field_type::ForeignKey;
}
if (is_constraint_set(c, utils::constraints::PRIMARY_KEY)) {
return field_type::PrimaryKey;
}
return field_type::Attribute;
}
template<>
record *create_prototype<record>(const std::vector<object::attribute_definition> &prototype)
{
record *create_prototype<record>(const std::vector<object::attribute_definition> &prototype) {
auto result = std::make_unique<record>();
for (const auto &col: prototype) {
result->append({col.name(), col.type(), col.attributes().size(), col.index()});
result->append({
col.name(),
col.type(),
determine_field_type(col.attributes().options()),
col.attributes().size(),
col.index()
});
}
return result.release();
}