Skip to content

Commit

Permalink
Simplify, remove experiment on arrays of pairs/tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 3, 2024
1 parent 2df02a1 commit 2225972
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 76 deletions.
37 changes: 11 additions & 26 deletions include/tao/pq/parameter_traits_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,26 @@ namespace tao::pq
namespace internal
{
template< typename T >
void to_array( std::string& data, const T& v );

template< typename T, std::size_t... Is >
void to_array_indexed( std::string& data, const T& v, std::index_sequence< Is... > /*unused*/ )
requires( !pq::is_array_parameter< T > ) && ( parameter_traits< T >::columns == 1 )
void to_array( std::string& data, const T& v )
{
data += '{';
( ( v.template element< Is >( data ), data += ',' ), ... );
*data.rbegin() = '}';
parameter_traits< T >( v ).template element< 0 >( data );
}

template< typename T >
requires pq::is_array_parameter< T >
void to_array( std::string& data, const T& v )
{
if constexpr( pq::is_array_parameter< T > ) {
data += '{';
if( v.empty() ) {
data += '}';
}
else {
for( const auto& e : v ) {
internal::to_array( data, e );
data += ',';
}
*data.rbegin() = '}';
}
data += '{';
if( v.empty() ) {
data += '}';
}
else {
const auto t = parameter_traits< T >( v );
if constexpr( t.columns == 1 ) {
t.template element< 0 >( data );
}
else {
static_assert( t.columns > 1 );
internal::to_array_indexed( data, t, std::make_index_sequence< t.columns >() );
for( const auto& e : v ) {
internal::to_array( data, e );
data += ',';
}
*data.rbegin() = '}';
}
}

Expand Down
13 changes: 2 additions & 11 deletions include/tao/pq/result_traits_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <utility>
#include <vector>

#include <tao/pq/internal/unreachable.hpp>
#include <tao/pq/result_traits.hpp>

namespace tao::pq
Expand Down Expand Up @@ -70,27 +69,19 @@ namespace tao::pq
}

template< typename T >
requires( !pq::is_array_result< T > ) && ( result_traits_size< T > >= 2 )
requires pq::is_array_result< T >
[[nodiscard]] auto parse( const char*& value ) -> T
{
if( *value++ != '{' ) {
throw std::invalid_argument( "expected '{'" );
}
throw std::runtime_error( "NOT YET IMPLEMENTED" );
}

template< typename T >
requires pq::is_array_result< T >
[[nodiscard]] auto parse( const char*& value ) -> T
{
T container;
if( *value++ != '{' ) {
throw std::invalid_argument( "expected '{'" );
}
if( *value == '}' ) {
++value;
return container;
}

while( true ) {
using value_type = typename T::value_type;
if constexpr( requires { container.push_back( parse< value_type >( value ) ); } ) {
Expand Down
39 changes: 0 additions & 39 deletions src/test/pq/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,6 @@ namespace
TEST_ASSERT( r == v );
}

{
connection->execute( "DROP TABLE IF EXISTS tao_array_test" );
connection->execute( "CREATE TABLE tao_array_test ( a TEXT[][] NOT NULL )" );

using type = std::vector< std::pair< std::string, std::string > >;
const type v = { { "1", "F\"O\\O" }, { "4", " XYZ " } };
connection->execute( "INSERT INTO tao_array_test VALUES ( $1 )", v );

// TODO: Add result_traits...
// const auto r = connection->execute( "SELECT * FROM tao_array_test" ).as< type >();
// TEST_ASSERT( r == v );
}

{
connection->execute( "DROP TABLE IF EXISTS tao_array_test" );
connection->execute( "CREATE TABLE tao_array_test ( a TEXT[][] NOT NULL )" );

using type = std::vector< std::tuple< int, std::string, std::optional< std::string > > >;
const type v = { { 1, "F\"O\\O", std::nullopt }, { 4, " XYZ ", "BAR" } };
connection->execute( "INSERT INTO tao_array_test VALUES ( $1 )", v );

// TODO: Add result_traits...
// const auto r = connection->execute( "SELECT * FROM tao_array_test" ).as< type >();
// TEST_ASSERT( r == v );
}

{
connection->execute( "DROP TABLE IF EXISTS tao_array_test" );
connection->execute( "CREATE TABLE tao_array_test ( a TEXT[] NOT NULL )" );

// note: a tuple with only one element is *not* treated as an array
using type = std::vector< std::tuple< std::string > >;
const type v = { { "F\"O\\O" }, { " XYZ " } };
connection->execute( "INSERT INTO tao_array_test VALUES ( $1 )", v );

const auto r = connection->execute( "SELECT * FROM tao_array_test" ).as< type >();
TEST_ASSERT( r == v );
}

{
connection->execute( "DROP TABLE IF EXISTS tao_array_test" );
connection->execute( "CREATE TABLE tao_array_test ( a BYTEA[][] NOT NULL )" );
Expand Down

0 comments on commit 2225972

Please sign in to comment.