relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
meta.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <tuple>
5#include <type_traits>
6#include <vector>
7
8namespace relx::connection {
9
14template <typename T>
15void convert_and_assign(T& target, const std::string& value) {
16 if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view> ||
17 std::is_same_v<T, char> || std::is_same_v<T, char16_t> ||
18 std::is_same_v<T, char32_t>) {
19 target = value;
20 } else if constexpr (std::is_same_v<T, bool>) {
21 // Handle PostgreSQL-style boolean values ('t', 'f', etc.)
22 target = (value == "1" || value == "true" || value == "TRUE" || value == "True" ||
23 value == "t" || value == "T" || value == "yes" || value == "YES" || value == "Y");
24 } else if constexpr (std::is_integral_v<T>) {
25 // // Handle empty strings
26 // if (value.empty()) {
27 // target = 0;
28 // } else {
29 target = static_cast<T>(std::stoll(value));
30 // }
31 } else if constexpr (std::is_floating_point_v<T>) {
32 // if (value.empty()) {
33 // target = 0.0;
34 // } else {
35 target = std::stod(value);
36 // }
37 } else {
38 static_assert(std::is_same_v<T, bool>, "Unsupported type conversion");
39 // needed
40 }
41}
42
49template <typename Tuple, size_t... Indices>
50void apply_tuple_assignment(Tuple& tuple, const std::vector<std::string>& row,
51 std::index_sequence<Indices...>) {
52 // For each index in the tuple, convert the string value to the appropriate type
53 (convert_and_assign(std::get<Indices>(tuple), row[Indices]), ...);
54}
55
60template <typename Tuple>
61void map_row_to_tuple(Tuple& tuple, const std::vector<std::string>& row) {
62 // Apply tuple assignment using fold expressions and parameter pack expansion
64 tuple, row, std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
65}
66
67} // namespace relx::connection
void convert_and_assign(T &target, const std::string &value)
Convert a string value to the target type and assign it.
Definition meta.hpp:15
void map_row_to_tuple(Tuple &tuple, const std::vector< std::string > &row)
Helper function to map a result row to a tuple (and thus to a struct)
Definition meta.hpp:61
void apply_tuple_assignment(Tuple &tuple, const std::vector< std::string > &row, std::index_sequence< Indices... >)
Helper function to perform the tuple assignment with index sequence.
Definition meta.hpp:50