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
+30 -7
View File
@@ -6,6 +6,7 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
namespace matador::sql {
@@ -26,28 +27,50 @@ public:
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);
struct basic_backend_context {
virtual ~basic_backend_context() = default;
[[nodiscard]] virtual connection_impl* create(const connection_info&) = 0;
virtual void destroy(connection_impl*) = 0;
[[nodiscard]] virtual const sql::dialect* dialect() const = 0;
};
class backend_context final : public basic_backend_context {
public:
explicit backend_context(const std::string &connection_type);
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();
~backend_context() override;
[[nodiscard]] connection_impl* create(const connection_info&) override;
void destroy(connection_impl *conn) override;
[[nodiscard]] const class dialect* dialect() const override;
private:
typedef connection_impl*(*create_func)(const connection_info&);
typedef void (*destroy_func)(connection_impl*);
typedef const dialect*(*dialect_func)();
typedef const class dialect*(*dialect_func)();
create_func create_connection{};
destroy_func destroy_connection{};
dialect_func get_dialect{};
utils::library lib;
};
class noop_backend_context final : public basic_backend_context {
public:
connection_impl *create(const connection_info &info) override;
void destroy(connection_impl *impl) override;
[[nodiscard]] const sql::dialect *dialect() const override;
private:
std::unordered_set<std::unique_ptr<connection_impl>> noop_connections_;
};
private:
using backends_t = std::unordered_map<std::string, std::unique_ptr<backend_context>>;
using backends_t = std::unordered_map<std::string, std::unique_ptr<basic_backend_context>>;
backends_t backends_;
std::string backends_path_;
};
}
#endif //QUERY_BACKEND_PROVIDER_HPP
+26
View File
@@ -0,0 +1,26 @@
#ifndef QUERY_NOOP_CONNECTION_HPP
#define QUERY_NOOP_CONNECTION_HPP
#include "matador/sql/connection_impl.hpp"
namespace matador::sql {
class noop_connection final : public connection_impl
{
public:
explicit noop_connection(const connection_info &info);
void open() override;
void close() override;
bool is_open() override;
size_t execute(const std::string &stmt) override;
std::unique_ptr<query_result_impl> fetch(const std::string &stmt) override;
std::unique_ptr<statement_impl> prepare(query_context context) override;
record describe(const std::string &table) override;
bool exists(const std::string &schema_name, const std::string &table_name) override;
private:
bool is_open_{false};
};
}
#endif //QUERY_NOOP_CONNECTION_HPP
+2 -3
View File
@@ -9,14 +9,13 @@
namespace matador::sql {
class query_part;
struct query_data
{
// SqlCommands command;
std::vector<std::unique_ptr<query_part>> parts;
std::vector<column_definition> columns;
std::unordered_map<std::type_index, table> table_map_by_index;
using table_ref = std::reference_wrapper<table>;
std::unordered_map<std::string, table_ref> table_map_by_name;
};
}
+2 -3
View File
@@ -240,8 +240,7 @@ public:
if (!schema_->exists<Type>()) {
schema_->attach<Type>(table.name);
}
data_.parts.push_back(std::make_unique<query_create_table_part>(table, column_generator::generate<Type>(*schema_)));
return {connection_, schema_, data_};
return this->table(table, column_generator::generate<Type>(*schema_));
}
};
@@ -261,7 +260,7 @@ public:
query_into_intermediate into(const sql::table &table, std::initializer_list<column> column_names);
template<class Type>
query_into_intermediate into(const sql::table &table)
{;
{
data_.parts.push_back(std::make_unique<query_into_part>(table, column_name_generator::generate<Type>(*schema_)));
return {connection_, schema_, data_};
}