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.
 
 
 

229 lines
6.4 KiB

  1. #pragma once
  2. #include <cppmariadb.h>
  3. #include <cpphibernate/config.h>
  4. #include "../classes.h"
  5. namespace cpphibernate {
  6. namespace mariadb {
  7. /**
  8. * @brief Base class for all mariadb driver context classes.
  9. */
  10. struct base_context
  11. {
  12. const schema_t& schema; //!< schema to use for the operation
  13. ::cppmariadb::connection& connection; //!< mariadb connection to use for executing queries
  14. /**
  15. * @brief Constructor.
  16. */
  17. inline base_context(
  18. const schema_t& p_schema,
  19. ::cppmariadb::connection& p_connection);
  20. };
  21. /**
  22. * @brief Mariadb driver context for initializing the database.
  23. */
  24. struct init_context
  25. : public base_context
  26. {
  27. bool recreate;
  28. inline init_context(
  29. const schema_t& p_schema,
  30. ::cppmariadb::connection& p_connection,
  31. bool p_recreate);
  32. };
  33. #if 0
  34. /* init_context */
  35. struct init_context
  36. : public base_context
  37. {
  38. bool recreate;
  39. inline init_context(
  40. const schema_t& p_schema,
  41. ::cppmariadb::connection& p_connection,
  42. bool p_recreate)
  43. : base_context (p_schema, p_connection)
  44. , recreate (p_recreate)
  45. { }
  46. };
  47. namespace __impl
  48. {
  49. struct change_context_impl
  50. {
  51. template<typename T_context, typename T_data>
  52. constexpr decltype(auto) operator()(const T_context& context, T_data& data) const
  53. {
  54. auto new_context = context;
  55. new_context.set(data);
  56. return new_context;
  57. }
  58. };
  59. }
  60. constexpr decltype(auto) change_context = __impl::change_context_impl { };
  61. /* data_context */
  62. struct data_context
  63. : public base_context
  64. {
  65. private:
  66. friend __impl::change_context_impl;
  67. mutable size_t _dataset_id;
  68. mutable void* _dataset;
  69. mutable const table_t* _table;
  70. protected:
  71. template<typename T_dataset>
  72. inline void* set(T_dataset& dataset, size_t dataset_id = 0) const;
  73. public:
  74. template<typename T_dataset>
  75. inline decltype(auto) get() const;
  76. inline data_context(
  77. const schema_t& p_schema,
  78. ::cppmariadb::connection& p_connection)
  79. : base_context (p_schema, p_connection)
  80. , _dataset_id (0)
  81. , _dataset (nullptr)
  82. , _table (nullptr)
  83. { }
  84. template<typename T_data>
  85. inline data_context(
  86. T_data& p_data,
  87. const schema_t& p_schema,
  88. ::cppmariadb::connection& p_connection)
  89. : base_context (p_schema, p_connection)
  90. , _dataset_id (misc::get_type_id(hana::type_c<mp::decay_t<T_data>>))
  91. , _dataset (&p_data)
  92. , _table (nullptr)
  93. { }
  94. };
  95. /* filter_context */
  96. struct filter_context
  97. : public data_context
  98. {
  99. const filter_t& filter;
  100. inline filter_context(
  101. const schema_t& p_schema,
  102. ::cppmariadb::connection& p_connection,
  103. const filter_t& p_filter)
  104. : data_context (p_schema, p_connection)
  105. , filter (p_filter)
  106. { }
  107. template<typename T_data>
  108. inline filter_context(
  109. T_data& p_data,
  110. const schema_t& p_schema,
  111. ::cppmariadb::connection& p_connection,
  112. const filter_t& p_filter)
  113. : data_context (p_data, p_schema, p_connection)
  114. , filter (p_filter)
  115. { }
  116. };
  117. /* create_update_context */
  118. struct create_update_context
  119. : public filter_context
  120. {
  121. bool is_update;
  122. const table_t* derived_table;
  123. const field_t* owner_field;
  124. std::string owner_key;
  125. ssize_t index;
  126. template<typename T_data>
  127. inline create_update_context(
  128. T_data& p_data,
  129. const schema_t& p_schema,
  130. ::cppmariadb::connection& p_connection,
  131. const filter_t& p_filter,
  132. bool p_is_update)
  133. : filter_context(p_data, p_schema, p_connection, p_filter)
  134. , is_update (p_is_update)
  135. , derived_table (nullptr)
  136. , owner_field (nullptr)
  137. , index (-1)
  138. { }
  139. };
  140. /* read_context */
  141. struct read_context
  142. : public filter_context
  143. {
  144. protected:
  145. size_t base_dataset_id;
  146. public:
  147. bool is_dynamic;
  148. std::string where;
  149. std::string limit;
  150. std::string order_by;
  151. protected:
  152. inline read_context(
  153. const schema_t& p_schema,
  154. ::cppmariadb::connection& p_connection,
  155. const filter_t& p_filter)
  156. : filter_context (p_schema, p_connection, p_filter)
  157. , is_dynamic (false)
  158. , base_dataset_id (0)
  159. { }
  160. public:
  161. virtual ~read_context() = default;
  162. template<typename T_dataset>
  163. inline T_dataset& emplace(const table_t* table = nullptr) const;
  164. void emplace() const
  165. { emplace_intern(nullptr, 0); }
  166. void finish() const
  167. { finish_intern(); }
  168. private:
  169. virtual void* emplace_intern(void* data, size_t dataset_id) const = 0;
  170. virtual void finish_intern () const = 0;
  171. };
  172. using read_context_ptr = std::unique_ptr<read_context>;
  173. /* destroy_context */
  174. struct destroy_context
  175. : public data_context
  176. {
  177. std::string where;
  178. template<typename T_data>
  179. inline destroy_context(
  180. T_data& p_data,
  181. const schema_t& p_schema,
  182. ::cppmariadb::connection& p_connection)
  183. : data_context(
  184. p_data,
  185. p_schema,
  186. p_connection)
  187. { }
  188. };
  189. #endif
  190. } }