query/src/sql/backend_provider.cpp

46 lines
1.6 KiB
C++

#include "matador/sql/backend_provider.hpp"
#include <cstdint>
#include <stdexcept>
#include <utility>
namespace matador::sql {
backend_provider::backend_provider(std::string backends_path)
: backends_path_(std::move(backends_path)) {}
connection_impl *backend_provider::create_connection(const std::string &connection_type)
{
auto it = backends_.find(connection_type);
if (it == backends_.end()) {
it = backends_.emplace(connection_type, backend_context{connection_type, backends_path_}).first;
}
return (*it->second.create_connection)();
}
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, backend_context{connection_type, backends_path_}).first;
}
return *(*it->second.get_dialect)();
}
backend_provider::backend_context::backend_context(const std::string &connection_type,
const std::string &backends_path)
{
if (!lib.load(backends_path + "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")));
}
backend_provider::backend_context::~backend_context()
{
lib.unload();
}
}