You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.9 KiB

  1. #pragma once
  2. beg_namespace_cpphibernate_driver_mariadb
  3. {
  4. /* destroy_impl_t */
  5. template<typename T_dataset, typename = void>
  6. struct destroy_impl_t
  7. {
  8. using dataset_type = T_dataset;
  9. static inline void apply(const destroy_context& context, bool strict = true)
  10. {
  11. auto dataset_id = misc::get_type_id(hana::type_c<dataset_type>);
  12. auto& connection = context.connection;
  13. auto& schema = context.schema;
  14. auto& table = schema.table(dataset_id);
  15. assert(table.primary_key_field);
  16. transaction_lock trans(connection);
  17. if (!table.primary_key_field->is_default(context))
  18. {
  19. table.destroy(context);
  20. }
  21. else if (strict)
  22. {
  23. throw misc::hibernate_exception("can not destroy dataset with no primary key assigned!");
  24. }
  25. trans.commit();
  26. }
  27. };
  28. /* destroy_impl_t - nullable */
  29. template<typename T_dataset>
  30. struct destroy_impl_t<
  31. T_dataset,
  32. mp::enable_if<misc::is_nullable<T_dataset>>>
  33. {
  34. using dataset_type = T_dataset;
  35. using nullable_helper_type = misc::nullable_helper<dataset_type>;
  36. static inline void apply(const destroy_context& context, bool strict = true)
  37. {
  38. auto& dataset = context.get<dataset_type>();
  39. auto* value = nullable_helper_type::get(dataset);
  40. if (value)
  41. {
  42. using new_dataset_type = mp::decay_t<decltype(*value)>;
  43. using new_destroy_impl_type = destroy_impl_t<new_dataset_type>;
  44. auto new_context = change_context(context, *value);
  45. new_destroy_impl_type::apply(new_context, strict);
  46. }
  47. else if (strict)
  48. {
  49. throw misc::hibernate_exception("can not destroy nullable type with no value!");
  50. }
  51. }
  52. };
  53. /* destroy_impl_t - container */
  54. template<typename T_dataset>
  55. struct destroy_impl_t<
  56. T_dataset,
  57. mp::enable_if<misc::is_container<T_dataset>>>
  58. {
  59. using dataset_type = T_dataset;
  60. static inline void apply(const destroy_context& context, bool strict = true)
  61. {
  62. auto& connection = context.connection;
  63. auto& dataset = context.get<dataset_type>();
  64. transaction_lock trans(connection);
  65. for (auto& x : dataset)
  66. {
  67. using new_dataset_type = mp::decay_t<decltype(x)>;
  68. using new_destroy_impl_type = destroy_impl_t<new_dataset_type>;
  69. auto new_context = change_context(context, x);
  70. new_destroy_impl_type::apply(new_context, strict);
  71. }
  72. trans.commit();
  73. }
  74. };
  75. }
  76. end_namespace_cpphibernate_driver_mariadb