Skip to content

Commit

Permalink
Experimental log handler
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 25, 2024
1 parent 7f07d2a commit 80f7914
Show file tree
Hide file tree
Showing 4 changed files with 409 additions and 66 deletions.
65 changes: 57 additions & 8 deletions include/tao/pq/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <tao/pq/internal/poll.hpp>
#include <tao/pq/internal/zsv.hpp>
#include <tao/pq/isolation_level.hpp>
#include <tao/pq/log.hpp>
#include <tao/pq/notification.hpp>
#include <tao/pq/parameter.hpp>
#include <tao/pq/pipeline_status.hpp>
Expand Down Expand Up @@ -66,6 +67,7 @@ namespace tao::pq
std::function< poll::callback > m_poll;
std::function< void( const notification& ) > m_notification_handler;
std::map< std::string, std::function< void( const char* ) >, std::less<> > m_notification_handlers;
std::shared_ptr< log > m_log;

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

Expand Down Expand Up @@ -122,19 +124,57 @@ namespace tao::pq

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

[[nodiscard]] auto poll_callback() const noexcept -> const std::function< poll::callback >&;
void set_poll_callback( std::function< poll::callback > poll_cb ) noexcept;
void reset_poll_callback();
[[nodiscard]] auto poll_callback() const noexcept -> decltype( auto )
{
return m_poll;
}

void set_poll_callback( std::function< poll::callback > poll_cb ) noexcept
{
m_poll = std::move( poll_cb );
}

void reset_poll_callback()
{
m_poll = internal::poll;
}

[[nodiscard]] auto notification_handler() const noexcept -> decltype( auto )
{
return m_notification_handler;
}

void set_notification_handler( std::function< void( const notification& ) > handler ) noexcept
{
m_notification_handler = std::move( handler );
}

void reset_notification_handler() noexcept
{
m_notification_handler = nullptr;
}

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

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

void reset_notification_handler() noexcept;
void reset_notification_handler( const std::string_view channel ) noexcept;

[[nodiscard]] auto log_handler() const noexcept -> decltype( auto )
{
return m_log;
}

void set_log_handler( const std::shared_ptr< pq::log >& log ) noexcept
{
m_log = log;
}

void reset_log_handler() noexcept
{
m_log = nullptr;
}

[[nodiscard]] auto status() const noexcept -> connection_status;
[[nodiscard]] auto transaction_status() const noexcept -> pq::transaction_status;

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

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

[[nodiscard]] auto flush() -> bool;

[[nodiscard]] auto direct() -> std::shared_ptr< pq::transaction >;

[[nodiscard]] auto transaction() -> std::shared_ptr< pq::transaction >;
Expand Down Expand Up @@ -190,8 +232,15 @@ namespace tao::pq
return m_timeout;
}

void set_timeout( const std::chrono::milliseconds timeout );
void reset_timeout() noexcept;
void set_timeout( const std::chrono::milliseconds timeout ) noexcept
{
m_timeout = timeout;
}

void reset_timeout() noexcept
{
m_timeout = std::nullopt;
}

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

Expand Down
100 changes: 100 additions & 0 deletions include/tao/pq/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2024 Daniel Frey and Dr. Colin Hirsch
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)

#ifndef TAO_PQ_LOG_HPP
#define TAO_PQ_LOG_HPP

#include <chrono>
#include <functional>

#include <libpq-fe.h>

#include <tao/pq/poll.hpp>

namespace tao::pq
{
class connection;

struct log
{
struct connection_pool_t
{
// TODO...

} connection_pool;

struct connection_t
{
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[] ) >;
using send_query_result_t = std::function< void( connection&, int result ) >;

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[] ) >;
using send_query_prepared_result_t = std::function< void( connection&, int result ) >;

using wait_t = std::function< void( connection&, bool wait_for_write, std::chrono::steady_clock::time_point end ) >;

using poll_t = std::function< void( connection&, int socket, bool wait_for_write, int timeout_ms ) >;
using poll_result_t = std::function< void( connection&, int socket, poll::status status ) >;

using is_busy_result_t = std::function< void( const connection&, int result ) >; // noexcept

using consume_input_t = std::function< void( connection& ) >;
using consume_input_result_t = std::function< void( connection&, int result ) >;

using flush_t = std::function< void( connection& ) >;
using flush_result_t = std::function< void( connection&, int result ) >;

using get_result_t = std::function< void( connection&, std::chrono::steady_clock::time_point end ) >;

struct : send_query_t
{
send_query_result_t result;
using send_query_t::operator=;
} send_query;

struct : send_query_prepared_t
{
send_query_prepared_result_t result;
using send_query_prepared_t::operator=;
} send_query_prepared;

wait_t wait;

struct : poll_t
{
poll_result_t result;
using poll_t::operator=;
} poll;

struct
{
is_busy_result_t result;
} is_busy;

struct : consume_input_t
{
consume_input_result_t result;
using consume_input_t::operator=;
} consume_input;

struct : flush_t
{
flush_result_t result;
using flush_t::operator=;
} flush;

get_result_t get_result;

} connection;

struct transaction_t
{
// TODO...

} transaction;
};

} // namespace tao::pq

#endif
Loading

0 comments on commit 80f7914

Please sign in to comment.