Abstract IResult interface for Result

This is to allow faking the result for testing the CrudModel.
This commit is contained in:
eelke 2022-02-06 12:19:43 +01:00
parent 06504ecc1f
commit c346430b25
6 changed files with 103 additions and 67 deletions

View file

@ -1,60 +1,19 @@
#ifndef PGSQL_RESULT_H
#define PGSQL_RESULT_H
#include "Pgsql_IResult.h"
#include "Pgsql_Row.h"
#include "Pgsql_ErrorDetails.h"
namespace Pgsql {
/** \brief Non-copyable but movable wrapper for a postgresql result.
/** \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 {
class Result: public IResult {
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();
@ -65,7 +24,7 @@ namespace Pgsql {
Result(Result &&rhs);
Result& operator=(Result &&rhs);
operator bool() const;
virtual operator bool() const override;
ExecStatusType resultStatus();
@ -79,26 +38,16 @@ namespace Pgsql {
/** Retrieves all the error fields. */
ErrorDetails diagDetails();
const_iterator begin() const
{
return const_iterator(*this, 0);
}
int tuplesAffected() const override;
int rows() const override;
int cols() const override;
const_iterator end() const
{
return const_iterator(*this, rows());
}
const char* getColName(int idx) const override;
int tuplesAffected() const;
int rows() const;
int cols() const;
const char* getColName(int idx) const;
const char* val(int col, int row) const;
Value get(int col, int row) const;
Oid type(int col) const;
bool null(int col, int row) const;
const char* val(int col, int row) const override;
Value get(int col, int row) const override;
Oid type(int col) const override;
bool null(int col, int row) const override;
/// Return the oid of the table this is a column of
/// when the column isn't a table column InvalidOid is returned