added connection impl class
This commit is contained in:
@@ -20,7 +20,7 @@ public:
|
||||
query_update_intermediate update(const std::string &table);
|
||||
query_delete_intermediate remove();
|
||||
|
||||
result fetch(const std::string &sql);
|
||||
query_result<record> fetch(const std::string &sql);
|
||||
std::pair<size_t, std::string> execute(const std::string &sql);
|
||||
|
||||
private:
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef QUERY_CONNECTION_IMPL_HPP
|
||||
#define QUERY_CONNECTION_IMPL_HPP
|
||||
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class connection_impl
|
||||
{
|
||||
public:
|
||||
virtual ~connection_impl() = default;
|
||||
|
||||
virtual void open() = 0;
|
||||
virtual void close() = 0;
|
||||
virtual bool is_open() = 0;
|
||||
|
||||
virtual void execute(const std::string &stmt) = 0;
|
||||
virtual void prepare(const std::string &stmt) = 0;
|
||||
|
||||
protected:
|
||||
explicit connection_impl(connection_info info);
|
||||
|
||||
[[nodiscard]] const connection_info &info() const;
|
||||
|
||||
private:
|
||||
connection_info info_;
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_CONNECTION_IMPL_HPP
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef QUERY_CONNECTION_INFO_HPP
|
||||
#define QUERY_CONNECTION_INFO_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
struct connection_info
|
||||
{
|
||||
std::string type;
|
||||
std::string user;
|
||||
std::string password;
|
||||
std::string hostname;
|
||||
unsigned short port{};
|
||||
std::string database;
|
||||
std::string driver;
|
||||
|
||||
static connection_info parse(const std::string &info, unsigned short default_port = 0, const std::string &default_driver = "");
|
||||
static std::string to_string(const connection_info &ci);
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_CONNECTION_INFO_HPP
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/key_value_pair.hpp"
|
||||
#include "matador/sql/result.hpp"
|
||||
#include "matador/sql/query_result.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
@@ -27,15 +28,28 @@ private:
|
||||
query_builder &query_;
|
||||
};
|
||||
|
||||
class query_execute_finish : public query_intermediate
|
||||
{
|
||||
public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
std::pair<size_t, std::string> execute();
|
||||
};
|
||||
|
||||
class query_select_finish : public query_intermediate
|
||||
{
|
||||
protected:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
public:
|
||||
result fetch_all();
|
||||
result fetch_one();
|
||||
result fetch_value();
|
||||
query_result<record> fetch_all();
|
||||
record fetch_one();
|
||||
template<typename Type>
|
||||
Type fetch_value()
|
||||
{
|
||||
auto result = fetch_all();
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
class query_limit_intermediate : public query_select_finish
|
||||
@@ -108,14 +122,6 @@ public:
|
||||
|
||||
};
|
||||
|
||||
class query_execute_finish : public query_intermediate
|
||||
{
|
||||
public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
std::pair<size_t, std::string> execute();
|
||||
};
|
||||
|
||||
class query_into_intermediate : public query_intermediate
|
||||
{
|
||||
public:
|
||||
@@ -124,20 +130,12 @@ public:
|
||||
query_execute_finish values(std::initializer_list<any_type> values);
|
||||
};
|
||||
|
||||
class query_create_drop_finish : public query_intermediate
|
||||
{
|
||||
public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
void execute();
|
||||
};
|
||||
|
||||
class query_create_intermediate : query_intermediate
|
||||
{
|
||||
public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
query_create_drop_finish table(const std::string &table, std::initializer_list<column> columns);
|
||||
query_execute_finish table(const std::string &table, std::initializer_list<column> columns);
|
||||
};
|
||||
|
||||
class query_drop_intermediate : query_intermediate
|
||||
@@ -145,7 +143,7 @@ class query_drop_intermediate : query_intermediate
|
||||
public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
query_create_drop_finish table(const std::string &table);
|
||||
query_execute_finish table(const std::string &table);
|
||||
};
|
||||
|
||||
class query_insert_intermediate : public query_intermediate
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
#ifndef QUERY_QUERY_RESULT_HPP
|
||||
#define QUERY_QUERY_RESULT_HPP
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
template < typename Type >
|
||||
class query_result;
|
||||
|
||||
template < typename Type >
|
||||
class query_result_iterator
|
||||
{
|
||||
public:
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = Type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = value_type*; /**< Shortcut for the pointer type. */
|
||||
using reference = value_type&; /**< Shortcut for the reference type */
|
||||
|
||||
public:
|
||||
query_result_iterator() = default;
|
||||
explicit query_result_iterator(query_result<Type> &res, Type *obj = nullptr)
|
||||
: obj_(obj)
|
||||
, result_(res)
|
||||
{}
|
||||
query_result_iterator(query_result_iterator&& x) noexcept
|
||||
: obj_(std::move(x.obj_))
|
||||
, result_(x.result_)
|
||||
{}
|
||||
|
||||
query_result_iterator& operator=(query_result_iterator&& x) noexcept
|
||||
{
|
||||
result_ = x.result_;
|
||||
obj_ = std::move(x.obj_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~query_result_iterator() = default;
|
||||
|
||||
bool operator==(const query_result_iterator& rhs)
|
||||
{
|
||||
return obj_ == rhs.obj_;
|
||||
}
|
||||
|
||||
bool operator!=(const query_result_iterator& rhs)
|
||||
{
|
||||
return obj_ != rhs.obj_;
|
||||
}
|
||||
|
||||
pointer operator->()
|
||||
{
|
||||
return obj_.get();
|
||||
}
|
||||
|
||||
reference operator&()
|
||||
{
|
||||
return &obj_.get();
|
||||
}
|
||||
|
||||
std::unique_ptr<Type> operator*()
|
||||
{
|
||||
return std::move(obj_);
|
||||
}
|
||||
|
||||
pointer get()
|
||||
{
|
||||
return obj_.get();
|
||||
}
|
||||
|
||||
pointer release()
|
||||
{
|
||||
return obj_.release();
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<Type> obj_;
|
||||
query_result<Type> &result_;
|
||||
};
|
||||
|
||||
template < typename Type >
|
||||
class query_result
|
||||
{
|
||||
public:
|
||||
query_result(std::string sql) : sql_(std::move(sql)) {}
|
||||
|
||||
[[nodiscard]] const std::string& str() const { return sql_; }
|
||||
|
||||
Type begin() { return {}; }
|
||||
private:
|
||||
std::string sql_;
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_QUERY_RESULT_HPP
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef QUERY_RECORD_HPP
|
||||
#define QUERY_RECORD_HPP
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class record
|
||||
{
|
||||
private:
|
||||
using column_by_index = std::vector<column>;
|
||||
using column_index_pair = std::pair<std::reference_wrapper<column>, size_t>;
|
||||
using column_by_name_map = std::unordered_map<std::string, column_index_pair>;
|
||||
|
||||
public:
|
||||
using iterator = column_by_index::iterator;
|
||||
using const_iterator = column_by_index::const_iterator;
|
||||
|
||||
record() = default;
|
||||
record(std::initializer_list<column> columns);
|
||||
~record() = default;
|
||||
|
||||
template < typename Type >
|
||||
void append(const std::string &name, long size = -1)
|
||||
{
|
||||
append(make_column<Type>(name, size));
|
||||
}
|
||||
|
||||
void append(column col);
|
||||
|
||||
[[nodiscard]] const column& at(const std::string &name) const;
|
||||
const column& at(size_t index);
|
||||
|
||||
iterator find(const std::string &column_name);
|
||||
const_iterator find(const std::string &column_name) const;
|
||||
|
||||
iterator begin();
|
||||
[[nodiscard]] const_iterator begin() const;
|
||||
[[nodiscard]] const_iterator cbegin() const;
|
||||
|
||||
iterator end();
|
||||
[[nodiscard]] const_iterator end() const;
|
||||
[[nodiscard]] const_iterator cend() const;
|
||||
|
||||
[[nodiscard]] size_t size() const;
|
||||
|
||||
private:
|
||||
column_by_index columns_;
|
||||
column_by_name_map columns_by_name_;
|
||||
};
|
||||
|
||||
}
|
||||
#endif //QUERY_RECORD_HPP
|
||||
Reference in New Issue
Block a user