entity query builder progress

This commit is contained in:
2024-03-07 19:57:42 +01:00
parent 496e94ddcd
commit 2b552c4383
9 changed files with 59 additions and 34 deletions
+15 -6
View File
@@ -14,23 +14,24 @@ template < typename PrimaryKeyType >
class entity_query_builder
{
public:
explicit entity_query_builder(const PrimaryKeyType &pk)
: pk_(pk) {}
explicit entity_query_builder(const PrimaryKeyType &pk, const schema &scm)
: pk_(pk)
, schema_(scm) {}
// determine pk
// collect eager relations for joins
template<class EntityType>
std::optional<query_context> build(connection &db, const schema &scm) {
std::optional<query_context> build(connection &db) {
EntityType obj;
matador::utils::access::process(*this, obj);
const auto info = scm.info<EntityType>();
const auto info = schema_.info<EntityType>();
if (!info) {
return std::nullopt;
}
columns_.clear();
table_name_ = info.value().name;
query q(db, scm);
query q(db, schema_);
return q.select<EntityType>().from({table_name_}).build();
// auto from_intermediate = q.select<EntityType>().from({"t"});
// return {};
@@ -66,7 +67,9 @@ public:
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::EAGER) {
// collect join information
column col{id};
// collect join information (determine primary key of foreign entity)
}
columns_.emplace_back(table_name_, id, "");
}
@@ -75,6 +78,11 @@ public:
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
{
if (attr.fetch() == utils::fetch_type::EAGER) {
auto info = schema_.info<typename Pointer::value_type>();
if (info) {
column lcol(table_name_, id, "");
// column rcol(info.value())
}
// collect join information
}
columns_.emplace_back(table_name_, id, "");
@@ -97,6 +105,7 @@ public:
private:
PrimaryKeyType pk_;
std::vector<column> columns_;
const schema &schema_;
std::string table_name_;
};
+7
View File
@@ -26,6 +26,13 @@ public:
return visitor.result;
}
bool is_integer() const;
bool is_floating_point() const;
bool is_bool() const;
bool is_string() const;
bool is_blob() const;
bool is_null() const;
friend std::ostream& operator<<(std::ostream &out, const field &col);
private:
+1 -2
View File
@@ -48,12 +48,11 @@ public:
void append(column_definition col);
[[nodiscard]] bool has_primary_key() const;
[[nodiscard]] const column_definition& primary_key() const;
[[nodiscard]] std::optional<column_definition> primary_key() const;
[[nodiscard]] const std::vector<column_definition>& columns() const;
[[nodiscard]] const column_definition& at(const column &col) const;
// [[nodiscard]] const column_definition& at(const std::string &name) const;
[[nodiscard]] const column_definition& at(size_t index) const;
iterator find(const std::string &column_name);
-2
View File
@@ -17,8 +17,6 @@ struct table_info
{
std::string name;
record prototype;
void create(connection &conn, schema &scm) const;
void drop(connection &conn, schema &scm) const;
};
class schema