diff --git a/src/backends/sqlite3/session.cpp b/src/backends/sqlite3/session.cpp index 90ce6537..099a9158 100644 --- a/src/backends/sqlite3/session.cpp +++ b/src/backends/sqlite3/session.cpp @@ -11,10 +11,11 @@ #include "soci-cstrtoi.h" -#include -#include #include -#include + +#include + +#include #include 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; - // 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 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 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) ); }