added sqlite db backend progress
This commit is contained in:
parent
8b5859d858
commit
ae236746ad
|
|
@ -2,6 +2,7 @@ set(HEADER
|
|||
include/sqlite_connection.hpp
|
||||
include/sqlite_error.hpp
|
||||
include/sqlite_dialect.hpp
|
||||
include/sqlite_query_result.hpp
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
#ifndef QUERY_SQLITE_CONNECTION_HPP
|
||||
#define QUERY_SQLITE_CONNECTION_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifdef matador_sqlite_EXPORTS
|
||||
#define MATADOR_SQLITE_API __declspec(dllexport)
|
||||
#else
|
||||
#define MATADOR_SQLITE_API __declspec(dllimport)
|
||||
#endif
|
||||
#pragma warning(disable: 4355)
|
||||
#else
|
||||
#define MATADOR_SQLITE_API
|
||||
#endif
|
||||
|
||||
#include "matador/sql/connection_impl.hpp"
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
|
@ -10,7 +21,7 @@ namespace matador::backends::sqlite {
|
|||
class sqlite_connection : public matador::sql::connection_impl
|
||||
{
|
||||
public:
|
||||
explicit sqlite_connection(sql::connection_info info);
|
||||
explicit sqlite_connection(const sql::connection_info &info);
|
||||
void open() override;
|
||||
void close() override;
|
||||
bool is_open() override;
|
||||
|
|
@ -23,4 +34,12 @@ private:
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
MATADOR_SQLITE_API matador::sql::connection_impl* create_database(const matador::sql::connection_info &info);
|
||||
|
||||
MATADOR_SQLITE_API void destroy_database(matador::sql::connection_impl *db);
|
||||
}
|
||||
|
||||
#endif //QUERY_SQLITE_CONNECTION_HPP
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
#ifndef QUERY_SQLITE_DIALECT_HPP
|
||||
#define QUERY_SQLITE_DIALECT_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifdef matador_sqlite_EXPORTS
|
||||
#define MATADOR_SQLITE_API __declspec(dllexport)
|
||||
#else
|
||||
#define MATADOR_SQLITE_API __declspec(dllimport)
|
||||
#endif
|
||||
#pragma warning(disable: 4355)
|
||||
#else
|
||||
#define MATADOR_SQLITE_API
|
||||
#endif
|
||||
|
||||
#include "matador/sql/dialect.hpp"
|
||||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
}
|
||||
|
||||
extern "C" [[maybe_unused]] const matador::sql::dialect* get_dialect();
|
||||
extern "C" [[maybe_unused]] MATADOR_SQLITE_API const matador::sql::dialect* get_dialect();
|
||||
|
||||
#endif //QUERY_SQLITE_DIALECT_HPP
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef QUERY_SQLITE_QUERY_RESULT_HPP
|
||||
#define QUERY_SQLITE_QUERY_RESULT_HPP
|
||||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
class sqlite_query_result {
|
||||
public:
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_SQLITE_QUERY_RESULT_HPP
|
||||
|
|
@ -5,9 +5,8 @@
|
|||
|
||||
namespace matador::backends::sqlite {
|
||||
|
||||
sqlite_connection::sqlite_connection(sql::connection_info info)
|
||||
: connection_impl(std::move(info)) {
|
||||
|
||||
sqlite_connection::sqlite_connection(const sql::connection_info &info)
|
||||
: connection_impl(info) {
|
||||
}
|
||||
|
||||
void sqlite_connection::open()
|
||||
|
|
@ -30,7 +29,7 @@ void sqlite_connection::close()
|
|||
|
||||
bool sqlite_connection::is_open()
|
||||
{
|
||||
return false;
|
||||
return sqlite_db_ != nullptr;
|
||||
}
|
||||
|
||||
void sqlite_connection::execute(const std::string &stmt)
|
||||
|
|
@ -44,3 +43,17 @@ void sqlite_connection::prepare(const std::string &stmt)
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
MATADOR_SQLITE_API matador::sql::connection_impl *create_database(const matador::sql::connection_info &info)
|
||||
{
|
||||
return new matador::backends::sqlite::sqlite_connection(info);
|
||||
}
|
||||
|
||||
MATADOR_SQLITE_API void destroy_database(matador::sql::connection_impl *db)
|
||||
{
|
||||
delete db;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
[[maybe_unused]] const matador::sql::dialect* get_dialect() {
|
||||
using namespace matador::sql;
|
||||
static dialect d {{
|
||||
const static dialect d{{
|
||||
{ dialect::token_t::BEGIN, "BEGIN TRANSACTION"},
|
||||
{ dialect::token_t::COMMIT, "COMMIT TRANSACTION"},
|
||||
{ dialect::token_t::ROLLBACK, "ROLLBACK TRANSACTION"}
|
||||
|
|
|
|||
|
|
@ -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_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
namespace matador::utils::os {
|
||||
|
||||
std::string error_string(unsigned long error);
|
||||
|
||||
std::string getenv(const char* name);
|
||||
|
||||
[[maybe_unused]] std::string getenv(const std::string &name);
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ set(SQL_SOURCES
|
|||
sql/connection_info.cpp
|
||||
sql/connection_impl.cpp
|
||||
sql/session.cpp
|
||||
sql/backend_provider.cpp)
|
||||
sql/backend_provider.cpp
|
||||
sql/query_result_impl.cpp)
|
||||
|
||||
set(SQL_HEADER
|
||||
../include/matador/sql/dialect.hpp
|
||||
|
|
@ -29,7 +30,8 @@ set(SQL_HEADER
|
|||
../include/matador/sql/connection_info.hpp
|
||||
../include/matador/sql/connection_pool.hpp
|
||||
../include/matador/sql/session.hpp
|
||||
../include/matador/sql/backend_provider.hpp)
|
||||
../include/matador/sql/backend_provider.hpp
|
||||
../include/matador/sql/query_result_impl.hpp)
|
||||
|
||||
set(UTILS_HEADER
|
||||
../include/matador/utils/field_attributes.hpp
|
||||
|
|
|
|||
|
|
@ -1,35 +1,51 @@
|
|||
#include "matador/sql/backend_provider.hpp"
|
||||
|
||||
#include "matador/utils/os.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::sql {
|
||||
backend_provider::backend_provider(std::string backends_path)
|
||||
: backends_path_(std::move(backends_path)) {}
|
||||
backend_provider::backend_provider()
|
||||
: backends_path_(utils::os::getenv("MATADOR_BACKENDS_PATH"))
|
||||
{}
|
||||
|
||||
connection_impl *backend_provider::create_connection(const std::string &connection_type)
|
||||
backend_provider &backend_provider::instance() {
|
||||
static backend_provider provider;
|
||||
return provider;
|
||||
}
|
||||
|
||||
connection_impl *backend_provider::create_connection(const std::string &connection_type, const connection_info &info)
|
||||
{
|
||||
auto it = backends_.find(connection_type);
|
||||
if (it == backends_.end()) {
|
||||
it = backends_.emplace(connection_type, backend_context{connection_type, backends_path_}).first;
|
||||
it = backends_.emplace(connection_type, std::make_unique<backend_context>(connection_type, backends_path_)).first;
|
||||
}
|
||||
return (*it->second.create_connection)();
|
||||
return (*it->second->create_connection)(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->second->destroy_connection)(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, backend_context{connection_type, backends_path_}).first;
|
||||
it = backends_.emplace(connection_type, std::make_unique<backend_context>(connection_type, backends_path_)).first;
|
||||
}
|
||||
return *(*it->second.get_dialect)();
|
||||
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)) {
|
||||
if (!lib.load("matador-" + connection_type)) {
|
||||
throw std::runtime_error("couldn't load library '" + connection_type + "'");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,30 +1,49 @@
|
|||
#include <utility>
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
#include "matador/sql/connection_impl.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
connection::connection(std::string dns)
|
||||
: dns_(std::move(dns)) {}
|
||||
connection::connection(connection_info info)
|
||||
: connection_info_(std::move(info))
|
||||
{
|
||||
connection_ = backend_provider::instance().create_connection(connection_info_.type, connection_info_);
|
||||
}
|
||||
|
||||
connection::connection(const std::string& dns)
|
||||
: connection(connection_info::parse(dns))
|
||||
{}
|
||||
|
||||
connection::~connection()
|
||||
{
|
||||
if (connection_->is_open()) {
|
||||
connection_->close();
|
||||
}
|
||||
backend_provider::instance().destroy_connection(connection_info_.type, connection_);
|
||||
connection_ = nullptr;
|
||||
}
|
||||
|
||||
void connection::open()
|
||||
{
|
||||
is_open_ = true;
|
||||
connection_->open();
|
||||
}
|
||||
|
||||
void connection::close()
|
||||
{
|
||||
is_open_ = false;
|
||||
connection_->close();
|
||||
}
|
||||
|
||||
bool connection::is_open() const
|
||||
{
|
||||
return is_open_;
|
||||
return connection_->is_open();
|
||||
}
|
||||
|
||||
const std::string &connection::dns() const
|
||||
const connection_info &connection::info() const
|
||||
{
|
||||
return dns_;
|
||||
return connection_info_;
|
||||
}
|
||||
|
||||
query_result<record> connection::fetch(const std::string &sql)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
#include <utility>
|
||||
|
||||
#include "matador/sql/connection_impl.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
connection_impl::connection_impl(connection_info info)
|
||||
: info_(std::move(info)){}
|
||||
connection_impl::connection_impl(const connection_info &info)
|
||||
: info_(info){}
|
||||
|
||||
const connection_info &connection_impl::info() const {
|
||||
return info_;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
#include "matador/sql/query_result_impl.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
void query_result_impl::on_primary_key(const char *id, std::string &value, size_t size)
|
||||
{
|
||||
read_value(id, column_index_++, value, size);
|
||||
}
|
||||
|
||||
void query_result_impl::on_revision(const char *id, unsigned long long int &rev)
|
||||
{
|
||||
read_value(id, column_index_++, rev);
|
||||
}
|
||||
|
||||
void query_result_impl::on_attribute(const char *id, char *value, const utils::field_attributes &attr)
|
||||
{
|
||||
read_value(id, column_index_++, value, attr.size());
|
||||
}
|
||||
|
||||
void query_result_impl::on_attribute(const char *id, std::string &value, const utils::field_attributes &attr)
|
||||
{
|
||||
read_value(id, column_index_++, value, attr.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,12 +1,14 @@
|
|||
#include "matador/sql/session.hpp"
|
||||
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
session::session(connection_pool<connection> &pool)
|
||||
: pool_(pool)
|
||||
, query_(dialect_) {}
|
||||
, query_(backend_provider::instance().connection_dialect(pool.info().type)) {}
|
||||
|
||||
query_create_intermediate session::create()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "matador/utils/library.hpp"
|
||||
#include "matador/utils/os.hpp"
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
|
|
@ -13,16 +14,20 @@ library::~library()
|
|||
|
||||
bool library::load()
|
||||
{
|
||||
auto path = os::getenv("MATADOR_BACKENDS_PATH");
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
handle_ = LoadLibrary((lib_ + ".dll").c_str());
|
||||
auto cookie = AddDllDirectory(std::wstring(path.begin(), path.end()).c_str());
|
||||
handle_ = LoadLibraryExA((lib_ + ".dll").c_str(), nullptr, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
|
||||
RemoveDllDirectory(cookie);
|
||||
#elif defined(__APPLE__)
|
||||
handle_ = dlopen(std::string("lib" + lib_ + ".dylib").c_str(), RTLD_LAZY);
|
||||
#else
|
||||
handle_ = dlopen(std::string("./lib" + lib_ + ".so").c_str(), RTLD_LAZY);
|
||||
handle_ = dlopen((path + "/lib" + lib_ + ".so").c_str(), RTLD_LAZY);
|
||||
#endif
|
||||
if (!handle_) {
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
DWORD errorMessageID = ::GetLastError();
|
||||
auto errstr = utils::os::error_string(errorMessageID);
|
||||
#else
|
||||
// TODO: handle win32 and linux error
|
||||
fprintf(stdout, "dlopen error: %s", dlerror());
|
||||
|
|
@ -61,7 +66,8 @@ bool library::unload()
|
|||
func_ptr library::function(const std::string &f) const
|
||||
{
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
return GetProcAddress(handle_, f.c_str());
|
||||
auto *addr = GetProcAddress(handle_, f.c_str());
|
||||
return addr;
|
||||
#else
|
||||
return dlsym(handle_, f.c_str());
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace matador::utils::os {
|
||||
|
||||
std::string getenv(const char *name) {
|
||||
|
|
@ -10,8 +14,9 @@ std::string getenv(const char *name) {
|
|||
size_t len{};
|
||||
const auto error = getenv_s(&len, var, 1024, name);
|
||||
if (error > 0) {
|
||||
throw std::logic_error("failed to get environment variable");
|
||||
throw std::logic_error(error_string(error));
|
||||
};
|
||||
|
||||
return var;
|
||||
#else
|
||||
return ::getenv(name);
|
||||
|
|
@ -21,4 +26,23 @@ std::string getenv(const char *name) {
|
|||
[[maybe_unused]] std::string getenv(const std::string &name) {
|
||||
return getenv(name.c_str());
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
std::string error_string(unsigned long error) {
|
||||
char* lpMsgBuf;
|
||||
auto bufLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr,
|
||||
error,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPTSTR) &lpMsgBuf,
|
||||
0,
|
||||
nullptr);
|
||||
std::string result;
|
||||
if (bufLen) {
|
||||
result.append(lpMsgBuf, lpMsgBuf+bufLen);
|
||||
LocalFree(lpMsgBuf);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
|
||||
#include "matador/utils/os.hpp"
|
||||
|
|
@ -14,7 +15,9 @@ TEST_CASE("Load backend", "[backend provider]") {
|
|||
|
||||
REQUIRE(!path.empty());
|
||||
|
||||
backend_provider provider(path);
|
||||
|
||||
const auto &d = provider.connection_dialect("sqlite");
|
||||
connection_info ci{};
|
||||
const auto &d = backend_provider::instance().connection_dialect("sqlite");
|
||||
auto *connection = backend_provider::instance().create_connection("sqlite", ci);
|
||||
REQUIRE(connection != nullptr);
|
||||
backend_provider::instance().destroy_connection("sqlite", connection);
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ using namespace matador::sql;
|
|||
TEST_CASE("Create connection pool", "[connection pool]") {
|
||||
using pool_t = connection_pool<connection>;
|
||||
|
||||
pool_t pool("db", 4);
|
||||
pool_t pool("sqlite://sqlite.db", 4);
|
||||
|
||||
REQUIRE(pool.size() == 4);
|
||||
REQUIRE(pool.idle() == 4);
|
||||
|
|
@ -17,7 +17,7 @@ TEST_CASE("Create connection pool", "[connection pool]") {
|
|||
auto ptr = pool.acquire();
|
||||
REQUIRE(ptr.valid());
|
||||
REQUIRE(ptr->is_open());
|
||||
REQUIRE(!ptr->dns().empty());
|
||||
// REQUIRE(!ptr->dns().empty());
|
||||
|
||||
REQUIRE(pool.idle() == 3);
|
||||
REQUIRE(pool.inuse() == 1);
|
||||
|
|
@ -31,7 +31,7 @@ TEST_CASE("Create connection pool", "[connection pool]") {
|
|||
auto ptr2 = pool.acquire();
|
||||
REQUIRE(ptr2.valid());
|
||||
REQUIRE(ptr2->is_open());
|
||||
REQUIRE(!ptr2->dns().empty());
|
||||
// REQUIRE(!ptr2->dns().empty());
|
||||
|
||||
REQUIRE(pool.idle() == 3);
|
||||
REQUIRE(pool.inuse() == 1);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Execute create table statement", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
const auto res = s.create()
|
||||
|
|
@ -22,7 +22,7 @@ TEST_CASE("Execute create table statement", "[connection]") {
|
|||
}
|
||||
|
||||
TEST_CASE("Execute drop table statement", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
const auto res = s.drop()
|
||||
|
|
@ -33,7 +33,7 @@ TEST_CASE("Execute drop table statement", "[connection]") {
|
|||
}
|
||||
|
||||
TEST_CASE("Execute select statement with where clause", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.select({"id", "name", "color"})
|
||||
|
|
@ -45,7 +45,7 @@ TEST_CASE("Execute select statement with where clause", "[connection]") {
|
|||
}
|
||||
|
||||
TEST_CASE("Execute select statement with order by", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.select({"id", "name", "color"})
|
||||
|
|
@ -58,7 +58,7 @@ TEST_CASE("Execute select statement with order by", "[connection]") {
|
|||
}
|
||||
|
||||
TEST_CASE("Execute select statement with group by and order by", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.select({"id", "name", "color"})
|
||||
|
|
@ -72,7 +72,7 @@ TEST_CASE("Execute select statement with group by and order by", "[connection]")
|
|||
}
|
||||
|
||||
TEST_CASE("Execute insert statement", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.insert()
|
||||
|
|
@ -84,7 +84,7 @@ TEST_CASE("Execute insert statement", "[connection]") {
|
|||
}
|
||||
|
||||
TEST_CASE("Execute update statement", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.update("person")
|
||||
|
|
@ -99,7 +99,7 @@ TEST_CASE("Execute update statement", "[connection]") {
|
|||
}
|
||||
|
||||
TEST_CASE("Execute delete statement", "[connection]") {
|
||||
connection_pool<connection> pool("dns", 4);
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.remove()
|
||||
|
|
|
|||
Loading…
Reference in New Issue