82 lines
2.6 KiB
C++
82 lines
2.6 KiB
C++
#include "matador/sql/backend_provider.hpp"
|
|
// #include "matador/sql/noop_connection.hpp"
|
|
// #include "matador/sql/dialect_builder.hpp"
|
|
|
|
#include "matador/sql/interface/connection_impl.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
#include <cstdint>
|
|
|
|
namespace matador::sql {
|
|
backend_provider::backend_provider() = default;
|
|
// {
|
|
// backends_.emplace("noop", std::make_unique<noop_backend_context>());
|
|
// }
|
|
|
|
backend_provider &backend_provider::instance() {
|
|
static backend_provider provider;
|
|
return provider;
|
|
}
|
|
|
|
connection_impl *backend_provider::create_connection(const std::string &connection_type, const connection_info &info)
|
|
{
|
|
auto it = backends_.find(connection_type);
|
|
if (it == backends_.end()) {
|
|
it = backends_.emplace(connection_type, std::make_unique<backend_service>(connection_type)).first;
|
|
}
|
|
return it->second->create(info);
|
|
}
|
|
|
|
void backend_provider::destroy_connection(const std::string &connection_type, connection_impl *c)
|
|
{
|
|
auto it = backends_.find(connection_type);
|
|
if (it == backends_.end()) {
|
|
it = backends_.emplace(connection_type, std::make_unique<backend_service>(connection_type)).first;
|
|
}
|
|
return it->second->destroy(c);
|
|
}
|
|
|
|
const dialect &backend_provider::connection_dialect(const std::string &connection_type) {
|
|
auto it = backends_.find(connection_type);
|
|
if (it == backends_.end()) {
|
|
it = backends_.emplace(connection_type, std::make_unique<backend_service>(connection_type)).first;
|
|
}
|
|
return *it->second->dialect();
|
|
}
|
|
void backend_provider::register_backend(const std::string &connection_type, std::unique_ptr<basic_backend_service> &&service) {
|
|
backends_.emplace(connection_type, std::move(service));
|
|
}
|
|
|
|
backend_provider::backend_service::backend_service(const std::string &connection_type)
|
|
{
|
|
if (!lib.load("matador-" + connection_type)) {
|
|
throw std::runtime_error("couldn't load library '" + connection_type + "'");
|
|
}
|
|
|
|
create_connection = reinterpret_cast<create_func>(reinterpret_cast<std::uintptr_t>(lib.function("create_database")));
|
|
destroy_connection = reinterpret_cast<destroy_func>(reinterpret_cast<std::uintptr_t>(lib.function("destroy_database")));
|
|
get_dialect = reinterpret_cast<dialect_func >(reinterpret_cast<std::uintptr_t>(lib.function("get_dialect")));
|
|
}
|
|
|
|
connection_impl *backend_provider::backend_service::create(const connection_info &info)
|
|
{
|
|
return (create_connection)(info);
|
|
}
|
|
|
|
void backend_provider::backend_service::destroy(connection_impl *conn)
|
|
{
|
|
(destroy_connection)(conn);
|
|
}
|
|
|
|
const dialect *backend_provider::backend_service::dialect() const
|
|
{
|
|
return (get_dialect)();
|
|
}
|
|
|
|
backend_provider::backend_service::~backend_service()
|
|
{
|
|
lib.unload();
|
|
}
|
|
|
|
} |