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 <ranges>
4#include <sstream>
5#include <string>
6#include <tuple>
7#include <type_traits>
8#include <vector>
9
10namespace relx::query {
11
13template <typename Tuple>
14static constexpr bool is_empty_tuple() {
15 return std::tuple_size_v<Tuple> == 0;
16}
17
19template <typename Tuple>
20std::string tuple_to_sql(const Tuple& tuple, const char* separator) {
21 std::stringstream ss;
22 int i = 0;
23 std::apply(
24 [&](const auto&... items) { ((ss << (i++ > 0 ? separator : "") << items.to_sql()), ...); },
25 tuple);
26 return ss.str();
27}
28
30template <typename Tuple>
31std::vector<std::string> tuple_bind_params(const Tuple& tuple) {
32 std::vector<std::string> params;
33
34 std::apply(
35 [&](const auto&... items) {
36 auto process_item = [&params](const auto& item) {
37 auto item_params = item.bind_params();
38 if (!item_params.empty()) {
39 params.insert(params.end(), item_params.begin(), item_params.end());
40 }
41 };
42
43 (process_item(items), ...);
44 },
45 tuple);
46
47 return params;
48}
49
51template <typename Func, typename Tuple>
52static void apply_tuple(Func&& func, const Tuple& tuple) {
53 std::apply([&func](const auto&... args) { (func(args), ...); }, tuple);
54}
55
57template <typename T>
59
60template <typename Class, typename T>
61struct class_of_t<T Class::*> {
62 using type = Class;
63};
64
65template <typename T>
67
69template <auto MemberPtr>
71 using table_type = class_of_t_t<decltype(MemberPtr)>;
72 using column_type = std::remove_reference_t<decltype(std::declval<table_type>().*MemberPtr)>;
73};
74
76template <auto MemberPtr>
77constexpr auto column_name_of() {
78 using column_t = typename column_type_of<MemberPtr>::column_type;
79 return column_t::name;
80}
81
82} // namespace relx::query
std::string tuple_to_sql(const Tuple &tuple, const char *separator)
Helper to convert a tuple of expressions to SQL.
Definition meta.hpp:20
static constexpr bool is_empty_tuple()
Helper to check if a tuple is empty.
Definition meta.hpp:14
static void apply_tuple(Func &&func, const Tuple &tuple)
Helper to apply a function to each element of a tuple.
Definition meta.hpp:52
constexpr auto column_name_of()
Helper to get column name from member pointer.
Definition meta.hpp:77
typename class_of_t< T >::type class_of_t_t
Definition meta.hpp:66
std::vector< std::string > tuple_bind_params(const Tuple &tuple)
Helper to collect bind parameters from a tuple of expressions.
Definition meta.hpp:31
Helper to extract class type from a member pointer.
Definition meta.hpp:58
Helper to extract column type from member pointer.
Definition meta.hpp:70
std::remove_reference_t< decltype(std::declval< table_type >().*MemberPtr)> column_type
Definition meta.hpp:72
class_of_t_t< decltype(MemberPtr)> table_type
Definition meta.hpp:71