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 if constexpr (requires { read(*it, ret); })
30 read(*it++, ret);
31 else
32 boost::pfr::for_each_field(ret, [&](auto& v) { read(*it++, v); });
33 check(it == std::ranges::end(r), "out of fields");
34 return ret;
35});
36
38rowset to_rowset(std::ranges::input_range auto&& r)
39{
40 auto ret = rowset{
41 boost::pfr::names_as_array<std::ranges::range_value_t<decltype(r)>>() |
42 std::ranges::to<decltype(rowset::columns)>()};
43 for (auto&& item : r) {
44 auto& row = ret.rows.emplace_back();
45 auto vis = [&](auto& v) { write(row.emplace_back(), v); };
46 boost::pfr::for_each_field(item, vis);
47 }
48 return ret;
49}
50
51} // namespace boat::db
52
53#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:38
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