Database layer now support automatic type conversion and the Result has iterator support.

This also means you can use for each to loop through a result.
This commit is contained in:
Eelke Klein 2017-01-25 06:50:57 +01:00
parent 6e852f466f
commit 56fbd20635
2 changed files with 155 additions and 24 deletions

View file

@ -46,6 +46,26 @@ ErrorDetails ErrorDetails::createErrorDetailsFromPGresult(const PGresult *result
return r;
}
bool Row::next()
{
if (m_row < m_result.getRows()) {
++m_row;
return true;
}
return false;
}
Value Row::get(int col) const
{
return m_result.get(col, m_row);
}
//Value Row::get(const char *colname) const
//{
//}
Result::Result(PGresult *res)
: result(res)
{
@ -165,6 +185,14 @@ const char * Result::getVal(int col, int row) const
return PQgetvalue(result, row, col);
}
Value Result::get(int col, int row) const
{
return Value(
PQgetvalue(result, row, col),
PQftype(result, col)
);
}
Oid Result::type(int col) const
{
return PQftype(result, col);

View file

@ -6,6 +6,7 @@
#include <cassert>
#include <QString>
#include <codecvt>
#include <memory>
namespace Pgsql {
@ -66,37 +67,127 @@ namespace Pgsql {
std::string sourceFunction;
};
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;
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;
};
/** Non-copyable but movable wrapper for a postgresql result. */
class Result {
public:
// class iterator {
// public:
// iterator(Result &r, int rw)
// : result(r), row(rw)
// {}
class const_iterator {
public:
const_iterator(const Result &r, int rw)
: m_row(r, rw)
{}
// iterator operator++()
// {
// iterator t(*this);
// ++row;
// return t;
// }
const_iterator operator++()
{
const_iterator t(*this);
m_row.next();
return t;
}
// iterator& operator++(int)
// {
// ++row;
// return *this;
// }
const_iterator& operator++(int)
{
m_row.next();
return *this;
}
// bool operator!=(const iterator &rhs)
// {
// assert()
// }
bool operator==(const const_iterator &rhs)
{
return m_row == rhs.m_row;
}
// private:
// Result &result;
// int 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);
@ -122,7 +213,15 @@ namespace Pgsql {
/** 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;
@ -131,6 +230,7 @@ namespace Pgsql {
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;
@ -297,3 +397,6 @@ namespace Pgsql {
//
// };
}