entity query builder progress

This commit is contained in:
Sascha Kuehl 2024-03-05 20:14:34 +01:00
parent be610ffcad
commit 496e94ddcd
4 changed files with 88 additions and 13 deletions

View File

@ -22,7 +22,11 @@ struct column
column(std::string table_name, std::string name, std::string as)
: table(std::move(table_name))
, name(std::move(name))
, alias(std::move(as)) {} // NOLINT(*-explicit-constructor)
, alias(std::move(as)) {}
column(std::string table_name, const char* name, std::string as)
: table(std::move(table_name))
, name(name)
, alias(std::move(as)) {}
column& as(std::string a) {
alias = std::move(a);

View File

@ -1,7 +1,12 @@
#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
#define QUERY_ENTITY_QUERY_BUILDER_HPP
#include "matador/sql/connection.hpp"
#include "matador/sql/query_context.hpp"
#include "matador/sql/query.hpp"
#include "matador/sql/query_intermediates.hpp"
#include <iostream>
namespace matador::sql {
@ -9,43 +14,78 @@ template < typename PrimaryKeyType >
class entity_query_builder
{
public:
explicit entity_query_builder(const PrimaryKeyType &pk)
: pk_(pk) {}
// determine pk
// collect eager relations for joins
template<class EntityType>
query_context build() {
std::optional<query_context> build(connection &db, const schema &scm) {
EntityType obj;
matador::utils::access::process(*this, obj);
return {};
const auto info = scm.info<EntityType>();
if (!info) {
return std::nullopt;
}
columns_.clear();
table_name_ = info.value().name;
query q(db, scm);
return q.select<EntityType>().from({table_name_}).build();
// auto from_intermediate = q.select<EntityType>().from({"t"});
// return {};
}
template < class V >
void on_primary_key(const char *id, V &, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type* = 0)
{
auto b = std::is_integral_v<PrimaryKeyType>;
std::cout << "is matching primary key: " << std::boolalpha << b << "\n";
columns_.emplace_back(table_name_, id, "");
}
void on_primary_key(const char *id, std::string &, size_t)
{
auto b = std::is_same_v<PrimaryKeyType, std::string>;
std::cout << "is matching primary key: " << std::boolalpha << b << "\n";
columns_.emplace_back(table_name_, id, "");
}
void on_revision(const char *id, unsigned long long &/*rev*/)
{
columns_.emplace_back(table_name_, id, "");
}
void on_revision(const char * /*id*/, unsigned long long &/*rev*/) {}
template<typename Type>
void on_attribute(const char * /*id*/, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes)
{
columns_.emplace_back(table_name_, id, "");
}
template<class Pointer>
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &/*attr*/)
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::EAGER) {
// collect join information
}
columns_.emplace_back(table_name_, id, "");
}
template<class Pointer>
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::EAGER) {
// collect join information
}
columns_.emplace_back(table_name_, id, "");
}
template<class ContainerType>
void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::EAGER) {
// collect join information
}
}
template<class ContainerType>
@ -56,6 +96,8 @@ public:
private:
PrimaryKeyType pk_;
std::vector<column> columns_;
std::string table_name_;
};
}

View File

@ -1,11 +1,33 @@
#include <catch2/catch_test_macros.hpp>
#include <matador/sql/connection.hpp>
#include <matador/sql/entity_query_builder.hpp>
#include "models/author.hpp"
#include "models/book.hpp"
struct schiff {
std::string id;
template<typename Operator>
void process(Operator &op)
{
namespace field = matador::utils::access;
field::primary_key(op, "id", id, 255);
}
};
using namespace matador::sql;
TEST_CASE("Create sql query for entity", "[query][entity][builder]") {
connection noop("noop://noop.db");
schema scm("noop");
scm.attach<matador::test::author>("authors");
scm.attach<matador::test::book>("books");
entity_query_builder eqb(17);
auto context = eqb.build<matador::test::book>(noop, scm);
std::cout << "SQL: " << context.value().sql << "\n";
context = eqb.build<schiff>(noop, scm);
}

View File

@ -7,18 +7,25 @@
namespace matador::test {
struct category
struct author
{
unsigned long id{};
std::string name;
std::string first_name;
std::string last_name;
std::string date_of_birth;
unsigned short year_of_birth{};
bool distinguished{false};
template<class Operator>
template<typename Operator>
void process(Operator &op)
{
namespace field = matador::utils::access;
using namespace matador::utils;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255);
field::attribute(op, "first_name", first_name, 63);
field::attribute(op, "last_name", last_name, 63);
field::attribute(op, "date_of_birth", date_of_birth, 31);
field::attribute(op, "year_of_birth", year_of_birth);
field::attribute(op, "distinguished", distinguished);
}
};