From efb3e71556996bae3b5910a164b7e5b5ba71e341 Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 24 Dec 2018 07:46:13 +0100 Subject: [PATCH] The getAsArray and getAsVector function now use StringToArrayElem template for conversion to the array element type. A five line construct had been copy pasted to achieve conversion by using the capabilities of the Value object however this was not going to work for types that are not known to the database as adding support for these to Value would be a bad idea. So StringToArrayElem was introduced with a default implementation that relies on Value. --- pgsql/Pgsql_Value.h | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/pgsql/Pgsql_Value.h b/pgsql/Pgsql_Value.h index 100ccdf..2ce89cb 100644 --- a/pgsql/Pgsql_Value.h +++ b/pgsql/Pgsql_Value.h @@ -13,6 +13,9 @@ namespace Pgsql { + template + E StringToArrayElem(std::string_view sv); + /** \brief Class that is returned as value of a cell to facilitate auto conversion. */ class Value { @@ -40,6 +43,7 @@ namespace Pgsql { bool isString() const; + /// 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 @@ -61,11 +65,7 @@ namespace Pgsql { 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::elem()); - value_type v; - v << val; - insert_iter = v; + insert_iter = StringToArrayElem(*res.value); } else { if (nullhandling == NullHandling::Throw) @@ -93,11 +93,7 @@ namespace Pgsql { 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::elem()); - value_type v; - v << val; - insert_iter = v; + insert_iter = StringToArrayElem(*res.value); } else { insert_iter = value_for_nulls; @@ -120,11 +116,7 @@ namespace Pgsql { 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::elem()); - value_type v; - v << val; - insert_iter = v; + insert_iter = StringToArrayElem(*res.value); } else { insert_iter = std::nullopt; @@ -148,11 +140,7 @@ namespace Pgsql { for (;;) { while (*pos != 0 && *pos != ' ') ++pos; // The cast (to prevent warning) should be save as start should always be <= pos - std::string str(start, static_cast(pos-start)); - Value val(str.c_str(), OidFor::elem()); - E v; - v << val; - insert_iter = v; + insert_iter = StringToArrayElem(std::string_view(start, static_cast(pos-start))); if (*pos == 0) break; start = ++pos; // skip space and set new start to match @@ -186,7 +174,16 @@ namespace Pgsql { s = v.operator T(); } - + template + E StringToArrayElem(std::string_view sv) + { + std::string str(sv.data(), sv.length()); + Value val(str.c_str(), OidFor::elem()); + E v; + v << val; + return v; + } + } // end namespace Pgsql #endif // PGSQL_VALUE_H