session and insert query builder has many to many progress
This commit is contained in:
@@ -64,6 +64,7 @@ struct insert_step {
|
||||
std::function<void(const sql::record &)> apply_returning{};
|
||||
std::function<void(const utils::identifier &)> apply_primary_key{};
|
||||
std::function<void(sql::statement &)> bind_object{};
|
||||
std::function<void()> make_object_persistent{};
|
||||
};
|
||||
|
||||
template<class ObjectType>
|
||||
@@ -139,17 +140,28 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
if (processing_many_to_many_relations_.find(id) != processing_many_to_many_relations_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto it = schema_.find(std::string{id});
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
}
|
||||
|
||||
using relation_value_type = object::many_to_many_relation<ObjectType, ForeignType>;
|
||||
using relation_value_type = object::many_to_many_relation<ForeignType, ObjectType>;
|
||||
|
||||
if (std::type_index(typeid(relation_value_type)) != it->second.node().info().type_index()) {
|
||||
throw query_builder_exception(query_build_error::InvalidRelationType);
|
||||
}
|
||||
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
}
|
||||
|
||||
const auto rel_it = processing_many_to_many_relations_.insert(id);
|
||||
std::vector<insert_step> rel_steps;
|
||||
for (auto &obj : objects) {
|
||||
if (!obj) {
|
||||
continue;
|
||||
@@ -164,24 +176,32 @@ public:
|
||||
build_for(obj, relation_steps_);
|
||||
}
|
||||
|
||||
auto rel = object::make_object<relation_value_type>(join_column, inverse_join_column, ptr_, obj);
|
||||
auto rel = object::make_object<relation_value_type>(join_column, inverse_join_column, obj, ptr_);
|
||||
|
||||
// Extract FK value from the foreign object
|
||||
insert_step rel_step{};
|
||||
// const auto pk = rel_step.pk_accessor.get(*obj);
|
||||
// if (pk.is_valid()) {
|
||||
// continue;
|
||||
// }
|
||||
access::process(*this, *rel);
|
||||
|
||||
relation_steps_.push_back(std::move(rel_step));
|
||||
insert_step step{};
|
||||
step.pk_generator = utils::generator_type::None;
|
||||
step.ctx = cit->second.insert;
|
||||
step.bind_object = [rel](sql::statement &stmt) { stmt.bind(*rel); };
|
||||
step.make_object_persistent = [] {};
|
||||
|
||||
rel_steps.push_back(std::move(step));
|
||||
}
|
||||
relation_steps_.insert(relation_steps_.end(), rel_steps.begin(), rel_steps.end());
|
||||
processing_many_to_many_relations_.erase(id);
|
||||
}
|
||||
|
||||
template<class ForeignType>
|
||||
void on_has_many_to_many(const char *id, object::collection<object::object_ptr<ForeignType>> &objects, const utils::foreign_attributes &attr) {
|
||||
if (!utils::is_cascade_type_set(attr.cascade(), utils::cascade_type::Insert)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (processing_many_to_many_relations_.find(id) != processing_many_to_many_relations_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
object::join_columns_collector collector;
|
||||
auto join_columns = collector.collect<ForeignType>();
|
||||
|
||||
@@ -196,6 +216,13 @@ public:
|
||||
throw query_builder_exception(query_build_error::InvalidRelationType);
|
||||
}
|
||||
|
||||
const auto cit = contexts_by_type_.find(it->second.node().info().type_index());
|
||||
if (cit == contexts_by_type_.end()) {
|
||||
throw query_builder_exception(query_build_error::UnknownType);
|
||||
}
|
||||
|
||||
const auto rel_it = processing_many_to_many_relations_.insert(id);
|
||||
std::vector<insert_step> rel_steps;
|
||||
for (auto &obj : objects) {
|
||||
if (!obj) {
|
||||
continue;
|
||||
@@ -212,23 +239,20 @@ public:
|
||||
|
||||
auto rel = object::make_object<relation_value_type>(join_columns.inverse_join_column, join_columns.join_column, obj, ptr_);
|
||||
|
||||
// Extract FK value from the foreign object
|
||||
insert_step rel_step{};
|
||||
// const auto pk = rel_step.pk_accessor.get(*obj);
|
||||
// if (pk.is_valid()) {
|
||||
// continue;
|
||||
// }
|
||||
access::process(*this, *rel);
|
||||
|
||||
relation_steps_.push_back(std::move(rel_step));
|
||||
insert_step step{};
|
||||
step.pk_generator = utils::generator_type::None;
|
||||
step.ctx = cit->second.insert;
|
||||
step.bind_object = [rel](sql::statement &stmt) { stmt.bind(*rel); };
|
||||
step.make_object_persistent = [] {};
|
||||
|
||||
rel_steps.push_back(std::move(step));
|
||||
}
|
||||
relation_steps_.insert(relation_steps_.end(), rel_steps.begin(), rel_steps.end());
|
||||
processing_many_to_many_relations_.erase(id);
|
||||
}
|
||||
|
||||
template<class CollectionType>
|
||||
void process_has_many_to_many(const char *id, CollectionType &objects, const char *join_column, const char *inverse_join_column) {
|
||||
if (id == nullptr || join_column == nullptr || inverse_join_column == nullptr) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
private:
|
||||
template<class EntityType>
|
||||
static std::pair<std::type_index, const void *> make_visit_key(const object::object_ptr<EntityType> &ptr) {
|
||||
@@ -292,6 +316,7 @@ private:
|
||||
step.pk_accessor.set(*ptr, id);
|
||||
};
|
||||
}
|
||||
step.make_object_persistent = [ptr] { ptr.change_state(object::object_state::Persistent); };
|
||||
steps.push_back(std::move(step));
|
||||
}
|
||||
|
||||
@@ -318,6 +343,7 @@ private:
|
||||
std::vector<insert_step> steps_;
|
||||
std::vector<insert_step> relation_steps_;
|
||||
std::unordered_set<std::pair<std::type_index, const void *>, visit_key_hash> visited_;
|
||||
std::unordered_set<std::string> processing_many_to_many_relations_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_INSERT_QUERY_BUILDER_HPP
|
||||
Reference in New Issue
Block a user