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.
 
 
 

93 lines
2.8 KiB

  1. #pragma once
  2. #include <cpphibernate/config.h>
  3. #include "base_context.h"
  4. namespace cpphibernate {
  5. namespace mariadb {
  6. namespace __impl
  7. {
  8. /**
  9. * @brief Helper class to create the change_context predicate.
  10. */
  11. template<typename X, typename = void>
  12. struct change_context_builder;
  13. }
  14. struct table_t;
  15. /**
  16. * @brief Predicate to change the stored dataset of any data_context.
  17. */
  18. constexpr decltype(auto) change_context = cppmp::generic_predicate<__impl::change_context_builder> { };
  19. /**
  20. * @brief Mariadb driver context that helds a specific dataset.
  21. */
  22. struct data_context
  23. : public base_context
  24. {
  25. private:
  26. mutable size_t _dataset_id; //!< Unique type id of the dataset that is stored in this context.
  27. mutable void * _dataset; //!< Pointer to the stored dataset.
  28. mutable const table_t * _table; //!< Table this context/dataset belongs to
  29. template<typename X, typename T_enable>
  30. friend struct __impl::change_context_builder;
  31. protected:
  32. /**
  33. * @brief Constructor.
  34. *
  35. * @param[in] p_schema Mariadb driver schema to use for the operation.
  36. * @param[in] p_connection Mariadb connection to execute queries with.
  37. */
  38. inline data_context(
  39. const schema_t& p_schema,
  40. ::cppmariadb::connection& p_connection);
  41. public:
  42. /**
  43. * @brief Constructor.
  44. *
  45. * @param[in] p_schema Mariadb driver schema to use for the operation.
  46. * @param[in] p_connection Mariadb connection to execute queries with.
  47. * @param[in] p_dataset Dataset to store in the context.
  48. */
  49. template<typename T_dataset>
  50. inline data_context(
  51. const schema_t& p_schema,
  52. ::cppmariadb::connection& p_connection,
  53. T_dataset& p_dataset);
  54. /**
  55. * @brief Get a reference to the stored dataset if the type matches.
  56. * If an invalid type is requested an exception is thrown.
  57. */
  58. template<typename T_dataset>
  59. inline decltype(auto) get() const;
  60. protected:
  61. /**
  62. * @brief Set the dataset that is stored in this context.
  63. *
  64. * @param[in] dataset Dataset to store in the context.
  65. * @param[in] dataset_id Unique id of the dataset stored in the context.
  66. *
  67. * @return Pointer to the stored dataset.
  68. */
  69. template<typename T_dataset>
  70. inline void * set(T_dataset& dataset, size_t dataset_id = 0) const;
  71. /**
  72. * @brief Clear the context (set the stored dataset to nullptr).
  73. */
  74. inline void clear() const;
  75. };
  76. } }