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.
 
 
 

161 lines
5.2 KiB

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