2017-02-18 12:05:48 +01:00
|
|
|
|
#ifndef PGSQL_VALUE_H
|
|
|
|
|
|
#define PGSQL_VALUE_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "Pgsql_declare.h"
|
2017-12-16 21:40:19 +01:00
|
|
|
|
#include "ArrayParser.h"
|
|
|
|
|
|
#include <sstream>
|
2017-02-18 12:05:48 +01:00
|
|
|
|
#include <QString>
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Pgsql {
|
|
|
|
|
|
|
2017-12-16 21:40:19 +01:00
|
|
|
|
|
2017-02-18 12:05:48 +01:00
|
|
|
|
/** \brief Class that is returned as value of a cell to facilitate auto conversion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
class Value {
|
|
|
|
|
|
public:
|
|
|
|
|
|
Value(const char *val, Oid typ);
|
|
|
|
|
|
QString asQString() const;
|
2017-08-22 12:45:45 +02:00
|
|
|
|
const char* c_str() const { return m_val; }
|
|
|
|
|
|
|
2017-02-18 12:05:48 +01:00
|
|
|
|
operator QString() const;
|
|
|
|
|
|
operator QDateTime() const;
|
2017-12-09 10:45:13 +01:00
|
|
|
|
operator QDate() const;
|
|
|
|
|
|
operator QTime() const;
|
2017-02-18 12:05:48 +01:00
|
|
|
|
operator std::string() const;
|
2017-12-17 09:06:18 +01:00
|
|
|
|
operator int16_t() const;
|
|
|
|
|
|
operator int32_t() const;
|
2017-02-18 12:05:48 +01:00
|
|
|
|
operator Oid() const;
|
2017-12-17 09:06:18 +01:00
|
|
|
|
operator int64_t() const;
|
2017-02-18 12:05:48 +01:00
|
|
|
|
operator bool() const;
|
2017-12-10 10:35:46 +01:00
|
|
|
|
operator float() const;
|
|
|
|
|
|
operator double() const;
|
2017-12-09 10:45:13 +01:00
|
|
|
|
|
|
|
|
|
|
bool isString() const;
|
|
|
|
|
|
|
2017-12-17 09:06:18 +01:00
|
|
|
|
enum class NullHandling {
|
|
|
|
|
|
Ignore,
|
|
|
|
|
|
Throw
|
|
|
|
|
|
};
|
2017-12-16 21:40:19 +01:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* \param insert_iter An insert_iterator which is used to add the elements of the array to a container.
|
|
|
|
|
|
*/
|
|
|
|
|
|
template <typename E, typename I>
|
2017-12-17 09:06:18 +01:00
|
|
|
|
void getAsArray(I insert_iter, NullHandling nullhandling = NullHandling::Throw) const
|
2017-12-16 21:40:19 +01:00
|
|
|
|
{
|
|
|
|
|
|
using value_type = E;
|
|
|
|
|
|
ArrayParser parser(m_val);
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
auto res = parser.GetNextElem();
|
|
|
|
|
|
if (res.ok) {
|
2017-12-17 09:06:18 +01:00
|
|
|
|
if (res.value) {
|
|
|
|
|
|
std::string str(res.value->data(), res.value->length());
|
|
|
|
|
|
Value val(str.c_str(), ANYOID);
|
|
|
|
|
|
value_type v;
|
|
|
|
|
|
v << val;
|
|
|
|
|
|
insert_iter = v;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
if (nullhandling == NullHandling::Throw)
|
|
|
|
|
|
throw std::runtime_error("Unexpected NULL value in array");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename E, typename I>
|
|
|
|
|
|
void getAsArray(I insert_iter, const E &value_for_nulls) 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(), ANYOID);
|
|
|
|
|
|
value_type v;
|
|
|
|
|
|
v << val;
|
|
|
|
|
|
insert_iter = v;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
insert_iter = value_for_nulls;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename E, typename I>
|
|
|
|
|
|
void getAsArrayOfOptional(I insert_iter) 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(), ANYOID);
|
|
|
|
|
|
value_type v;
|
|
|
|
|
|
v << val;
|
|
|
|
|
|
insert_iter = v;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
insert_iter = std::nullopt;
|
|
|
|
|
|
}
|
2017-12-16 21:40:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-02-18 12:05:48 +01:00
|
|
|
|
private:
|
|
|
|
|
|
const char *m_val;
|
|
|
|
|
|
Oid m_typ;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-08-22 12:45:45 +02:00
|
|
|
|
template <typename T>
|
|
|
|
|
|
void operator<<(T &s, const Value &v)
|
|
|
|
|
|
{
|
|
|
|
|
|
s = static_cast<T>(v);
|
|
|
|
|
|
}
|
2017-08-26 11:44:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
2017-02-18 12:05:48 +01:00
|
|
|
|
} // end namespace Pgsql
|
|
|
|
|
|
|
|
|
|
|
|
#endif // PGSQL_VALUE_H
|