From 2ad4a2601f7448e366d44a503cc2b3d07be4a64c Mon Sep 17 00:00:00 2001 From: eelke Date: Sat, 16 Dec 2017 21:40:19 +0100 Subject: [PATCH] Value::getAsArray function to parse array convert to correct type and uses inserter to fill container. Unit tests included. --- pgsql/Pgsql_Value.h | 35 +++++++++++++++++++++++++-------- tests/PgsqlTests/PgsqlTests.pro | 6 +++++- tests/PgsqlTests/tst_Value.cpp | 23 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/pgsql/Pgsql_Value.h b/pgsql/Pgsql_Value.h index 50a6a22..b5b92e3 100644 --- a/pgsql/Pgsql_Value.h +++ b/pgsql/Pgsql_Value.h @@ -2,11 +2,14 @@ #define PGSQL_VALUE_H #include "Pgsql_declare.h" +#include "ArrayParser.h" +#include #include #include namespace Pgsql { + /** \brief Class that is returned as value of a cell to facilitate auto conversion. */ class Value { @@ -30,19 +33,35 @@ namespace Pgsql { bool isString() const; + /** + * + * \param insert_iter An insert_iterator which is used to add the elements of the array to a container. + */ + template + 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: const char *m_val; 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 void operator<<(T &s, const Value &v) { diff --git a/tests/PgsqlTests/PgsqlTests.pro b/tests/PgsqlTests/PgsqlTests.pro index 017b97f..18845c9 100644 --- a/tests/PgsqlTests/PgsqlTests.pro +++ b/tests/PgsqlTests/PgsqlTests.pro @@ -8,12 +8,16 @@ CONFIG += qt QT += core +QMAKE_CXXFLAGS += /std:c++17 + INCLUDEPATH += C:\prog\include C:\Prog\include\pgsql HEADERS += 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 diff --git a/tests/PgsqlTests/tst_Value.cpp b/tests/PgsqlTests/tst_Value.cpp index 092b9f1..64b1ffe 100644 --- a/tests/PgsqlTests/tst_Value.cpp +++ b/tests/PgsqlTests/tst_Value.cpp @@ -2,6 +2,9 @@ #include #include "Pgsql_Value.h" #include "PrintTo_Qt.h" +#include +#include +#include using namespace testing; using namespace Pgsql; @@ -64,3 +67,23 @@ TEST(Pgsql_Value, isString_varchar) Pgsql::Value v("1", VARCHAROID); ASSERT_EQ(v.isString(), true); } + +TEST(Pgsql_Value, getAsArray_Ints) +{ + Pgsql::Value v("{1,2}", TEXTARRAYOID); + std::vector r; + v.getAsArray(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 r; + v.getAsArray(std::inserter(r, r.end())); + + ASSERT_EQ(r.size(), 2); +}