Skip to content

Commit

Permalink
More noexcept
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 10, 2024
1 parent 025529f commit a3f3994
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
24 changes: 9 additions & 15 deletions include/tao/pq/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef TAO_PQ_RESULT_HPP
#define TAO_PQ_RESULT_HPP

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <format>
Expand Down Expand Up @@ -46,13 +47,6 @@ namespace tao::pq
const std::size_t m_columns;
const std::size_t m_rows;

void check_has_result_set() const
{
if( m_columns == 0 ) {
throw std::logic_error( "statement does not yield a result set" );
}
}

void check_row( const std::size_t row ) const;

explicit result( PGresult* pgresult );
Expand All @@ -69,13 +63,13 @@ namespace tao::pq
[[nodiscard]] auto name( const std::size_t column ) const -> std::string;
[[nodiscard]] auto index( const internal::zsv in_name ) const -> std::size_t;

[[nodiscard]] auto size() const -> std::size_t
[[nodiscard]] auto size() const noexcept -> std::size_t
{
check_has_result_set();
assert( m_columns != 0 );
return m_rows;
}

[[nodiscard]] auto empty() const -> bool
[[nodiscard]] auto empty() const noexcept -> bool
{
return size() == 0;
}
Expand Down Expand Up @@ -196,15 +190,15 @@ namespace tao::pq
};

public:
[[nodiscard]] auto begin() const -> const_iterator;
[[nodiscard]] auto end() const -> const_iterator;
[[nodiscard]] auto begin() const noexcept -> const_iterator;
[[nodiscard]] auto end() const noexcept -> const_iterator;

[[nodiscard]] auto cbegin() const
[[nodiscard]] auto cbegin() const noexcept
{
return begin();
}

[[nodiscard]] auto cend() const
[[nodiscard]] auto cend() const noexcept
{
return end();
}
Expand Down Expand Up @@ -265,7 +259,7 @@ namespace tao::pq
requires result_type< typename T::value_type >
[[nodiscard]] auto as_container() const -> T
{
check_has_result_set();
assert( m_columns != 0 );
T nrv;
if constexpr( requires { nrv.reserve( size() ); } ) {
nrv.reserve( size() );
Expand Down
10 changes: 5 additions & 5 deletions src/lib/pq/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace tao::pq
{
void result::check_row( const std::size_t row ) const
{
check_has_result_set();
assert( m_columns != 0 );
if( !( row < m_rows ) ) {
if( m_rows == 0 ) {
throw std::out_of_range( std::format( "row {} out of range, result is empty", row ) );
Expand Down Expand Up @@ -81,22 +81,22 @@ namespace tao::pq

auto result::index( const internal::zsv in_name ) const -> std::size_t
{
assert( m_columns != 0 );
const int column = PQfnumber( m_pgresult.get(), in_name );
if( column < 0 ) {
assert( column == -1 );
check_has_result_set();
throw std::out_of_range( std::format( "column '{}' not found", in_name.value ) );
}
return column;
}

auto result::begin() const -> result::const_iterator
auto result::begin() const noexcept -> result::const_iterator
{
check_has_result_set();
assert( m_columns != 0 );
return const_iterator( row( *this, 0, 0, m_columns ) );
}

auto result::end() const -> result::const_iterator
auto result::end() const noexcept -> result::const_iterator
{
return const_iterator( row( *this, size(), 0, m_columns ) );
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/pq/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace
TEST_THROWS( connection->execute( "drop_table", 42 ) );

// a statement which is not a query does not return "affected rows"
TEST_THROWS( connection->execute( "drop_table" ).rows_affected() );
TEST_ASSERT( connection->execute( "drop_table" ).columns() == 0 );

// deallocate a prepared statement
connection->deallocate( "drop_table" );
Expand Down Expand Up @@ -115,7 +115,7 @@ namespace
connection->execute( "CREATE TABLE tao_connection_test ( a INTEGER PRIMARY KEY, b INTEGER )" );

// a DELETE statement does not yield a result set
TEST_THROWS( connection->execute( "DELETE FROM tao_connection_test" ).empty() );
TEST_ASSERT( connection->execute( "DELETE FROM tao_connection_test" ).columns() == 0 );

// out of range access throws
TEST_THROWS( connection->execute( "SELECT * FROM tao_connection_test" ).at( 0 ) );
Expand Down

0 comments on commit a3f3994

Please sign in to comment.