added sqlite db backend progress
This commit is contained in:
@@ -3,29 +3,39 @@
|
||||
|
||||
#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
|
||||
{
|
||||
public:
|
||||
explicit backend_provider(std::string backends_path);
|
||||
private:
|
||||
backend_provider();
|
||||
|
||||
connection_impl* create_connection(const std::string &connection_type);
|
||||
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)();
|
||||
typedef connection_impl*(*create_func)(const connection_info&);
|
||||
typedef void (*destroy_func)(connection_impl*);
|
||||
typedef const dialect*(*dialect_func)();
|
||||
|
||||
@@ -35,7 +45,7 @@ private:
|
||||
utils::library lib;
|
||||
};
|
||||
private:
|
||||
using backends_t = std::unordered_map<std::string, backend_context>;
|
||||
using backends_t = std::unordered_map<std::string, std::unique_ptr<backend_context>>;
|
||||
backends_t backends_;
|
||||
std::string backends_path_;
|
||||
};
|
||||
|
||||
@@ -1,32 +1,36 @@
|
||||
#ifndef QUERY_CONNECTION_HPP
|
||||
#define QUERY_CONNECTION_HPP
|
||||
|
||||
#include "matador/sql/query_intermediates.hpp"
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
#include "matador/sql/query_result.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class connection_impl;
|
||||
|
||||
class connection
|
||||
{
|
||||
public:
|
||||
connection() = default;
|
||||
explicit connection(std::string dns);
|
||||
explicit connection(connection_info info);
|
||||
explicit connection(const std::string& dns);
|
||||
~connection();
|
||||
|
||||
void open();
|
||||
void close();
|
||||
[[nodiscard]] bool is_open() const;
|
||||
|
||||
[[nodiscard]] const std::string& dns() const;
|
||||
[[nodiscard]] const connection_info& info() const;
|
||||
|
||||
query_result<record> fetch(const std::string &sql);
|
||||
std::pair<size_t, std::string> execute(const std::string &sql);
|
||||
|
||||
private:
|
||||
std::string dns_;
|
||||
const connection_info connection_info_;
|
||||
bool is_open_{false};
|
||||
connection_impl *connection_{};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -18,12 +18,12 @@ public:
|
||||
virtual void prepare(const std::string &stmt) = 0;
|
||||
|
||||
protected:
|
||||
explicit connection_impl(connection_info info);
|
||||
explicit connection_impl(const connection_info &info);
|
||||
|
||||
[[nodiscard]] const connection_info &info() const;
|
||||
|
||||
private:
|
||||
connection_info info_;
|
||||
const connection_info & info_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -40,10 +40,11 @@ template < class Connection >
|
||||
class connection_pool
|
||||
{
|
||||
public:
|
||||
connection_pool(const std::string &db, unsigned int count) {
|
||||
connection_pool(const std::string &dns, unsigned int count)
|
||||
: info_(connection_info::parse(dns)) {
|
||||
connection_repo_.reserve(count);
|
||||
for (auto i = 0U; i < count; ++i) {
|
||||
connection_repo_.emplace_back(db + std::to_string(i+1));
|
||||
connection_repo_.emplace_back(info_);
|
||||
idle_connections_.push(&connection_repo_.back());
|
||||
idle_connections_.back()->open();
|
||||
}
|
||||
@@ -87,6 +88,9 @@ public:
|
||||
return inuse_connections_.size();
|
||||
}
|
||||
|
||||
const connection_info &info() const {
|
||||
return info_;
|
||||
}
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
std::vector<Connection> connection_repo_;
|
||||
@@ -95,6 +99,8 @@ private:
|
||||
using connection_set = std::unordered_set<pointer>;
|
||||
connections idle_connections_;
|
||||
connection_set inuse_connections_;
|
||||
|
||||
const connection_info info_;
|
||||
};
|
||||
|
||||
template<class Connection>
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#ifndef QUERY_QUERY_RESULT_IMPL_HPP
|
||||
#define QUERY_QUERY_RESULT_IMPL_HPP
|
||||
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class query_result_impl
|
||||
{
|
||||
public:
|
||||
virtual void read_value(const char *id, size_t index, char &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, short &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, int &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, long &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, long long &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, unsigned char &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, unsigned short &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, unsigned int &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, unsigned long &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, unsigned long long &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, bool &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, float &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, double &value) = 0;
|
||||
// virtual void read_value(const char *id, size_t index, matador::time &value) = 0;
|
||||
// virtual void read_value(const char *id, size_t index, matador::date &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, char *value, size_t s) = 0;
|
||||
virtual void read_value(const char *id, size_t index, std::string &value) = 0;
|
||||
virtual void read_value(const char *id, size_t index, std::string &value, size_t s) = 0;
|
||||
|
||||
template<typename ValueType>
|
||||
void on_primary_key(const char *id, ValueType &value, typename std::enable_if<std::is_integral<ValueType>::value && !std::is_same<bool, ValueType>::value>::type* = 0)
|
||||
{
|
||||
read_value(id, column_index_++, value);
|
||||
}
|
||||
void on_primary_key(const char *id, std::string &value, size_t size);
|
||||
void on_revision(const char *id, unsigned long long &rev);
|
||||
|
||||
template < class Type >
|
||||
void on_attribute(const char *id, Type &x, const utils::field_attributes &/*attr*/ = utils::null_attributes)
|
||||
{
|
||||
read_value(id, column_index_++, x);
|
||||
}
|
||||
void on_attribute(const char *id, char *value, const utils::field_attributes &attr = utils::null_attributes);
|
||||
void on_attribute(const char *id, std::string &value, const utils::field_attributes &attr = utils::null_attributes);
|
||||
|
||||
// void on_belongs_to(const char *id, matador::identifiable_holder &x, cascade_type);
|
||||
// void on_has_one(const char *id, matador::identifiable_holder &x, cascade_type);
|
||||
|
||||
// void on_has_many(const char *, abstract_container &, const char *, const char *, cascade_type) {}
|
||||
// void on_has_many(const char *, abstract_container &, cascade_type) {}
|
||||
|
||||
protected:
|
||||
size_t column_index_ = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_QUERY_RESULT_IMPL_HPP
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
#include "matador/sql/query_intermediates.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
@@ -23,7 +25,6 @@ public:
|
||||
|
||||
private:
|
||||
connection_pool<connection> &pool_;
|
||||
dialect dialect_;
|
||||
query_builder query_;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user