2016-12-26 16:06:55 +01:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <pgsql/libpq-fe.h>
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
2017-01-25 06:50:57 +01:00
|
|
|
|
#include <codecvt>
|
2016-12-27 15:41:11 +01:00
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
namespace Pgsql {
|
|
|
|
|
|
|
2017-01-18 20:50:53 +01:00
|
|
|
|
const Oid oid_bool = 16;
|
|
|
|
|
|
const Oid oid_int2 = 21;
|
|
|
|
|
|
const Oid oid_int4 = 23;
|
|
|
|
|
|
const Oid oid_int8 = 20;
|
|
|
|
|
|
const Oid oid_float4 = 700;
|
|
|
|
|
|
const Oid oid_float8 = 701;
|
|
|
|
|
|
const Oid oid_numeric = 1700;
|
|
|
|
|
|
const Oid oid_oid = 26;
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2017-01-15 21:01:40 +01:00
|
|
|
|
// 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-26 16:06:55 +01:00
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
class ErrorDetails {
|
|
|
|
|
|
public:
|
|
|
|
|
|
static ErrorDetails createErrorDetailsFromPGresult(const PGresult *res);
|
|
|
|
|
|
|
2017-01-21 08:09:12 +01:00
|
|
|
|
std::string errorMessage;
|
2016-12-29 13:48:35 +01:00
|
|
|
|
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;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-25 06:50:57 +01:00
|
|
|
|
class Value {
|
|
|
|
|
|
public:
|
|
|
|
|
|
Value(const char *val, Oid typ)
|
|
|
|
|
|
: m_val(val), m_typ(typ)
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
QString asQString() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return QString::fromUtf8(m_val);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
operator QString() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return QString::fromUtf8(m_val);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
operator std::string() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
operator short() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return (short)std::atoi(m_val);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
operator int() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return std::atoi(m_val);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
operator Oid() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return operator int();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
operator __int64() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return strtoull(m_val, nullptr, 10);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
operator bool() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return strcmp(m_val, "t") == 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
const char *m_val;
|
|
|
|
|
|
Oid m_typ;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Result;
|
|
|
|
|
|
|
|
|
|
|
|
class Row {
|
|
|
|
|
|
public:
|
|
|
|
|
|
Row(const Result &result, int row)
|
|
|
|
|
|
: m_result(result)
|
|
|
|
|
|
, m_row(row)
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
bool next();
|
|
|
|
|
|
bool operator==(const Row& rhs)
|
|
|
|
|
|
{
|
|
|
|
|
|
return &m_result == &rhs.m_result
|
|
|
|
|
|
&& m_row == rhs.m_row;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Value get(int col) const;
|
|
|
|
|
|
//Value get(const char *colname) const;
|
|
|
|
|
|
//bool get(int col, QString &s);
|
|
|
|
|
|
private:
|
|
|
|
|
|
const Result& m_result;
|
|
|
|
|
|
int m_row;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
/** Non-copyable but movable wrapper for a postgresql result. */
|
|
|
|
|
|
class Result {
|
|
|
|
|
|
public:
|
2017-01-25 06:50:57 +01:00
|
|
|
|
class const_iterator {
|
|
|
|
|
|
public:
|
|
|
|
|
|
const_iterator(const Result &r, int rw)
|
|
|
|
|
|
: m_row(r, rw)
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator operator++()
|
|
|
|
|
|
{
|
|
|
|
|
|
const_iterator t(*this);
|
|
|
|
|
|
m_row.next();
|
|
|
|
|
|
return t;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator& operator++(int)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_row.next();
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(const const_iterator &rhs)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_row == rhs.m_row;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(const const_iterator &rhs)
|
|
|
|
|
|
{
|
|
|
|
|
|
return !operator==(rhs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const Row& operator*()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_row;
|
|
|
|
|
|
}
|
|
|
|
|
|
const Row& operator->()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_row;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
Row m_row;
|
|
|
|
|
|
};
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
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();
|
|
|
|
|
|
|
2017-01-25 06:50:57 +01:00
|
|
|
|
const_iterator begin() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return const_iterator(*this, 0);
|
|
|
|
|
|
}
|
2016-12-29 13:48:35 +01:00
|
|
|
|
|
2017-01-25 06:50:57 +01:00
|
|
|
|
const_iterator end() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return const_iterator(*this, getRows());
|
|
|
|
|
|
}
|
2016-12-29 13:48:35 +01:00
|
|
|
|
|
2017-01-16 18:56:07 +01:00
|
|
|
|
int tuplesAffected() const;
|
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;
|
2017-01-25 06:50:57 +01:00
|
|
|
|
Value get(int col, int row) const;
|
2017-01-18 20:50:53 +01:00
|
|
|
|
Oid type(int col) const;
|
|
|
|
|
|
bool null(int col, int row) const;
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
|
|
|
|
|
// iterator begin();
|
|
|
|
|
|
private:
|
|
|
|
|
|
PGresult *result = nullptr;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
|
|
|
|
|
|
class Canceller {
|
|
|
|
|
|
public:
|
2017-01-08 09:58:34 +01:00
|
|
|
|
Canceller() = default;
|
2016-12-29 13:48:35 +01:00
|
|
|
|
Canceller(PGcancel *c);
|
|
|
|
|
|
Canceller(const Canceller&) = delete;
|
|
|
|
|
|
Canceller& operator=(const Canceller&) = delete;
|
|
|
|
|
|
Canceller(Canceller&& rhs);
|
|
|
|
|
|
Canceller& operator=(Canceller&& rhs);
|
|
|
|
|
|
~Canceller();
|
2017-01-08 09:58:34 +01:00
|
|
|
|
|
|
|
|
|
|
bool cancel(std::string *error);
|
2016-12-29 13:48:35 +01:00
|
|
|
|
private:
|
2017-01-08 09:58:34 +01:00
|
|
|
|
PGcancel *m_cancel = nullptr;
|
2016-12-29 13:48:35 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2017-01-06 07:23:40 +01:00
|
|
|
|
|
|
|
|
|
|
bool connectStart(const std::string ¶ms)
|
|
|
|
|
|
{
|
|
|
|
|
|
return connectStart(params.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
bool connectStart(const QString ¶ms)
|
|
|
|
|
|
{
|
|
|
|
|
|
return connectStart(params.toUtf8().data());
|
|
|
|
|
|
}
|
2017-01-15 21:01:40 +01:00
|
|
|
|
bool connectStart(const char * const *keywords,
|
|
|
|
|
|
const char * const *values);
|
2016-12-27 15:41:11 +01:00
|
|
|
|
|
|
|
|
|
|
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);
|
2017-01-08 09:58:34 +01:00
|
|
|
|
bool sendQuery(const std::string &command)
|
|
|
|
|
|
{
|
|
|
|
|
|
return sendQuery(command.c_str());
|
|
|
|
|
|
}
|
2016-12-27 21:22:49 +01:00
|
|
|
|
bool sendQuery(const QString &command)
|
|
|
|
|
|
{
|
|
|
|
|
|
return sendQuery(command.toUtf8().data());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-08 09:58:34 +01:00
|
|
|
|
std::shared_ptr<Result> getResult();
|
2016-12-27 15:41:11 +01:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
//
|
|
|
|
|
|
// };
|
|
|
|
|
|
}
|
2017-01-25 06:50:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|