query/source/orm/sql/statement.cpp

71 lines
1.8 KiB
C++

#include "matador/sql/statement.hpp"
#include "matador/sql/record.hpp"
#include "matador/sql/field.hpp"
#include <algorithm>
#include <utility>
namespace matador::sql {
statement::statement(const std::shared_ptr<statement_proxy>& proxy, logger_ptr logger)
: statement_proxy_(proxy)
, logger_(std::move(logger)){}
statement &statement::bind(const size_t pos, const char *value) {
statement_proxy_->bind(pos, value, strlen(value));
return *this;
}
statement &statement::bind(const size_t pos, std::string &val, const size_t size)
{
statement_proxy_->bind(pos, val, size);
return *this;
}
utils::result<size_t, utils::error> statement::execute() const {
// logger_.info(statement_->query_.sql);
return statement_proxy_->execute();
}
//bool is_unknown(const std::vector<object::column_definition> &columns) {
// return std::all_of(std::begin(columns), std::end(columns), [](const auto &col) {
// return col.is_unknown();
// });
//}
utils::result<query_result<record>, utils::error> statement::fetch() const {
// if (is_unknown(statement_->query_.prototype)) {
//
// }
auto result = statement_proxy_->fetch();
if (!result.is_ok()) {
return utils::failure(result.err());
}
// logger_.info(statement_->query_.sql);
return utils::ok(query_result<record>{std::move(*result)});
}
utils::result<std::optional<record>, utils::error> statement::fetch_one() const {
// logger_.info(statement_->query_.sql);
auto result = statement_proxy_->fetch();
if (!result.is_ok()) {
return utils::failure(result.err());
}
query_result<record> records(std::move(*result));
auto first = records.begin();
if (first == records.end()) {
return utils::ok(std::optional<record>{std::nullopt});
}
return utils::ok(std::optional{*first.release()});
}
void statement::reset() const
{
statement_proxy_->reset();
}
}