Skip to content

Commit

Permalink
Fix associative containers for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 3, 2024
1 parent c2458df commit 88be1c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
23 changes: 16 additions & 7 deletions include/tao/pq/result_traits_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@ namespace tao::pq
}

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

template< typename T >
[[nodiscard]] auto parse_element( const char*& value ) -> T
{
if( *value == '"' ) {
++value;
std::string input;
while( const auto* pos = std::strpbrk( value, "\\\"" ) ) {
Expand Down Expand Up @@ -117,7 +121,12 @@ namespace tao::pq
}
while( true ) {
using value_type = std::decay_t< decltype( container ) >::value_type;
container.push_back( internal::parse_element< value_type >( value ) );
if constexpr( requires { container.push_back( internal::parse_element< value_type >( value ) ); } ) {
container.push_back( internal::parse_element< value_type >( value ) );
}
else {
container.insert( internal::parse_element< value_type >( value ) );
}
switch( *value++ ) {
case ',':
case ';':
Expand Down
3 changes: 2 additions & 1 deletion src/test/pq/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <exception>
#include <iostream>
#include <optional>
#include <set>
#include <string>
#include <vector>

Expand Down Expand Up @@ -69,7 +70,7 @@ namespace
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::vector< std::string > >;
using type = std::vector< std::set< std::string > >;
const type v = { { "1", "F\"O\\O", "NULL" }, { "4", " XYZ ", "6" } };
connection->execute( "INSERT INTO tao_array_test VALUES ( $1 )", v );

Expand Down

0 comments on commit 88be1c2

Please sign in to comment.