Skip to content

Commit

Permalink
Allow mixed parameters for execute(), send(), ...
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Dec 19, 2023
1 parent 2861aae commit f0cdf44
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
32 changes: 32 additions & 0 deletions include/tao/pq/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ namespace tao::pq
int m_lengths[ Max ];
int m_formats[ Max ];

template< std::size_t >
friend class parameter;

friend class transaction;

template< typename T, std::size_t... Is >
Expand Down Expand Up @@ -132,9 +135,16 @@ namespace tao::pq

~parameter()
{
#if defined( __GNUC__ ) && !defined( __clang__ )
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
for( std::size_t i = 0; i != m_pos; ++i ) {
delete m_params[ i ];
}
#if defined( __GNUC__ ) && !defined( __clang__ )
#pragma GCC diagnostic pop
#endif
}

explicit parameter( const parameter& p )
Expand Down Expand Up @@ -165,6 +175,28 @@ namespace tao::pq
}
};

namespace internal
{
template< typename A >
inline constexpr bool contains_parameter_impl = false;

template< std::size_t Max >
inline constexpr bool contains_parameter_impl< parameter< Max > > = true;

template< typename... As >
inline constexpr bool contains_parameter = ( contains_parameter_impl< std::decay_t< As > > || ... );

template< typename A >
inline constexpr std::size_t parameter_size_impl = parameter_traits< A >::columns;

template< std::size_t Max >
inline constexpr std::size_t parameter_size_impl< parameter< Max > > = Max;

template< typename... As >
inline constexpr std::size_t parameter_size = ( parameter_size_impl< std::decay_t< As > > + ... + 0 );

} // namespace internal

} // namespace tao::pq

#endif
27 changes: 9 additions & 18 deletions include/tao/pq/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,20 @@ namespace tao::pq
if constexpr( sizeof...( As ) == 0 ) {
send_params( statement, 0, nullptr, nullptr, nullptr, nullptr );
}
else if constexpr( internal::contains_parameter< As... > ) {
if constexpr( sizeof...( As ) == 1 ) {
( send_params( statement, as.m_size, as.m_types, as.m_values, as.m_lengths, as.m_formats ), ... );
}
else {
const parameter< internal::parameter_size< As... > > p( std::forward< As >( as )... );
send_params( statement, p.m_size, p.m_types, p.m_values, p.m_lengths, p.m_formats );
}
}
else {
send_traits( statement, parameter_traits< std::decay_t< As > >( std::forward< As >( as ) )... );
}
}

template< std::size_t Max >
void send( const internal::zsv statement, const parameter< Max >& as )
{
send_params( statement, as.m_size, as.m_types, as.m_values, as.m_lengths, as.m_formats );
}

template< std::size_t Max >
void send( const internal::zsv statement, parameter< Max >& as )
{
send( statement, const_cast< const parameter< Max >& >( as ) );
}

template< std::size_t Max >
void send( const internal::zsv statement, parameter< Max >&& as )
{
send( statement, const_cast< const parameter< Max >& >( as ) );
}

[[nodiscard]] auto get_result( const std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now() ) -> result;

template< typename... As >
Expand Down
7 changes: 7 additions & 0 deletions src/test/pq/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ void run()
tr->execute( "insert_user", p2 );
}

{
tao::pq::parameter< 1 > p;
std::string s = "Alice";
p.bind( std::move( s ) );
tr->execute( "insert_user", p, 44 );
}

// commit transaction
tr->commit();
}
Expand Down

0 comments on commit f0cdf44

Please sign in to comment.