-
Notifications
You must be signed in to change notification settings - Fork 41
/
traits.cpp
103 lines (88 loc) · 3.7 KB
/
traits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2020 Daniel Frey and Dr. Colin Hirsch
// Please see LICENSE for license or visit https://github.com/taocpp/taopq/
#include "../getenv.hpp"
#include "../macros.hpp"
#include <tao/pq/connection.hpp>
#include <tao/pq/result_traits_tuple.hpp>
namespace example
{
class user
{
private:
int a, b, c, d;
public:
explicit user( int i )
: a( i ), b( i + 1 ), c( i + 2 ), d( i + 3 )
{}
[[nodiscard]] auto to_taopq_param() const noexcept
{
return std::tie( a, b, c, d );
}
};
struct user2
{
int a, b, c, d;
explicit user2( int i )
: a( i ), b( i + 1 ), c( i + 2 ), d( i + 3 )
{}
};
[[nodiscard]] auto to_taopq_param( const user2& v ) noexcept
{
return std::tie( v.a, v.b, v.c, v.d );
}
} // namespace example
void run()
{
const auto connection = tao::pq::connection::create( tao::pq::internal::getenv( "TAOPQ_TEST_DATABASE", "dbname=template1" ) );
connection->execute( "DROP TABLE IF EXISTS tao_parameter_test" );
connection->execute( "CREATE TABLE tao_parameter_test ( a INTEGER PRIMARY KEY, b INTEGER, c INTEGER, d INTEGER )" );
TEST_EXECUTE( connection->execute( "INSERT INTO tao_parameter_test VALUES ( 1, 2, 3, 4 )" ) );
TEST_EXECUTE( connection->execute( "INSERT INTO tao_parameter_test VALUES ( $1, $2, $3, $4 )", 2, 3, 4, 5 ) );
TEST_EXECUTE( connection->execute( "INSERT INTO tao_parameter_test VALUES ( $1, $2, $3, $4 )", 3, std::make_tuple( 4, 5 ), 6 ) );
TEST_EXECUTE( connection->execute( "INSERT INTO tao_parameter_test VALUES ( $1, $2, $3, $4 )", std::make_tuple( 4, 5 ), std::make_tuple( 6, 7 ) ) );
TEST_EXECUTE( connection->execute( "INSERT INTO tao_parameter_test VALUES ( $1, $2, $3, $4 )", std::make_tuple( 5, std::make_tuple( 6, 7 ), 8 ) ) );
TEST_EXECUTE( connection->execute( "INSERT INTO tao_parameter_test VALUES ( $1, $2, $3, $4 )", example::user( 6 ) ) );
TEST_EXECUTE( connection->execute( "INSERT INTO tao_parameter_test VALUES ( $1, $2, $3, $4 )", example::user2( 7 ) ) );
const auto result = connection->execute( "SELECT * FROM tao_parameter_test" );
TEST_ASSERT( result.size() == 7 );
for( const auto& row : result ) {
{
const auto v = row.tuple< int, int, int, int >();
TEST_ASSERT( std::get< 1 >( v ) == std::get< 0 >( v ) + 1 );
TEST_ASSERT( std::get< 2 >( v ) == std::get< 1 >( v ) + 1 );
TEST_ASSERT( std::get< 3 >( v ) == std::get< 2 >( v ) + 1 );
}
{
const auto v = row.tuple< std::tuple< int, int >, std::tuple< int, int > >();
TEST_ASSERT( std::get< 1 >( std::get< 0 >( v ) ) == std::get< 0 >( std::get< 0 >( v ) ) + 1 );
TEST_ASSERT( std::get< 0 >( std::get< 1 >( v ) ) == std::get< 1 >( std::get< 0 >( v ) ) + 1 );
TEST_ASSERT( std::get< 1 >( std::get< 1 >( v ) ) == std::get< 0 >( std::get< 1 >( v ) ) + 1 );
}
{
const auto v = row.tuple< int, std::tuple< int, int >, int >();
TEST_ASSERT( std::get< 0 >( std::get< 1 >( v ) ) == std::get< 0 >( v ) + 1 );
TEST_ASSERT( std::get< 1 >( std::get< 1 >( v ) ) == std::get< 0 >( std::get< 1 >( v ) ) + 1 );
TEST_ASSERT( std::get< 2 >( v ) == std::get< 1 >( std::get< 1 >( v ) ) + 1 );
}
{
const auto [ a, b, c, d ] = row.tuple< int, int, int, int >();
TEST_ASSERT( b == a + 1 );
TEST_ASSERT( c == b + 1 );
TEST_ASSERT( d == c + 1 );
}
}
}
int main() //NOLINT(bugprone-exception-escape)
{
try {
run();
}
catch( const std::exception& e ) {
std::cerr << "exception: " << e.what() << std::endl;
throw;
}
catch( ... ) {
std::cerr << "unknown exception" << std::endl;
throw;
}
}