summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/test/statement.cpp
blob: b9e96f66783f495f957a8775b08cb4f547af15ca (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
//
// Copyright (c) 2022 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/connection.hpp>
#include "test.hpp"

#include <boost/json.hpp>
#include <boost/algorithm/string.hpp>

#include <unordered_map>


using namespace boost;


BOOST_AUTO_TEST_CASE(statement)
{
  sqlite::connection conn;
  conn.connect(":memory:");
#if SQLITE_VERSION_NUMBER >= 3020000
  std::unique_ptr<int> data{new int(42)};

  auto ip = data.get();
  auto q = conn.prepare("select $1;").execute(std::make_tuple(std::move(data)));
  sqlite::row r = q.current();
  BOOST_CHECK(r.size() == 1u);

  auto v = r.at(0).get_value();
  BOOST_CHECK(v.type() == sqlite::value_type::null);
  BOOST_CHECK(v.get_pointer<int>() != nullptr);
  BOOST_CHECK(v.get_pointer<int>() == ip);
  BOOST_CHECK(v.get_pointer<double>() == nullptr);
#endif
  BOOST_CHECK_THROW(conn.prepare("select * from nothing where name = $name;").execute({}), boost::system::system_error);
}


BOOST_AUTO_TEST_CASE(decltype_)
{
  sqlite::connection conn;
  conn.connect(":memory:");
  conn.execute(
#include "test-db.sql"
  );
  auto q = conn.prepare("select* from author;");

  BOOST_CHECK(boost::iequals(q.declared_type(0), "INTEGER"));
  BOOST_CHECK(boost::iequals(q.declared_type(1), "TEXT"));
  BOOST_CHECK(boost::iequals(q.declared_type(2), "TEXT"));

  BOOST_CHECK_THROW(conn.prepare("elect * from nothing;"), boost::system::system_error);
}


BOOST_AUTO_TEST_CASE(map)
{
  sqlite::connection conn;
  conn.connect(":memory:");
  conn.execute(
#include "test-db.sql"
  );
  auto q = conn.prepare("select * from author where first_name = $name;").execute({{"name", 42}});
  BOOST_CHECK_THROW(conn.prepare("select * from nothing where name = $name;").execute({{"n4ame", 123}}), boost::system::system_error);

  std::unordered_map<std::string, variant2::variant<int, std::string>> params = {{"name", 42}};
  q = conn.prepare("select * from author where first_name = $name;").execute(params);
  BOOST_CHECK_THROW(conn.prepare("select * from nothing where name = $name;").execute(params), boost::system::system_error);

  BOOST_CHECK_THROW(conn.prepare("elect * from nothing;"), boost::system::system_error);
}