Slightly more complex then you may expect because the tablespace specified by the tables tends to be oid 0 which means the default tablespace is used. However this does not mean pg_default, it means the tablespace as defined as standard in the database definition. So we need to know what the current dbname is retrieve it's details from the catalog and retrieve that tablespace to know what to show for an oid of 0.
139 lines
3.1 KiB
C++
139 lines
3.1 KiB
C++
#pragma once
|
|
|
|
#include <functional>
|
|
#include <string>
|
|
#include <libpq-fe.h>
|
|
#include <cassert>
|
|
#include <QString>
|
|
|
|
#include <vector>
|
|
#include <codecvt>
|
|
#include <memory>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#include "Pgsql_Result.h"
|
|
|
|
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 Result;
|
|
class Params;
|
|
|
|
/** \brief Wrapper for a cancel object from libpq.
|
|
*/
|
|
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;
|
|
};
|
|
|
|
/** \brief Class for connecting to the database.
|
|
*
|
|
* The class isolates the programmer from the worst C style parts
|
|
* of the libpq API but is mostly a very thin wrapper.
|
|
*/
|
|
class Connection {
|
|
public:
|
|
Connection();
|
|
~Connection();
|
|
|
|
Connection(const Connection &rhs) = delete;
|
|
Connection& operator=(const Connection &rhs) = delete;
|
|
|
|
Connection(Connection &&rhs);
|
|
Connection& operator=(Connection &&rhs);
|
|
|
|
bool connect(const char *params);
|
|
bool connect(const QString ¶ms)
|
|
{
|
|
return connect(params.toUtf8().data());
|
|
}
|
|
|
|
bool connect(const char *const * keywords, const char* const * values, int expand_dbname);
|
|
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());
|
|
}
|
|
|
|
Result queryParam(const char * command, const Params ¶ms);
|
|
Result queryParam(const QString &command, const Params ¶ms);
|
|
|
|
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());
|
|
}
|
|
|
|
bool sendQueryParams(const char * command, const Params ¶ms);
|
|
|
|
std::shared_ptr<Result> getResult();
|
|
|
|
bool consumeInput();
|
|
bool isBusy();
|
|
|
|
void setNoticeReceiver(std::function<void(const PGresult *)> callback);
|
|
|
|
std::string escapeLiteral(const std::string_view &literal);
|
|
QString escapeLiteral(const QString &literal);
|
|
std::string escapeIdentifier(const std::string_view &ident);
|
|
QString escapeIdentifier(const QString &ident);
|
|
|
|
QString getDBName() const;
|
|
private:
|
|
PGconn *conn = nullptr;
|
|
std::function<void(const PGresult *)> notifyReceiver;
|
|
|
|
static void notifyReceiveFunc(void *arg, const PGresult *result);
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|