AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
rows.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <concepts>
23#include <cstddef>
24#include <cstdint>
25#include <functional>
26#include <optional>
27#include <string>
28#include <string_view>
29#include <type_traits>
30#include <utility>
31#include <vector>
32
33#include "mapping.hpp"
34#include "sql/resultSet.hpp"
35
36namespace anch::sql {
37
47 template<typename V>
48 concept ReadableScalar = std::same_as<V, std::string>
49 || std::same_as<V, int16_t> || std::same_as<V, int32_t> || std::same_as<V, int64_t>
50 || std::same_as<V, uint16_t> || std::same_as<V, uint32_t> || std::same_as<V, uint64_t>
51 || std::same_as<V, double> || std::same_as<V, float> || std::same_as<V, bool>
52 || std::same_as<V, anch::date::Date> || std::same_as<V, anch::date::Time> || std::same_as<V, anch::date::DateTime>
54#ifdef ANCH_UUID
55 || std::same_as<V, anch::UUID>
56#endif
57 ;
58
67 template<typename V>
69 || (requires { typename V::value_type; } && std::same_as<V, std::optional<typename V::value_type>>
71
80 template<typename T>
81 concept Row = (anch::Described<T> && std::default_initializable<T>) || anch::sql::Readable<T>;
82
98 template<anch::Described T>
99 class RowMapper {
100 public:
104 struct Field {
106 std::string name;
107
109 std::function<void(T&, ResultSet&, std::size_t, std::string_view)> write;
110 };
111
112 // Attributes +
113 private:
115 std::vector<Field> _fields;
116 // Attributes -
117
118 // Constructors +
119 private:
123 RowMapper();
124 // Constructors -
125
126 // Methods +
127 public:
133 static const RowMapper<T>& instance();
134
142 const Field* find(std::string_view column) const noexcept;
143
149 const std::vector<Field>& fields() const noexcept;
150
151 // Fields registration (anch::ObjectMapper concept ; constraints are ignored) +
163 template<typename P, typename... C>
164 RowMapper<T>& registerField(const std::string& name, P T::* member, C&&...);
165
174 template<typename P, typename... C>
175 RowMapper<T>& registerField(const std::string&, std::function<P(const T&)>, C&&...);
176
188 template<typename P, typename... C>
189 RowMapper<T>& registerField(const std::string& name, std::function<void(T&, const P&)> setter, C&&...);
190
202 template<typename P, typename... C>
203 RowMapper<T>& registerField(const std::string& name, std::function<P(const T&)>, std::function<void(T&, const P&)> setter, C&&...);
204 // Fields registration -
205 // Methods -
206
207 };
208
209}
210
211namespace anch::sql::detail {
212
214 template<typename T>
215 struct RowColumn {
216 std::size_t index;
217 std::string name;
218 };
219
221 template<anch::Described T>
222 struct RowColumn<T> {
223 std::size_t index;
224 std::string name;
225 const typename anch::sql::RowMapper<T>::Field* field;
226 };
227
228}
229
230namespace anch::sql {
231
244 template<anch::sql::Row T>
245 class RowReader {
246
247 // Attributes +
248 private:
250 std::vector<anch::sql::detail::RowColumn<T>> _columns;
251 // Attributes -
252
253 // Constructors +
254 public:
263 explicit RowReader(const ResultSet& result);
264 // Constructors -
265
266 // Methods +
267 public:
277 void read(ResultSet& result, T& row) const;
278
289 T read(ResultSet& result) const;
290 // Methods -
291
292 };
293
307 template<anch::sql::Row T>
308 void readRow(ResultSet& result, T& row);
309
323 template<anch::sql::Row T>
324 T rowAs(ResultSet& result);
325
326}
327
328#include "sql/impl/rows.hpp"
SQL result representation.
Definition resultSet.hpp:73
RowMapper< T > & registerField(const std::string &name, P T::*member, C &&...)
static const RowMapper< T > & instance()
const std::vector< Field > & fields() const noexcept
const Field * find(std::string_view column) const noexcept
void read(ResultSet &result, T &row) const
T read(ResultSet &result) const
RowReader(const ResultSet &result)
Type described by an anch::Mapping specialization.
Definition mapping.hpp:66
Definition rows.hpp:68
Definition rows.hpp:48
Definition rows.hpp:81
Definition resultSet.hpp:54
SQL namespace.
Definition clauses.hpp:28
void readRow(ResultSet &result, T &row)
T rowAs(ResultSet &result)
AnCH framework base namespace.
Definition app.hpp:30
Field which can be written.
Definition rows.hpp:104
std::string name
Definition rows.hpp:106
std::function< void(T &, ResultSet &, std::size_t, std::string_view)> write
Definition rows.hpp:109