schema progress (does not compile)
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
namespace matador::orm {
|
||||
|
||||
struct entity_query_data {
|
||||
std::shared_ptr<query::table> root_table;
|
||||
const query::table* root_table{nullptr};
|
||||
std::string pk_column_name{};
|
||||
std::vector<query::column> columns{};
|
||||
std::unordered_map<std::string, sql::statement> lazy_loading_statements{};
|
||||
@@ -34,7 +34,7 @@ struct entity_query_data {
|
||||
|
||||
class criteria_transformer final : public query::criteria_visitor {
|
||||
public:
|
||||
criteria_transformer(const query::schema &repo, const std::unordered_map<std::string, std::shared_ptr<query::table>>& tables_by_name);
|
||||
criteria_transformer(const query::schema &repo, const std::unordered_map<std::string, query::table>& tables_by_name);
|
||||
void visit( const query::between_criteria& node ) override;
|
||||
void visit( const query::binary_criteria& node ) override;
|
||||
void visit( const query::binary_column_criteria& node ) override;
|
||||
@@ -49,7 +49,7 @@ private:
|
||||
|
||||
private:
|
||||
const query::schema &repo_;
|
||||
const std::unordered_map<std::string, std::shared_ptr<query::table>>& tables_by_name_;
|
||||
const std::unordered_map<std::string, query::table>& tables_by_name_;
|
||||
};
|
||||
|
||||
class session_query_builder final {
|
||||
@@ -60,15 +60,16 @@ public:
|
||||
|
||||
template<class EntityType>
|
||||
utils::result<entity_query_data, query_build_error> build(query::criteria_ptr clause = {}) {
|
||||
const auto info = schema_.repo().info<EntityType>();
|
||||
if (!info) {
|
||||
const auto it = schema_.find(typeid(EntityType));
|
||||
if (it == schema_.end()) {
|
||||
return utils::failure(query_build_error::UnknownType);
|
||||
}
|
||||
table_info_stack_.push({info.value(), std::make_shared<query::table>(info.value().get().name(), build_alias('t', ++table_index))});
|
||||
entity_query_data_ = { table_info_stack_.top().table };
|
||||
processed_tables_.insert({info->get().name(), entity_query_data_.root_table});
|
||||
table_info_stack_.push({it->second.node().info(), it->second.table().as(build_alias('t', ++table_index))});
|
||||
entity_query_data_ = { &table_info_stack_.top().table };
|
||||
processed_tables_.insert({it->second.name(), *entity_query_data_.root_table});
|
||||
try {
|
||||
access::process(*this, info->get().prototype());
|
||||
EntityType obj;
|
||||
access::process(*this, obj);
|
||||
|
||||
if (clause) {
|
||||
criteria_transformer transformer{schema_, processed_tables_};
|
||||
@@ -101,45 +102,77 @@ public:
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr)
|
||||
{
|
||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr)
|
||||
{
|
||||
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct NoopDeleter {
|
||||
void operator()(const T*) const {}
|
||||
};
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char * /*id*/, ContainerType &, const char *join_column, const utils::foreign_attributes &attr) {
|
||||
if (attr.fetch() == utils::fetch_type::Eager) {
|
||||
const auto result = schema_.repo().basic_info(typeid(typename ContainerType::value_type::value_type));
|
||||
if (!result) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
|
||||
const auto &info = result.value().get();
|
||||
auto next = processed_tables_.find(info.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
return;
|
||||
}
|
||||
table_info_stack_.push({*result, std::make_shared<query::table>(info.name(), build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first;
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this , obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
if (!info.has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
append_join(
|
||||
query::column{table_info_stack_.top().table.get(), table_info_stack_.top().info.get().primary_key_attribute()->name()},
|
||||
query::column{next->second.get(), join_column}
|
||||
);
|
||||
if (attr.fetch() != utils::fetch_type::Eager) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto it = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
|
||||
auto next = processed_tables_.find(it->second.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
// node already processed
|
||||
return;
|
||||
}
|
||||
|
||||
table_info_stack_.push({it->second.node().info(), it->second.table().as(build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({it->second.name(), table_info_stack_.top().table}).first;
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this , obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
append_join(
|
||||
query::column{&table_info_stack_.top().table, table_info_stack_.top().info.primary_key_attribute()->name()},
|
||||
query::column{&next->second, join_column}
|
||||
);
|
||||
|
||||
|
||||
// const auto result = schema_.repo().basic_info(typeid(typename ContainerType::value_type::value_type));
|
||||
// if (!result) {
|
||||
// throw query_builder_exception{query_build_error::UnknownType};
|
||||
// }
|
||||
//
|
||||
// const auto &info = result.value().get();
|
||||
// auto next = processed_tables_.find(info.name());
|
||||
// if (next != processed_tables_.end()) {
|
||||
// return;
|
||||
// }
|
||||
// table_info_stack_.push({*result, std::make_shared<query::table>(info.name(), build_alias('t', ++table_index))});
|
||||
// next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first;
|
||||
// typename ContainerType::value_type::value_type obj;
|
||||
// access::process(*this , obj);
|
||||
// table_info_stack_.pop();
|
||||
//
|
||||
// if (!info.has_primary_key()) {
|
||||
// throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
// }
|
||||
//
|
||||
// append_join(
|
||||
// query::column{table_info_stack_.top().table.get(), table_info_stack_.top().info.get().primary_key_attribute()->name()},
|
||||
// query::column{next->second.get(), join_column}
|
||||
// );
|
||||
}
|
||||
|
||||
template<class ContainerType>
|
||||
@@ -147,38 +180,42 @@ public:
|
||||
if (attr.fetch() != utils::fetch_type::Eager) {
|
||||
return;
|
||||
}
|
||||
const auto result = schema_.repo().basic_info(typeid(typename ContainerType::value_type::value_type));
|
||||
if (!result) {
|
||||
const auto result = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (result == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
|
||||
const auto &info = result.value().get();
|
||||
auto next = processed_tables_.find(info.name());
|
||||
auto next = processed_tables_.find(result->second.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
// attribute was already processed
|
||||
return;
|
||||
}
|
||||
|
||||
auto relation = processed_tables_.find(id);
|
||||
if (relation == processed_tables_.end()) {
|
||||
relation = processed_tables_.insert({id, std::make_shared<query::table>(id, build_alias('t', ++table_index))}).first;
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
relation = processed_tables_.emplace(id, it->second.table().as(build_alias('t', ++table_index))).first;
|
||||
}
|
||||
|
||||
table_info_stack_.push({*result, std::make_shared<query::table>(info.name(), build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first;
|
||||
table_info_stack_.push({result->second.node().info(), result->second.table().as(build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({result->second.name(), table_info_stack_.top().table}).first;
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this , obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
if (!info.has_primary_key()) {
|
||||
if (!result->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
append_join(
|
||||
query::column{table_info_stack_.top().table.get(), table_info_stack_.top().info.get().primary_key_attribute()->name()},
|
||||
query::column{relation->second.get(), join_column}
|
||||
query::column{&table_info_stack_.top().table, table_info_stack_.top().info.primary_key_attribute()->name()},
|
||||
query::column{&relation->second, join_column}
|
||||
);
|
||||
append_join(
|
||||
query::column{relation->second.get(), inverse_join_column},
|
||||
query::column{next->second.get(), info.primary_key_attribute()->name()}
|
||||
query::column{&relation->second, inverse_join_column},
|
||||
query::column{&next->second, result->second.node().info().primary_key_attribute()->name()}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -187,40 +224,45 @@ public:
|
||||
if (attr.fetch() != utils::fetch_type::Eager) {
|
||||
return;
|
||||
}
|
||||
const auto result = schema_.repo().basic_info(typeid(typename ContainerType::value_type::value_type));
|
||||
if (!result) {
|
||||
const auto result = schema_.find(typeid(typename ContainerType::value_type::value_type));
|
||||
if (result == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
|
||||
const auto &info = result.value().get();
|
||||
auto next = processed_tables_.find(info.name());
|
||||
auto next = processed_tables_.find(result->second.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
// attribute was already processed
|
||||
return;
|
||||
}
|
||||
|
||||
auto relation = processed_tables_.find(id);
|
||||
if (relation == processed_tables_.end()) {
|
||||
relation = processed_tables_.insert({id, std::make_shared<query::table>(id, build_alias('t', ++table_index))}).first;
|
||||
const auto it = schema_.find(id);
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
const auto t = it->second.table().as(build_alias('t', ++table_index));
|
||||
relation = processed_tables_.insert({id, t}).first;
|
||||
}
|
||||
table_info_stack_.push({*result, std::make_shared<query::table>(info.name(), build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first;
|
||||
table_info_stack_.push({result->second.node().info(), result->second.table().as(build_alias('t', ++table_index))});
|
||||
next = processed_tables_.insert({result->second.name(), table_info_stack_.top().table}).first;
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this , obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
if (!info.has_primary_key()) {
|
||||
if (!result->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
const auto join_columns = join_columns_collector_.collect<typename ContainerType::value_type::value_type>();
|
||||
|
||||
append_join(
|
||||
query::column{table_info_stack_.top().table.get(), table_info_stack_.top().info.get().primary_key_attribute()->name()},
|
||||
query::column{relation->second.get(), join_columns.inverse_join_column}
|
||||
query::column{&table_info_stack_.top().table, table_info_stack_.top().info.primary_key_attribute()->name()},
|
||||
query::column{&relation->second, join_columns.inverse_join_column}
|
||||
);
|
||||
append_join(
|
||||
query::column{relation->second.get(), join_columns.join_column},
|
||||
query::column{next->second.get(), info.primary_key_attribute()->name()}
|
||||
query::column{&relation->second, join_columns.join_column},
|
||||
query::column{&next->second, result->second.node().info().primary_key_attribute()->name()}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -234,12 +276,14 @@ private:
|
||||
|
||||
private:
|
||||
struct table_info {
|
||||
std::reference_wrapper<const object::basic_object_info> info;
|
||||
std::shared_ptr<query::table> table;
|
||||
const object::basic_object_info &info;
|
||||
query::table table;
|
||||
// std::reference_wrapper<const object::basic_object_info> info;
|
||||
// std::shared_ptr<query::table> table;
|
||||
};
|
||||
|
||||
std::stack<table_info> table_info_stack_{};
|
||||
std::unordered_map<std::string, std::shared_ptr<query::table>> processed_tables_{};
|
||||
std::unordered_map<std::string, query::table> processed_tables_{};
|
||||
const query::schema &schema_;
|
||||
entity_query_data entity_query_data_{};
|
||||
unsigned int column_index{0};
|
||||
@@ -250,30 +294,31 @@ private:
|
||||
|
||||
template<class Pointer>
|
||||
void session_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) {
|
||||
const auto info = schema_.repo().info<typename Pointer::value_type>();
|
||||
if (!info) {
|
||||
const auto it = schema_.find(typeid(typename Pointer::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{query_build_error::UnknownType};
|
||||
}
|
||||
if (!info->get().has_primary_key()) {
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
const auto foreign_table = std::make_shared<query::table>(info->get().name(), build_alias('t', ++table_index));
|
||||
const auto& info = it->second.node().info();
|
||||
auto foreign_table = it->second.table().as(build_alias('t', ++table_index));
|
||||
if (attr.fetch() == utils::fetch_type::Eager) {
|
||||
|
||||
auto next = processed_tables_.find(info->get().name());
|
||||
auto next = processed_tables_.find(info.name());
|
||||
if (next != processed_tables_.end()) {
|
||||
return;
|
||||
}
|
||||
table_info_stack_.push({info.value(), foreign_table});
|
||||
next = processed_tables_.insert({info->get().name(), table_info_stack_.top().table}).first;
|
||||
table_info_stack_.push({info, std::move(foreign_table)});
|
||||
next = processed_tables_.insert({info.name(), table_info_stack_.top().table}).first;
|
||||
typename Pointer::value_type obj;
|
||||
access::process(*this, obj);
|
||||
table_info_stack_.pop();
|
||||
|
||||
append_join(
|
||||
query::column{table_info_stack_.top().table.get(), id},
|
||||
query::column{next->second.get(), info->get().primary_key_attribute()->name()}
|
||||
query::column{&table_info_stack_.top().table, id},
|
||||
query::column{&next->second, info.primary_key_attribute()->name()}
|
||||
);
|
||||
} else {
|
||||
push(id);
|
||||
@@ -281,8 +326,8 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u
|
||||
using namespace matador::query;
|
||||
// create select query
|
||||
auto result = matador::query::query::select(generator::columns<typename Pointer::value_type>(schema_, generator::column_generator_options::ForceLazy))
|
||||
.from(*foreign_table)
|
||||
.where(column(foreign_table.get(), info->get().primary_key_attribute()->name(), "") == _)
|
||||
.from(foreign_table)
|
||||
.where(column(&foreign_table, info.primary_key_attribute()->name(), "") == _)
|
||||
.prepare(executor_);
|
||||
if (!result) {
|
||||
throw query_builder_exception(query_build_error::QueryError, result.release_error());
|
||||
|
||||
Reference in New Issue
Block a user