query/include/matador/sql/backend_provider.hpp

54 lines
1.5 KiB
C++

#ifndef QUERY_BACKEND_PROVIDER_HPP
#define QUERY_BACKEND_PROVIDER_HPP
#include "matador/utils/library.hpp"
#include <memory>
#include <string>
#include <unordered_map>
namespace matador::sql {
class connection_impl;
struct connection_info;
class dialect;
class backend_provider
{
private:
backend_provider();
public:
static backend_provider& instance();
connection_impl* create_connection(const std::string &connection_type, const connection_info &info);
void destroy_connection(const std::string &connection_type, connection_impl *c);
const dialect& connection_dialect(const std::string &connection_type);
private:
struct backend_context {
backend_context(const std::string &connection_type,
const std::string &backends_path);
backend_context(const backend_context&) = delete;
backend_context& operator=(const backend_context&) = delete;
backend_context(backend_context&&) noexcept = default;
backend_context& operator=(backend_context&&) noexcept = default;
~backend_context();
typedef connection_impl*(*create_func)(const connection_info&);
typedef void (*destroy_func)(connection_impl*);
typedef const dialect*(*dialect_func)();
create_func create_connection{};
destroy_func destroy_connection{};
dialect_func get_dialect{};
utils::library lib;
};
private:
using backends_t = std::unordered_map<std::string, std::unique_ptr<backend_context>>;
backends_t backends_;
std::string backends_path_;
};
}
#endif //QUERY_BACKEND_PROVIDER_HPP