query/include/matador/sql/statement.hpp

195 lines
5.3 KiB
C++

#ifndef QUERY_STATEMENT_HPP
#define QUERY_STATEMENT_HPP
#include "matador/sql/abstract_sql_logger.hpp"
#include "matador/sql/query_result.hpp"
#include "matador/sql/interface/statement_proxy.hpp"
#include "matador/utils/error.hpp"
#include "matador/utils/result.hpp"
#include <memory>
namespace matador::sql {
namespace detail {
template<class Type>
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 proxy The statement proxy implementation object
* @param logger The logger handler to write SQL log messages to
*/
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;
/**
* Assignment move constructor for statement
*
* @param x The statement to move from
* @return Reference to this
*/
statement &operator=(statement &&x) noexcept;
statement(const statement &x);
statement &operator=(const statement &x);
statement &bind(size_t pos, const char *value);
statement &bind(size_t pos, std::string &val, size_t size);
/**
* Bind an object to the statement starting
* at the given position index.
*
* @param obj The object to bind
* @return The next index to bind
*/
template<class Type>
statement &bind(const Type &obj);
template<typename Type>
statement &bind(size_t pos, Type &value);
/**
* Executes the prepared statement and returns
* the number of affected rows.
*
* @return The number of affected rows
*/
[[nodiscard]] utils::result<size_t, utils::error> execute() const;
/**
* Fetches the result of the prepared
* statement. If a prepared statement was not
* a SELECT statement, an empty query result set
* is returned.
*
* @tparam Type Type of the fetched result
* @return The query result set
*/
template<class Type>
utils::result<query_result<Type>, utils::error> fetch();
/**
* Fetches the result of the prepared
* statement. The type is the record representing an
* unknown variable type.
* If the prepared statement was not
* a SELECT statement, an empty query result set
* is returned.
*
* @return The query result set
*/
[[nodiscard]] utils::result<query_result<record>, utils::error> fetch() const;
/**
* Fetches the first result of a prepared statement.
* 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
*/
template<class Type>
utils::result<std::unique_ptr<Type>, utils::error> fetch_one();
/**
* Fetches the first result of a prepared statement.
* 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
*/
[[nodiscard]] utils::result<std::optional<record>, utils::error> fetch_one() const;
/**
* Resets the prepared statement to
* reuse it.
*/
void reset() const;
/**
* Returns the current binding position.
* Is set to the initial position after
* a call to reset().
*
* @return The current binding position
*/
[[nodiscard]] size_t bind_pos() const;
/**
* Returns the statements underlying SQL
* query string.
*
* @return The underlying SQL string
*/
[[nodiscard]] std::string sql() const;
[[nodiscard]] utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch_internal() const;
private:
template<class Type>
friend class detail::identifier_binder;
private:
std::shared_ptr<statement_proxy> statement_proxy_;
std::unique_ptr<utils::attribute_writer> bindings_;
std::shared_ptr<abstract_sql_logger> logger_;
};
template<typename Type>
statement &statement::bind(size_t pos, Type &value) {
statement_proxy_->bind(pos, value, *bindings_);
return *this;
}
template<class Type>
statement &statement::bind(const Type &obj) {
statement_proxy_->bind(obj, *bindings_);
return *this;
}
template<class Type>
utils::result<query_result<Type>, utils::error> statement::fetch() {
return statement_proxy_->fetch(*bindings_).and_then([](std::unique_ptr<query_result_impl> &&value) {
const auto prototype = value->prototype();
return utils::ok(query_result<Type>(std::forward<decltype(value)>(value), [prototype] {
return detail::create_prototype<Type>(prototype);
}));
});
}
template<class Type>
utils::result<std::unique_ptr<Type>, utils::error> statement::fetch_one() {
auto result = statement_proxy_->fetch(*bindings_);
if (!result.is_ok()) {
return utils::failure(result.err());
}
const auto prototype = result.value()->prototype();
auto records = query_result<Type>(result.release(), [prototype] {
return detail::create_prototype<Type>(prototype);
});
auto first = records.begin();
if (first == records.end()) {
return utils::ok(std::unique_ptr<Type>{nullptr});
}
return utils::ok(std::unique_ptr<Type>{first.release()});
}
}
#endif //QUERY_STATEMENT_HPP