Updating rows kinda works.

Blocking calls are still used.
This commit is contained in:
eelke 2018-02-18 07:15:43 +01:00
parent 99d738ee65
commit 628c16e2f4
10 changed files with 179 additions and 81 deletions

View file

@ -153,6 +153,18 @@ Result Connection::query(const char * command)
return Result(result);
}
Result Connection::queryParam(const char * command, const Params &params)
{
PGresult *result = PQexecParams(conn, command, params.size(), params.types(),
params.values(), params.lengths(), params.formats(), 0);
return Result(result);
}
Result Connection::queryParam(const QString &command, const Params &params)
{
return queryParam(command.toUtf8().data(), params);
}
bool Connection::sendQuery(const char *query)
{

View file

@ -92,11 +92,15 @@ namespace Pgsql {
std::string getErrorMessage() const;
Result query(const char * command);
Result query(const QString &command)
{
return query(command.toUtf8().data());
}
Result queryParam(const char * command, const Params &params);
Result queryParam(const QString &command, const Params &params);
bool sendQuery(const char * query);
bool sendQuery(const std::string &command)
{

View file

@ -5,6 +5,8 @@
#include <QString>
#include <libpq-fe.h>
#include "Pgsql_declare.h"
#include "Pgsql_oids.h"
#include <boost/optional.hpp>
namespace Pgsql {
@ -18,8 +20,12 @@ namespace Pgsql {
~Params();
void add(const QString &s, Oid oid=VARCHAROID);
void add(const char *data, Oid oid=VARCHAROID);
void add(const QString &s, Oid oid=varchar_oid);
void add(const char *data, Oid oid=varchar_oid);
void add(boost::optional<std::string> s, Oid oid=varchar_oid)
{
add(s ? s->c_str() : nullptr, oid);
}
//void addBinary(const char *data, int length, Oid oid);
void clear();

View file

@ -183,8 +183,10 @@ Value Result::get(int col, int row) const
colRangeCheck(col);
rowRangeCheck(row);
bool is_null = PQgetisnull(result, row, col);
char *ptr = is_null ? nullptr : PQgetvalue(result, row, col);
return Value(
PQgetvalue(result, row, col),
ptr,
PQftype(result, col)
);
}

View file

@ -33,4 +33,7 @@ Value Row::get(int col) const
//{
//}
Row::const_iterator Row::end() const
{
return const_iterator(*this, m_result.cols());
}

View file

@ -14,6 +14,51 @@ namespace Pgsql {
*/
class Row {
public:
class const_iterator {
public:
const_iterator(const Row &r, int col)
: m_row(r)
, m_col(col)
{}
const_iterator operator++()
{
const_iterator t(*this);
++m_col;
return t;
}
const_iterator& operator++(int)
{
++m_col;
return *this;
}
bool operator==(const const_iterator &rhs)
{
return m_row == rhs.m_row && m_col == rhs.m_col;
}
bool operator!=(const const_iterator &rhs)
{
return !operator==(rhs);
}
const Value operator*()
{
return m_row.get(m_col);
}
const Value operator->()
{
return m_row.get(m_col);
}
private:
const Row &m_row;
int m_col;
};
Row(const Result &result, int row);
bool next();
@ -22,6 +67,12 @@ namespace Pgsql {
//Value get(const char *colname) const;
//bool get(int col, QString &s);
const_iterator begin() const
{
return const_iterator(*this, 0);
}
const_iterator end() const;
private:
const Result& m_result;

View file

@ -16,9 +16,12 @@ namespace Pgsql {
*/
class Value {
public:
const char *empty_str = "";
Value(const char *val, Oid typ);
QString asQString() const;
const char* c_str() const { return m_val; }
bool null() const { return m_val == nullptr; }
const char* c_str() const { return m_val == nullptr ? empty_str : m_val; }
operator QString() const;
operator QDateTime() const;
@ -42,25 +45,31 @@ namespace Pgsql {
template <typename E, typename I>
void getAsArray(I insert_iter, NullHandling nullhandling = NullHandling::Throw) const
{
using value_type = E;
ArrayParser parser(m_val);
for (;;) {
auto res = parser.GetNextElem();
if (res.ok) {
if (res.value) {
std::string str(res.value->data(), res.value->length());
Value val(str.c_str(), OidFor<E>::elem());
value_type v;
v << val;
insert_iter = v;
}
else {
if (nullhandling == NullHandling::Throw)
throw std::runtime_error("Unexpected NULL value in array");
if (m_val == nullptr) {
if (nullhandling == NullHandling::Throw)
throw std::runtime_error("Unexpected NULL value in array");
}
else {
using value_type = E;
ArrayParser parser(m_val);
for (;;) {
auto res = parser.GetNextElem();
if (res.ok) {
if (res.value) {
std::string str(res.value->data(), res.value->length());
Value val(str.c_str(), OidFor<E>::elem());
value_type v;
v << val;
insert_iter = v;
}
else {
if (nullhandling == NullHandling::Throw)
throw std::runtime_error("Unexpected NULL value in array");
}
}
else
break;
}
else
break;
}
}