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.
 
 
 

96 lines
3.1 KiB

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