2016-12-26 16:06:55 +01:00
|
|
|
|
#include "PgsqlConn.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace Pgsql;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Result::Result(PGresult *res)
|
|
|
|
|
|
: result(res)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (res == nullptr) {
|
|
|
|
|
|
throw std::runtime_error("Passing nullptr to Result::Result is not allowed");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result::~Result()
|
|
|
|
|
|
{
|
|
|
|
|
|
PQclear(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Result::Result(Result &&rhs)
|
|
|
|
|
|
: result(rhs.result)
|
|
|
|
|
|
{
|
|
|
|
|
|
rhs.result = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result& Result::operator=(Result &&rhs)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (result) {
|
|
|
|
|
|
PQclear(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
result = rhs.result;
|
|
|
|
|
|
rhs.result = nullptr;
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result::operator bool() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return result != nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ExecStatusType Result::getResultStatus()
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQresultStatus(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string Result::getResStatus()
|
|
|
|
|
|
{
|
|
|
|
|
|
// return PQresStatus(result);
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Result::getRows() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQntuples(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Result::getCols() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQnfields(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* const Result::getColName(int idx) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQfname(result, idx);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * Result::getVal(int col, int row) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQgetvalue(result, row, col);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Connection::connect(const char *params)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
conn = PQconnectdb(params);
|
|
|
|
|
|
if (conn) {
|
|
|
|
|
|
ConnStatusType status = PQstatus(conn);
|
|
|
|
|
|
result = (status == CONNECTION_OK);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
bool Connection::connectStart(const char* params)
|
|
|
|
|
|
{
|
|
|
|
|
|
conn = PQconnectStart(params);
|
|
|
|
|
|
return conn != nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PostgresPollingStatusType Connection::connectPoll()
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQconnectPoll(conn);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ConnStatusType Connection::status()
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQstatus(conn);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Connection::socket()
|
|
|
|
|
|
{
|
|
|
|
|
|
return PQsocket(conn);
|
|
|
|
|
|
}
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
|
|
|
|
|
std::string Connection::getErrorMessage() const
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string result;
|
|
|
|
|
|
if (conn) {
|
|
|
|
|
|
result = PQerrorMessage(conn);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
result = "no connection";
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
Result Connection::query(const char * command)
|
2016-12-26 16:06:55 +01:00
|
|
|
|
{
|
2016-12-27 15:41:11 +01:00
|
|
|
|
PGresult *result = PQexec(conn, command);
|
2016-12-26 16:06:55 +01:00
|
|
|
|
if (result) {
|
|
|
|
|
|
return Result(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
throw std::runtime_error("Failed to allocate result object");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-12-27 15:41:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Connection::sendQuery(const char *query)
|
|
|
|
|
|
{
|
|
|
|
|
|
int res = PQsendQuery(conn, query);
|
|
|
|
|
|
return res == 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Result> Connection::getResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
PGresult *r = PQgetResult(conn);
|
|
|
|
|
|
if (r) {
|
|
|
|
|
|
return std::make_unique<Result>(r);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|