added noop_connection for testing

This commit is contained in:
2024-02-26 20:06:46 +01:00
parent b1f2d94c7a
commit 2d9a4f3866
8 changed files with 175 additions and 27 deletions
+2
View File
@@ -32,6 +32,7 @@ set(SQL_SOURCES
sql/query.cpp
sql/query_parts.cpp
sql/query_compiler.cpp
sql/noop_connection.cpp
)
set(SQL_HEADER
@@ -79,6 +80,7 @@ set(SQL_HEADER
../include/matador/sql/query_compiler.hpp
../include/matador/sql/query_data.hpp
../include/matador/sql/table.hpp
../include/matador/sql/noop_connection.hpp
)
set(QUERY_SOURCES
+48 -13
View File
@@ -1,14 +1,14 @@
#include "matador/sql/backend_provider.hpp"
#include "matador/sql/noop_connection.hpp"
#include "matador/sql/dialect_builder.hpp"
#include "matador/utils/os.hpp"
#include <cstdint>
#include <stdexcept>
namespace matador::sql {
backend_provider::backend_provider()
: backends_path_(utils::os::getenv("MATADOR_BACKENDS_PATH"))
{}
{
backends_.emplace("noop", std::make_unique<noop_backend_context>());
}
backend_provider &backend_provider::instance() {
static backend_provider provider;
@@ -19,31 +19,30 @@ connection_impl *backend_provider::create_connection(const std::string &connecti
{
auto it = backends_.find(connection_type);
if (it == backends_.end()) {
it = backends_.emplace(connection_type, std::make_unique<backend_context>(connection_type, backends_path_)).first;
it = backends_.emplace(connection_type, std::make_unique<backend_context>(connection_type)).first;
}
return (*it->second->create_connection)(info);
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_context>(connection_type, backends_path_)).first;
it = backends_.emplace(connection_type, std::make_unique<backend_context>(connection_type)).first;
}
(*it->second->destroy_connection)(c);
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_context>(connection_type, backends_path_)).first;
it = backends_.emplace(connection_type, std::make_unique<backend_context>(connection_type)).first;
}
return *(*it->second->get_dialect)();
return *it->second->dialect();
}
backend_provider::backend_context::backend_context(const std::string &connection_type,
const std::string &backends_path)
backend_provider::backend_context::backend_context(const std::string &connection_type)
{
if (!lib.load("matador-" + connection_type)) {
throw std::runtime_error("couldn't load library '" + connection_type + "'");
@@ -54,9 +53,45 @@ backend_provider::backend_context::backend_context(const std::string &connection
get_dialect = reinterpret_cast<dialect_func >(reinterpret_cast<std::uintptr_t>(lib.function("get_dialect")));
}
connection_impl *backend_provider::backend_context::create(const connection_info &info)
{
return (create_connection)(info);
}
void backend_provider::backend_context::destroy(connection_impl *conn)
{
(destroy_connection)(conn);
}
const class dialect *backend_provider::backend_context::dialect() const
{
return (get_dialect)();
}
backend_provider::backend_context::~backend_context()
{
lib.unload();
}
connection_impl *backend_provider::noop_backend_context::create(const connection_info &info)
{
return noop_connections_.insert(std::make_unique<noop_connection>(info)).first->get();
}
void backend_provider::noop_backend_context::destroy(connection_impl *impl)
{
auto it = std::find_if(noop_connections_.begin(), noop_connections_.end(), [impl](const auto &item) {
return impl == item.get();
});
if (it != noop_connections_.end()) {
noop_connections_.erase(it);
}
}
const dialect *backend_provider::noop_backend_context::dialect() const
{
static sql::dialect dialect_ = dialect_builder::builder().create().build();
return &dialect_;
}
}
+52
View File
@@ -0,0 +1,52 @@
#include "matador/sql/noop_connection.hpp"
#include "matador/sql/query_context.hpp"
#include "matador/sql/record.hpp"
#include <string>
#include <memory>
namespace matador::sql {
noop_connection::noop_connection(const connection_info &info)
: connection_impl(info) {}
void noop_connection::open()
{
is_open_ = true;
}
void noop_connection::close()
{
is_open_ = false;
}
bool noop_connection::is_open()
{
return is_open_;
}
size_t noop_connection::execute(const std::string &stmt)
{
return 0;
}
std::unique_ptr<query_result_impl> noop_connection::fetch(const std::string &stmt)
{
return {};
}
std::unique_ptr<statement_impl> noop_connection::prepare(query_context context)
{
return {};
}
record noop_connection::describe(const std::string &table)
{
return {};
}
bool noop_connection::exists(const std::string &schema_name, const std::string &table_name)
{
return false;
}
}