Value::getAsArray function to parse array convert to correct type and
uses inserter to fill container. Unit tests included.
This commit is contained in:
parent
ec930218cd
commit
2ad4a2601f
3 changed files with 55 additions and 9 deletions
|
|
@ -2,11 +2,14 @@
|
||||||
#define PGSQL_VALUE_H
|
#define PGSQL_VALUE_H
|
||||||
|
|
||||||
#include "Pgsql_declare.h"
|
#include "Pgsql_declare.h"
|
||||||
|
#include "ArrayParser.h"
|
||||||
|
#include <sstream>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
namespace Pgsql {
|
namespace Pgsql {
|
||||||
|
|
||||||
|
|
||||||
/** \brief Class that is returned as value of a cell to facilitate auto conversion.
|
/** \brief Class that is returned as value of a cell to facilitate auto conversion.
|
||||||
*/
|
*/
|
||||||
class Value {
|
class Value {
|
||||||
|
|
@ -30,19 +33,35 @@ namespace Pgsql {
|
||||||
|
|
||||||
bool isString() const;
|
bool isString() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* \param insert_iter An insert_iterator which is used to add the elements of the array to a container.
|
||||||
|
*/
|
||||||
|
template <typename E, typename I>
|
||||||
|
void getAsArray(I insert_iter) const
|
||||||
|
{
|
||||||
|
using value_type = E;
|
||||||
|
ArrayParser parser(m_val);
|
||||||
|
for (;;) {
|
||||||
|
auto res = parser.GetNextElem();
|
||||||
|
if (res.ok) {
|
||||||
|
std::string str(res.value->data(), res.value->length());
|
||||||
|
// std::istringstream iss(str);
|
||||||
|
Value val(str.c_str(), ANYOID);
|
||||||
|
value_type v;
|
||||||
|
// iss >> v;
|
||||||
|
v << val;
|
||||||
|
insert_iter = v;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
const char *m_val;
|
const char *m_val;
|
||||||
Oid m_typ;
|
Oid m_typ;
|
||||||
};
|
};
|
||||||
|
|
||||||
// void operator<<(QString &s, const Value &v);
|
|
||||||
// void operator<<(QDateTime &l, const Value &v);
|
|
||||||
// void operator<<(std::string &l, const Value &v);
|
|
||||||
// void operator<<(short &l, const Value &v);
|
|
||||||
// void operator<<(int &l, const Value &v);
|
|
||||||
// void operator<<(Oid &l, const Value &v);
|
|
||||||
// void operator<<(__int64 &l, const Value &v);
|
|
||||||
// void operator<<(bool &l, const Value &v);
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void operator<<(T &s, const Value &v)
|
void operator<<(T &s, const Value &v)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,16 @@ CONFIG += qt
|
||||||
|
|
||||||
QT += core
|
QT += core
|
||||||
|
|
||||||
|
QMAKE_CXXFLAGS += /std:c++17
|
||||||
|
|
||||||
INCLUDEPATH += C:\prog\include C:\Prog\include\pgsql
|
INCLUDEPATH += C:\prog\include C:\Prog\include\pgsql
|
||||||
|
|
||||||
HEADERS +=
|
HEADERS +=
|
||||||
|
|
||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
tst_Value.cpp
|
tst_Value.cpp \
|
||||||
|
tst_ArrayParser.cpp \
|
||||||
|
tst_Pgsql_oids.cpp
|
||||||
|
|
||||||
|
|
||||||
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../pgsql/release/ -lpgsql
|
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../pgsql/release/ -lpgsql
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
#include <gmock/gmock-matchers.h>
|
#include <gmock/gmock-matchers.h>
|
||||||
#include "Pgsql_Value.h"
|
#include "Pgsql_Value.h"
|
||||||
#include "PrintTo_Qt.h"
|
#include "PrintTo_Qt.h"
|
||||||
|
#include <iterator>
|
||||||
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using namespace testing;
|
using namespace testing;
|
||||||
using namespace Pgsql;
|
using namespace Pgsql;
|
||||||
|
|
@ -64,3 +67,23 @@ TEST(Pgsql_Value, isString_varchar)
|
||||||
Pgsql::Value v("1", VARCHAROID);
|
Pgsql::Value v("1", VARCHAROID);
|
||||||
ASSERT_EQ(v.isString(), true);
|
ASSERT_EQ(v.isString(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Pgsql_Value, getAsArray_Ints)
|
||||||
|
{
|
||||||
|
Pgsql::Value v("{1,2}", TEXTARRAYOID);
|
||||||
|
std::vector<int> r;
|
||||||
|
v.getAsArray<int>(std::back_inserter(r));
|
||||||
|
|
||||||
|
ASSERT_EQ(r.size(), 2);
|
||||||
|
ASSERT_EQ(r[0], 1);
|
||||||
|
ASSERT_EQ(r[1], 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Pgsql_Value, getAsArray_QDateTime)
|
||||||
|
{
|
||||||
|
Pgsql::Value v("{\"2017-12-11 10:11:22\",\"2017-12-13 12:00:11\"}", TEXTARRAYOID);
|
||||||
|
std::set<QDateTime> r;
|
||||||
|
v.getAsArray<QDateTime>(std::inserter(r, r.end()));
|
||||||
|
|
||||||
|
ASSERT_EQ(r.size(), 2);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue