summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/test/static_resultset.cpp
blob: 6a9b7c0abbef7061d2f219c11b954b5aafb89d8c (plain)
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
//
// Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//


#include <boost/sqlite/static_resultset.hpp>
#include <boost/sqlite/connection.hpp>

#include <boost/describe/class.hpp>

#include "test.hpp"

using namespace boost;

BOOST_AUTO_TEST_CASE(tuple)
{
        sqlite::connection conn;
    conn.connect(":memory:");
    conn.execute(
#include "test-db.sql"
    );

    using tup = std::tuple<sqlite_int64, sqlite::string_view, sqlite::string_view>;

    bool found = false;
    for (tup t : conn.query<tup>("select id, first_name, last_name from author where last_name = 'hodges';"))
    {
      BOOST_CHECK(std::get<1>(t) == "richard");
      found = true;
    }
    BOOST_CHECK(found);

    found = false;
    for (tup t : conn.prepare("select id, first_name, last_name from author where last_name = ?;")
                     .execute<tup>({"hodges"}))
    {
      BOOST_CHECK(std::get<1>(t) == "richard");
      found = true;
    }
    BOOST_CHECK(found);

    BOOST_CHECK_THROW(conn.query<tup>("select first_name, last_name from author where last_name = 'hodges';"),
                      system::system_error);
    conn.close();
}

struct author
{
  std::string last_name;
  std::string first_name;
};

#if __cplusplus < 202002L
BOOST_DESCRIBE_STRUCT(author, (), (last_name, first_name));
#endif

#if __cplusplus > 201402L

BOOST_AUTO_TEST_CASE(reflection)
{
        sqlite::connection conn;
    conn.connect(":memory:");
    conn.execute(
#include "test-db.sql"
    );


  bool found = false;
  for (author t : conn.query<author>("select first_name, last_name from author where last_name = 'hodges';"))
  {
    BOOST_CHECK(t.first_name == "richard");
    BOOST_CHECK(t.last_name == "hodges");
    found = true;
  }
  BOOST_CHECK(found);

  found = false;
  for (author t : conn.prepare("select first_name, last_name from author where last_name = ?;")
                      .execute<author>({"hodges"}))
  {
    BOOST_CHECK(t.first_name == "richard");
    BOOST_CHECK(t.last_name == "hodges");
    found = true;
  }
  BOOST_CHECK(found);

  BOOST_CHECK_THROW(conn.query<author>("select id, first_name, last_name from author where last_name = 'hodges';"),
  system::system_error);
  conn.close();
}

#endif