|
- #pragma once
-
- #include "destroy.h"
-
- #include "../context/destroy_context.inl"
-
- namespace cpphibernate {
- namespace mariadb {
-
- /* destroy_impl_t - default */
-
- template<typename T_dataset, typename>
- 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<dataset_type>);
- 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<typename T_dataset>
- struct destroy_impl_t<
- T_dataset,
- mp::enable_if_t<is_nullable_v<T_dataset>>>
- {
- using dataset_type = T_dataset;
- using nullable_helper_type = nullable_helper<dataset_type>;
-
- static inline void apply(const destroy_context& context, bool strict = true)
- {
- auto& dataset = context.get<dataset_type>();
- auto* value = nullable_helper_type::get(dataset);
-
- if (value)
- {
- using new_dataset_type = mp::decay_t<decltype(*value)>;
- using new_destroy_impl_type = destroy_impl_t<new_dataset_type>;
- 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<typename T_dataset>
- struct destroy_impl_t<
- T_dataset,
- mp::enable_if_t<is_container_v<T_dataset>>>
- {
- using dataset_type = T_dataset;
-
- static inline void apply(const destroy_context& context, bool strict = true)
- {
- auto& connection = context.connection;
- auto& dataset = context.get<dataset_type>();
-
- ::cppmariadb::transaction_lock trans(connection);
- for (auto& x : dataset)
- {
- using new_dataset_type = mp::decay_t<decltype(x)>;
- using new_destroy_impl_type = destroy_impl_t<new_dataset_type>;
- auto new_context = change_context(context, x);
- new_destroy_impl_type::apply(new_context, strict);
- }
- trans.commit();
- }
- };
-
- } }
|