52 lines
1.9 KiB
C++
52 lines
1.9 KiB
C++
#include "matador/query/intermediates/fetchable_query.hpp"
|
|
|
|
#include "matador/sql/executor.hpp"
|
|
#include "matador/sql/statement.hpp"
|
|
|
|
namespace matador::query {
|
|
|
|
utils::result<sql::query_result<sql::record>, utils::error> fetchable_query::fetch_all(const sql::executor &exec) const {
|
|
query_compiler compiler;
|
|
context_->mode = query_mode::Direct;
|
|
return exec.fetch(compiler.compile(*context_, exec.dialect(), std::nullopt))
|
|
.and_then([](auto &&res) {
|
|
return utils::ok(sql::query_result<sql::record>(std::forward<decltype(res)>(res)));
|
|
});
|
|
}
|
|
|
|
utils::result<std::optional<sql::record>, utils::error> fetchable_query::fetch_one(const sql::executor &exec) const {
|
|
query_compiler compiler;
|
|
context_->mode = query_mode::Direct;
|
|
auto result = exec.fetch(compiler.compile(*context_, exec.dialect(), std::nullopt));
|
|
if (!result.is_ok()) {
|
|
return utils::failure(result.err());
|
|
}
|
|
|
|
sql::query_result<sql::record> records(std::move(*result));
|
|
auto first = records.begin();
|
|
if (first == records.end()) {
|
|
return utils::ok(std::optional<sql::record>{std::nullopt});
|
|
}
|
|
|
|
return utils::ok(std::optional{*first.get()});
|
|
}
|
|
|
|
std::string fetchable_query::str(const sql::executor &exec) const {
|
|
query_compiler compiler;
|
|
context_->mode = query_mode::Direct;
|
|
return exec.str(compiler.compile(*context_, exec.dialect(), std::nullopt));
|
|
}
|
|
|
|
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetchable_query::fetch(const sql::executor &exec) const {
|
|
query_compiler compiler;
|
|
context_->mode = query_mode::Direct;
|
|
return exec.fetch(compiler.compile(*context_, exec.dialect(), std::nullopt));
|
|
}
|
|
|
|
utils::result<sql::statement, utils::error> fetchable_query::prepare(const sql::executor &exec) const {
|
|
query_compiler compiler;
|
|
context_->mode = query_mode::Prepared;
|
|
return exec.prepare(compiler.compile(*context_, exec.dialect(), std::nullopt));
|
|
}
|
|
|
|
} |