has many fetch with join
This commit is contained in:
parent
784f6e768d
commit
8ab8274d93
|
|
@ -44,6 +44,7 @@ public:
|
||||||
[[nodiscard]] const char *column(size_t index) const override;
|
[[nodiscard]] const char *column(size_t index) const override;
|
||||||
utils::result<bool, utils::error> fetch() override;
|
utils::result<bool, utils::error> fetch() override;
|
||||||
[[nodiscard]] size_t start_column_index() const override;
|
[[nodiscard]] size_t start_column_index() const override;
|
||||||
|
void unshift() override;
|
||||||
|
|
||||||
void read_value(const char *id, size_t index, int8_t &value) override;
|
void read_value(const char *id, size_t index, int8_t &value) override;
|
||||||
void read_value(const char *id, size_t index, int16_t &value) override;
|
void read_value(const char *id, size_t index, int16_t &value) override;
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,12 @@ size_t postgres_result_reader::start_column_index() const {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void postgres_result_reader::unshift() {
|
||||||
|
if (row_index_ > 0) {
|
||||||
|
--row_index_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, int8_t &value) {
|
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, int8_t &value) {
|
||||||
if (auto res = utils::to<int8_t>(column(index)); res.is_ok()) {
|
if (auto res = utils::to<int8_t>(column(index)); res.is_ok()) {
|
||||||
value = res.value();
|
value = res.value();
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ public:
|
||||||
object_ptr &operator=(object_ptr &&other) = default;
|
object_ptr &operator=(object_ptr &&other) = default;
|
||||||
|
|
||||||
using value_type = Type;
|
using value_type = Type;
|
||||||
Type* operator->() { return ptr_.get(); }
|
Type* operator->() const { return ptr_.get(); }
|
||||||
Type& operator*() const { return *ptr_; }
|
Type& operator*() const { return *ptr_; }
|
||||||
|
|
||||||
[[nodiscard]] bool empty() const { return ptr_ == nullptr; }
|
[[nodiscard]] bool empty() const { return ptr_ == nullptr; }
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,17 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
namespace matador::utils {
|
||||||
|
enum class constraints : unsigned char;
|
||||||
|
}
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
|
enum struct field_type {
|
||||||
|
Attribute,
|
||||||
|
PrimaryKey,
|
||||||
|
ForeignKey
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
@ -17,11 +26,12 @@ class field {
|
||||||
public:
|
public:
|
||||||
explicit field(std::string name);
|
explicit field(std::string name);
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
field(std::string name, Type value, const size_t size = 0, const int index = -1)
|
field(std::string name, Type value, const field_type type = field_type::Attribute, const size_t size = 0, const int index = -1)
|
||||||
: name_(std::move(name))
|
: name_(std::move(name))
|
||||||
|
, type_(type)
|
||||||
, index_(index)
|
, index_(index)
|
||||||
, value_(value, size) {}
|
, value_(value, size) {}
|
||||||
field(std::string name, utils::basic_type dt, size_t size = 0, int index = -1);
|
field(std::string name, utils::basic_type dt, field_type type = field_type::Attribute, size_t size = 0, int index = -1);
|
||||||
field(const field &x) = default;
|
field(const field &x) = default;
|
||||||
field& operator=(const field &x) = default;
|
field& operator=(const field &x) = default;
|
||||||
field(field &&x) noexcept;
|
field(field &&x) noexcept;
|
||||||
|
|
@ -35,6 +45,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const std::string& name() const;
|
[[nodiscard]] const std::string& name() const;
|
||||||
|
[[nodiscard]] field_type type() const;
|
||||||
[[nodiscard]] size_t size() const;
|
[[nodiscard]] size_t size() const;
|
||||||
[[nodiscard]] int index() const;
|
[[nodiscard]] int index() const;
|
||||||
|
|
||||||
|
|
@ -53,18 +64,25 @@ public:
|
||||||
[[nodiscard]] bool is_blob() const;
|
[[nodiscard]] bool is_blob() const;
|
||||||
[[nodiscard]] bool is_null() const;
|
[[nodiscard]] bool is_null() const;
|
||||||
|
|
||||||
|
[[nodiscard]] bool is_primary_key() const;
|
||||||
|
[[nodiscard]] bool is_foreign_key() const;
|
||||||
|
[[nodiscard]] bool is_attribute() const;
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream &out, const field &col);
|
friend std::ostream& operator<<(std::ostream &out, const field &col);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static utils::constraints determine_constraint(field_type type);
|
||||||
|
|
||||||
template<class Operator>
|
template<class Operator>
|
||||||
void process(Operator &op) {
|
void process(Operator &op) {
|
||||||
op.on_attribute(name_.c_str(), value_, value_.size());
|
op.on_attribute(name_.c_str(), value_, { value_.size(), determine_constraint(type_) } );
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class record;
|
friend class record;
|
||||||
|
|
||||||
std::string name_;
|
std::string name_;
|
||||||
|
field_type type_{field_type::Attribute};
|
||||||
int index_{-1};
|
int index_{-1};
|
||||||
|
|
||||||
utils::value value_;
|
utils::value value_;
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ public:
|
||||||
[[nodiscard]] virtual const char* column(size_t index) const = 0;
|
[[nodiscard]] virtual const char* column(size_t index) const = 0;
|
||||||
[[nodiscard]] virtual utils::result<bool, utils::error> fetch() = 0;
|
[[nodiscard]] virtual utils::result<bool, utils::error> fetch() = 0;
|
||||||
[[nodiscard]] virtual size_t start_column_index() const = 0;
|
[[nodiscard]] virtual size_t start_column_index() const = 0;
|
||||||
|
virtual void unshift() = 0;
|
||||||
|
|
||||||
template<class Type>
|
template<class Type>
|
||||||
void bind(Type &obj) {
|
void bind(Type &obj) {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@
|
||||||
#include "matador/utils/identifier.hpp"
|
#include "matador/utils/identifier.hpp"
|
||||||
|
|
||||||
#include "matador/sql/interface/query_result_reader.hpp"
|
#include "matador/sql/interface/query_result_reader.hpp"
|
||||||
|
#include "matador/sql/internal/query_result_pk_resolver.hpp"
|
||||||
|
#include "matador/sql/record.hpp"
|
||||||
|
|
||||||
#include "matador/object/attribute_definition.hpp"
|
#include "matador/object/attribute_definition.hpp"
|
||||||
|
|
||||||
|
|
@ -40,19 +42,19 @@ public:
|
||||||
template<typename ValueType>
|
template<typename ValueType>
|
||||||
void on_primary_key(const char *id, ValueType &value, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>>* = nullptr);
|
void on_primary_key(const char *id, ValueType &value, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>>* = nullptr);
|
||||||
void on_primary_key(const char *id, std::string &value, size_t size);
|
void on_primary_key(const char *id, std::string &value, size_t size);
|
||||||
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
|
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) { ++column_index_; }
|
||||||
|
|
||||||
template < class Type >
|
template < class Type >
|
||||||
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) { ++column_index_; }
|
||||||
template < class Pointer >
|
template < class Pointer >
|
||||||
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) { ++column_index_; }
|
||||||
template < class Pointer >
|
template < class Pointer >
|
||||||
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {}
|
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) { ++column_index_; }
|
||||||
|
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
void on_has_many(const char * /*id*/, ContainerType &, const char * /*join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
void on_has_many_to_many(const char *id, ContainerType &c, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {}
|
void on_has_many_to_many(const char *id, ContainerType &c, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {}
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
|
||||||
|
|
@ -86,6 +88,7 @@ public:
|
||||||
{
|
{
|
||||||
utils::data_type_traits<Type>::read_value(*reader_, id, column_index_++, x);
|
utils::data_type_traits<Type>::read_value(*reader_, id, column_index_++, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_attribute(const char *id, char *value, const utils::field_attributes &attr = utils::null_attributes);
|
void on_attribute(const char *id, char *value, const utils::field_attributes &attr = utils::null_attributes);
|
||||||
void on_attribute(const char *id, std::string &value, const utils::field_attributes &attr = utils::null_attributes);
|
void on_attribute(const char *id, std::string &value, const utils::field_attributes &attr = utils::null_attributes);
|
||||||
void on_attribute(const char *id, utils::value &val, const utils::field_attributes &attr = utils::null_attributes);
|
void on_attribute(const char *id, utils::value &val, const utils::field_attributes &attr = utils::null_attributes);
|
||||||
|
|
@ -97,23 +100,25 @@ public:
|
||||||
}
|
}
|
||||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||||
pk_reader_.read(*x, column_index_++);
|
pk_reader_.read(*x, column_index_++);
|
||||||
} else if (const auto ti = std::type_index(typeid(*x)); processed_types_.count(ti) == 0) {
|
} else {
|
||||||
processed_types_.insert(ti);
|
const auto ti = std::type_index(typeid(*x));
|
||||||
type_stack_.push(ti);
|
type_stack_.push(ti);
|
||||||
access::process(*this, *x);
|
access::process(*this, *x);
|
||||||
type_stack_.pop();
|
type_stack_.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template < class Pointer >
|
template < class Pointer >
|
||||||
void on_has_one(const char * /*id*/, Pointer &x, const utils::foreign_attributes &attr)
|
void on_has_one(const char * /*id*/, Pointer &x, const utils::foreign_attributes &attr) {
|
||||||
{
|
|
||||||
if (x.empty()) {
|
if (x.empty()) {
|
||||||
x.reset(new typename Pointer::value_type);
|
x.reset(new typename Pointer::value_type);
|
||||||
}
|
}
|
||||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||||
pk_reader_.read(*x, column_index_++);
|
pk_reader_.read(*x, column_index_++);
|
||||||
} else if (const auto ti = std::type_index(typeid(*x)); processed_types_.count(ti) == 0) {
|
} else {
|
||||||
processed_types_.insert(ti);
|
const auto ti = std::type_index(typeid(*x));
|
||||||
|
type_stack_.push(ti);
|
||||||
|
access::process(*this, *x);
|
||||||
|
type_stack_.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -125,9 +130,8 @@ public:
|
||||||
void on_has_many(const char * /*id*/, ContainerType &cont, const char * /*join_column*/, const utils::foreign_attributes &attr) {
|
void on_has_many(const char * /*id*/, ContainerType &cont, const char * /*join_column*/, const utils::foreign_attributes &attr) {
|
||||||
if ( attr.fetch() == utils::fetch_type::LAZY ) {
|
if ( attr.fetch() == utils::fetch_type::LAZY ) {
|
||||||
// pk_reader_.read(*id, column_index_++);
|
// pk_reader_.read(*id, column_index_++);
|
||||||
} else /*if (const auto ti = std::type_index(typeid(typename ContainerType::value_type::value_type)); processed_types_.count(ti) == 0)*/ {
|
} else {
|
||||||
const auto ti = std::type_index(typeid(typename ContainerType::value_type::value_type));
|
const auto ti = std::type_index(typeid(typename ContainerType::value_type::value_type));
|
||||||
processed_types_.insert(ti);
|
|
||||||
auto obj = std::make_unique<typename ContainerType::value_type::value_type>();
|
auto obj = std::make_unique<typename ContainerType::value_type::value_type>();
|
||||||
type_stack_.push(ti);
|
type_stack_.push(ti);
|
||||||
access::process(*this, *obj);
|
access::process(*this, *obj);
|
||||||
|
|
@ -148,37 +152,58 @@ public:
|
||||||
reader_->bind(obj);
|
reader_->bind(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool pk_has_changed() const {
|
||||||
|
return !last_pk_.is_null() && last_pk_ != current_pk_;
|
||||||
|
}
|
||||||
|
|
||||||
template<class Type>
|
template<class Type>
|
||||||
bool fetch(Type &obj) {
|
bool fetch(Type &obj) {
|
||||||
bool result = false;
|
bool first = true;
|
||||||
do {
|
do {
|
||||||
column_index_ = reader_->start_column_index();
|
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
|
||||||
auto fetched = reader_->fetch();
|
return !first;
|
||||||
if (!fetched.is_ok()) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if (!*fetched) {
|
last_pk_ = current_pk_;
|
||||||
return false;
|
current_pk_ = discover_current_primary_key(obj);
|
||||||
|
if (pk_has_changed()) {
|
||||||
|
reader_->unshift();
|
||||||
|
last_pk_.clear();
|
||||||
|
current_pk_.clear();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
processed_types_.insert(typeid(Type));
|
first = false;
|
||||||
type_stack_.push(typeid(Type));
|
type_stack_.push(typeid(Type));
|
||||||
|
column_index_ = reader_->start_column_index();
|
||||||
access::process(*this, obj);
|
access::process(*this, obj);
|
||||||
type_stack_.pop();
|
type_stack_.pop();
|
||||||
std::cout << "last pk: " << last_pk_.str() << std::endl;
|
|
||||||
std::cout << "current pk: " << current_pk_.str() << std::endl;
|
|
||||||
std::cout << "last == current: " << std::boolalpha << (last_pk_ == current_pk_) << std::endl;
|
|
||||||
} while (last_pk_ == current_pk_);
|
} while (last_pk_ == current_pk_);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool fetch(record &rec) {
|
||||||
|
if (auto fetched = reader_->fetch(); !fetched.is_ok() || !*fetched) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
column_index_ = reader_->start_column_index();
|
||||||
|
access::process(*this, rec);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] const std::vector<object::attribute_definition>& prototype() const;
|
[[nodiscard]] const std::vector<object::attribute_definition>& prototype() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<class Type>
|
||||||
|
utils::identifier discover_current_primary_key(const Type &obj) {
|
||||||
|
internal::query_result_pk_resolver resolver(*reader_);
|
||||||
|
return resolver.discover(obj);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
size_t column_index_ = 0;
|
size_t column_index_ = 0;
|
||||||
std::vector<object::attribute_definition> prototype_;
|
std::vector<object::attribute_definition> prototype_;
|
||||||
std::unique_ptr<query_result_reader> reader_;
|
std::unique_ptr<query_result_reader> reader_;
|
||||||
detail::pk_reader pk_reader_;
|
detail::pk_reader pk_reader_;
|
||||||
std::unordered_set<std::type_index> processed_types_;
|
|
||||||
std::stack<std::type_index> type_stack_;
|
std::stack<std::type_index> type_stack_;
|
||||||
utils::identifier current_pk_{};
|
utils::identifier current_pk_{};
|
||||||
utils::identifier last_pk_{};
|
utils::identifier last_pk_{};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
#ifndef QUERY_RESULT_PK_RESOLVER_HPP
|
||||||
|
#define QUERY_RESULT_PK_RESOLVER_HPP
|
||||||
|
|
||||||
|
#include "matador/utils/access.hpp"
|
||||||
|
#include "matador/utils/field_attributes.hpp"
|
||||||
|
#include "matador/utils/foreign_attributes.hpp"
|
||||||
|
#include "matador/utils/identifier.hpp"
|
||||||
|
|
||||||
|
#include "matador/sql/interface/query_result_reader.hpp"
|
||||||
|
|
||||||
|
#include <stack>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace matador::sql::internal {
|
||||||
|
|
||||||
|
class query_result_pk_resolver final {
|
||||||
|
public:
|
||||||
|
explicit query_result_pk_resolver(query_result_reader &reader) : reader_(reader) {}
|
||||||
|
|
||||||
|
template<class Type>
|
||||||
|
utils::identifier discover(Type &obj) {
|
||||||
|
column_index_ = reader_.start_column_index();
|
||||||
|
pk_.clear();
|
||||||
|
access::process(*this, obj);
|
||||||
|
|
||||||
|
return pk_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ValueType>
|
||||||
|
void on_primary_key(const char *id, ValueType &/*value*/, std::enable_if_t<std::is_integral_v<ValueType> && !std::is_same_v<bool, ValueType>>* = nullptr) {
|
||||||
|
if (!type_stack_.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ValueType value;
|
||||||
|
utils::data_type_traits<ValueType>::read_value(reader_, id, column_index_++, value);
|
||||||
|
pk_ = value;
|
||||||
|
}
|
||||||
|
void on_primary_key(const char *id, std::string &value, size_t size);
|
||||||
|
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) { ++column_index_; }
|
||||||
|
|
||||||
|
template < class Type >
|
||||||
|
void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) { ++column_index_; }
|
||||||
|
void on_attribute(const char *id, const utils::value &x, const utils::field_attributes &attr = utils::null_attributes);
|
||||||
|
|
||||||
|
template < class Pointer >
|
||||||
|
void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &attr) { on_foreign_key<typename Pointer::value_type>(attr.fetch() ); }
|
||||||
|
template < class Pointer >
|
||||||
|
void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &attr) { on_foreign_key<typename Pointer::value_type>(attr.fetch() ); }
|
||||||
|
|
||||||
|
template<class ContainerType>
|
||||||
|
void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
template<class ContainerType>
|
||||||
|
void on_has_many_to_many(const char *id, ContainerType &c, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
template<class ContainerType>
|
||||||
|
void on_has_many_to_many(const char *id, ContainerType &c, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<class Type>
|
||||||
|
void on_foreign_key(const utils::fetch_type fetch) {
|
||||||
|
if (fetch == utils::fetch_type::LAZY) {
|
||||||
|
++column_index_;
|
||||||
|
} else {
|
||||||
|
const Type obj;
|
||||||
|
type_stack_.push(typeid(Type));
|
||||||
|
access::process(*this, obj);
|
||||||
|
type_stack_.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
size_t column_index_{};
|
||||||
|
utils::identifier pk_;
|
||||||
|
query_result_reader &reader_;
|
||||||
|
std::stack<std::type_index> type_stack_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //QUERY_RESULT_PK_RESOLVER_HPP
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
#ifndef QUERY_RECORD_HPP
|
#ifndef QUERY_RECORD_HPP
|
||||||
#define QUERY_RECORD_HPP
|
#define QUERY_RECORD_HPP
|
||||||
|
|
||||||
#include "matador/utils/access.hpp"
|
|
||||||
|
|
||||||
#include "matador/sql/field.hpp"
|
#include "matador/sql/field.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -34,8 +32,7 @@ public:
|
||||||
~record() = default;
|
~record() = default;
|
||||||
|
|
||||||
template<class Operator>
|
template<class Operator>
|
||||||
void process(Operator &op)
|
void process(Operator &op) {
|
||||||
{
|
|
||||||
for(auto &f : fields_) {
|
for(auto &f : fields_) {
|
||||||
f.get().process(op);
|
f.get().process(op);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -163,7 +163,7 @@ attribute_definition make_column<std::string>(const std::string &name, utils::fi
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size) {
|
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<>
|
template<>
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ add_library(matador-orm STATIC
|
||||||
../../include/matador/sql/interface/statement_impl.hpp
|
../../include/matador/sql/interface/statement_impl.hpp
|
||||||
../../include/matador/sql/internal/object_result_binder.hpp
|
../../include/matador/sql/internal/object_result_binder.hpp
|
||||||
../../include/matador/sql/internal/query_result_impl.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_context.hpp
|
||||||
../../include/matador/sql/query_macro.hpp
|
../../include/matador/sql/query_macro.hpp
|
||||||
../../include/matador/sql/query_result.hpp
|
../../include/matador/sql/query_result.hpp
|
||||||
|
|
@ -121,6 +122,7 @@ add_library(matador-orm STATIC
|
||||||
sql/interface/query_result_reader.cpp
|
sql/interface/query_result_reader.cpp
|
||||||
sql/interface/statement_impl.cpp
|
sql/interface/statement_impl.cpp
|
||||||
sql/internal/object_result_binder.cpp
|
sql/internal/object_result_binder.cpp
|
||||||
|
sql/internal/query_result_pk_resolver.cpp
|
||||||
sql/object_parameter_binder.cpp
|
sql/object_parameter_binder.cpp
|
||||||
sql/query_result.cpp
|
sql/query_result.cpp
|
||||||
sql/record.cpp
|
sql/record.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
#include "matador/sql/field.hpp"
|
#include "matador/sql/field.hpp"
|
||||||
|
|
||||||
|
#include "matador/utils/constraints.hpp"
|
||||||
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
@ -9,23 +11,24 @@ field::field(std::string name)
|
||||||
, value_(nullptr)
|
, value_(nullptr)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
field::field(std::string name, const utils::basic_type dt, const size_t size, const int index)
|
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))
|
: name_(std::move(name))
|
||||||
, index_(index)
|
, type_(type)
|
||||||
, value_(dt, size) {}
|
, index_(index)
|
||||||
|
, value_(dt, size) {}
|
||||||
|
|
||||||
field::field(field &&x) noexcept
|
field::field(field &&x) noexcept
|
||||||
: name_(std::move(x.name_))
|
: name_(std::move(x.name_))
|
||||||
, index_(x.index_)
|
, type_(x.type_)
|
||||||
, value_(std::move(x.value_))
|
, index_(x.index_)
|
||||||
{
|
, value_(std::move(x.value_)) {
|
||||||
x.value_ = nullptr;
|
x.value_ = nullptr;
|
||||||
x.index_ = -1;
|
x.index_ = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
field &field::operator=(field &&x) noexcept
|
field &field::operator=(field &&x) noexcept {
|
||||||
{
|
|
||||||
name_ = std::move(x.name_);
|
name_ = std::move(x.name_);
|
||||||
|
type_ = x.type_;
|
||||||
index_ = x.index_;
|
index_ = x.index_;
|
||||||
value_ = std::move(x.value_);
|
value_ = std::move(x.value_);
|
||||||
x.index_ = -1;
|
x.index_ = -1;
|
||||||
|
|
@ -39,6 +42,10 @@ const std::string &field::name() const
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
field_type field::type() const {
|
||||||
|
return type_;
|
||||||
|
}
|
||||||
|
|
||||||
size_t field::size() const
|
size_t field::size() const
|
||||||
{
|
{
|
||||||
return value_.size();
|
return value_.size();
|
||||||
|
|
@ -55,6 +62,19 @@ std::ostream &operator<<(std::ostream &out, const field &col)
|
||||||
return out;
|
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
|
std::string field::str() const
|
||||||
{
|
{
|
||||||
return as<std::string>().value_or("");
|
return as<std::string>().value_or("");
|
||||||
|
|
@ -95,4 +115,17 @@ bool field::is_null() const
|
||||||
return value_.is_null();
|
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_;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -5,12 +5,27 @@
|
||||||
|
|
||||||
namespace matador::sql::detail {
|
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<>
|
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>();
|
auto result = std::make_unique<record>();
|
||||||
for (const auto &col: prototype) {
|
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();
|
return result.release();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,9 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
|
||||||
|
|
||||||
all_departments = find_result.release();
|
all_departments = find_result.release();
|
||||||
for (auto it = all_departments.begin(); it != all_departments.end(); ++it) {
|
for (auto it = all_departments.begin(); it != all_departments.end(); ++it) {
|
||||||
std::cout << "department: " << it->name << " (employees: " << it->employees.size() << ")\n";
|
std::cout << "department: " << it->name << " (id: " << it->id << ", employees: " << it->employees.size() << ")\n";
|
||||||
|
for (const auto& emp : it->employees) {
|
||||||
|
std::cout << "\temployee: " << emp->first_name << " " << emp->last_name << "( " << emp->id << ")\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ struct employee {
|
||||||
field::primary_key(op, "id", id);
|
field::primary_key(op, "id", id);
|
||||||
field::attribute(op, "first_name", first_name, 63);
|
field::attribute(op, "first_name", first_name, 63);
|
||||||
field::attribute(op, "last_name", last_name, 63);
|
field::attribute(op, "last_name", last_name, 63);
|
||||||
field::belongs_to(op, "dep_id", dep, utils::fetch_type::EAGER);
|
field::belongs_to(op, "dep_id", dep, utils::fetch_type::LAZY);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ struct flight {
|
||||||
namespace field = matador::access;
|
namespace field = matador::access;
|
||||||
using namespace matador::utils;
|
using namespace matador::utils;
|
||||||
field::primary_key(op, "id", id);
|
field::primary_key(op, "id", id);
|
||||||
field::has_one(op, "airplane_id", plane, {utils::cascade_type::ALL, utils::fetch_type::EAGER});
|
field::has_one(op, "airplane_id", plane, {utils::cascade_type::ALL, fetch_type::EAGER});
|
||||||
field::attribute(op, "pilot_name", pilot_name, 255);
|
field::attribute(op, "pilot_name", pilot_name, 255);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ size_t test_result_reader::start_column_index() const {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_result_reader::unshift() {}
|
||||||
|
|
||||||
void test_result_reader::read_value(const char *id, const size_t index, int8_t &value) {
|
void test_result_reader::read_value(const char *id, const size_t index, int8_t &value) {
|
||||||
value = -8;
|
value = -8;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ public:
|
||||||
[[nodiscard]] const char *column(size_t index) const override;
|
[[nodiscard]] const char *column(size_t index) const override;
|
||||||
[[nodiscard]] utils::result<bool, utils::error> fetch() override;
|
[[nodiscard]] utils::result<bool, utils::error> fetch() override;
|
||||||
[[nodiscard]] size_t start_column_index() const override;
|
[[nodiscard]] size_t start_column_index() const override;
|
||||||
|
void unshift() override;
|
||||||
|
|
||||||
void read_value(const char *id, size_t index, int8_t &value) override;
|
void read_value(const char *id, size_t index, int8_t &value) override;
|
||||||
void read_value(const char *id, size_t index, int16_t &value) override;
|
void read_value(const char *id, size_t index, int16_t &value) override;
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ TEST_CASE("Test field", "[field]") {
|
||||||
REQUIRE(bool_val.has_value());
|
REQUIRE(bool_val.has_value());
|
||||||
REQUIRE(bool_val.value());
|
REQUIRE(bool_val.value());
|
||||||
|
|
||||||
f = sql::field("name", utils::blob{ 7,8,6,5,4,3 }, 0, 1);
|
f = sql::field("name", utils::blob{ 7,8,6,5,4,3 }, sql::field_type::Attribute, 0, 1);
|
||||||
REQUIRE(f.index() == 1);
|
REQUIRE(f.index() == 1);
|
||||||
REQUIRE(!f.is_null());
|
REQUIRE(!f.is_null());
|
||||||
REQUIRE(!f.is_integer());
|
REQUIRE(!f.is_integer());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue