pgLab/pgsql/Pgsql_Connection.cpp

278 lines
6.2 KiB
C++
Raw Normal View History

#include "Pgsql_Connection.h"
#include "Pgsql_declare.h"
#include "Pgsql_PgException.h"
#include "Pgsql_Params.h"
#include <memory>
#include <stdexcept>
using namespace Pgsql;
Connection::Connection() = default;
Connection::~Connection()
{
close();
}
Connection::Connection(Connection &&rhs)
: conn(rhs.conn)
{
rhs.conn = nullptr;
}
Connection& Connection::operator=(Connection &&rhs)
{
close();
conn = rhs.conn;
rhs.conn = nullptr;
return *this;
}
void Connection::close()
{
if (conn) {
PQfinish(conn);
conn = nullptr;
}
}
Canceller Connection::getCancel()
{
Canceller c(PQgetCancel(conn));
return c;
}
void Connection::connect(const char *params)
{
conn = PQconnectdb(params);
testForConnectionError(conn);
}
void Connection::connect(const char *const * keywords, const char* const * values, int expand_dbname)
{
conn = PQconnectdbParams(keywords, values, expand_dbname);
testForConnectionError(conn);
}
bool Connection::connectStart(const char* params)
{
conn = PQconnectStart(params);
return conn != nullptr;
}
bool Connection::connectStart(const char * const *keywords,
const char * const *values)
{
conn = PQconnectStartParams(keywords, values, 0);
return conn != nullptr;
}
PostgresPollingStatusType Connection::connectPoll()
{
return PQconnectPoll(conn);
}
ConnStatusType Connection::status()
{
return PQstatus(conn);
}
int Connection::socket()
{
return PQsocket(conn);
}
std::string Connection::getErrorMessage() const
{
std::string result;
if (conn) {
result = PQerrorMessage(conn);
}
else {
result = "no connection";
}
return result;
}
Result Connection::query(const char * command)
{
PGresult *result = PQexec(conn, command);
throwError(result);
return Result(result);
}
Result Connection::queryParam(const char * command, const Params &params)
{
PGresult *result = PQexecParams(conn, command, params.size(), params.types(),
params.values(), params.lengths(), params.formats(), 0);
throwError(result);
return Result(result);
}
Result Connection::queryParam(const QString &command, const Params &params)
{
return queryParam(command.toUtf8().data(), params);
}
bool Connection::sendQuery(const char *query)
{
int res = PQsendQuery(conn, query);
return res == 1;
}
bool Connection::sendQueryParams(const char * command, const Params &params)
{
int res = PQsendQueryParams(conn, command, params.size(), params.types(),
params.values(), params.lengths(), params.formats(),
0); // text format
return res == 1;
}
std::shared_ptr<Result> Connection::getResult()
{
PGresult *r = PQgetResult(conn);
if (r) {
throwError(r);
return std::make_shared<Result>(r);
}
else {
return nullptr;
}
}
std::shared_ptr<Result> Connection::getResultNoThrow()
{
PGresult *r = PQgetResult(conn);
if (r) {
return std::make_shared<Result>(r);
}
else {
return nullptr;
}
}
bool Connection::consumeInput()
{
int res = PQconsumeInput(conn);
return res == 1;
}
bool Connection::isBusy()
{
int res = PQisBusy(conn);
return res == 1;
}
void Connection::setNoticeReceiver(std::function<void(const PGresult *)> callback)
{
notifyReceiver = callback;
PQsetNoticeReceiver(conn,
Connection::notifyReceiveFunc
, reinterpret_cast<void*>(this));
}
std::string Connection::escapeLiteral(const std::string_view &literal)
{
std::unique_ptr<char, void(*)(char*)> result(
PQescapeLiteral(conn, literal.data(), literal.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
return std::string(result.get());
}
throw std::runtime_error("escapeLiteral(string_view) failed");
}
QString Connection::escapeLiteral(const QString &literal)
{
auto u8 = literal.toUtf8();
std::unique_ptr<char, void(*)(char*)> result(
PQescapeLiteral(conn, u8.data(), u8.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
return QString::fromUtf8(result.get());
}
throw std::runtime_error("escapeLiteral(QString) failed");
}
std::string Connection::escapeIdentifier(const std::string_view &ident)
{
std::unique_ptr<char, void(*)(char*)> result(
PQescapeIdentifier(conn, ident.data(), ident.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
return std::string(result.get());
}
throw std::runtime_error("escapeIdentifier failed");
}
QString Connection::escapeIdentifier(const QString &ident)
{
auto u8 = ident.toUtf8();
std::unique_ptr<char, void(*)(char*)> result(
PQescapeIdentifier(conn, u8.data(), u8.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
return QString::fromUtf8(result.get());
}
throw std::runtime_error("escapeIdentifier(QString) failed");
}
void Connection::notifyReceiveFunc(void *arg, const PGresult *result)
{
Connection *c = reinterpret_cast<Connection *>(arg);
c->notifyReceiver(result);
}
2018-09-02 10:30:30 +00:00
QString Connection::getDBName() const
{
return QString::fromUtf8(PQdb(conn));
}
void Connection::testForConnectionError(PGconn *conn)
{
std::string error_msg;
if (conn) {
ConnStatusType status = PQstatus(conn);
if (status == CONNECTION_OK)
return;
error_msg = PQerrorMessage(conn);
}
else {
error_msg = "Unknown connection failure (maybe out of memory?)";
}
throw PgConnectionError(error_msg);
}
void Connection::throwError(PGresult *result) const
{
auto state = PQresultStatus(result);
if (state == PGRES_BAD_RESPONSE) {
// communication problem
}
else if (state == PGRES_FATAL_ERROR) {
auto details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
throw PgResultError(details);
}
}
//PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
//PGRES_COMMAND_OK, /* a query command that doesn't return
// * anything was executed properly by the
// * backend */
//PGRES_TUPLES_OK, /* a query command that returns tuples was
// * executed properly by the backend, PGresult
// * contains the result tuples */
//PGRES_COPY_OUT, /* Copy Out data transfer in progress */
//PGRES_COPY_IN, /* Copy In data transfer in progress */
//PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the
// * backend */
//PGRES_NONFATAL_ERROR, /* notice or warning message */
//PGRES_FATAL_ERROR, /* query failed */
//PGRES_COPY_BOTH, /* Copy In/Out data transfer in progress */
//PGRES_SINGLE_TUPLE /* single tuple from larger resultset */