61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
#include "postgres_statement.hpp"
|
|
#include "postgres_error.hpp"
|
|
#include "postgres_result_reader.hpp"
|
|
|
|
namespace matador::backends::postgres {
|
|
|
|
postgres_statement::postgres_statement(PGconn *db, PGresult *result, std::string name, const sql::query_context &query)
|
|
: statement_impl(query)
|
|
, db_(db)
|
|
, result_(result)
|
|
, name_(std::move(name))
|
|
, binder_(query_.bind_vars.size())
|
|
{}
|
|
|
|
utils::result<size_t, utils::error> postgres_statement::execute()
|
|
{
|
|
PGresult *res = PQexecPrepared(db_,
|
|
name_.c_str(),
|
|
static_cast<int>(binder_.params().values.size()),
|
|
binder_.params().values.data(),
|
|
binder_.params().lengths.data(),
|
|
binder_.params().formats.data(),
|
|
0);
|
|
|
|
if (is_result_error(res)) {
|
|
return utils::failure(make_error(sql::error_code::EXECUTE_FAILED, res, db_, "Failed to execute statement", query_.sql));
|
|
}
|
|
|
|
const auto *tuples = PQcmdTuples(res);
|
|
if (strlen(tuples) == 0) {
|
|
return utils::ok(static_cast<size_t>(0));
|
|
}
|
|
|
|
return utils::ok(static_cast<size_t>(std::stoul(tuples)));
|
|
}
|
|
|
|
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_statement::fetch()
|
|
{
|
|
PGresult *res = PQexecPrepared(db_,
|
|
name_.c_str(),
|
|
static_cast<int>(binder_.params().values.size()),
|
|
binder_.params().values.data(),
|
|
binder_.params().lengths.data(),
|
|
binder_.params().formats.data(),
|
|
0);
|
|
|
|
if (is_result_error(res)) {
|
|
return utils::failure(make_error(sql::error_code::FETCH_FAILED, res, db_, "Failed to fetch statement", query_.sql));
|
|
}
|
|
|
|
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), query_.prototype));
|
|
}
|
|
|
|
void postgres_statement::reset() {}
|
|
|
|
utils::attribute_writer& postgres_statement::binder()
|
|
{
|
|
return binder_;
|
|
}
|
|
|
|
} |