207 lines
3.5 KiB
C++
207 lines
3.5 KiB
C++
|
|
#include "PgsqlConn.h"
|
|||
|
|
#include "Pgsql_declare.h"
|
|||
|
|
#include "Pgsql_Params.h"
|
|||
|
|
#include <stdexcept>
|
|||
|
|
|
|||
|
|
|
|||
|
|
using namespace Pgsql;
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
Canceller::Canceller(PGcancel *c)
|
|||
|
|
: m_cancel(c)
|
|||
|
|
{}
|
|||
|
|
|
|||
|
|
Canceller::Canceller(Canceller&& rhs)
|
|||
|
|
: m_cancel(rhs.m_cancel)
|
|||
|
|
{
|
|||
|
|
rhs.m_cancel = nullptr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Canceller& Canceller::operator=(Canceller&& rhs)
|
|||
|
|
{
|
|||
|
|
if (m_cancel) {
|
|||
|
|
PQfreeCancel(m_cancel);
|
|||
|
|
}
|
|||
|
|
m_cancel = rhs.m_cancel;
|
|||
|
|
rhs.m_cancel = nullptr;
|
|||
|
|
return *this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Canceller::~Canceller()
|
|||
|
|
{
|
|||
|
|
if (m_cancel) {
|
|||
|
|
PQfreeCancel(m_cancel);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Canceller::cancel(std::string *error)
|
|||
|
|
{
|
|||
|
|
const int errbuf_size = 256;
|
|||
|
|
char errbuf[errbuf_size];
|
|||
|
|
bool res = PQcancel(m_cancel, errbuf, errbuf_size);
|
|||
|
|
if (!res && error) {
|
|||
|
|
*error = errbuf;
|
|||
|
|
}
|
|||
|
|
return res;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Connection::connect(const char *params)
|
|||
|
|
{
|
|||
|
|
bool result = false;
|
|||
|
|
conn = PQconnectdb(params);
|
|||
|
|
if (conn) {
|
|||
|
|
ConnStatusType status = PQstatus(conn);
|
|||
|
|
result = (status == CONNECTION_OK);
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Connection::connect(const char *const * keywords, const char* const * values, int expand_dbname)
|
|||
|
|
{
|
|||
|
|
bool result = false;
|
|||
|
|
conn = PQconnectdbParams(keywords, values, expand_dbname);
|
|||
|
|
if (conn) {
|
|||
|
|
ConnStatusType status = PQstatus(conn);
|
|||
|
|
result = (status == CONNECTION_OK);
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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);
|
|||
|
|
return Result(result);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
bool Connection::sendQuery(const char *query)
|
|||
|
|
{
|
|||
|
|
int res = PQsendQuery(conn, query);
|
|||
|
|
return res == 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool Connection::sendQueryParams(const char * command, const Params ¶ms)
|
|||
|
|
{
|
|||
|
|
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) {
|
|||
|
|
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));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Connection::notifyReceiveFunc(void *arg, const PGresult *result)
|
|||
|
|
{
|
|||
|
|
Connection *c = reinterpret_cast<Connection *>(arg);
|
|||
|
|
c->notifyReceiver(result);
|
|||
|
|
}
|