Stop using std::istringstream for parsing numbers in MySQL backend

Use SOCI cstring_to_xxx() functions for consistency with the other
backends and because they should also be more slightly more efficient.
This commit is contained in:
Vadim Zeitlin
2026-04-27 23:51:06 +02:00
parent 897dac8f9c
commit 4131025e8b
3 changed files with 19 additions and 31 deletions

View File

@@ -10,12 +10,13 @@
#include "soci/mysql/soci-mysql.h"
#include "soci-cstrtod.h"
#include "soci-cstrtoi.h"
#include "soci-compiler.h"
// std
#include <cstddef>
#include <ctime>
#include <locale>
#include <sstream>
#include <type_traits>
#include <vector>
namespace soci
@@ -48,12 +49,19 @@ bool is_infinity_or_nan(T x)
SOCI_GCC_WARNING_RESTORE(float-equal)
}
template <typename T>
template <typename T, std::enable_if_t<std::is_signed<T>::value, int> = 0>
void parse_num(char const *buf, T &x)
{
std::istringstream iss(buf);
iss >> x;
if (iss.fail() || (iss.eof() == false))
if (!cstring_to_integer(x, buf))
{
throw soci_error("Cannot convert data.");
}
}
template <typename T, std::enable_if_t<std::is_unsigned<T>::value, int> = 0>
void parse_num(char const *buf, T &x)
{
if (!cstring_to_unsigned(x, buf))
{
throw soci_error("Cannot convert data.");
}

View File

@@ -87,28 +87,10 @@ void mysql_standard_into_type_backend::post_fetch(
}
break;
case x_int8:
{
int32_t tmp = 0;
parse_num(buf, tmp);
if (tmp < (std::numeric_limits<int8_t>::min)() ||
tmp > (std::numeric_limits<int8_t>::max)())
{
throw soci_error("Cannot convert data.");
}
exchange_type_cast<x_int8>(data_) = static_cast<int8_t>(tmp);
}
parse_num(buf, exchange_type_cast<x_int8>(data_));
break;
case x_uint8:
{
uint32_t tmp = 0;
parse_num(buf, tmp);
if (tmp < (std::numeric_limits<uint8_t>::min)() ||
tmp > (std::numeric_limits<uint8_t>::max)())
{
throw soci_error("Cannot convert data.");
}
exchange_type_cast<x_uint8>(data_) = static_cast<uint8_t>(tmp);
}
parse_num(buf, exchange_type_cast<x_uint8>(data_));
break;
case x_int16:
parse_num(buf, exchange_type_cast<x_int16>(data_));

View File

@@ -109,17 +109,15 @@ void mysql_vector_into_type_backend::post_fetch(bool gotData, indicator *ind)
break;
case x_int8:
{
int32_t tmp = 0;
parse_num(buf, tmp);
int8_t val = static_cast<int8_t>(tmp);
int8_t val;
parse_num(buf, val);
set_invector_(data_, i, val);
}
break;
case x_uint8:
{
uint32_t tmp = 0;
parse_num(buf, tmp);
uint8_t val = static_cast<uint8_t>(tmp);
uint8_t val;
parse_num(buf, val);
set_invector_(data_, i, val);
}
break;