Improved error checking in Value + more to array conversion control.

This commit is contained in:
eelke 2017-12-17 09:06:18 +01:00
parent 1fe7d3c56d
commit d9854d81fa
3 changed files with 153 additions and 18 deletions

View file

@ -45,24 +45,44 @@ Value::operator std::string() const
return m_val;
}
Value::operator short() const
Value::operator int16_t() const
{
return (short)std::atoi(m_val);
int32_t r = operator int32_t();
if (r <= std::numeric_limits<int16_t>::max()
&& r >= std::numeric_limits<int16_t>::min())
return int16_t(r);
throw std::runtime_error("Value conversion to int16_t failed (out of range)");
}
Value::operator int() const
Value::operator int32_t() const
{
return std::atoi(m_val);
const int len = std::strlen(m_val);
if (len > 0) {
char *endptr = nullptr;
long result = std::strtol(m_val, &endptr, 10);
if (endptr == m_val + len)
return result;
}
throw std::runtime_error("Value conversion to int32_t failed");
}
Value::operator Oid() const
{
return operator int();
return operator int32_t();
}
Value::operator long long() const
Value::operator int64_t() const
{
return std::strtoull(m_val, nullptr, 10);
const int len = std::strlen(m_val);
if (len > 0) {
char *endptr = nullptr;
int64_t result = std::strtoll(m_val, &endptr, 10);
if (endptr == m_val + len)
return result;
}
throw std::runtime_error("Value conversion to int64_t failed");
// return std::strtoll(m_val, nullptr, 10);
}
Value::operator bool() const

View file

@ -23,35 +23,91 @@ namespace Pgsql {
operator QDate() const;
operator QTime() const;
operator std::string() const;
operator short() const;
operator int() const;
operator int16_t() const;
operator int32_t() const;
operator Oid() const;
operator long long() const;
operator int64_t() const;
operator bool() const;
operator float() const;
operator double() const;
bool isString() const;
enum class NullHandling {
Ignore,
Throw
};
/**
*
* \param insert_iter An insert_iterator which is used to add the elements of the array to a container.
*/
template <typename E, typename I>
void getAsArray(I insert_iter) const
void getAsArray(I insert_iter, NullHandling nullhandling = NullHandling::Throw) 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;
if (res.value) {
std::string str(res.value->data(), res.value->length());
Value val(str.c_str(), ANYOID);
value_type v;
v << val;
insert_iter = v;
}
else {
if (nullhandling == NullHandling::Throw)
throw std::runtime_error("Unexpected NULL value in array");
}
}
else
break;
}
}
template <typename E, typename I>
void getAsArray(I insert_iter, const E &value_for_nulls) const
{
using value_type = E;
ArrayParser parser(m_val);
for (;;) {
auto res = parser.GetNextElem();
if (res.ok) {
if (res.value) {
std::string str(res.value->data(), res.value->length());
Value val(str.c_str(), ANYOID);
value_type v;
v << val;
insert_iter = v;
}
else {
insert_iter = value_for_nulls;
}
}
else
break;
}
}
template <typename E, typename I>
void getAsArrayOfOptional(I insert_iter) const
{
using value_type = E;
ArrayParser parser(m_val);
for (;;) {
auto res = parser.GetNextElem();
if (res.ok) {
if (res.value) {
std::string str(res.value->data(), res.value->length());
Value val(str.c_str(), ANYOID);
value_type v;
v << val;
insert_iter = v;
}
else {
insert_iter = std::nullopt;
}
}
else
break;

View file

@ -87,3 +87,62 @@ TEST(Pgsql_Value, getAsArray_QDateTime)
ASSERT_EQ(r.size(), 2);
}
TEST(Pgsql_Value, getAsArray_throws_on_NULL)
{
Pgsql::Value v("{1,NULL,2}", TEXTARRAYOID);
std::vector<int> r;
try {
v.getAsArray<int>(std::back_inserter(r));
FAIL();
} catch (std::runtime_error &) {
SUCCEED();
} catch (...) {
FAIL();
}
}
TEST(Pgsql_Value, getAsArray_default_on_NULL)
{
Pgsql::Value v("{1,NULL,2}", TEXTARRAYOID);
std::vector<int> r;
try {
v.getAsArray<int>(std::back_inserter(r), -1);
ASSERT_EQ(r.size(), 3);
ASSERT_EQ(r[0], 1);
ASSERT_EQ(r[1], -1);
ASSERT_EQ(r[2], 2);
} catch (...) {
FAIL();
}
}
TEST(Pgsql_Value, getAsArray_ignore_NULL)
{
Pgsql::Value v("{1,NULL,2}", TEXTARRAYOID);
std::vector<int> r;
try {
v.getAsArray<int>(std::back_inserter(r), Value::NullHandling::Ignore);
ASSERT_EQ(r.size(), 2);
ASSERT_EQ(r[0], 1);
ASSERT_EQ(r[1], 2);
} catch (...) {
FAIL();
}
}
TEST(Pgsql_Value, getAsArrayOptional)
{
Pgsql::Value v("{1,NULL,2}", TEXTARRAYOID);
std::vector<std::optional<int>> r;
try {
v.getAsArrayOfOptional<int>(std::back_inserter(r));
ASSERT_EQ(r.size(), 3);
ASSERT_EQ(r[0], 1);
ASSERT_EQ(r[1], std::nullopt);
ASSERT_EQ(r[2], 2);
} catch (...) {
FAIL();
}
}