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