Skip to content

Commit 34bdab6

Browse files
committed
Add support for single row mode and chunked mode
1 parent 9cafd37 commit 34bdab6

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

include/tao/pq/result.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace tao::pq
5454
public:
5555
[[nodiscard]] auto has_rows_affected() const noexcept -> bool;
5656
[[nodiscard]] auto rows_affected() const -> std::size_t;
57+
[[nodiscard]] auto is_final() const -> bool;
5758

5859
[[nodiscard]] auto columns() const noexcept -> std::size_t
5960
{

include/tao/pq/transaction.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ namespace tao::pq
141141
}
142142
}
143143

144+
void set_single_row_mode();
145+
#if defined( LIBPQ_HAS_CHUNK_MODE )
146+
void set_chunk_mode( const std::size_t rows );
147+
#endif
148+
144149
[[nodiscard]] auto get_result( const std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now() ) -> result;
145150

146151
template< typename... As >

src/lib/pq/result.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ namespace tao::pq
4545
switch( PQresultStatus( pgresult ) ) {
4646
case PGRES_COMMAND_OK:
4747
case PGRES_TUPLES_OK:
48+
case PGRES_SINGLE_TUPLE:
49+
#if defined( LIBPQ_HAS_CHUNK_MODE )
50+
case PGRES_TUPLES_CHUNK:
51+
#endif
4852
return;
4953

5054
case PGRES_EMPTY_QUERY:
@@ -74,6 +78,24 @@ namespace tao::pq
7478
return internal::from_chars< std::size_t >( str );
7579
}
7680

81+
auto result::is_final() const -> bool
82+
{
83+
switch( PQresultStatus( m_pgresult.get() ) ) {
84+
case PGRES_COMMAND_OK:
85+
case PGRES_TUPLES_OK:
86+
return true;
87+
88+
case PGRES_SINGLE_TUPLE:
89+
#if defined( LIBPQ_HAS_CHUNK_MODE )
90+
case PGRES_TUPLES_CHUNK:
91+
#endif
92+
return false;
93+
94+
default:
95+
TAO_PQ_UNREACHABLE; // LCOV_EXCL_LINE
96+
}
97+
}
98+
7799
auto result::name( const std::size_t column ) const -> std::string
78100
{
79101
if( column >= m_columns ) {

src/lib/pq/transaction.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ namespace tao::pq
141141
m_connection->send_params( statement, n_params, types, values, lengths, formats );
142142
}
143143

144+
void transaction::set_single_row_mode()
145+
{
146+
check_current_transaction();
147+
if( PQsetSingleRowMode( m_connection->underlying_raw_ptr() ) == 0 ) {
148+
throw std::runtime_error( "unable to switch to single row mode" );
149+
}
150+
}
151+
152+
#if defined( LIBPQ_HAS_CHUNK_MODE )
153+
void transaction::set_chunk_mode( const std::size_t rows )
154+
{
155+
check_current_transaction();
156+
if( PQsetChunkedRowsMode( m_connection->underlying_raw_ptr(), rows ) == 0 ) {
157+
throw std::runtime_error( "unable to switch to chunk mode" );
158+
}
159+
}
160+
#endif
161+
144162
auto transaction::get_result( const std::chrono::steady_clock::time_point start ) -> result
145163
{
146164
check_current_transaction();
@@ -159,6 +177,12 @@ namespace tao::pq
159177
m_connection->clear_results( end );
160178
throw std::runtime_error( "unexpected COPY TO statement" );
161179

180+
case PGRES_SINGLE_TUPLE:
181+
#if defined( LIBPQ_HAS_CHUNK_MODE )
182+
case PGRES_TUPLES_CHUNK:
183+
#endif
184+
return pq::result( result.release() );
185+
162186
default:;
163187
}
164188
while( auto next = m_connection->get_result( end ) ) {

0 commit comments

Comments
 (0)