OZO 「お象」
Boost.Asio and libpq based asynchronous PostgreSQL unofficial header-only C++17 client library.
|
|
3 #include <ozo/connection_info.h>
4 #include <ozo/transaction_status.h>
6 #include <ozo/connector.h>
7 #include <ozo/core/thread_safety.h>
8 #include <ozo/detail/connection_pool.h>
45 template <
typename Rep>
53 template <
typename O
idMap,
typename Statistics = none_t>
54 class connection_rep {
56 using oid_map_type =
OidMap;
57 using native_handle_type =
typename ozo::pg::conn::pointer;
58 using statistics_type = Statistics;
59 using error_context_type = std::string;
61 const ozo::pg::conn& safe_native_handle()
const & {
return safe_handle_;}
62 ozo::pg::conn& safe_native_handle() & {
return safe_handle_;}
64 const oid_map_type& oid_map() const & {
return oid_map_;}
65 oid_map_type& oid_map() & {
return oid_map_;}
67 const auto& statistics() const & {
return statistics_;}
69 template <
typename Key,
typename Value>
70 void update_statistics(
const Key&, Value&&) noexcept {
71 static_assert(std::is_void_v<Key>,
"update_statistics is not supperted");
75 return error_context_;
77 void set_error_context(error_context_type v) {
78 error_context_ = std::move(v);
82 ozo::pg::conn&& safe_handle,
84 error_context_type error_context = {},
85 Statistics statistics = Statistics{})
86 : safe_handle_(std::move(safe_handle)),
87 oid_map_(std::move(oid_map)),
88 error_context_(std::move(error_context)),
89 statistics_(std::move(statistics)) {}
91 ozo::pg::conn safe_handle_;
92 oid_map_type oid_map_;
93 error_context_type error_context_;
94 statistics_type statistics_;
115 template <
typename Rep,
typename Executor = asio::io_context::executor_type>
144 template <
typename Key,
typename Value>
145 void update_statistics(
const Key& key, Value&& v) noexcept {
146 ozo::unwrap(rep_).update_statistics(key, std::forward<Value(v)>);
186 template <
typename WaitHandler>
199 template <
typename WaitHandler>
230 bool is_bad() const noexcept;
240 operator
bool () const noexcept {
return !
is_bad();}
252 using stream_type =
typename detail::connection_stream<executor_type>::type;
259 template <
typename ...Ts>
260 struct is_connection<pooled_connection<Ts...>> : std::true_type {};
262 template <
typename T>
263 struct connection_traits<yamail::resource_pool::handle<T>> :
264 connection_traits<typename yamail::resource_pool::handle<T>::value_type> {};
301 template <
typename Source,
typename ThreadSafety = std::decay_t<decltype(thread_safe)>>
306 using connection_rep_type = ozo::connection_rep<typename ozo::unwrap_type<ozo::connection_type<Source>>::oid_map_type>;
308 using impl_type = detail::get_connection_pool_impl_t<connection_rep_type, ThreadSafety>;
319 source_(std::move(source)) {}
324 using connection_type = std::shared_ptr<pooled_connection<yamail::resource_pool::handle<connection_rep_type>>>;
336 template <
typename TimeConstra
int,
typename Handler>
340 return impl_.stats();
343 auto operator [](io_context& io) {
357 auto queue_timeout(none_t)
const {
366 template <
typename ...Ts>
367 [[deprecated]]
auto make_connector(connection_pool<Ts...>& source, io_context& io,
const connection_pool_timeouts& timeouts) {
368 return bind_get_connection_timeout(source[io], timeouts.connect);
372 template <
typename ...Ts>
373 [[deprecated]]
auto make_connector(connection_pool<Ts...>& source, io_context& io) {
377 static_assert(
ConnectionProvider<decltype(std::declval<connection_pool<connection_info<>>>()[std::declval<io_context&>()])>,
"is not a ConnectionProvider");
379 template <
typename T>
380 struct is_connection_pool : std::false_type {};
382 template <
typename ...Args>
383 struct is_connection_pool<connection_pool<Args...>> : std::true_type {};
385 template <
typename T>
386 constexpr
auto ConnectionPool = is_connection_pool<std::decay_t<T>>::value;
402 template <
typename ConnectionSource,
typename ThreadSafety = decltype(thread_safe)>
405 static_assert(ozo::ConnectionSource<ConnectionSource>,
"source should model ConnectionSource concept");
407 std::forward<ConnectionSource>(source), config,
thread_safety};
412 #include <ozo/impl/connection_pool.h>
Connection pool implementation.
Definition: connection_pool.h:302
error_code close() noexcept
Definition: connection_pool.h:156
constexpr decltype(auto) unwrap(T &&v) noexcept(noexcept(unwrap_impl< std::decay_t< T >>::apply(std::forward< T >(v))))
Unwraps argument underlying value or forwards the argument.
Definition: unwrap.h:57
Default model for the ConnectionProvider concept.
Definition: connector.h:66
boost::system::error_code error_code
Error code representation of the library.
Definition: error.h:38
Executor executor_type
The type of the executor associated with the object.
Definition: connection_pool.h:123
typename Rep::oid_map_type oid_map_type
Oid map of types that are used with the connection.
Definition: connection_pool.h:48
time_traits::duration idle_timeout
time interval to close connection after last usage
Definition: connection_pool.h:23
void async_wait_write(WaitHandler &&handler)
Definition: connection_pool.h:145
native_handle_type native_handle() const noexcept
Definition: connection_pool.h:136
void cancel() noexcept
Definition: connection_pool.h:163
time_traits::duration queue
[[IGNORED]]
Definition: connection_pool.h:36
void operator()(io_context &io, TimeConstraint t, Handler &&handler)
Definition: connection_pool.h:112
executor_type get_executor() const noexcept
Definition: connection_pool.h:174
bool is_open() const noexcept
Definition: connection_pool.h:248
std::size_t capacity
maximum number of stored connections
Definition: connection_pool.h:21
constexpr auto OidMap
Map of C++ types to corresponding PostgreSQL types OIDs.
Definition: type_traits.h:534
Pool bound model for Connection concept.
Definition: connection_pool.h:116
Connection provider concept
connection_pool(Source source, const connection_pool_config &config, const ThreadSafety &=ThreadSafety{})
Definition: connection_pool.h:317
std::chrono::steady_clock::time_point time_point
Time point type of the library.
Definition: time_traits.h:14
typename Rep::native_handle_type native_handle_type
Native connection handle type.
Definition: connection_pool.h:47
Connection pool configuration.
Definition: connection_pool.h:20
Connection traits depend on a representation type.
Definition: connection_pool.h:46
void async_wait_read(WaitHandler &&handler)
Definition: connection_pool.h:151
typename Rep::statistics_type statistics_type
Connection statistics to be collected.
Definition: connection_pool.h:50
const oid_map_type & oid_map() const noexcept
Definition: connection_pool.h:142
typename Rep::error_context_type error_context_type
Additional error context which could provide context depended information for errors.
Definition: connection_pool.h:49
Connection source concept
std::shared_ptr< pooled_connection< yamail::resource_pool::handle< connection_rep_type > >> connection_type
Definition: connection_pool.h:324
defines admissibility to use in multithreaded environment without additional synchronization
Definition: thread_safety.h:17
Rep rep_type
Connection representation type.
Definition: connection_pool.h:118
const error_context_type & get_error_context() const noexcept
Definition: connection_pool.h:155
time_traits::duration lifespan
time interval to keep connection open
Definition: connection_pool.h:24
typename connection_traits< rep_type >::statistics_type statistics_type
Connection statistics to be collected.
Definition: connection_pool.h:122
std::chrono::steady_clock::duration duration
Time duration type of the library.
Definition: time_traits.h:13
void set_error_context(error_context_type v={})
Definition: connection_pool.h:165
constexpr time_traits::duration time_left(time_traits::time_point t, time_traits::time_point now) noexcept
Time left to deadline.
Definition: deadline.h:77
auto make_connection_pool(ConnectionSource &&source, const connection_pool_config &config, const ThreadSafety &thread_safety=ThreadSafety{})
Connection pool construct helper function.
Definition: connection_pool.h:403
bool is_bad() const noexcept
Definition: connection_pool.h:169
std::size_t queue_capacity
maximum number of queued requests to get available connection
Definition: connection_pool.h:22
typename connection_traits< rep_type >::native_handle_type native_handle_type
Native connection handle type.
Definition: connection_pool.h:119
[[DEPRECATED]] Timeouts for the ozo::get_connection() operation
Definition: connection_pool.h:34
time_traits::duration connect
maximum time interval to establish to or wait for free connection with DBMS
Definition: connection_pool.h:35
typename connection_traits< rep_type >::oid_map_type oid_map_type
Oid map of types that are used with the connection.
Definition: connection_pool.h:120
typename connection_traits< rep_type >::error_context_type error_context_type
Additional error context which could provide context depended information for errors.
Definition: connection_pool.h:121
const auto & get_error_context(const Connection &conn)
Get additional error context.
Definition: connection.h:124