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; 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::Result(PGresult *res)
: result(res) : result(res)
{ {
@ -165,6 +185,14 @@ const char * Result::getVal(int col, int row) const
return PQgetvalue(result, row, col); 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 Oid Result::type(int col) const
{ {
return PQftype(result, col); return PQftype(result, col);

View file

@ -6,6 +6,7 @@
#include <cassert> #include <cassert>
#include <QString> #include <QString>
#include <codecvt>
#include <memory> #include <memory>
namespace Pgsql { namespace Pgsql {
@ -66,37 +67,127 @@ namespace Pgsql {
std::string sourceFunction; 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. */ /** Non-copyable but movable wrapper for a postgresql result. */
class Result { class Result {
public: public:
// class iterator { class const_iterator {
// public: public:
// iterator(Result &r, int rw) const_iterator(const Result &r, int rw)
// : result(r), row(rw) : m_row(r, rw)
// {} {}
// iterator operator++() const_iterator operator++()
// { {
// iterator t(*this); const_iterator t(*this);
// ++row; m_row.next();
// return t; return t;
// } }
// iterator& operator++(int) const_iterator& operator++(int)
// { {
// ++row; m_row.next();
// return *this; return *this;
// } }
// bool operator!=(const iterator &rhs) bool operator==(const const_iterator &rhs)
// { {
// assert() return m_row == rhs.m_row;
// } }
// private: bool operator!=(const const_iterator &rhs)
// Result &result; {
// int row; return !operator==(rhs);
// }; }
const Row& operator*()
{
return m_row;
}
const Row& operator->()
{
return m_row;
}
private:
Row m_row;
};
Result() = default; Result() = default;
Result(PGresult *result); Result(PGresult *result);
@ -122,7 +213,15 @@ namespace Pgsql {
/** Retrieves all the error fields. */ /** Retrieves all the error fields. */
ErrorDetails diagDetails(); ErrorDetails diagDetails();
const_iterator begin() const
{
return const_iterator(*this, 0);
}
const_iterator end() const
{
return const_iterator(*this, getRows());
}
int tuplesAffected() const; int tuplesAffected() const;
int getRows() const; int getRows() const;
@ -131,6 +230,7 @@ namespace Pgsql {
const char* const getColName(int idx) const; const char* const getColName(int idx) const;
const char * getVal(int col, int row) const; const char * getVal(int col, int row) const;
Value get(int col, int row) const;
Oid type(int col) const; Oid type(int col) const;
bool null(int col, int row) const; bool null(int col, int row) const;
@ -297,3 +397,6 @@ namespace Pgsql {
// //
// }; // };
} }