-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupby.hpp
102 lines (77 loc) · 2.41 KB
/
groupby.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef _SQL_GROUPBY_HPP
#define _SQL_GROUPBY_HPP
/**
* Copyright 2015 Maxim Musolov
* This code distribute under private license
*/
#include <string>
#include <type_traits>
#include <query-freaks/sql_base.hpp>
namespace sql {
namespace helper {
/** select impl - helper class */
template <std::size_t N, class... Ent>
struct impl_groupby_part;
/** add space at the end of expression */
template <std::size_t N>
struct impl_groupby_part<N>
{
static constexpr size_t value = N;
// operator implementation for specialization
template <class ...Args>
std::string operator()(binder<Args...>& b) {
return std::string(" ");
}
std::string operator()() {
return std::string(" ");
}
};
/** mid expression for fields order */
template <std::size_t N, class F, class ...Ent>
struct impl_groupby_part<N,F,Ent...>
{
static constexpr size_t value = N;
/** expression contains query parameters */
template <class ...Args>
std::string operator()(binder<Args...>& b) {
return std::string(", ") + F()(b) + impl_groupby_part<N+1, Ent...>()(b);
}
/** expression without parameters */
std::string operator()() {
return std::string(", ") + F()() + impl_groupby_part<N+1, Ent...>()();
}
};
/** first entry of selecting fields order */
template <class F, class ...Ent>
struct impl_groupby_part<0,F,Ent...>
{
static constexpr size_t value = 0;
// operator implementation of first entry
template <class ...Args>
std::string operator()(binder<Args...>& b) {
return std::string("GROUP BY ") + F()(b) + impl_groupby_part<1, Ent...>()(b);
}
/** expression without query parameters */
std::string operator()() {
return std::string("GROUP BY ") + F()() + impl_groupby_part<1, Ent...>()();
}
};
};
/** define sql::select */
template <typename ...Ent>
struct groupby
{
public:
groupby() {}
/** implement select directive */
template <class ...Args>
std::string operator()(binder<Args...>& b) {
return helper::impl_groupby_part<0,Ent...>()(b);
}
/** expression without query parameters */
std::string operator()() {
return helper::impl_groupby_part<0,Ent...>()();
}
};
};
#endif