mirror of
https://github.com/allankoechke/soci.git
synced 2026-11-30 10:58:24 +00:00
Stop using ostringstream for errors in SQLite3 backend code
Use fmt::format() to build them directly instead, this is much simpler. Also get rid of useless and trivial check_sqlite_err() function to simplify the code a bit more.
This commit is contained in:
@@ -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
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user