#pragma once #include #include "classes.h" #include "impl/driver_impl.h" namespace cpphibernate { namespace mariadb { /** * @brief Mariadb driver. * * The normal cpphibernate context will be inherited from this class. So all methods * defined here will be part of the context class. */ struct driver_t { private: driver_impl_t _impl; //!< Driver implementation. ::cppmariadb::connection * _connection; //!< Mariadb connection to use for queries. public: /** * @brief Constructor. Initializes the driver with the passed schema. */ template inline driver_t(T_schema&& p_schema); /** * @brief Print the schema of the driver to the passed stream. */ inline std::ostream& print(std::ostream& os) const; /** * @brief Get the connection currently assigned to the driver. */ inline ::cppmariadb::connection * connection() const; /** * @brief Set the connection to use for queries. * This will invalidate all cached queries and strings. */ inline void connection(::cppmariadb::connection * p_connection); protected: /** * @brief Get the imlementation object of the driver. */ inline auto& impl() const; /* public: using lock_type = std::unique_ptr; private: ::cppmariadb::connection* _connection; schema_t _schema; filter_t _filter; public: template mariadb_driver_t(T_schema&& p_schema) : _schema(make_schema(std::forward(p_schema))) { } cpphibernate_copyable(mariadb_driver_t, delete); cpphibernate_moveable(mariadb_driver_t, default); inline const ::cppmariadb::connection& connection() const { return *_connection; } inline void connection(::cppmariadb::connection& p_connection) { _connection = &p_connection; } template inline void set_filter_inclusive(T_args&&... args) { _filter.set_inclusive(_schema, std::forward(args)...); } template inline void set_filter_exclusive(T_args&&... args) { _filter.set_exclusive(_schema, std::forward(args)...); } inline void clear_filter() { _filter.clear(); } inline lock_type lock() { return std::make_unique(*_connection); } template inline std::string build_query(const std::string& query, T_modifiers&& modifiers) const { auto where = build_where(_schema, modifiers).query(*_connection); std::ostringstream os; os << query; if (!where.empty()) os << " " << where; return os.str(); } protected: inline void init_impl(bool recreate) const { transaction_lock trans(*_connection); _schema.init(init_context(_schema, *_connection, recreate)); trans.commit(); } template inline void create_impl(T_dataset& dataset) const { create_update_impl_t::apply( create_update_context(dataset, _schema, *_connection, _filter, false)); } template inline void read_impl(T_dataset& dataset, T_modifiers&& modifiers) const { using dataset_type = mp::decay_t; using real_dataset_type = misc::real_dataset_t; auto dataset_id = misc::get_type_id(hana::type_c); auto& table = _schema.table(dataset_id); auto context = make_read_context(dataset, _schema, *_connection, _filter); context.where = build_where(_schema, modifiers).query(*_connection); context.limit = build_limit(modifiers).query(*_connection); context.order_by = build_order_by(_schema, modifiers).query(*_connection); transaction_lock trans(*_connection); table.read(context); trans.commit(); } template inline void update_impl(T_dataset& dataset) const { create_update_impl_t::apply( create_update_context(dataset, _schema, *_connection, _filter, true)); } template inline void destroy_impl(T_dataset& dataset) const { using dataset_type = mp::decay_t; using real_dataset_type = misc::real_dataset_t; auto dataset_id = misc::get_type_id(hana::type_c); auto& table = _schema.table(dataset_id); destroy_context context(dataset, _schema, *_connection); context.where = table.get_where_primary_key(context); destroy_impl_t::apply(context); } */ }; } } #include "driver.inl"