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.
 
 
 

235 lines
6.7 KiB

  1. #pragma once
  2. namespace cpphibernate {
  3. namespace mariadb {
  4. /**
  5. * @brief Base class for all mariadb driver context classes.
  6. */
  7. struct base_context
  8. {
  9. const schema_t& schema; //!< schema to use for the operation
  10. ::cppmariadb::connection& connection; //!< mariadb connection to use for executing queries
  11. /**
  12. * @brief Constructor.
  13. *
  14. * @param[in] p_schema Mariadb driver schema to use for the operation.
  15. * @param[in] p_connection Mariadb connection to execute queries with.
  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; //!< Drop existing tables before createing new once.
  28. /**
  29. * @brief Constructor.
  30. *
  31. * @param[in] p_schema Mariadb driver schema to use for the operation.
  32. * @param[in] p_connection Mariadb connection to execute queries with.
  33. * @param[in] p_recreate Drop existing tables before createing new once.
  34. */
  35. inline init_context(
  36. const schema_t& p_schema,
  37. ::cppmariadb::connection& p_connection,
  38. bool p_recreate);
  39. };
  40. /**
  41. * @brief Mariadb driver context that helds a specific dataset.
  42. */
  43. struct data_context
  44. : public base_context
  45. {
  46. private:
  47. mutable size_t _dataset_id; //!< Unique type id of the dataset that is stored in this context.
  48. mutable void* _dataset; //!< Pointer to the stored dataset.
  49. mutable const table_t* _table; //!< Table this context/dataset belongs to
  50. friend __impl::change_context_builder;
  51. public:
  52. /**
  53. * @brief Constructor.
  54. *
  55. * @param[in] p_schema Mariadb driver schema to use for the operation.
  56. * @param[in] p_connection Mariadb connection to execute queries with.
  57. * @param[in] p_dataset Dataset to store in the context.
  58. */
  59. template<typename T_dataset>
  60. inline data_context(
  61. const schema_t& p_schema,
  62. ::cppmariadb::connection& p_connection,
  63. T_dataset& p_dataset);
  64. /**
  65. * @brief Get a reference to the stored dataset if the type matches.
  66. * If an invalid type is requested an exception is thrown.
  67. */
  68. template<typename T_dataset>
  69. inline decltype(auto) get() const;
  70. private:
  71. /**
  72. * @brief Set the dataset that is stored in this context.
  73. */
  74. template<typename T_dataset>
  75. inline void set(T_dataset& dataset);
  76. };
  77. namespace __impl
  78. {
  79. /**
  80. * @brief Helper class to create the change_context predicate.
  81. */
  82. template<typename X, typename = void>
  83. struct change_context_builder;
  84. }
  85. /**
  86. * @brief Change the stored dataset of any data_context.
  87. *
  88. * @param[in] context Any data_context.
  89. * @param[in] dataset New dataset of the context.
  90. *
  91. * @return The new conext that stores the passed dataset.
  92. */
  93. constexpr decltype(auto) change_context = cppmp::generic_predicate<__impl::change_context_builder> { };
  94. #if 0
  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. } }