Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 27, 2024
1 parent 5f263c3 commit 7010467
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
4 changes: 2 additions & 2 deletions include/tao/pq/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace tao::pq
};

public:
connection( const private_key /*unused*/, const std::string& connection_info, std::function< poll::callback > poll_cb );
connection( const private_key /*unused*/, const std::string& connection_info );

connection( const connection& ) = delete;
connection( connection&& ) = delete;
Expand All @@ -120,7 +120,7 @@ namespace tao::pq

~connection() = default;

[[nodiscard]] static auto create( const std::string& connection_info, std::function< poll::callback > poll_cb = internal::poll ) -> std::shared_ptr< connection >;
[[nodiscard]] static auto create( const std::string& connection_info ) -> std::shared_ptr< connection >;

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

Expand Down
36 changes: 28 additions & 8 deletions include/tao/pq/connection_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,45 @@ namespace tao::pq
};

public:
connection_pool( const private_key /*unused*/, const std::string_view connection_info, std::function< poll::callback > poll_cb );
connection_pool( const private_key /*unused*/, const std::string_view connection_info );

void get() const = delete;

template< typename T = connection_pool >
[[nodiscard]] static auto create( const std::string_view connection_info, std::function< poll::callback > poll_cb = internal::poll ) -> std::shared_ptr< T >
[[nodiscard]] static auto create( const std::string_view connection_info ) -> std::shared_ptr< T >
{
return std::make_shared< T >( private_key(), connection_info, std::move( poll_cb ) );
return std::make_shared< T >( private_key(), connection_info );
}

[[nodiscard]] auto timeout() const noexcept -> decltype( auto )
{
return m_timeout;
}

void set_timeout( const std::chrono::milliseconds timeout ) noexcept;
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 poll_callback() const noexcept -> decltype( auto )
{
return m_poll;
}

[[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();
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 connection() -> std::shared_ptr< connection >;

Expand Down
8 changes: 4 additions & 4 deletions src/lib/pq/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,10 @@ namespace tao::pq
}
}

connection::connection( const private_key /*unused*/, const std::string& connection_info, std::function< poll::callback > poll_cb )
connection::connection( const private_key /*unused*/, const std::string& connection_info )
: m_pgconn( PQconnectdb( connection_info.c_str() ), &PQfinish ),
m_current_transaction( nullptr ),
m_poll( std::move( poll_cb ) )
m_poll( internal::poll )
{
if( !is_open() ) {
// note that we can not access the sqlstate after PQconnectdb(),
Expand All @@ -452,9 +452,9 @@ namespace tao::pq
}
}

auto connection::create( const std::string& connection_info, std::function< poll::callback > poll_cb ) -> std::shared_ptr< connection >
auto connection::create( const std::string& connection_info ) -> std::shared_ptr< connection >
{
return std::make_shared< connection >( private_key(), connection_info, std::move( poll_cb ) );
return std::make_shared< connection >( private_key(), connection_info );
}

auto connection::error_message() const -> const char*
Expand Down
38 changes: 4 additions & 34 deletions src/lib/pq/connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,27 @@

#include <tao/pq/connection_pool.hpp>

#include <chrono>
#include <functional>
#include <memory>
#include <optional>
#include <string_view>
#include <utility>

#include <tao/pq/connection.hpp>
#include <tao/pq/internal/poll.hpp>
#include <tao/pq/poll.hpp>

namespace tao::pq
{
auto connection_pool::v_create() const -> std::unique_ptr< pq::connection >
{
return std::make_unique< pq::connection >( pq::connection::private_key(), m_connection_info, m_poll );
return std::make_unique< pq::connection >( pq::connection::private_key(), m_connection_info );
}

connection_pool::connection_pool( const private_key /*unused*/, const std::string_view connection_info, std::function< poll::callback > poll_cb )
connection_pool::connection_pool( const private_key /*unused*/, const std::string_view connection_info )
: m_connection_info( connection_info ),
m_poll( std::move( poll_cb ) )
m_poll( internal::poll )
{}

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

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

auto connection_pool::poll_callback() const noexcept -> const std::function< poll::callback >&
{
return m_poll;
}

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

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

auto connection_pool::connection() -> std::shared_ptr< pq::connection >
{
auto result = get();
auto result = internal::pool< pq::connection >::get();
if( m_timeout ) {
result->set_timeout( *m_timeout );
}
Expand Down

0 comments on commit 7010467

Please sign in to comment.