|
|
|
@@ -128,14 +128,14 @@ public:
|
|
|
|
|
}
|
|
|
|
|
template<class CollectionType>
|
|
|
|
|
static void on_has_many(const char * /*id*/, object::collection<CollectionType> &/*con*/, const char *, const utils::foreign_attributes &/*attr*/) {}
|
|
|
|
|
template<class Collection>
|
|
|
|
|
void on_has_many_to_many(const char *id, Collection &container, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes & ) {
|
|
|
|
|
|
|
|
|
|
template<class ForeignType>
|
|
|
|
|
void on_has_many_to_many(const char *id, object::collection<object::object_ptr<ForeignType>> &objects, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr) {
|
|
|
|
|
if (id == nullptr || join_column == nullptr || inverse_join_column == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (current_entity_pk_ == 0ULL) {
|
|
|
|
|
// Without a local PK we cannot create relation rows
|
|
|
|
|
// (PK may be assigned later by Identity; that case needs a different mechanism than requested here)
|
|
|
|
|
|
|
|
|
|
if (!utils::is_cascade_type_set(attr.cascade(), utils::cascade_type::Insert)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -143,38 +143,92 @@ public:
|
|
|
|
|
if (it == schema_.end()) {
|
|
|
|
|
throw query_builder_exception(query_build_error::UnknownType);
|
|
|
|
|
}
|
|
|
|
|
const table &relation_table = it->second.table();
|
|
|
|
|
|
|
|
|
|
for (auto &obj : container) {
|
|
|
|
|
using relation_value_type = object::many_to_many_relation<ObjectType, ForeignType>;
|
|
|
|
|
|
|
|
|
|
if (std::type_index(typeid(relation_value_type)) != it->second.node().info().type_index()) {
|
|
|
|
|
throw query_builder_exception(query_build_error::InvalidRelationType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto &obj : objects) {
|
|
|
|
|
if (!obj) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure target exists as dependency (deps first)
|
|
|
|
|
if (!obj.is_persistent()) {
|
|
|
|
|
using dep_t = std::remove_reference_t<decltype(*obj)>;
|
|
|
|
|
build_for<dep_t>(obj, steps_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract FK value from the foreign object
|
|
|
|
|
insert_step rel_step{};
|
|
|
|
|
const auto pk = rel_step.pk_accessor.get(*obj);
|
|
|
|
|
if (pk.is_valid()) {
|
|
|
|
|
if (obj.is_persistent()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build INSERT into relation table with the 2 FK columns
|
|
|
|
|
// rel_step.query = executable_query{
|
|
|
|
|
// insert()
|
|
|
|
|
// .into(relation_table, {table_column{&relation_table, join_column}, table_column{&relation_table, inverse_join_column}})
|
|
|
|
|
// .values({utils::database_type{current_entity_pk_}, utils::database_type{fk.value}})
|
|
|
|
|
// };
|
|
|
|
|
// Ensure target exists as dependency (deps first)
|
|
|
|
|
if (obj.is_transient()) {
|
|
|
|
|
build_for(obj, relation_steps_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto rel = object::make_object<relation_value_type>(join_column, inverse_join_column, ptr_, obj);
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
relation_steps_.push_back(std::move(rel_step));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
template<class C>
|
|
|
|
|
static void on_has_many_to_many(const char * /*id*/, C &, const utils::foreign_attributes & ) {}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object::join_columns_collector collector;
|
|
|
|
|
auto join_columns = collector.collect<ForeignType>();
|
|
|
|
|
|
|
|
|
|
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<ForeignType, ObjectType>;
|
|
|
|
|
|
|
|
|
|
if (std::type_index(typeid(relation_value_type)) != it->second.node().info().type_index()) {
|
|
|
|
|
throw query_builder_exception(query_build_error::InvalidRelationType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto &obj : objects) {
|
|
|
|
|
if (!obj) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (obj.is_persistent()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure target exists as dependency (deps first)
|
|
|
|
|
if (obj.is_transient()) {
|
|
|
|
|
build_for(obj, relation_steps_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
relation_steps_.push_back(std::move(rel_step));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
@@ -264,9 +318,6 @@ 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::uint64_t current_entity_pk_{0};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
#endif //MATADOR_INSERT_QUERY_BUILDER_HPP
|