added sqlite connection class (progress)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#ifndef QUERY_CONNECTION_HPP
|
||||
#define QUERY_CONNECTION_HPP
|
||||
|
||||
#include "matador/sql/connection_intermediates.hpp"
|
||||
#include "matador/sql/query_intermediates.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
|
||||
@@ -12,20 +12,21 @@ namespace matador::sql {
|
||||
class connection
|
||||
{
|
||||
public:
|
||||
connection();
|
||||
query_create_intermediate create();
|
||||
query_drop_intermediate drop();
|
||||
query_select_intermediate select(std::initializer_list<std::string> column_names);
|
||||
query_insert_intermediate insert();
|
||||
query_update_intermediate update(const std::string &table);
|
||||
query_delete_intermediate remove();
|
||||
connection() = default;
|
||||
explicit connection(std::string dns);
|
||||
|
||||
void open();
|
||||
void close();
|
||||
[[nodiscard]] bool is_open() const;
|
||||
|
||||
[[nodiscard]] const std::string& dns() const;
|
||||
|
||||
query_result<record> fetch(const std::string &sql);
|
||||
std::pair<size_t, std::string> execute(const std::string &sql);
|
||||
|
||||
private:
|
||||
dialect dialect_;
|
||||
query_builder query_;
|
||||
std::string dns_;
|
||||
bool is_open_{false};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#ifndef QUERY_CONNECTION_POOL_HPP
|
||||
#define QUERY_CONNECTION_POOL_HPP
|
||||
|
||||
#include <utility>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
template < class Connection >
|
||||
class connection_pool;
|
||||
|
||||
template < class Connection >
|
||||
class connection_ptr
|
||||
{
|
||||
public:
|
||||
connection_ptr(Connection *c, connection_pool<Connection> &pool)
|
||||
: connection_(c), pool_(pool) {}
|
||||
~connection_ptr();
|
||||
connection_ptr(const connection_ptr &) = delete;
|
||||
connection_ptr& operator=(const connection_ptr &) = delete;
|
||||
connection_ptr(connection_ptr &&) noexcept = default;
|
||||
connection_ptr& operator=(connection_ptr &&) noexcept = default;
|
||||
|
||||
Connection* operator->() { return connection_; }
|
||||
Connection& operator*() { return *connection_; }
|
||||
|
||||
[[nodiscard]] bool valid() const { return connection_ != nullptr; }
|
||||
|
||||
private:
|
||||
friend class connection_pool<Connection>;
|
||||
|
||||
Connection *connection_;
|
||||
connection_pool<Connection> &pool_;
|
||||
};
|
||||
|
||||
template < class Connection >
|
||||
class connection_pool
|
||||
{
|
||||
public:
|
||||
connection_pool(const std::string &db, unsigned int count) {
|
||||
connection_repo_.reserve(count);
|
||||
for (auto i = 0U; i < count; ++i) {
|
||||
connection_repo_.emplace_back(db + std::to_string(i+1));
|
||||
idle_connections_.push(&connection_repo_.back());
|
||||
idle_connections_.back()->open();
|
||||
}
|
||||
}
|
||||
|
||||
connection_ptr<Connection> acquire() {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
if (idle_connections_.empty()) {
|
||||
return {nullptr, *this};
|
||||
}
|
||||
|
||||
auto ptr = idle_connections_.front();
|
||||
idle_connections_.pop();
|
||||
inuse_connections_.insert(ptr);
|
||||
return { ptr, *this };
|
||||
}
|
||||
|
||||
void release(Connection *c) {
|
||||
if (c == nullptr) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
|
||||
if (inuse_connections_.erase(c) > 0) {
|
||||
idle_connections_.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
void release(connection_ptr<Connection> &c) {
|
||||
release(c.connection_);
|
||||
c.connection_ = nullptr;
|
||||
}
|
||||
|
||||
std::size_t size() const { return connection_repo_.size(); }
|
||||
std::size_t idle() const {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
return idle_connections_.size();
|
||||
}
|
||||
std::size_t inuse() const {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
return inuse_connections_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
std::vector<Connection> connection_repo_;
|
||||
using pointer = Connection*;
|
||||
using connections = std::queue<pointer>;
|
||||
using connection_set = std::unordered_set<pointer>;
|
||||
connections idle_connections_;
|
||||
connection_set inuse_connections_;
|
||||
};
|
||||
|
||||
template<class Connection>
|
||||
connection_ptr<Connection>::~connection_ptr() {
|
||||
pool_.release(connection_);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_CONNECTION_POOL_HPP
|
||||
+7
-7
@@ -1,5 +1,5 @@
|
||||
#ifndef QUERY_CONNECTION_INTERMEDIATES_HPP
|
||||
#define QUERY_CONNECTION_INTERMEDIATES_HPP
|
||||
#ifndef QUERY_QUERY_INTERMEDIATES_HPP
|
||||
#define QUERY_QUERY_INTERMEDIATES_HPP
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/key_value_pair.hpp"
|
||||
@@ -11,20 +11,20 @@
|
||||
namespace matador::sql {
|
||||
|
||||
class basic_condition;
|
||||
class connection;
|
||||
class session;
|
||||
class query_builder;
|
||||
|
||||
class query_intermediate
|
||||
{
|
||||
public:
|
||||
query_intermediate(connection &db, query_builder &query);
|
||||
query_intermediate(session &db, query_builder &query);
|
||||
|
||||
protected:
|
||||
connection& db();
|
||||
session& db();
|
||||
query_builder& query();
|
||||
|
||||
private:
|
||||
connection &db_;
|
||||
session &db_;
|
||||
query_builder &query_;
|
||||
};
|
||||
|
||||
@@ -195,4 +195,4 @@ public:
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_CONNECTION_INTERMEDIATES_HPP
|
||||
#endif //QUERY_QUERY_INTERMEDIATES_HPP
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef QUERY_SESSION_HPP
|
||||
#define QUERY_SESSION_HPP
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class session
|
||||
{
|
||||
public:
|
||||
explicit session(connection_pool<connection> &pool);
|
||||
|
||||
query_create_intermediate create();
|
||||
query_drop_intermediate drop();
|
||||
query_select_intermediate select(std::initializer_list<std::string> column_names);
|
||||
query_insert_intermediate insert();
|
||||
query_update_intermediate update(const std::string &table);
|
||||
query_delete_intermediate remove();
|
||||
|
||||
query_result<record> fetch(const std::string &sql);
|
||||
std::pair<size_t, std::string> execute(const std::string &sql);
|
||||
|
||||
private:
|
||||
connection_pool<connection> &pool_;
|
||||
dialect dialect_;
|
||||
query_builder query_;
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_SESSION_HPP
|
||||
@@ -0,0 +1,107 @@
|
||||
#ifndef QUERY_LIBRARY_HPP
|
||||
#define QUERY_LIBRARY_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
#define _WINSOCKAPI_
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
#ifndef MATADOR_DOXYGEN_DOC
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
typedef FARPROC func_ptr;
|
||||
#else
|
||||
typedef void *func_ptr;
|
||||
#endif
|
||||
#endif /* MATADOR_DOXYGEN_DOC */
|
||||
|
||||
/**
|
||||
* @class library
|
||||
* @brief Helps to load and unload
|
||||
* external libraries.
|
||||
*
|
||||
* This class represents a loader and un-loader
|
||||
* for an external library. The path to the library
|
||||
* is given by a string.
|
||||
* It can be loaded and unloaded. Furthermore it
|
||||
* provides a method to get a pointer to an function
|
||||
* exported by the library. The function is identified
|
||||
* by a name.
|
||||
*/
|
||||
class library
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create a unspecified library serializable
|
||||
*/
|
||||
library() = default;
|
||||
|
||||
/**
|
||||
* Create a library for the given lib path
|
||||
*
|
||||
* @param lib The path to the library to map.
|
||||
*/
|
||||
explicit library(std::string lib);
|
||||
|
||||
~library();
|
||||
|
||||
/**
|
||||
* Load the underlying library.
|
||||
* If the library path is invalid
|
||||
* or the library cannot be loaded
|
||||
* false is returned.
|
||||
*
|
||||
* @return True on successful library loading.
|
||||
*/
|
||||
bool load();
|
||||
|
||||
/**
|
||||
* Sets the library (and subsequently close and
|
||||
* overwrite an already instantiated library) and
|
||||
* load the underlying library. If the library
|
||||
* path is invalid or the library cannot be loaded
|
||||
* false is returned.
|
||||
*
|
||||
* @param lib The path to the library to load.
|
||||
* @return True on successful library loading.
|
||||
*/
|
||||
bool load(const std::string &lib);
|
||||
|
||||
/**
|
||||
* Unload an loaded library. If
|
||||
* the library isn't loaded or an
|
||||
* error occurred while unloading
|
||||
* false is returned.
|
||||
*
|
||||
* @return True on successful unloading.
|
||||
*/
|
||||
bool unload();
|
||||
|
||||
/**
|
||||
* Returns the pointer to the exported
|
||||
* function of the library identified
|
||||
* by the given name.
|
||||
*
|
||||
* @param f The name of the exported function.
|
||||
* @return The pointer to the function.
|
||||
*/
|
||||
[[nodiscard]] func_ptr function(const std::string &f) const;
|
||||
|
||||
private:
|
||||
std::string lib_;
|
||||
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
HMODULE handle_ = nullptr;
|
||||
#else
|
||||
void *handle_ = nullptr;
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_LIBRARY_HPP
|
||||
Reference in New Issue
Block a user