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
This commit is contained in:
parent
791db7dd80
commit
6313a5a918
1 changed files with 23 additions and 11 deletions
|
|
@ -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 <typename E, typename I>
|
||||
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 <typename E, typename I>
|
||||
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 <typename E, typename I>
|
||||
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 <typename E, typename I>
|
||||
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<size_t>(pos-start));
|
||||
Value val(str.c_str(), OidFor<E>::elem());
|
||||
E v;
|
||||
v << val;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue