From 6313a5a918cc93e1a0450a2682ce21b03b71f1a8 Mon Sep 17 00:00:00 2001 From: eelke Date: Sat, 17 Nov 2018 16:24:12 +0100 Subject: [PATCH] Made behaviour of getAsArray versions and getAsVector more consistent. The version `void getAsArray(I insert_iter, NullHandling nullhandling = NullHandling::Throw) const` used the nullhandling param to also throw an error when the whole array is NULL however as empty arrays are often saved as NULL values and the distinction is often not important I decided that behaviour was more annoying then useful. You can easily use null() if the distinction is important. getAsArrayOfOptional and getAsVector completely forgot to check for NULL value getAsVector also didn't test for empty strings which appears to be possible --- pgsql/Pgsql_Value.h | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/pgsql/Pgsql_Value.h b/pgsql/Pgsql_Value.h index c2ad771..258dd27 100644 --- a/pgsql/Pgsql_Value.h +++ b/pgsql/Pgsql_Value.h @@ -40,16 +40,19 @@ namespace Pgsql { bool isString() const; - /** - * - * \param insert_iter An insert_iterator which is used to add the elements of the array to a container. - */ + /// 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. template void getAsArray(I insert_iter, NullHandling nullhandling = NullHandling::Throw) const { if (m_val == nullptr) { - if (nullhandling == NullHandling::Throw) - throw std::runtime_error("Unexpected NULL value for array"); + return; } else { using value_type = E; @@ -75,6 +78,10 @@ namespace Pgsql { } } + /// 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. template void getAsArray(I insert_iter, const E &value_for_nulls) const { @@ -101,9 +108,12 @@ namespace Pgsql { } } + /// Retrieves an array typed value, and passes the elements to the insert iterator template void getAsArrayOfOptional(I insert_iter) const { + if (m_val == nullptr) return; + using value_type = E; ArrayParser parser(m_val); for (;;) { @@ -125,18 +135,20 @@ namespace Pgsql { } } - /** 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 - */ + /// 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 template void getAsVector(I insert_iter) const { + if (m_val == nullptr || *m_val == '\0') return; + const char * pos = m_val; const char * start = pos; for (;;) { while (*pos != 0 && *pos != ' ') ++pos; - std::string str(start, pos-start); + // 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;