196 lines
3.5 KiB
C++
196 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <functional>
|
|
#include <string>
|
|
#include <pgsql/libpq-fe.h>
|
|
#include <cassert>
|
|
#include <QString>
|
|
|
|
namespace Pgsql {
|
|
|
|
/*
|
|
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;
|
|
|
|
};
|
|
|
|
/** 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(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 getResultStatus();
|
|
std::string getResStatus();
|
|
|
|
|
|
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 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());
|
|
}
|
|
|
|
void close();
|
|
|
|
std::string getErrorMessage() const;
|
|
|
|
Result Query(const char * query);
|
|
Result Query(const QString &query)
|
|
{
|
|
return Query(query.toUtf8().data());
|
|
}
|
|
|
|
private:
|
|
PGconn *conn = nullptr;
|
|
};
|
|
|
|
class Canceller {
|
|
public:
|
|
void Cancel();
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
//
|
|
// };
|
|
}
|