Skip to content

Commit 80f7914

Browse files
committed
Experimental log handler
1 parent 7f07d2a commit 80f7914

File tree

4 files changed

+409
-66
lines changed

4 files changed

+409
-66
lines changed

include/tao/pq/connection.hpp

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <tao/pq/internal/poll.hpp>
2424
#include <tao/pq/internal/zsv.hpp>
2525
#include <tao/pq/isolation_level.hpp>
26+
#include <tao/pq/log.hpp>
2627
#include <tao/pq/notification.hpp>
2728
#include <tao/pq/parameter.hpp>
2829
#include <tao/pq/pipeline_status.hpp>
@@ -66,6 +67,7 @@ namespace tao::pq
6667
std::function< poll::callback > m_poll;
6768
std::function< void( const notification& ) > m_notification_handler;
6869
std::map< std::string, std::function< void( const char* ) >, std::less<> > m_notification_handlers;
70+
std::shared_ptr< log > m_log;
6971

7072
[[nodiscard]] auto escape_identifier( const std::string_view identifier ) const -> std::unique_ptr< char, decltype( &PQfreemem ) >;
7173

@@ -122,19 +124,57 @@ namespace tao::pq
122124

123125
[[nodiscard]] auto error_message() const -> const char*;
124126

125-
[[nodiscard]] auto poll_callback() const noexcept -> const std::function< poll::callback >&;
126-
void set_poll_callback( std::function< poll::callback > poll_cb ) noexcept;
127-
void reset_poll_callback();
127+
[[nodiscard]] auto poll_callback() const noexcept -> decltype( auto )
128+
{
129+
return m_poll;
130+
}
131+
132+
void set_poll_callback( std::function< poll::callback > poll_cb ) noexcept
133+
{
134+
m_poll = std::move( poll_cb );
135+
}
136+
137+
void reset_poll_callback()
138+
{
139+
m_poll = internal::poll;
140+
}
141+
142+
[[nodiscard]] auto notification_handler() const noexcept -> decltype( auto )
143+
{
144+
return m_notification_handler;
145+
}
146+
147+
void set_notification_handler( std::function< void( const notification& ) > handler ) noexcept
148+
{
149+
m_notification_handler = std::move( handler );
150+
}
151+
152+
void reset_notification_handler() noexcept
153+
{
154+
m_notification_handler = nullptr;
155+
}
128156

129-
[[nodiscard]] auto notification_handler() const -> std::function< void( const notification& ) >;
130157
[[nodiscard]] auto notification_handler( const std::string_view channel ) const -> std::function< void( const char* payload ) >;
131158

132-
void set_notification_handler( const std::function< void( const notification& ) >& handler );
133159
void set_notification_handler( const std::string_view channel, const std::function< void( const char* payload ) >& handler );
134160

135-
void reset_notification_handler() noexcept;
136161
void reset_notification_handler( const std::string_view channel ) noexcept;
137162

163+
[[nodiscard]] auto log_handler() const noexcept -> decltype( auto )
164+
{
165+
return m_log;
166+
}
167+
168+
void set_log_handler( const std::shared_ptr< pq::log >& log ) noexcept
169+
{
170+
m_log = log;
171+
}
172+
173+
void reset_log_handler() noexcept
174+
{
175+
m_log = nullptr;
176+
}
177+
138178
[[nodiscard]] auto status() const noexcept -> connection_status;
139179
[[nodiscard]] auto transaction_status() const noexcept -> pq::transaction_status;
140180

@@ -156,6 +196,8 @@ namespace tao::pq
156196

157197
[[nodiscard]] auto is_busy() const noexcept -> bool;
158198

199+
[[nodiscard]] auto flush() -> bool;
200+
159201
[[nodiscard]] auto direct() -> std::shared_ptr< pq::transaction >;
160202

161203
[[nodiscard]] auto transaction() -> std::shared_ptr< pq::transaction >;
@@ -190,8 +232,15 @@ namespace tao::pq
190232
return m_timeout;
191233
}
192234

193-
void set_timeout( const std::chrono::milliseconds timeout );
194-
void reset_timeout() noexcept;
235+
void set_timeout( const std::chrono::milliseconds timeout ) noexcept
236+
{
237+
m_timeout = timeout;
238+
}
239+
240+
void reset_timeout() noexcept
241+
{
242+
m_timeout = std::nullopt;
243+
}
195244

196245
[[nodiscard]] auto password( const internal::zsv passwd, const internal::zsv user, const internal::zsv algorithm = "scram-sha-256" ) -> std::string;
197246

include/tao/pq/log.hpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) 2024 Daniel Frey and Dr. Colin Hirsch
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4+
5+
#ifndef TAO_PQ_LOG_HPP
6+
#define TAO_PQ_LOG_HPP
7+
8+
#include <chrono>
9+
#include <functional>
10+
11+
#include <libpq-fe.h>
12+
13+
#include <tao/pq/poll.hpp>
14+
15+
namespace tao::pq
16+
{
17+
class connection;
18+
19+
struct log
20+
{
21+
struct connection_pool_t
22+
{
23+
// TODO...
24+
25+
} connection_pool;
26+
27+
struct connection_t
28+
{
29+
using send_query_t = std::function< void( connection&, const char* statement, int n_params, const Oid types[], const char* const values[], const int lengths[], const int formats[] ) >;
30+
using send_query_result_t = std::function< void( connection&, int result ) >;
31+
32+
using send_query_prepared_t = std::function< void( connection&, const char* statement, int n_params, const char* const values[], const int lengths[], const int formats[] ) >;
33+
using send_query_prepared_result_t = std::function< void( connection&, int result ) >;
34+
35+
using wait_t = std::function< void( connection&, bool wait_for_write, std::chrono::steady_clock::time_point end ) >;
36+
37+
using poll_t = std::function< void( connection&, int socket, bool wait_for_write, int timeout_ms ) >;
38+
using poll_result_t = std::function< void( connection&, int socket, poll::status status ) >;
39+
40+
using is_busy_result_t = std::function< void( const connection&, int result ) >; // noexcept
41+
42+
using consume_input_t = std::function< void( connection& ) >;
43+
using consume_input_result_t = std::function< void( connection&, int result ) >;
44+
45+
using flush_t = std::function< void( connection& ) >;
46+
using flush_result_t = std::function< void( connection&, int result ) >;
47+
48+
using get_result_t = std::function< void( connection&, std::chrono::steady_clock::time_point end ) >;
49+
50+
struct : send_query_t
51+
{
52+
send_query_result_t result;
53+
using send_query_t::operator=;
54+
} send_query;
55+
56+
struct : send_query_prepared_t
57+
{
58+
send_query_prepared_result_t result;
59+
using send_query_prepared_t::operator=;
60+
} send_query_prepared;
61+
62+
wait_t wait;
63+
64+
struct : poll_t
65+
{
66+
poll_result_t result;
67+
using poll_t::operator=;
68+
} poll;
69+
70+
struct
71+
{
72+
is_busy_result_t result;
73+
} is_busy;
74+
75+
struct : consume_input_t
76+
{
77+
consume_input_result_t result;
78+
using consume_input_t::operator=;
79+
} consume_input;
80+
81+
struct : flush_t
82+
{
83+
flush_result_t result;
84+
using flush_t::operator=;
85+
} flush;
86+
87+
get_result_t get_result;
88+
89+
} connection;
90+
91+
struct transaction_t
92+
{
93+
// TODO...
94+
95+
} transaction;
96+
};
97+
98+
} // namespace tao::pq
99+
100+
#endif

0 commit comments

Comments
 (0)