entity query builder progress
This commit is contained in:
parent
496e94ddcd
commit
2b552c4383
|
|
@ -14,23 +14,24 @@ template < typename PrimaryKeyType >
|
||||||
class entity_query_builder
|
class entity_query_builder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit entity_query_builder(const PrimaryKeyType &pk)
|
explicit entity_query_builder(const PrimaryKeyType &pk, const schema &scm)
|
||||||
: pk_(pk) {}
|
: pk_(pk)
|
||||||
|
, schema_(scm) {}
|
||||||
|
|
||||||
// determine pk
|
// determine pk
|
||||||
// collect eager relations for joins
|
// collect eager relations for joins
|
||||||
template<class EntityType>
|
template<class EntityType>
|
||||||
std::optional<query_context> build(connection &db, const schema &scm) {
|
std::optional<query_context> build(connection &db) {
|
||||||
EntityType obj;
|
EntityType obj;
|
||||||
matador::utils::access::process(*this, obj);
|
matador::utils::access::process(*this, obj);
|
||||||
|
|
||||||
const auto info = scm.info<EntityType>();
|
const auto info = schema_.info<EntityType>();
|
||||||
if (!info) {
|
if (!info) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
columns_.clear();
|
columns_.clear();
|
||||||
table_name_ = info.value().name;
|
table_name_ = info.value().name;
|
||||||
query q(db, scm);
|
query q(db, schema_);
|
||||||
return q.select<EntityType>().from({table_name_}).build();
|
return q.select<EntityType>().from({table_name_}).build();
|
||||||
// auto from_intermediate = q.select<EntityType>().from({"t"});
|
// auto from_intermediate = q.select<EntityType>().from({"t"});
|
||||||
// return {};
|
// return {};
|
||||||
|
|
@ -66,7 +67,9 @@ public:
|
||||||
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) {
|
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, "");
|
columns_.emplace_back(table_name_, id, "");
|
||||||
}
|
}
|
||||||
|
|
@ -75,6 +78,11 @@ public:
|
||||||
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
||||||
{
|
{
|
||||||
if (attr.fetch() == utils::fetch_type::EAGER) {
|
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
|
// collect join information
|
||||||
}
|
}
|
||||||
columns_.emplace_back(table_name_, id, "");
|
columns_.emplace_back(table_name_, id, "");
|
||||||
|
|
@ -97,6 +105,7 @@ public:
|
||||||
private:
|
private:
|
||||||
PrimaryKeyType pk_;
|
PrimaryKeyType pk_;
|
||||||
std::vector<column> columns_;
|
std::vector<column> columns_;
|
||||||
|
const schema &schema_;
|
||||||
std::string table_name_;
|
std::string table_name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,13 @@ public:
|
||||||
return visitor.result;
|
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);
|
friend std::ostream& operator<<(std::ostream &out, const field &col);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -48,12 +48,11 @@ public:
|
||||||
void append(column_definition col);
|
void append(column_definition col);
|
||||||
|
|
||||||
[[nodiscard]] bool has_primary_key() const;
|
[[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 std::vector<column_definition>& columns() const;
|
||||||
|
|
||||||
[[nodiscard]] const column_definition& at(const column &col) 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;
|
[[nodiscard]] const column_definition& at(size_t index) const;
|
||||||
|
|
||||||
iterator find(const std::string &column_name);
|
iterator find(const std::string &column_name);
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,6 @@ struct table_info
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
record prototype;
|
record prototype;
|
||||||
void create(connection &conn, schema &scm) const;
|
|
||||||
void drop(connection &conn, schema &scm) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class schema
|
class schema
|
||||||
|
|
|
||||||
|
|
@ -12,4 +12,33 @@ std::ostream &operator<<(std::ostream &out, const field &col)
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool field::is_integer() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool field::is_floating_point() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool field::is_bool() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool field::is_string() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool field::is_blob() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool field::is_null() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -52,10 +52,10 @@ bool record::has_primary_key() const
|
||||||
return pk_index_ > -1;
|
return pk_index_ > -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const column_definition &record::primary_key() const
|
std::optional<column_definition> record::primary_key() const
|
||||||
{
|
{
|
||||||
if (!has_primary_key()) {
|
if (!has_primary_key()) {
|
||||||
throw std::logic_error("record has no primary key");
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
return columns_[pk_index_];
|
return columns_[pk_index_];
|
||||||
|
|
@ -63,16 +63,9 @@ const column_definition &record::primary_key() const
|
||||||
|
|
||||||
const column_definition &record::at(const column &col) const
|
const column_definition &record::at(const column &col) const
|
||||||
{
|
{
|
||||||
auto ref = columns_by_name_.at(col.name).first;
|
|
||||||
return columns_by_name_.at(col.name).first;
|
return columns_by_name_.at(col.name).first;
|
||||||
}
|
}
|
||||||
|
|
||||||
//const column_definition &record::at(const std::string &name) const
|
|
||||||
//{
|
|
||||||
// auto ref = columns_by_name_.at(name).first;
|
|
||||||
// return columns_by_name_.at(name).first;
|
|
||||||
//}
|
|
||||||
|
|
||||||
const column_definition &record::at(size_t index) const
|
const column_definition &record::at(size_t index) const
|
||||||
{
|
{
|
||||||
return columns_.at(index);
|
return columns_.at(index);
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,6 @@
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
void table_info::create(connection &conn, schema &scm) const
|
|
||||||
{
|
|
||||||
conn.query(scm).create().table(name, prototype.columns()).execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
void table_info::drop(connection &conn, schema &scm) const
|
|
||||||
{
|
|
||||||
conn.query(scm).drop().table(name).execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
schema::schema(std::string name)
|
schema::schema(std::string name)
|
||||||
: name_(std::move(name)) {}
|
: name_(std::move(name)) {}
|
||||||
|
|
||||||
|
|
@ -44,7 +34,7 @@ std::pair<std::string, std::string> schema::reference(const std::type_index &ti)
|
||||||
if (!it->second.prototype.has_primary_key()) {
|
if (!it->second.prototype.has_primary_key()) {
|
||||||
throw std::logic_error("table doesn't has primary key");
|
throw std::logic_error("table doesn't has primary key");
|
||||||
}
|
}
|
||||||
return { it->second.name, it->second.prototype.primary_key().name() };
|
return { it->second.name, it->second.prototype.primary_key().value().name() };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ void session::create_schema()
|
||||||
{
|
{
|
||||||
auto c = pool_.acquire();
|
auto c = pool_.acquire();
|
||||||
for (const auto &t : *schema_) {
|
for (const auto &t : *schema_) {
|
||||||
t.second.create(*c, *schema_);
|
c->query(*schema_).create().table(t.second.name, t.second.prototype.columns()).execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,9 @@ TEST_CASE("Create sql query for entity", "[query][entity][builder]") {
|
||||||
scm.attach<matador::test::author>("authors");
|
scm.attach<matador::test::author>("authors");
|
||||||
scm.attach<matador::test::book>("books");
|
scm.attach<matador::test::book>("books");
|
||||||
|
|
||||||
entity_query_builder eqb(17);
|
entity_query_builder eqb(17, scm);
|
||||||
|
|
||||||
auto context = eqb.build<matador::test::book>(noop, scm);
|
auto context = eqb.build<matador::test::book>(noop);
|
||||||
std::cout << "SQL: " << context.value().sql << "\n";
|
std::cout << "SQL: " << context.value().sql << "\n";
|
||||||
context = eqb.build<schiff>(noop, scm);
|
context = eqb.build<schiff>(noop);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue