2017-02-18 12:05:48 +01:00
|
|
|
|
#ifndef PGSQL_VALUE_H
|
|
|
|
|
|
#define PGSQL_VALUE_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "Pgsql_declare.h"
|
2017-12-17 11:27:42 +01:00
|
|
|
|
#include "Pgsql_oids.h"
|
2017-12-16 21:40:19 +01:00
|
|
|
|
#include "ArrayParser.h"
|
|
|
|
|
|
#include <sstream>
|
2017-12-20 22:07:20 +01:00
|
|
|
|
#include <vector>
|
2017-02-18 12:05:48 +01:00
|
|
|
|
#include <QString>
|
|
|
|
|
|
#include <QDateTime>
|
2018-11-08 21:50:49 +01:00
|
|
|
|
#include <optional>
|
2017-02-18 12:05:48 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Pgsql {
|
|
|
|
|
|
|
2017-12-16 21:40:19 +01:00
|
|
|
|
|
2018-12-24 07:46:13 +01:00
|
|
|
|
template <typename E>
|
|
|
|
|
|
E StringToArrayElem(std::string_view sv);
|
|
|
|
|
|
|
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:
|
2018-02-18 07:15:43 +01:00
|
|
|
|
const char *empty_str = "";
|
2017-02-18 12:05:48 +01:00
|
|
|
|
Value(const char *val, Oid typ);
|
|
|
|
|
|
QString asQString() const;
|
2018-02-18 07:15:43 +01:00
|
|
|
|
|
|
|
|
|
|
bool null() const { return m_val == nullptr; }
|
|
|
|
|
|
const char* c_str() const { return m_val == nullptr ? empty_str : m_val; }
|
2017-08-22 12:45:45 +02:00
|
|
|
|
|
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;
|
2018-10-07 19:40:06 +02:00
|
|
|
|
operator char() const;
|
2017-12-09 10:45:13 +01:00
|
|
|
|
|
|
|
|
|
|
bool isString() const;
|
|
|
|
|
|
|
2018-12-24 07:46:13 +01:00
|
|
|
|
|
2021-03-10 19:06:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-17 16:24:12 +01:00
|
|
|
|
/// Retrieves an array type value and passes them to an insert iterator.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// When the array it self is NULL this function behaves the same as for an empty array. No
|
|
|
|
|
|
/// elements are given to the insert iterator and the function returns. Use the null() function
|
|
|
|
|
|
/// to check for NULL values.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// \param insert_iter An insert_iterator which is used to add the elements of the array to a container.
|
|
|
|
|
|
/// \param nullhandling This only affects how NULL elements within the array are handled.
|
2017-12-16 21:40:19 +01:00
|
|
|
|
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
|
|
|
|
{
|
2018-02-18 07:15:43 +01:00
|
|
|
|
if (m_val == nullptr) {
|
2018-11-17 16:24:12 +01:00
|
|
|
|
return;
|
2018-02-18 07:15:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
using value_type = E;
|
2018-12-24 08:10:09 +01:00
|
|
|
|
ArrayParser parser(m_val, -1);
|
2018-02-18 07:15:43 +01:00
|
|
|
|
for (;;) {
|
|
|
|
|
|
auto res = parser.GetNextElem();
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
|
if (res.value) {
|
2018-12-24 07:46:13 +01:00
|
|
|
|
insert_iter = StringToArrayElem<E>(*res.value);
|
2018-02-18 07:15:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
if (nullhandling == NullHandling::Throw)
|
|
|
|
|
|
throw std::runtime_error("Unexpected NULL value in array");
|
|
|
|
|
|
}
|
2017-12-17 09:06:18 +01:00
|
|
|
|
}
|
2018-02-18 07:15:43 +01:00
|
|
|
|
else
|
|
|
|
|
|
break;
|
2017-12-17 09:06:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-17 16:24:12 +01:00
|
|
|
|
/// Retrieves an array typed value, and passes the element to the insert iterator
|
|
|
|
|
|
///
|
|
|
|
|
|
/// \param value_for_nulls Each time it encounters a NULL element in the array it
|
|
|
|
|
|
/// will instead pass this value to the insert iterator.
|
2017-12-17 09:06:18 +01:00
|
|
|
|
template <typename E, typename I>
|
|
|
|
|
|
void getAsArray(I insert_iter, const E &value_for_nulls) const
|
|
|
|
|
|
{
|
2018-09-19 08:45:01 +02:00
|
|
|
|
if (m_val == nullptr) return;
|
|
|
|
|
|
|
2017-12-17 09:06:18 +01:00
|
|
|
|
using value_type = E;
|
2018-12-24 08:10:09 +01:00
|
|
|
|
ArrayParser parser(m_val, -1);
|
2017-12-17 09:06:18 +01:00
|
|
|
|
for (;;) {
|
|
|
|
|
|
auto res = parser.GetNextElem();
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
|
if (res.value) {
|
2018-12-24 07:46:13 +01:00
|
|
|
|
insert_iter = StringToArrayElem<E>(*res.value);
|
2017-12-17 09:06:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
insert_iter = value_for_nulls;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-17 16:24:12 +01:00
|
|
|
|
/// Retrieves an array typed value, and passes the elements to the insert iterator
|
2017-12-17 09:06:18 +01:00
|
|
|
|
template <typename E, typename I>
|
|
|
|
|
|
void getAsArrayOfOptional(I insert_iter) const
|
|
|
|
|
|
{
|
2018-11-17 16:24:12 +01:00
|
|
|
|
if (m_val == nullptr) return;
|
|
|
|
|
|
|
2017-12-17 09:06:18 +01:00
|
|
|
|
using value_type = E;
|
2018-12-24 08:10:09 +01:00
|
|
|
|
ArrayParser parser(m_val, -1);
|
2017-12-17 09:06:18 +01:00
|
|
|
|
for (;;) {
|
|
|
|
|
|
auto res = parser.GetNextElem();
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
|
if (res.value) {
|
2018-12-24 07:46:13 +01:00
|
|
|
|
insert_iter = StringToArrayElem<E>(*res.value);
|
2017-12-17 09:06:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
insert_iter = std::nullopt;
|
|
|
|
|
|
}
|
2017-12-16 21:40:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-17 11:27:42 +01:00
|
|
|
|
|
2018-11-17 16:24:12 +01:00
|
|
|
|
/// De catalog uses vector types which are similar to array
|
|
|
|
|
|
/// but uses spaces as seperators. AFAIK there are only integral
|
|
|
|
|
|
/// vector types so this implementation is only tested for those
|
2017-12-17 11:27:42 +01:00
|
|
|
|
template <typename E, typename I>
|
|
|
|
|
|
void getAsVector(I insert_iter) const
|
|
|
|
|
|
{
|
2018-11-17 16:24:12 +01:00
|
|
|
|
if (m_val == nullptr || *m_val == '\0') return;
|
|
|
|
|
|
|
2017-12-17 11:27:42 +01:00
|
|
|
|
const char * pos = m_val;
|
|
|
|
|
|
const char * start = pos;
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
while (*pos != 0 && *pos != ' ') ++pos;
|
2018-11-17 16:24:12 +01:00
|
|
|
|
// The cast (to prevent warning) should be save as start should always be <= pos
|
2018-12-24 07:46:13 +01:00
|
|
|
|
insert_iter = StringToArrayElem<E>(std::string_view(start, static_cast<size_t>(pos-start)));
|
2017-12-17 11:27:42 +01:00
|
|
|
|
if (*pos == 0)
|
|
|
|
|
|
break;
|
|
|
|
|
|
start = ++pos; // skip space and set new start to match
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-20 22:07:20 +01:00
|
|
|
|
|
|
|
|
|
|
inline Oid getOid() const { return m_typ; }
|
2017-02-18 12:05:48 +01:00
|
|
|
|
private:
|
|
|
|
|
|
const char *m_val;
|
|
|
|
|
|
Oid m_typ;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-09-19 08:25:23 +02:00
|
|
|
|
template <typename T>
|
2018-11-08 21:50:49 +01:00
|
|
|
|
void operator<<(std::optional<T> &s, const Value &v)
|
2018-09-19 08:25:23 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (v.null())
|
2018-11-08 21:50:49 +01:00
|
|
|
|
s = std::optional<T>();
|
2018-09-19 08:25:23 +02:00
|
|
|
|
else
|
2018-11-17 19:32:55 +01:00
|
|
|
|
s = v.operator T();
|
2018-09-19 08:25:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
|
void operator<<(std::vector<T> &s, const Value &v)
|
|
|
|
|
|
{
|
|
|
|
|
|
v.getAsArray(std::back_inserter(s));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-22 12:45:45 +02:00
|
|
|
|
template <typename T>
|
|
|
|
|
|
void operator<<(T &s, const Value &v)
|
|
|
|
|
|
{
|
2017-12-20 22:07:20 +01:00
|
|
|
|
s = v.operator T();
|
2017-08-22 12:45:45 +02:00
|
|
|
|
}
|
2017-08-26 11:44:40 +02:00
|
|
|
|
|
2018-12-24 07:46:13 +01:00
|
|
|
|
template <typename E>
|
|
|
|
|
|
E StringToArrayElem(std::string_view sv)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string str(sv.data(), sv.length());
|
|
|
|
|
|
Value val(str.c_str(), OidFor<E>::elem());
|
|
|
|
|
|
E v;
|
|
|
|
|
|
v << val;
|
|
|
|
|
|
return v;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-18 12:05:48 +01:00
|
|
|
|
} // end namespace Pgsql
|
|
|
|
|
|
|
|
|
|
|
|
#endif // PGSQL_VALUE_H
|