#pragma once #include "destroy.h" #include "../context/destroy_context.inl" namespace cpphibernate { namespace mariadb { /* destroy_impl_t - default */ template struct destroy_impl_t { using dataset_type = T_dataset; static inline void apply(const destroy_context& context, bool strict = true) { auto dataset_id = get_type_id(hana::type_c); auto& connection = context.connection; auto& schema = context.schema; auto& table = schema.table(dataset_id); assert(table.primary_key_field); ::cppmariadb::transaction_lock trans(connection); if (!table.primary_key_field->is_default(context)) { auto& field = *table.primary_key_field; auto primary_key = field.get(context); assert(primary_key.has_value()); std::ostringstream os; os << "WHERE `" << field.table.name << "`.`" << field.name << "`=" << field.convert_to_open << "'" << connection.escape(*primary_key) << "'" << field.convert_to_close; auto new_context = context; new_context.where = os.str(); table.destroy(new_context); } else if (strict) { throw exception("can not destroy dataset with no primary key assigned!"); } trans.commit(); } }; /* destroy_impl_t - nullable */ template struct destroy_impl_t< T_dataset, mp::enable_if_t>> { using dataset_type = T_dataset; using nullable_helper_type = nullable_helper; static inline void apply(const destroy_context& context, bool strict = true) { auto& dataset = context.get(); auto* value = nullable_helper_type::get(dataset); if (value) { using new_dataset_type = mp::decay_t; using new_destroy_impl_type = destroy_impl_t; auto new_context = change_context(context, *value); new_destroy_impl_type::apply(new_context, strict); } else if (strict) { throw exception("can not destroy nullable type with no value!"); } } }; /* destroy_impl_t - container */ template struct destroy_impl_t< T_dataset, mp::enable_if_t>> { using dataset_type = T_dataset; static inline void apply(const destroy_context& context, bool strict = true) { auto& connection = context.connection; auto& dataset = context.get(); ::cppmariadb::transaction_lock trans(connection); for (auto& x : dataset) { using new_dataset_type = mp::decay_t; using new_destroy_impl_type = destroy_impl_t; auto new_context = change_context(context, x); new_destroy_impl_type::apply(new_context, strict); } trans.commit(); } }; } }