implemented the first version of statement_cache
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#include "matador/sql/abstract_sql_logger.hpp"
|
||||
#include "matador/sql/query_result.hpp"
|
||||
|
||||
#include "matador/sql/interface/statement_impl.hpp"
|
||||
#include "matador/sql/interface/statement_proxy.hpp"
|
||||
|
||||
#include "matador/utils/error.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
@@ -18,26 +18,28 @@ class identifier_binder;
|
||||
}
|
||||
|
||||
class statement_impl;
|
||||
class statement_proxy;
|
||||
|
||||
class statement {
|
||||
public:
|
||||
using logger_ptr = std::shared_ptr<abstract_sql_logger>;
|
||||
|
||||
/**
|
||||
* Creates a statement initialized from the
|
||||
* given statement implementation object holding
|
||||
* the implementation for the selected database
|
||||
*
|
||||
* @param impl The statement implementation object
|
||||
* @param logger The logger handler to write sql log messages to
|
||||
* @param proxy The statement proxy implementation object
|
||||
* @param logger The logger handler to write SQL log messages to
|
||||
*/
|
||||
explicit statement(std::unique_ptr<statement_impl> impl,
|
||||
const std::shared_ptr<abstract_sql_logger> &logger = std::make_shared<null_sql_logger>());
|
||||
explicit statement(const std::shared_ptr<statement_proxy>& proxy, logger_ptr logger = null_logger);
|
||||
/**
|
||||
* Copy move constructor for statement
|
||||
*
|
||||
* @param x The statement to move from
|
||||
*/
|
||||
statement(statement &&x) noexcept
|
||||
: statement_(std::move(x.statement_))
|
||||
: statement_proxy_(std::move(x.statement_proxy_))
|
||||
, logger_(std::move(x.logger_)) {
|
||||
}
|
||||
|
||||
@@ -48,11 +50,14 @@ public:
|
||||
* @return Reference to this
|
||||
*/
|
||||
statement &operator=(statement &&x) noexcept {
|
||||
statement_ = std::move(x.statement_);
|
||||
statement_proxy_ = std::move(x.statement_proxy_);
|
||||
logger_ = std::move(x.logger_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
statement(const statement &x) = default;
|
||||
statement &operator=(const statement &x) = default;
|
||||
|
||||
statement &bind(size_t pos, const char *value);
|
||||
statement &bind(size_t pos, std::string &val, size_t size);
|
||||
/**
|
||||
@@ -77,8 +82,8 @@ public:
|
||||
|
||||
/**
|
||||
* Fetches the result of the prepared
|
||||
* statement. If prepared statement was not
|
||||
* a SELECT statement an empty query result set
|
||||
* statement. If a prepared statement was not
|
||||
* a SELECT statement, an empty query result set
|
||||
* is returned.
|
||||
*
|
||||
* @tparam Type Type of the fetched result
|
||||
@@ -88,10 +93,10 @@ public:
|
||||
utils::result<query_result<Type>, utils::error> fetch();
|
||||
/**
|
||||
* Fetches the result of the prepared
|
||||
* statement. The type is record representing an
|
||||
* statement. The type is the record representing an
|
||||
* unknown variable type.
|
||||
* If prepared statement was not
|
||||
* a SELECT statement an empty query result set
|
||||
* If the prepared statement was not
|
||||
* a SELECT statement, an empty query result set
|
||||
* is returned.
|
||||
*
|
||||
* @return The query result set
|
||||
@@ -99,8 +104,8 @@ public:
|
||||
[[nodiscard]] utils::result<query_result<record>, utils::error> fetch() const;
|
||||
/**
|
||||
* Fetches the first result of a prepared statement.
|
||||
* If prepared statement is empty or not
|
||||
* a SELECT statement a nullptr is returned.
|
||||
* If a prepared statement is empty or not
|
||||
* a SELECT statement, a nullptr is returned.
|
||||
*
|
||||
* @tparam Type Type of the fetched result
|
||||
* @return The query result set
|
||||
@@ -109,9 +114,9 @@ public:
|
||||
utils::result<std::unique_ptr<Type>, utils::error> fetch_one();
|
||||
/**
|
||||
* Fetches the first result of a prepared statement.
|
||||
* The type is record representing an unknown variable type.
|
||||
* If prepared statement is empty or not
|
||||
* a SELECT statement a nullptr is returned.
|
||||
* The type is a record representing an unknown variable type.
|
||||
* If a prepared statement is empty or not
|
||||
* a SELECT statement, a nullptr is returned.
|
||||
*
|
||||
* @return The query result set
|
||||
*/
|
||||
@@ -128,25 +133,25 @@ private:
|
||||
friend class detail::identifier_binder;
|
||||
|
||||
private:
|
||||
std::unique_ptr<statement_impl> statement_;
|
||||
std::shared_ptr<statement_proxy> statement_proxy_;
|
||||
std::shared_ptr<abstract_sql_logger> logger_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
statement &statement::bind(size_t pos, Type &value) {
|
||||
statement_->bind(pos, value);
|
||||
statement_proxy_->bind(pos, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
statement &statement::bind(const Type &obj) {
|
||||
statement_->bind_object(obj);
|
||||
statement_proxy_->bind(obj);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
utils::result<query_result<Type>, utils::error> statement::fetch() {
|
||||
return statement_->fetch().and_then([](std::unique_ptr<query_result_impl> &&value) {
|
||||
return statement_proxy_->fetch().and_then([](std::unique_ptr<query_result_impl> &&value) {
|
||||
return utils::ok(query_result<Type>(std::forward<decltype(value)>(value)));
|
||||
});
|
||||
// if (!result.is_ok()) {
|
||||
@@ -157,7 +162,7 @@ utils::result<query_result<Type>, utils::error> statement::fetch() {
|
||||
|
||||
template<class Type>
|
||||
utils::result<std::unique_ptr<Type>, utils::error> statement::fetch_one() {
|
||||
auto result = statement_->fetch();
|
||||
auto result = statement_proxy_->fetch();
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user