boat
C++23 geospatial library
Loading...
Searching...
No Matches
rowset.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_DB_ROWSET_HPP
4#define BOAT_DB_ROWSET_HPP
5
7#include <boost/pfr.hpp>
8#include <vector>
9
10namespace boat::db {
11
12struct rowset {
13 std::vector<std::string> columns;
14 std::vector<std::vector<variant>> rows;
15
16 bool empty() const { return rows.empty(); }
17 auto begin() const { return rows.begin(); }
18 auto end() const { return rows.end(); }
19
21 variant const& value() const { return rows.at(0).at(0); }
22};
23
25template <class T>
26constexpr auto view = std::views::transform([](range_of<variant> auto&& r) {
27 T ret;
28 auto it = std::ranges::begin(r);
29 auto vis = [&](auto& v) {
30 check(it != std::ranges::end(r), "out of fields");
31 read(*it++, v);
32 };
33 if constexpr (requires { read(*it, ret); })
34 vis(ret);
35 else
36 boost::pfr::for_each_field(ret, vis);
37 check(it == std::ranges::end(r), "out of fields");
38 return ret;
39});
40
42rowset to_rowset(std::ranges::input_range auto&& r)
43{
44 auto ret = rowset{
45 boost::pfr::names_as_array<std::ranges::range_value_t<decltype(r)>>() |
46 std::ranges::to<decltype(rowset::columns)>()};
47 for (auto&& item : r) {
48 auto& row = ret.rows.emplace_back();
49 auto vis = [&](auto& v) { write(row.emplace_back(), v); };
50 boost::pfr::for_each_field(item, vis);
51 }
52 return ret;
53}
54
55} // namespace boat::db
56
57#endif // BOAT_DB_ROWSET_HPP
Definition geometry.hpp:9
void write(variant &out, geometry::ogc99 auto const &in)
Serializes an OGC geometry as WKB in a blob.
Definition geometry.hpp:21
void read(variant const &in, geometry::ogc99 auto &out)
Reads WKB from a blob; NULL produces a default-constructed geometry.
Definition geometry.hpp:12
constexpr auto view
Converts a rowset row to a structure.
Definition rowset.hpp:26
rowset to_rowset(std::ranges::input_range auto &&r)
Converts a range of structures to a rowset.
Definition rowset.hpp:42
Definition rowset.hpp:12
auto end() const
Definition rowset.hpp:18
bool empty() const
Definition rowset.hpp:16
auto begin() const
Definition rowset.hpp:17
variant const & value() const
First cell; throws std::out_of_range when absent.
Definition rowset.hpp:21
std::vector< std::string > columns
Definition rowset.hpp:13
std::vector< std::vector< variant > > rows
Definition rowset.hpp:14
Definition variant.hpp:14