Added list of databases and roles.

Roles works for atleast 9.3 and up.

Reorganizing code for communicating with database.
This commit is contained in:
eelke 2017-02-18 12:05:48 +01:00
parent 8c077b3d5f
commit 2d962334da
28 changed files with 881 additions and 428 deletions

View file

@ -12,6 +12,8 @@
#include <fmt/format.h>
#include "Pgsql_Result.h"
namespace Pgsql {
class Connection;
@ -36,213 +38,12 @@ namespace Pgsql {
// };
class ErrorDetails {
public:
static ErrorDetails createErrorDetailsFromPGresult(const PGresult *res);
std::string errorMessage;
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;
};
/** \brief Class that is returned as value of a cell to facilitate auto conversion.
*/
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;
/** \brief A reference to a specific row from a result.
*
* As it is a reference its contents won't be valid after its associated result has
* been destroyed.
*/
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;
};
/** \brief Non-copyable but movable wrapper for a postgresql result.
*
* This class makes sure the result is removed from memory. It also supplies an iterator for the
* rows from the result facilitating also the use of the cpp for each.
*/
class Result {
public:
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;
};
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();
const_iterator begin() const
{
return const_iterator(*this, 0);
}
const_iterator end() const
{
return const_iterator(*this, getRows());
}
int tuplesAffected() const;
int getRows() const;
int getCols() const;
const char* const getColName(int idx) const;
const char * getVal(int col, int row) const;
Value get(int col, int row) const;
Oid type(int col) const;
bool null(int col, int row) const;
// iterator begin();
private:
PGresult *result = nullptr;
};
/** \brief Wrapper for a cancel object from libpq.