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.
 
 
 

113 lines
3.5 KiB

  1. #pragma once
  2. #include "destroy.h"
  3. #include "../context/destroy_context.inl"
  4. namespace cpphibernate {
  5. namespace mariadb {
  6. /* destroy_impl_t - default */
  7. template<typename T_dataset, typename>
  8. struct destroy_impl_t
  9. {
  10. using dataset_type = T_dataset;
  11. static inline void apply(const destroy_context& context, bool strict = true)
  12. {
  13. auto dataset_id = get_type_id(hana::type_c<dataset_type>);
  14. auto& connection = context.connection;
  15. auto& schema = context.schema;
  16. auto& table = schema.table(dataset_id);
  17. assert(table.primary_key_field);
  18. ::cppmariadb::transaction_lock trans(connection);
  19. if (!table.primary_key_field->is_default(context))
  20. {
  21. auto& field = *table.primary_key_field;
  22. auto primary_key = field.get(context);
  23. assert(primary_key.has_value());
  24. std::ostringstream os;
  25. os << "WHERE `"
  26. << field.table.name
  27. << "`.`"
  28. << field.name
  29. << "`="
  30. << field.convert_to_open
  31. << "'"
  32. << connection.escape(*primary_key)
  33. << "'"
  34. << field.convert_to_close;
  35. auto new_context = context;
  36. new_context.where = os.str();
  37. table.destroy(new_context);
  38. }
  39. else if (strict)
  40. {
  41. throw exception("can not destroy dataset with no primary key assigned!");
  42. }
  43. trans.commit();
  44. }
  45. };
  46. /* destroy_impl_t - nullable */
  47. template<typename T_dataset>
  48. struct destroy_impl_t<
  49. T_dataset,
  50. mp::enable_if_t<is_nullable_v<T_dataset>>>
  51. {
  52. using dataset_type = T_dataset;
  53. using nullable_helper_type = nullable_helper<dataset_type>;
  54. static inline void apply(const destroy_context& context, bool strict = true)
  55. {
  56. auto& dataset = context.get<dataset_type>();
  57. auto* value = nullable_helper_type::get(dataset);
  58. if (value)
  59. {
  60. using new_dataset_type = mp::decay_t<decltype(*value)>;
  61. using new_destroy_impl_type = destroy_impl_t<new_dataset_type>;
  62. auto new_context = change_context(context, *value);
  63. new_destroy_impl_type::apply(new_context, strict);
  64. }
  65. else if (strict)
  66. {
  67. throw exception("can not destroy nullable type with no value!");
  68. }
  69. }
  70. };
  71. /* destroy_impl_t - container */
  72. template<typename T_dataset>
  73. struct destroy_impl_t<
  74. T_dataset,
  75. mp::enable_if_t<is_container_v<T_dataset>>>
  76. {
  77. using dataset_type = T_dataset;
  78. static inline void apply(const destroy_context& context, bool strict = true)
  79. {
  80. auto& connection = context.connection;
  81. auto& dataset = context.get<dataset_type>();
  82. ::cppmariadb::transaction_lock trans(connection);
  83. for (auto& x : dataset)
  84. {
  85. using new_dataset_type = mp::decay_t<decltype(x)>;
  86. using new_destroy_impl_type = destroy_impl_t<new_dataset_type>;
  87. auto new_context = change_context(context, x);
  88. new_destroy_impl_type::apply(new_context, strict);
  89. }
  90. trans.commit();
  91. }
  92. };
  93. } }