Compare commits
2 Commits
715f4dff8f
...
ae236746ad
| Author | SHA1 | Date |
|---|---|---|
|
|
ae236746ad | |
|
|
8b5859d858 |
|
|
@ -1,11 +1,14 @@
|
|||
set(HEADER
|
||||
include/sqlite_connection.hpp
|
||||
include/sqlite_error.hpp
|
||||
include/sqlite_dialect.hpp
|
||||
include/sqlite_query_result.hpp
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
src/sqlite_connection.cpp
|
||||
src/sqlite_error.cpp
|
||||
src/sqlite_dialect.cpp
|
||||
)
|
||||
|
||||
add_library(matador-sqlite SHARED ${SOURCES} ${HEADER})
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +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"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
#include "sqlite_dialect.hpp"
|
||||
|
||||
[[maybe_unused]] const matador::sql::dialect* get_dialect() {
|
||||
using namespace matador::sql;
|
||||
const static dialect d{{
|
||||
{ dialect::token_t::BEGIN, "BEGIN TRANSACTION"},
|
||||
{ dialect::token_t::COMMIT, "COMMIT TRANSACTION"},
|
||||
{ dialect::token_t::ROLLBACK, "ROLLBACK TRANSACTION"}
|
||||
}};
|
||||
return &d;
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
#ifndef QUERY_BACKEND_PROVIDER_HPP
|
||||
#define QUERY_BACKEND_PROVIDER_HPP
|
||||
|
||||
#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
|
||||
{
|
||||
private:
|
||||
backend_provider();
|
||||
|
||||
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)(const connection_info&);
|
||||
typedef void (*destroy_func)(connection_impl*);
|
||||
typedef const dialect*(*dialect_func)();
|
||||
|
||||
create_func create_connection{};
|
||||
destroy_func destroy_connection{};
|
||||
dialect_func get_dialect{};
|
||||
utils::library lib;
|
||||
};
|
||||
private:
|
||||
using backends_t = std::unordered_map<std::string, std::unique_ptr<backend_context>>;
|
||||
backends_t backends_;
|
||||
std::string backends_path_;
|
||||
};
|
||||
}
|
||||
#endif //QUERY_BACKEND_PROVIDER_HPP
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
namespace matador::sql {
|
||||
|
||||
class [[nodiscard]] dialect
|
||||
class dialect
|
||||
{
|
||||
public:
|
||||
enum class token_t : uint8_t
|
||||
|
|
@ -59,7 +59,15 @@ public:
|
|||
ESCAPE_CLOSING_BRACKET /**< The escape quotes differ; escape the closing one */
|
||||
};
|
||||
|
||||
using token_to_string_map = std::unordered_map<token_t, std::string>;
|
||||
using data_type_to_string_map = std::unordered_map<data_type_t, std::string>;
|
||||
|
||||
public:
|
||||
dialect() = default;
|
||||
dialect(const token_to_string_map &token_replace_map, const data_type_to_string_map &data_type_replace_map);
|
||||
explicit dialect(const data_type_to_string_map &data_type_replace_map);
|
||||
explicit dialect(const token_to_string_map &token_replace_map);
|
||||
|
||||
const std::string& token_at(token_t token) const;
|
||||
const std::string& data_type_at(data_type_t type) const;
|
||||
|
||||
|
|
@ -124,8 +132,6 @@ public:
|
|||
const std::vector<std::string>& columns() const;
|
||||
|
||||
private:
|
||||
using token_to_string_map = std::unordered_map<token_t, std::string>;
|
||||
|
||||
std::vector<std::string> host_vars_;
|
||||
std::vector<std::string> columns_;
|
||||
|
||||
|
|
@ -167,7 +173,6 @@ private:
|
|||
{token_t::NONE, ""}
|
||||
};
|
||||
|
||||
using data_type_to_string_map = std::unordered_map<data_type_t, std::string>;
|
||||
data_type_to_string_map data_types_ {
|
||||
{data_type_t::type_char, "TINYINT"},
|
||||
{data_type_t::type_short, "SMALLINT"},
|
||||
|
|
|
|||
|
|
@ -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_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef QUERY_OS_HPP
|
||||
#define QUERY_OS_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_OS_HPP
|
||||
26
main.cpp
26
main.cpp
|
|
@ -1,21 +1,17 @@
|
|||
#include <matador/sql/dialect.hpp>
|
||||
#include <matador/sql/query_builder.hpp>
|
||||
#include <matador/sql/types.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
using namespace matador;
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
sql::dialect d;
|
||||
|
||||
sql::query_builder query(d);
|
||||
|
||||
std::cout << query.create().table("person", {
|
||||
{ "id", sql::data_type_traits<unsigned long>::builtin_type() },
|
||||
{ "name", sql::data_type_traits<std::string>::builtin_type(255), 255 },
|
||||
{ "id", sql::data_type_traits<unsigned long>::builtin_type() }
|
||||
}).compile();
|
||||
const std::string env_var {"MATADOR_BACKENDS_PATH"};
|
||||
|
||||
// char var[1024];
|
||||
// size_t len{};
|
||||
// const auto error = getenv_s(&len, var, 1024, env_var.c_str());
|
||||
// if (error > 0) {
|
||||
// std::cout << "error: unknown env var " << env_var << "\n";
|
||||
// } else {
|
||||
// std::cout << "env var: " << var << "\n";
|
||||
// }
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,9 @@ set(SQL_SOURCES
|
|||
sql/record.cpp
|
||||
sql/connection_info.cpp
|
||||
sql/connection_impl.cpp
|
||||
sql/session.cpp)
|
||||
sql/session.cpp
|
||||
sql/backend_provider.cpp
|
||||
sql/query_result_impl.cpp)
|
||||
|
||||
set(SQL_HEADER
|
||||
../include/matador/sql/dialect.hpp
|
||||
|
|
@ -27,19 +29,23 @@ set(SQL_HEADER
|
|||
../include/matador/sql/connection_impl.hpp
|
||||
../include/matador/sql/connection_info.hpp
|
||||
../include/matador/sql/connection_pool.hpp
|
||||
../include/matador/sql/session.hpp)
|
||||
../include/matador/sql/session.hpp
|
||||
../include/matador/sql/backend_provider.hpp
|
||||
../include/matador/sql/query_result_impl.hpp)
|
||||
|
||||
set(UTILS_HEADER
|
||||
../include/matador/utils/field_attributes.hpp
|
||||
../include/matador/utils/string.hpp
|
||||
../include/matador/utils/constraints.hpp
|
||||
../include/matador/utils/library.hpp)
|
||||
../include/matador/utils/library.hpp
|
||||
../include/matador/utils/os.hpp)
|
||||
|
||||
set(UTILS_SOURCES
|
||||
utils/field_attributes.cpp
|
||||
utils/string.cpp
|
||||
sql/condition.cpp
|
||||
utils/library.cpp)
|
||||
utils/library.cpp
|
||||
utils/os.cpp)
|
||||
|
||||
add_library(matador STATIC ${SQL_SOURCES} ${SQL_HEADER} ${UTILS_SOURCES} ${UTILS_HEADER})
|
||||
target_include_directories(matador PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
#include "matador/sql/backend_provider.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"))
|
||||
{}
|
||||
|
||||
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, std::make_unique<backend_context>(connection_type, backends_path_)).first;
|
||||
}
|
||||
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, std::make_unique<backend_context>(connection_type, backends_path_)).first;
|
||||
}
|
||||
return *(*it->second->get_dialect)();
|
||||
}
|
||||
|
||||
backend_provider::backend_context::backend_context(const std::string &connection_type,
|
||||
const std::string &backends_path)
|
||||
{
|
||||
if (!lib.load("matador-" + connection_type)) {
|
||||
throw std::runtime_error("couldn't load library '" + connection_type + "'");
|
||||
}
|
||||
|
||||
create_connection = reinterpret_cast<create_func>(reinterpret_cast<std::uintptr_t>(lib.function("create_database")));
|
||||
destroy_connection = reinterpret_cast<destroy_func>(reinterpret_cast<std::uintptr_t>(lib.function("destroy_database")));
|
||||
get_dialect = reinterpret_cast<dialect_func >(reinterpret_cast<std::uintptr_t>(lib.function("get_dialect")));
|
||||
}
|
||||
|
||||
backend_provider::backend_context::~backend_context()
|
||||
{
|
||||
lib.unload();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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_;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,26 @@
|
|||
#include "matador/utils/string.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
dialect::dialect(const dialect::token_to_string_map &token_replace_map, const dialect::data_type_to_string_map &data_type_replace_map)
|
||||
{
|
||||
for (const auto &token : token_replace_map) {
|
||||
tokens_[token.first] = token.second;
|
||||
}
|
||||
|
||||
for (const auto &data_type : data_type_replace_map) {
|
||||
data_types_[data_type.first] = data_type.second;
|
||||
}
|
||||
}
|
||||
|
||||
dialect::dialect(const dialect::data_type_to_string_map &data_type_replace_map)
|
||||
: dialect({}, data_type_replace_map)
|
||||
{}
|
||||
|
||||
dialect::dialect(const dialect::token_to_string_map &token_replace_map)
|
||||
: dialect(token_replace_map, {})
|
||||
{}
|
||||
|
||||
const std::string& dialect::token_at(dialect::token_t token) const
|
||||
{
|
||||
return tokens_.at(token);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
#include "matador/utils/os.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace matador::utils::os {
|
||||
|
||||
std::string getenv(const char *name) {
|
||||
#ifdef _WIN32
|
||||
char var[1024];
|
||||
size_t len{};
|
||||
const auto error = getenv_s(&len, var, 1024, name);
|
||||
if (error > 0) {
|
||||
throw std::logic_error(error_string(error));
|
||||
};
|
||||
|
||||
return var;
|
||||
#else
|
||||
return ::getenv(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
[[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
|
||||
}
|
||||
|
|
@ -12,6 +12,11 @@ add_executable(tests builder.cpp
|
|||
session.cpp
|
||||
record.cpp
|
||||
connection_pool.cpp
|
||||
query.cpp)
|
||||
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain matador)
|
||||
query.cpp
|
||||
backend_provider.cpp)
|
||||
target_link_libraries(tests PRIVATE
|
||||
Catch2::Catch2WithMain
|
||||
matador
|
||||
${CMAKE_DL_LIBS}
|
||||
${SQLite3_LIBRARIES})
|
||||
target_include_directories(tests PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/include)
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
#include "matador/sql/backend_provider.hpp"
|
||||
|
||||
#include "matador/utils/os.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
|
||||
TEST_CASE("Load backend", "[backend provider]") {
|
||||
auto path = matador::utils::os::getenv("MATADOR_BACKENDS_PATH");
|
||||
if (path.back() != '\\') {
|
||||
path.push_back('\\');
|
||||
}
|
||||
|
||||
REQUIRE(!path.empty());
|
||||
|
||||
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