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

34
pgsql/Pgsql_IResult.h Normal file
View file

@ -0,0 +1,34 @@
#pragma once
#include "Pgsql_ResultConstIterator.h"
namespace Pgsql {
class IResult {
public:
virtual operator bool() const = 0;
virtual int tuplesAffected() const = 0;
virtual int rows() const = 0;
virtual int cols() const = 0;
virtual const char* getColName(int idx) const = 0;
virtual const char* val(int col, int row) const = 0;
virtual Value get(int col, int row) const = 0;
virtual Oid type(int col) const = 0;
virtual bool null(int col, int row) const = 0;
ResultConstIterator begin() const
{
return ResultConstIterator(*this, 0);
}
ResultConstIterator end() const
{
return ResultConstIterator(*this, rows());
}
};
}

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

View file

@ -0,0 +1,51 @@
#pragma once
#include "Pgsql_Row.h"
namespace Pgsql {
class IResult;
class ResultConstIterator {
public:
ResultConstIterator(const IResult &r, int rw)
: m_row(r, rw)
{}
ResultConstIterator operator++()
{
ResultConstIterator t(*this);
m_row.next();
return t;
}
ResultConstIterator& operator++(int)
{
m_row.next();
return *this;
}
bool operator==(const ResultConstIterator &rhs)
{
return m_row == rhs.m_row;
}
bool operator!=(const ResultConstIterator &rhs)
{
return !operator==(rhs);
}
const Row& operator*()
{
return m_row;
}
const Row& operator->()
{
return m_row;
}
private:
Row m_row;
};
}

View file

@ -3,7 +3,7 @@
using namespace Pgsql;
Row::Row(const Result &result, int row)
Row::Row(const IResult &result, int row)
: m_result(result)
, m_row(row)
{}

View file

@ -5,7 +5,7 @@
namespace Pgsql {
class Result;
class IResult;
/** \brief A reference to a specific row from a result.
*
@ -59,7 +59,7 @@ namespace Pgsql {
int m_col;
};
Row(const Result &result, int row);
Row(const IResult &result, int row);
bool next();
bool operator==(const Row& rhs) const;
@ -75,7 +75,7 @@ namespace Pgsql {
const_iterator end() const;
private:
const Result& m_result;
const IResult& m_result;
int m_row;
};

View file

@ -31,9 +31,11 @@ SOURCES += Pgsql_Connection.cpp \
Pgsql_Canceller.cpp
HEADERS += Pgsql_Connection.h \
Pgsql_IResult.h \
Pgsql_Params.h \
Pgsql_PgException.h \
Pgsql_Result.h \
Pgsql_ResultConstIterator.h \
Pgsql_Row.h \
Pgsql_Value.h \
Pgsql_declare.h \