mirror of
https://github.com/allankoechke/soci.git
synced 2026-11-30 10:58:24 +00:00
Merge branch 'drop-istringstream'
Remove almost all remaining uses of std::stringstream. See #1347.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "soci/error.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
|
||||
@@ -30,7 +31,7 @@ bool cstring_to_integer(T& result, char const* buf)
|
||||
char * end;
|
||||
const long long t = strtoll(buf, &end, 10);
|
||||
|
||||
if (end == buf || *end != '\0')
|
||||
if (end == buf || *end != '\0' || errno == ERANGE)
|
||||
return false;
|
||||
|
||||
// successfully converted to long long
|
||||
@@ -53,7 +54,7 @@ bool cstring_to_unsigned(T& result, char const* buf)
|
||||
char * end;
|
||||
const unsigned long long t = strtoull(buf, &end, 10);
|
||||
|
||||
if (end == buf || *end != '\0')
|
||||
if (end == buf || *end != '\0' || errno == ERANGE)
|
||||
return false;
|
||||
|
||||
// successfully converted to unsigned long long
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
|
||||
@@ -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_));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
|
||||
#include "soci-cstrtoi.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <sqlite3.h>
|
||||
#include <sstream>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
using namespace soci;
|
||||
@@ -24,15 +25,10 @@ using namespace sqlite_api;
|
||||
namespace // anonymous
|
||||
{
|
||||
|
||||
// Callback function used to construct the error message in the provided stream.
|
||||
//
|
||||
// SQLite3 own error message will be appended to it.
|
||||
using error_callback = std::function<void (std::ostream& ostr)>;
|
||||
|
||||
// helper function for hardcoded queries: this is a simple wrapper for
|
||||
// sqlite3_exec() which throws an exception on error.
|
||||
void execute_hardcoded(sqlite_api::sqlite3* conn, char const* const query,
|
||||
error_callback const& errCallback,
|
||||
char const* const errMsg,
|
||||
int (*callback)(void*, int, char**, char**) = nullptr,
|
||||
void* callback_arg = nullptr)
|
||||
{
|
||||
@@ -42,39 +38,16 @@ void execute_hardcoded(sqlite_api::sqlite3* conn, char const* const query,
|
||||
std::unique_ptr<char, void(*)(void*)> zErrMsgPtr(zErrMsg, sqlite3_free);
|
||||
if (res != SQLITE_OK)
|
||||
{
|
||||
std::ostringstream ss;
|
||||
errCallback(ss);
|
||||
|
||||
throw sqlite3_soci_error(conn, ss.str(), zErrMsg);
|
||||
throw sqlite3_soci_error(conn, errMsg, zErrMsg);
|
||||
}
|
||||
}
|
||||
|
||||
// Simpler to use overload which uses a hard coded error message.
|
||||
void execute_hardcoded(sqlite_api::sqlite3* conn, char const* const query, char const* const errMsg,
|
||||
int (*callback)(void*, int, char**, char**) = nullptr,
|
||||
void* callback_arg = nullptr)
|
||||
// overload allowing to pass std::string instead of const char*
|
||||
void execute_hardcoded(sqlite_api::sqlite3* conn,
|
||||
std::string const& query,
|
||||
std::string const& errMsg)
|
||||
{
|
||||
return execute_hardcoded(conn, query,
|
||||
[errMsg](std::ostream& ostr) { ostr << errMsg; },
|
||||
callback, callback_arg
|
||||
);
|
||||
}
|
||||
|
||||
void check_sqlite_err(sqlite_api::sqlite3* conn, int res,
|
||||
error_callback const& errCallback)
|
||||
{
|
||||
if (SQLITE_OK != res)
|
||||
{
|
||||
std::ostringstream ss;
|
||||
errCallback(ss);
|
||||
|
||||
throw sqlite3_soci_error(conn, ss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void check_sqlite_err(sqlite_api::sqlite3* conn, int res, char const* const errMsg)
|
||||
{
|
||||
return check_sqlite_err(conn, res, [errMsg](std::ostream& ostr) { ostr << errMsg; });
|
||||
execute_hardcoded(conn, query.c_str(), errMsg.c_str());
|
||||
}
|
||||
|
||||
} // namespace anonymous
|
||||
@@ -131,8 +104,8 @@ sqlite3_session_backend::sqlite3_session_backend(
|
||||
}
|
||||
if (params.get_option("timeout", val))
|
||||
{
|
||||
std::istringstream converter(val);
|
||||
converter >> timeout;
|
||||
if (!cstring_to_integer(timeout, val.c_str()) || timeout < 0)
|
||||
throw soci_error("Invalid value for timeout option: {}", val);
|
||||
}
|
||||
if (params.get_option("synchronous", val))
|
||||
{
|
||||
@@ -165,36 +138,31 @@ sqlite3_session_backend::sqlite3_session_backend(
|
||||
std::unique_ptr<sqlite_api::sqlite3, int(*)(sqlite_api::sqlite3*)>
|
||||
conn_ptr(conn, sqlite3_close);
|
||||
|
||||
check_sqlite_err(conn, res,
|
||||
[&dbname](std::ostream& ostr)
|
||||
{
|
||||
ostr << "Cannot establish connection to \"" << dbname << "\"";
|
||||
}
|
||||
);
|
||||
if (res != SQLITE_OK)
|
||||
throw sqlite3_soci_error(conn, fmt::format(R"(Cannot establish connection to "{}")", dbname));
|
||||
|
||||
// Set the timeout first to have effect on the following queries.
|
||||
res = sqlite3_busy_timeout(conn, timeout * 1000);
|
||||
check_sqlite_err(conn, res, "Failed to set busy timeout for connection. ");
|
||||
if (res != SQLITE_OK)
|
||||
throw sqlite3_soci_error(conn, "Failed to set busy timeout for connection.");
|
||||
|
||||
if (!synchronous.empty())
|
||||
{
|
||||
std::string const query("pragma synchronous=" + synchronous);
|
||||
execute_hardcoded(conn, query.c_str(),
|
||||
[&synchronous](std::ostream& ostr)
|
||||
{
|
||||
ostr << "Setting synchronous pragma to \"" << synchronous << "\" failed";
|
||||
}
|
||||
execute_hardcoded
|
||||
(
|
||||
conn,
|
||||
fmt::format("pragma synchronous={}", synchronous),
|
||||
fmt::format(R"(Setting synchronous pragma to "{}" failed)", synchronous)
|
||||
);
|
||||
}
|
||||
|
||||
if (!foreignKeys.empty())
|
||||
{
|
||||
std::string const query("pragma foreign_keys=" + foreignKeys);
|
||||
execute_hardcoded(conn, query.c_str(),
|
||||
[&foreignKeys](std::ostream& ostr)
|
||||
{
|
||||
ostr << "Setting foreign_keys pragma to \"" << foreignKeys << "\" failed";
|
||||
}
|
||||
execute_hardcoded
|
||||
(
|
||||
conn,
|
||||
fmt::format("pragma foreign_keys={}", foreignKeys),
|
||||
fmt::format(R"(Setting foreign_keys pragma to "{}" failed)", foreignKeys)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -596,7 +596,7 @@ void statement_impl::do_add_query_parameters()
|
||||
for (std::size_t i = 0; i != usize; ++i)
|
||||
{
|
||||
std::string name = get_name(*uses_[i], i, backEnd_);
|
||||
std::stringstream value;
|
||||
std::ostringstream value;
|
||||
uses_[i]->dump_value(value, backEnd_->get_row_to_dump());
|
||||
session_.add_query_parameter(std::move(name), value.str());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user