diff --git a/include/tao/pq/result.hpp b/include/tao/pq/result.hpp index 0485609..7adec8b 100644 --- a/include/tao/pq/result.hpp +++ b/include/tao/pq/result.hpp @@ -5,6 +5,7 @@ #ifndef TAO_PQ_RESULT_HPP #define TAO_PQ_RESULT_HPP +#include #include #include #include @@ -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 ); @@ -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; } @@ -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(); } @@ -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() ); diff --git a/src/lib/pq/result.cpp b/src/lib/pq/result.cpp index 1a92a96..91fcb26 100644 --- a/src/lib/pq/result.cpp +++ b/src/lib/pq/result.cpp @@ -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 ) ); @@ -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 ) ); } diff --git a/src/test/pq/connection.cpp b/src/test/pq/connection.cpp index 2fe3b1a..3ab08b3 100644 --- a/src/test/pq/connection.cpp +++ b/src/test/pq/connection.cpp @@ -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" ); @@ -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 ) );