Query window has now buttons with icons made in the designer for better looks. Depending on received responses from the database the tabcontrol with the message, data and explain tab now switches to the appropriate tab.
285 lines
5.8 KiB
C++
285 lines
5.8 KiB
C++
#pragma once
|
|
|
|
#include <functional>
|
|
#include <string>
|
|
#include <pgsql/libpq-fe.h>
|
|
#include <cassert>
|
|
#include <QString>
|
|
|
|
#include <memory>
|
|
|
|
namespace Pgsql {
|
|
|
|
class Connection;
|
|
/*
|
|
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;
|
|
|
|
// };
|
|
|
|
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;
|
|
};
|
|
|
|
/** 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;
|
|
// };
|
|
|
|
Result() = default;
|
|
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;
|
|
|
|
ExecStatusType resultStatus();
|
|
|
|
std::string getResStatus();
|
|
|
|
/** 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();
|
|
|
|
|
|
|
|
|
|
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;
|
|
};
|
|
|
|
|
|
class Canceller {
|
|
public:
|
|
Canceller() = default;
|
|
Canceller(PGcancel *c);
|
|
Canceller(const Canceller&) = delete;
|
|
Canceller& operator=(const Canceller&) = delete;
|
|
Canceller(Canceller&& rhs);
|
|
Canceller& operator=(Canceller&& rhs);
|
|
~Canceller();
|
|
|
|
bool cancel(std::string *error);
|
|
private:
|
|
PGcancel *m_cancel = nullptr;
|
|
};
|
|
|
|
|
|
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());
|
|
}
|
|
|
|
bool connectStart(const char *params);
|
|
|
|
bool connectStart(const std::string ¶ms)
|
|
{
|
|
return connectStart(params.c_str());
|
|
}
|
|
|
|
bool connectStart(const QString ¶ms)
|
|
{
|
|
return connectStart(params.toUtf8().data());
|
|
}
|
|
bool connectStart(const char * const *keywords,
|
|
const char * const *values);
|
|
|
|
PostgresPollingStatusType connectPoll();
|
|
ConnStatusType status();
|
|
int socket();
|
|
|
|
void close();
|
|
Canceller getCancel();
|
|
|
|
std::string getErrorMessage() const;
|
|
|
|
Result query(const char * command);
|
|
Result query(const QString &command)
|
|
{
|
|
return query(command.toUtf8().data());
|
|
}
|
|
|
|
bool sendQuery(const char * query);
|
|
bool sendQuery(const std::string &command)
|
|
{
|
|
return sendQuery(command.c_str());
|
|
}
|
|
bool sendQuery(const QString &command)
|
|
{
|
|
return sendQuery(command.toUtf8().data());
|
|
}
|
|
|
|
std::shared_ptr<Result> getResult();
|
|
|
|
bool consumeInput();
|
|
bool isBusy();
|
|
|
|
void setNoticeReceiver(std::function<void(const PGresult *)> callback);
|
|
private:
|
|
PGconn *conn = nullptr;
|
|
std::function<void(const PGresult *)> notifyReceiver;
|
|
|
|
static void notifyReceiveFunc(void *arg, const PGresult *result);
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
//
|
|
// };
|
|
}
|