2016-12-26 16:06:55 +01:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <pgsql/libpq-fe.h>
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
namespace Pgsql {
|
|
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
class Connection;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
/*
|
|
|
|
|
|
This library has multiple layers.
|
|
|
|
|
|
|
|
|
|
|
|
Layer 1 delivers lowlevel C++ wrappers for the basic libpq functionality. Adding
|
|
|
|
|
|
automatic resource management.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionParams {
|
|
|
|
|
|
public:
|
|
|
|
|
|
std::string host;
|
|
|
|
|
|
std::string hostaddr;
|
|
|
|
|
|
unsigned short port = 5432;
|
|
|
|
|
|
std::string dbname;
|
|
|
|
|
|
std::string user;
|
|
|
|
|
|
std::string password;
|
|
|
|
|
|
int connect_timeout = -1; ///< -1 omit (ie uses default)
|
|
|
|
|
|
std::string application_name;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
class ErrorDetails {
|
|
|
|
|
|
public:
|
|
|
|
|
|
static ErrorDetails createErrorDetailsFromPGresult(const PGresult *res);
|
|
|
|
|
|
|
|
|
|
|
|
std::string state; ///< PG_DIAG_SQLSTATE Error code as listed in https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
|
|
|
|
|
|
std::string severity;
|
|
|
|
|
|
std::string messagePrimary;
|
|
|
|
|
|
std::string messageDetail;
|
|
|
|
|
|
std::string messageHint;
|
|
|
|
|
|
int statementPosition; ///< First character is one, measured in characters not bytes!
|
|
|
|
|
|
int internalPosition;
|
|
|
|
|
|
std::string internalQuery;
|
|
|
|
|
|
std::string context;
|
|
|
|
|
|
std::string schemaName;
|
|
|
|
|
|
std::string tableName;
|
|
|
|
|
|
std::string columnName;
|
|
|
|
|
|
std::string datatypeName;
|
|
|
|
|
|
std::string constraintName;
|
|
|
|
|
|
std::string sourceFile;
|
|
|
|
|
|
std::string sourceLine;
|
|
|
|
|
|
std::string sourceFunction;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
/** Non-copyable but movable wrapper for a postgresql result. */
|
|
|
|
|
|
class Result {
|
|
|
|
|
|
public:
|
|
|
|
|
|
// class iterator {
|
|
|
|
|
|
// public:
|
|
|
|
|
|
// iterator(Result &r, int rw)
|
|
|
|
|
|
// : result(r), row(rw)
|
|
|
|
|
|
// {}
|
|
|
|
|
|
|
|
|
|
|
|
// iterator operator++()
|
|
|
|
|
|
// {
|
|
|
|
|
|
// iterator t(*this);
|
|
|
|
|
|
// ++row;
|
|
|
|
|
|
// return t;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// iterator& operator++(int)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// ++row;
|
|
|
|
|
|
// return *this;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// bool operator!=(const iterator &rhs)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// assert()
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// private:
|
|
|
|
|
|
// Result &result;
|
|
|
|
|
|
// int row;
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
2016-12-27 21:22:49 +01:00
|
|
|
|
Result() = default;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
Result(PGresult *result);
|
|
|
|
|
|
~Result();
|
|
|
|
|
|
|
|
|
|
|
|
Result(const Result &rhs) = delete;
|
|
|
|
|
|
Result& operator=(const Result &rhs) = delete;
|
|
|
|
|
|
|
|
|
|
|
|
Result(Result &&rhs);
|
|
|
|
|
|
Result& operator=(Result &&rhs);
|
|
|
|
|
|
|
|
|
|
|
|
operator bool() const;
|
|
|
|
|
|
|
2016-12-27 21:22:49 +01:00
|
|
|
|
ExecStatusType resultStatus();
|
2016-12-29 13:48:35 +01:00
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
std::string getResStatus();
|
|
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
/** Use this to retrieve an error code when this is an error result
|
|
|
|
|
|
*
|
|
|
|
|
|
* The possible code are listed in https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
|
|
|
|
|
|
*/
|
|
|
|
|
|
std::string diagSqlState();
|
|
|
|
|
|
/** Retrieves all the error fields. */
|
|
|
|
|
|
ErrorDetails diagDetails();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
|
|
|
|
|
int getRows() const;
|
|
|
|
|
|
int getCols() const;
|
|
|
|
|
|
|
|
|
|
|
|
const char* const getColName(int idx) const;
|
|
|
|
|
|
|
|
|
|
|
|
const char * getVal(int col, int row) const;
|
|
|
|
|
|
|
|
|
|
|
|
// iterator begin();
|
|
|
|
|
|
private:
|
|
|
|
|
|
PGresult *result = nullptr;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
|
|
|
|
|
|
class Canceller {
|
|
|
|
|
|
public:
|
|
|
|
|
|
Canceller(PGcancel *c);
|
|
|
|
|
|
Canceller(const Canceller&) = delete;
|
|
|
|
|
|
Canceller& operator=(const Canceller&) = delete;
|
|
|
|
|
|
Canceller(Canceller&& rhs);
|
|
|
|
|
|
Canceller& operator=(Canceller&& rhs);
|
|
|
|
|
|
~Canceller();
|
|
|
|
|
|
void cancel();
|
|
|
|
|
|
private:
|
|
|
|
|
|
PGcancel *m_cancel;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
class Connection {
|
|
|
|
|
|
public:
|
|
|
|
|
|
Connection();
|
|
|
|
|
|
~Connection();
|
|
|
|
|
|
|
|
|
|
|
|
Connection(const Connection &rhs) = delete;
|
|
|
|
|
|
Connection& operator=(const Connection &rhs) = delete;
|
|
|
|
|
|
|
|
|
|
|
|
Connection(Connection &&rhs);
|
|
|
|
|
|
Connection& operator=(Connection &&rhs);
|
|
|
|
|
|
|
|
|
|
|
|
// void connect(const ConnectionParams ¶ms);
|
|
|
|
|
|
bool connect(const char *params);
|
|
|
|
|
|
bool connect(const QString ¶ms)
|
|
|
|
|
|
{
|
|
|
|
|
|
return connect(params.toUtf8().data());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
bool connectStart(const char *params);
|
|
|
|
|
|
bool connectStart(const QString ¶ms)
|
|
|
|
|
|
{
|
|
|
|
|
|
return connectStart(params.toUtf8().data());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PostgresPollingStatusType connectPoll();
|
|
|
|
|
|
ConnStatusType status();
|
|
|
|
|
|
int socket();
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
void close();
|
2016-12-29 13:48:35 +01:00
|
|
|
|
Canceller getCancel();
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
|
|
|
|
|
std::string getErrorMessage() const;
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
Result query(const char * command);
|
|
|
|
|
|
Result query(const QString &command)
|
2016-12-26 16:06:55 +01:00
|
|
|
|
{
|
2016-12-27 15:41:11 +01:00
|
|
|
|
return query(command.toUtf8().data());
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
bool sendQuery(const char * query);
|
2016-12-27 21:22:49 +01:00
|
|
|
|
bool sendQuery(const QString &command)
|
|
|
|
|
|
{
|
|
|
|
|
|
return sendQuery(command.toUtf8().data());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
std::unique_ptr<Result> getResult();
|
|
|
|
|
|
|
|
|
|
|
|
bool consumeInput();
|
|
|
|
|
|
bool isBusy();
|
|
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
void setNoticeReceiver(std::function<void(const PGresult *)> callback);
|
2016-12-26 16:06:55 +01:00
|
|
|
|
private:
|
|
|
|
|
|
PGconn *conn = nullptr;
|
2016-12-29 13:48:35 +01:00
|
|
|
|
std::function<void(const PGresult *)> notifyReceiver;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
static void notifyReceiveFunc(void *arg, const PGresult *result);
|
2016-12-26 16:06:55 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
class Field {
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
std::string getName() const;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
//Tuples tuples;
|
|
|
|
|
|
int field;
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Tuples {
|
|
|
|
|
|
public:
|
|
|
|
|
|
int getRowCount() const;
|
|
|
|
|
|
int getColCount() const;
|
|
|
|
|
|
|
|
|
|
|
|
Field getField(const std::string &fname);
|
|
|
|
|
|
Field getField(const int idx);
|
|
|
|
|
|
|
|
|
|
|
|
class const_iterator {
|
|
|
|
|
|
public:
|
|
|
|
|
|
const_iterator& operator++();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void rewind();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class OkResult: public Result {
|
|
|
|
|
|
public:
|
|
|
|
|
|
/** If the result is a data result then returns an interface for processing this data.
|
|
|
|
|
|
|
|
|
|
|
|
The returned interface remains valid as long as this OkResult exists.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Tuples* hasTuples();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// class ErrorResult: public Result {
|
|
|
|
|
|
//
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
// class ITransaction {
|
|
|
|
|
|
// public:
|
|
|
|
|
|
//
|
|
|
|
|
|
// Canceller Query(std::string query,
|
|
|
|
|
|
// std::function<void(OkResult)> on_success,
|
|
|
|
|
|
// std::function<void()> on_cancelled,
|
|
|
|
|
|
// std::function<void(ErrorResult)> on_error);
|
|
|
|
|
|
//
|
|
|
|
|
|
// Canceller Query(std::string query,
|
|
|
|
|
|
// std::function<void(OkResult)> on_success,
|
|
|
|
|
|
// std::function<void()> on_cancelled,
|
|
|
|
|
|
// std::function<void(ErrorResult)> on_error);
|
|
|
|
|
|
//
|
|
|
|
|
|
//
|
|
|
|
|
|
// void Rollback(
|
|
|
|
|
|
// std::function<void(OkResult)> on_success,
|
|
|
|
|
|
// std::function<void(ErrorResult)> on_error);
|
|
|
|
|
|
// void Commit(
|
|
|
|
|
|
// std::function<void(OkResult)> on_success,
|
|
|
|
|
|
// std::function<void()> on_cancelled,
|
|
|
|
|
|
// std::function<void(ErrorResult)> on_error);
|
|
|
|
|
|
//
|
|
|
|
|
|
// };
|
|
|
|
|
|
}
|