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.

167 lines
5.6 KiB

  1. #pragma once
  2. #include "impl/driver_impl.h"
  3. namespace cpphibernate {
  4. namespace mariadb {
  5. /**
  6. * @brief Mariadb driver.
  7. *
  8. * The normal cpphibernate context will be inherited from this class. So all methods
  9. * defined here will be part of the context class.
  10. */
  11. struct driver_t
  12. {
  13. private:
  14. driver_impl_t _impl; //!< Driver implementation.
  15. ::cppmariadb::connection * _connection; //!< Mariadb connection to use for queries.
  16. public:
  17. /**
  18. * @brief Constructor. Initializes the driver with the passed schema.
  19. *
  20. * @param[in] p_schema Schema to use with this driver.
  21. */
  22. template<typename T_schema>
  23. inline driver_t(T_schema&& p_schema);
  24. /**
  25. * @brief Constructor. Initializes the driver with the passed schema.
  26. *
  27. * @param[in] p_schema Schema to use with this driver.
  28. * @param[in] p_connection Connection to the mariadb server to use for queries.
  29. */
  30. template<typename T_schema>
  31. inline driver_t(T_schema&& p_schema, ::cppmariadb::connection& p_connection);
  32. /**
  33. * @brief Print the schema of the driver to the passed stream.
  34. */
  35. inline std::ostream& print(std::ostream& os) const;
  36. /**
  37. * @brief Get the connection currently assigned to the driver.
  38. */
  39. inline ::cppmariadb::connection * connection() const;
  40. /**
  41. * @brief Set the connection to use for queries.
  42. * This will invalidate all cached queries and strings.
  43. */
  44. inline void connection(::cppmariadb::connection * p_connection);
  45. protected:
  46. /**
  47. * @brief Get the imlementation object of the driver.
  48. */
  49. inline auto& impl() const;
  50. /*
  51. public:
  52. using lock_type = std::unique_ptr<transaction_lock>;
  53. private:
  54. ::cppmariadb::connection* _connection;
  55. schema_t _schema;
  56. filter_t _filter;
  57. public:
  58. template<typename T_schema>
  59. mariadb_driver_t(T_schema&& p_schema)
  60. : _schema(make_schema(std::forward<T_schema>(p_schema)))
  61. { }
  62. cpphibernate_copyable(mariadb_driver_t, delete);
  63. cpphibernate_moveable(mariadb_driver_t, default);
  64. inline const ::cppmariadb::connection& connection() const
  65. { return *_connection; }
  66. inline void connection(::cppmariadb::connection& p_connection)
  67. { _connection = &p_connection; }
  68. template<typename... T_args>
  69. inline void set_filter_inclusive(T_args&&... args)
  70. { _filter.set_inclusive(_schema, std::forward<T_args>(args)...); }
  71. template<typename... T_args>
  72. inline void set_filter_exclusive(T_args&&... args)
  73. { _filter.set_exclusive(_schema, std::forward<T_args>(args)...); }
  74. inline void clear_filter()
  75. { _filter.clear(); }
  76. inline lock_type lock()
  77. { return std::make_unique<transaction_lock>(*_connection); }
  78. template<typename T_modifiers>
  79. inline std::string build_query(const std::string& query, T_modifiers&& modifiers) const
  80. {
  81. auto where = build_where(_schema, modifiers).query(*_connection);
  82. std::ostringstream os;
  83. os << query;
  84. if (!where.empty())
  85. os << " " << where;
  86. return os.str();
  87. }
  88. protected:
  89. inline void init_impl(bool recreate) const
  90. {
  91. transaction_lock trans(*_connection);
  92. _schema.init(init_context(_schema, *_connection, recreate));
  93. trans.commit();
  94. }
  95. template<typename T_dataset>
  96. inline void create_impl(T_dataset& dataset) const
  97. {
  98. create_update_impl_t<T_dataset>::apply(
  99. create_update_context(dataset, _schema, *_connection, _filter, false));
  100. }
  101. template<typename T_dataset, typename T_modifiers>
  102. inline void read_impl(T_dataset& dataset, T_modifiers&& modifiers) const
  103. {
  104. using dataset_type = mp::decay_t<T_dataset>;
  105. using real_dataset_type = misc::real_dataset_t<dataset_type>;
  106. auto dataset_id = misc::get_type_id(hana::type_c<real_dataset_type>);
  107. auto& table = _schema.table(dataset_id);
  108. auto context = make_read_context(dataset, _schema, *_connection, _filter);
  109. context.where = build_where(_schema, modifiers).query(*_connection);
  110. context.limit = build_limit(modifiers).query(*_connection);
  111. context.order_by = build_order_by(_schema, modifiers).query(*_connection);
  112. transaction_lock trans(*_connection);
  113. table.read(context);
  114. trans.commit();
  115. }
  116. template<typename T_dataset>
  117. inline void update_impl(T_dataset& dataset) const
  118. {
  119. create_update_impl_t<T_dataset>::apply(
  120. create_update_context(dataset, _schema, *_connection, _filter, true));
  121. }
  122. template<typename T_dataset>
  123. inline void destroy_impl(T_dataset& dataset) const
  124. {
  125. using dataset_type = mp::decay_t<T_dataset>;
  126. using real_dataset_type = misc::real_dataset_t<dataset_type>;
  127. auto dataset_id = misc::get_type_id(hana::type_c<real_dataset_type>);
  128. auto& table = _schema.table(dataset_id);
  129. destroy_context context(dataset, _schema, *_connection);
  130. context.where = table.get_where_primary_key(context);
  131. destroy_impl_t<T_dataset>::apply(context);
  132. }
  133. */
  134. };
  135. } }