| 1 | // |
| 2 | // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | |
| 8 | #ifndef BOOST_MYSQL_TEST_COMMON_INCLUDE_TEST_COMMON_CREATE_BASIC_HPP |
| 9 | #define BOOST_MYSQL_TEST_COMMON_INCLUDE_TEST_COMMON_CREATE_BASIC_HPP |
| 10 | |
| 11 | #include <boost/mysql/blob_view.hpp> |
| 12 | #include <boost/mysql/field_view.hpp> |
| 13 | #include <boost/mysql/row.hpp> |
| 14 | #include <boost/mysql/row_view.hpp> |
| 15 | #include <boost/mysql/rows.hpp> |
| 16 | #include <boost/mysql/rows_view.hpp> |
| 17 | |
| 18 | #include <boost/mysql/detail/access.hpp> |
| 19 | |
| 20 | #include <boost/test/unit_test.hpp> |
| 21 | |
| 22 | #include <cstddef> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace boost { |
| 26 | namespace mysql { |
| 27 | namespace test { |
| 28 | |
| 29 | template <class... Types> |
| 30 | BOOST_CXX14_CONSTEXPR std::array<field_view, sizeof...(Types)> make_fv_arr(Types&&... args) |
| 31 | { |
| 32 | return std::array<field_view, sizeof...(Types)>{{field_view(std::forward<Types>(args))...}}; |
| 33 | } |
| 34 | |
| 35 | template <class... Types> |
| 36 | std::vector<field_view> make_fv_vector(Types&&... args) |
| 37 | { |
| 38 | return std::vector<field_view>{field_view(std::forward<Types>(args))...}; |
| 39 | } |
| 40 | |
| 41 | inline row_view makerowv(const field_view* f, std::size_t size) noexcept |
| 42 | { |
| 43 | return detail::access::construct<row_view>(args&: f, args&: size); |
| 44 | } |
| 45 | |
| 46 | template <class... Types> |
| 47 | row makerow(Types&&... args) |
| 48 | { |
| 49 | auto fields = make_fv_arr(std::forward<Types>(args)...); |
| 50 | return row(makerowv(fields.data(), fields.size())); |
| 51 | } |
| 52 | |
| 53 | inline rows_view makerowsv(const field_view* fields, std::size_t num_fields, std::size_t num_columns) noexcept |
| 54 | { |
| 55 | return detail::access::construct<rows_view>(args&: fields, args&: num_fields, args&: num_columns); |
| 56 | } |
| 57 | |
| 58 | template <class... Types> |
| 59 | rows makerows(std::size_t num_columns, Types&&... args) |
| 60 | { |
| 61 | auto fields = make_fv_arr(std::forward<Types>(args)...); |
| 62 | return rows(makerowsv(fields.data(), fields.size(), num_columns)); |
| 63 | } |
| 64 | |
| 65 | constexpr time maket(int hours, int mins, int secs, int micros = 0) |
| 66 | { |
| 67 | return std::chrono::hours(hours) + std::chrono::minutes(mins) + std::chrono::seconds(secs) + |
| 68 | std::chrono::microseconds(micros); |
| 69 | } |
| 70 | |
| 71 | template <std::size_t N> |
| 72 | constexpr string_view makesv(const char (&value)[N]) |
| 73 | { |
| 74 | static_assert(N >= 1, "Expected a C-array literal" ); |
| 75 | return string_view(value, N - 1); // discard null terminator |
| 76 | } |
| 77 | |
| 78 | template <std::size_t N> |
| 79 | inline string_view makesv(const std::uint8_t (&value)[N]) |
| 80 | { |
| 81 | return string_view(reinterpret_cast<const char*>(value), N); |
| 82 | } |
| 83 | |
| 84 | inline string_view makesv(const std::uint8_t* value, std::size_t size) |
| 85 | { |
| 86 | return string_view(reinterpret_cast<const char*>(value), size); |
| 87 | } |
| 88 | |
| 89 | template <std::size_t N> |
| 90 | blob_view makebv(const char (&value)[N]) |
| 91 | { |
| 92 | static_assert(N >= 1, "Expected a C-array literal" ); |
| 93 | return blob_view(reinterpret_cast<const unsigned char*>(value), |
| 94 | N - 1); // discard null terminator |
| 95 | } |
| 96 | |
| 97 | template <std::size_t N> |
| 98 | blob makeb(const char (&value)[N]) |
| 99 | { |
| 100 | auto bv = makebv(value); |
| 101 | return blob(bv.begin(), bv.end()); |
| 102 | } |
| 103 | |
| 104 | } // namespace test |
| 105 | } // namespace mysql |
| 106 | } // namespace boost |
| 107 | |
| 108 | #endif /* TEST_TEST_COMMON_HPP_ */ |
| 109 | |