Const connection config (#494)

- The postgres connection does not change the connection_config thus passing it as const.
- Implies 'thread safety' when using the same config for multiple connections

Co-authored-by: Carel Combrink <carel.combrink@vastech.co.za>
This commit is contained in:
Carel
2023-06-20 06:28:22 +02:00
committed by GitHub
parent a72b172a52
commit eac9a6e5e3
4 changed files with 66 additions and 7 deletions

View File

@@ -182,7 +182,7 @@ namespace sqlpp
// ctor / dtor
connection();
connection(const std::shared_ptr<connection_config>& config);
connection(const std::shared_ptr<const connection_config>& config);
~connection();
connection(const connection&) = delete;
connection(connection&&);
@@ -190,7 +190,7 @@ namespace sqlpp
connection& operator=(connection&&);
// creates a connection handle and connects to database
void connectUsing(const std::shared_ptr<connection_config>& config) noexcept(false);
void connectUsing(const std::shared_ptr<const connection_config>& config) noexcept(false);
// Select stmt (returns a result)
template <typename Select>
@@ -391,7 +391,7 @@ namespace sqlpp
{
}
inline connection::connection(const std::shared_ptr<connection_config>& config)
inline connection::connection(const std::shared_ptr<const connection_config>& config)
: _handle(new detail::connection_handle(config))
{
}
@@ -417,7 +417,7 @@ namespace sqlpp
return *this;
}
inline void connection::connectUsing(const std::shared_ptr<connection_config>& config) noexcept(false)
inline void connection::connectUsing(const std::shared_ptr<const connection_config>& config) noexcept(false)
{
this->_handle.reset(new detail::connection_handle(config));
}

View File

@@ -60,11 +60,11 @@ namespace sqlpp
struct DLL_LOCAL connection_handle
{
std::shared_ptr<connection_config> config;
std::shared_ptr<const connection_config> config;
std::unique_ptr<PGconn, void(*)(PGconn*)> postgres;
std::set<std::string> prepared_statement_names;
connection_handle(const std::shared_ptr<connection_config>& config);
connection_handle(const std::shared_ptr<const connection_config>& config);
connection_handle(const connection_handle&) = delete;
connection_handle(connection_handle&&) = default;
connection_handle& operator=(const connection_handle&) = delete;
@@ -79,7 +79,7 @@ namespace sqlpp
void deallocate_prepared_statement(const std::string& name);
};
inline connection_handle::connection_handle(const std::shared_ptr<connection_config>& conf)
inline connection_handle::connection_handle(const std::shared_ptr<const connection_config>& conf)
: config(conf), postgres{nullptr, handle_cleanup}
{
#ifdef SQLPP_DYNAMIC_LOADING