Skip to content

Commit eac9a6e

Browse files
CJCombrinkCarel Combrink
and
Carel Combrink
authored
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>
1 parent a72b172 commit eac9a6e

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

include/sqlpp11/postgresql/connection.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ namespace sqlpp
182182

183183
// ctor / dtor
184184
connection();
185-
connection(const std::shared_ptr<connection_config>& config);
185+
connection(const std::shared_ptr<const connection_config>& config);
186186
~connection();
187187
connection(const connection&) = delete;
188188
connection(connection&&);
189189
connection& operator=(const connection&) = delete;
190190
connection& operator=(connection&&);
191191

192192
// creates a connection handle and connects to database
193-
void connectUsing(const std::shared_ptr<connection_config>& config) noexcept(false);
193+
void connectUsing(const std::shared_ptr<const connection_config>& config) noexcept(false);
194194

195195
// Select stmt (returns a result)
196196
template <typename Select>
@@ -391,7 +391,7 @@ namespace sqlpp
391391
{
392392
}
393393

394-
inline connection::connection(const std::shared_ptr<connection_config>& config)
394+
inline connection::connection(const std::shared_ptr<const connection_config>& config)
395395
: _handle(new detail::connection_handle(config))
396396
{
397397
}
@@ -417,7 +417,7 @@ namespace sqlpp
417417
return *this;
418418
}
419419

420-
inline void connection::connectUsing(const std::shared_ptr<connection_config>& config) noexcept(false)
420+
inline void connection::connectUsing(const std::shared_ptr<const connection_config>& config) noexcept(false)
421421
{
422422
this->_handle.reset(new detail::connection_handle(config));
423423
}

include/sqlpp11/postgresql/detail/connection_handle.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ namespace sqlpp
6060

6161
struct DLL_LOCAL connection_handle
6262
{
63-
std::shared_ptr<connection_config> config;
63+
std::shared_ptr<const connection_config> config;
6464
std::unique_ptr<PGconn, void(*)(PGconn*)> postgres;
6565
std::set<std::string> prepared_statement_names;
6666

67-
connection_handle(const std::shared_ptr<connection_config>& config);
67+
connection_handle(const std::shared_ptr<const connection_config>& config);
6868
connection_handle(const connection_handle&) = delete;
6969
connection_handle(connection_handle&&) = default;
7070
connection_handle& operator=(const connection_handle&) = delete;
@@ -79,7 +79,7 @@ namespace sqlpp
7979
void deallocate_prepared_statement(const std::string& name);
8080
};
8181

82-
inline connection_handle::connection_handle(const std::shared_ptr<connection_config>& conf)
82+
inline connection_handle::connection_handle(const std::shared_ptr<const connection_config>& conf)
8383
: config(conf), postgres{nullptr, handle_cleanup}
8484
{
8585
#ifdef SQLPP_DYNAMIC_LOADING
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2017, Volker Aßmann
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22+
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23+
* OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#include <iostream>
27+
#include <memory>
28+
#include <stdexcept>
29+
30+
#include <sqlpp11/postgresql/connection.h>
31+
#include <sqlpp11/postgresql/exception.h>
32+
#include <sqlpp11/sqlpp11.h>
33+
34+
namespace sql = sqlpp::postgresql;
35+
int BasicConstConfig(int, char*[])
36+
{
37+
const std::shared_ptr<const sql::connection_config> const_config = []()
38+
{
39+
auto config = std::make_shared<sql::connection_config>();
40+
config->host = "localhost";
41+
config->user = "unknown_user_must_fail";
42+
return config;
43+
}();
44+
45+
try
46+
{
47+
sql::connection db(const_config);
48+
49+
throw std::logic_error("should never reach this point");
50+
}
51+
catch (const sqlpp::postgresql::broken_connection& ex)
52+
{
53+
std::cout << "Got exception: '" << ex.what() << "'";
54+
return 0;
55+
}
56+
57+
return 1;
58+
}

tests/postgresql/usage/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ target_include_directories(sqlpp11_postgresql_testing INTERFACE ${CMAKE_CURRENT_
2828

2929
set(test_files
3030
Basic.cpp
31+
BasicConstConfig.cpp
3132
Blob.cpp
3233
Constructor.cpp
3334
Date.cpp

0 commit comments

Comments
 (0)