This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from fnc12/sqlite_orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_by_serializer.h
78 lines (66 loc) · 2.46 KB
/
order_by_serializer.h
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
#pragma once
#include <string> // std::string
#include <sstream> // std::stringstream
namespace sqlite_orm {
namespace internal {
template<class T, class SFINAE = void>
struct order_by_serializer;
template<class T, class Ctx>
std::string serialize_order_by(const T& t, const Ctx& context) {
order_by_serializer<T> serializer;
return serializer(t, context);
}
template<class O>
struct order_by_serializer<order_by_t<O>, void> {
using statement_type = order_by_t<O>;
template<class Ctx>
std::string operator()(const statement_type& orderBy, const Ctx& context) const {
std::stringstream ss;
auto newContext = context;
newContext.skip_table_name = false;
ss << serialize(orderBy.expression, newContext);
if(!orderBy._collate_argument.empty()) {
ss << " COLLATE " << orderBy._collate_argument;
}
switch(orderBy.asc_desc) {
case 1:
ss << " ASC";
break;
case -1:
ss << " DESC";
break;
}
return ss.str();
}
};
template<class C>
struct order_by_serializer<dynamic_order_by_t<C>, void> {
using statement_type = dynamic_order_by_t<C>;
template<class Ctx>
std::string operator()(const statement_type& orderBy, const Ctx&) const {
std::stringstream ss;
ss << static_cast<std::string>(orderBy) << " ";
int index = 0;
for(const dynamic_order_by_entry_t& entry: orderBy) {
if(index > 0) {
ss << ", ";
}
ss << entry.name;
if(!entry._collate_argument.empty()) {
ss << " COLLATE " << entry._collate_argument;
}
switch(entry.asc_desc) {
case 1:
ss << " ASC";
break;
case -1:
ss << " DESC";
break;
}
++index;
};
return ss.str();
}
};
}
}